66c621dc7ae6487a33dcf4b9e9e12f0225aa896f
[lhc/web/wiklou.git] / includes / specials / SpecialVersion.php
1 <?php
2
3 /**
4 * Give information about the version of MediaWiki, PHP, the DB and extensions
5 *
6 * @ingroup SpecialPage
7 *
8 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
9 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
10 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
11 */
12 class SpecialVersion extends SpecialPage {
13 private $firstExtOpened = true;
14
15 static $viewvcUrls = array(
16 'svn+ssh://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
17 'http://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
18 # Doesn't work at the time of writing but maybe some day:
19 'https://svn.wikimedia.org/viewvc/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
20 );
21
22 function __construct(){
23 parent::__construct( 'Version' );
24 }
25
26 /**
27 * main()
28 */
29 function execute( $par ) {
30 global $wgOut, $wgMessageCache, $wgSpecialVersionShowHooks, $wgContLang;
31 $wgMessageCache->loadAllMessages();
32
33 $this->setHeaders();
34 $this->outputHeader();
35
36 if( $wgContLang->isRTL() ) {
37 $wgOut->addHTML( '<div dir="rtl">' );
38 } else {
39 $wgOut->addHTML( '<div dir="ltr">' );
40 }
41 $text =
42 $this->MediaWikiCredits() .
43 $this->softwareInformation() .
44 $this->extensionCredits();
45 if ( $wgSpecialVersionShowHooks ) {
46 $text .= $this->wgHooks();
47 }
48 $wgOut->addWikiText( $text );
49 $wgOut->addHTML( $this->IPInfo() );
50 $wgOut->addHTML( '</div>' );
51 }
52
53 /**#@+
54 * @private
55 */
56
57 /**
58 * @return wiki text showing the license information
59 */
60 static function MediaWikiCredits() {
61 global $wgContLang;
62
63 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMsg( 'version-license' ) );
64
65 // This text is always left-to-right.
66 $ret .= '<div dir="ltr">';
67 $ret .= "__NOTOC__
68 This wiki is powered by '''[http://www.mediawiki.org/ MediaWiki]''',
69 copyright © 2001-2009 Magnus Manske, Brion Vibber, Lee Daniel Crocker,
70 Tim Starling, Erik Möller, Gabriel Wicke, Ævar Arnfjörð Bjarmason,
71 Niklas Laxström, Domas Mituzas, Rob Church, Yuri Astrakhan, Aryeh Gregor,
72 Aaron Schulz, Andrew Garrett, Raimond Spekking, Alexandre Emsenhuber,
73 Siebrand Mazeland, Chad Horohoe and others.
74
75 MediaWiki is free software; you can redistribute it and/or modify
76 it under the terms of the GNU General Public License as published by
77 the Free Software Foundation; either version 2 of the License, or
78 (at your option) any later version.
79
80 MediaWiki is distributed in the hope that it will be useful,
81 but WITHOUT ANY WARRANTY; without even the implied warranty of
82 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83 GNU General Public License for more details.
84
85 You should have received [{{SERVER}}{{SCRIPTPATH}}/COPYING a copy of the GNU General Public License]
86 along with this program; if not, write to the Free Software
87 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
88 or [http://www.gnu.org/licenses/old-licenses/gpl-2.0.html read it online].
89 ";
90 $ret .= '</div>';
91
92 return str_replace( "\t\t", '', $ret ) . "\n";
93 }
94
95 /**
96 * @return wiki text showing the third party software versions (apache, php, mysql).
97 */
98 static function softwareInformation() {
99 $dbr = wfGetDB( DB_SLAVE );
100
101 // Put the software in an array of form 'name' => 'version'. All messages should
102 // be loaded here, so feel free to use wfMsg*() in the 'name'. Raw HTML or wikimarkup
103 // can be used
104 $software = array();
105 $software['[http://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
106 $software['[http://www.php.net/ PHP]'] = phpversion() . " (" . php_sapi_name() . ")";
107 $software[$dbr->getSoftwareLink()] = $dbr->getServerVersion();
108
109 // Allow a hook to add/remove items
110 wfRunHooks( 'SoftwareInfo', array( &$software ) );
111
112 $out = Xml::element( 'h2', array( 'id' => 'mw-version-software' ), wfMsg( 'version-software' ) ) .
113 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-software' ) ) .
114 "<tr>
115 <th>" . wfMsg( 'version-software-product' ) . "</th>
116 <th>" . wfMsg( 'version-software-version' ) . "</th>
117 </tr>\n";
118 foreach( $software as $name => $version ) {
119 $out .= "<tr>
120 <td>" . $name . "</td>
121 <td>" . $version . "</td>
122 </tr>\n";
123 }
124 return $out . Xml::closeElement( 'table' );
125 }
126
127 /**
128 * Return a string of the MediaWiki version with SVN revision if available
129 *
130 * @return mixed
131 */
132 public static function getVersion( $flags = '' ) {
133 global $wgVersion, $IP;
134 wfProfileIn( __METHOD__ );
135
136 $info = self::getSvnInfo( $IP );
137 if ( !$info ) {
138 $version = $wgVersion;
139 } elseif( $flags === 'nodb' ) {
140 $version = "$wgVersion (r{$info['checkout-rev']})";
141 } else {
142 $version = $wgVersion .
143 wfMsg(
144 'version-svn-revision',
145 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
146 $info['checkout-rev']
147 );
148 }
149
150 wfProfileOut( __METHOD__ );
151 return $version;
152 }
153
154 /**
155 * Return a wikitext-formatted string of the MediaWiki version with a link to
156 * the SVN revision if available
157 *
158 * @return mixed
159 */
160 public static function getVersionLinked() {
161 global $wgVersion, $IP;
162 wfProfileIn( __METHOD__ );
163 $info = self::getSvnInfo( $IP );
164 if ( isset( $info['checkout-rev'] ) ) {
165 $linkText = wfMsg(
166 'version-svn-revision',
167 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
168 $info['checkout-rev']
169 );
170 if ( isset( $info['viewvc-url'] ) ) {
171 $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
172 } else {
173 $version = "$wgVersion $linkText";
174 }
175 } else {
176 $version = $wgVersion;
177 }
178 wfProfileOut( __METHOD__ );
179 return $version;
180 }
181
182 /** Generate wikitext showing extensions name, URL, author and description */
183 function extensionCredits() {
184 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunctions;
185
186 if ( ! count( $wgExtensionCredits ) && ! count( $wgExtensionFunctions ) && ! count( $wgSkinExtensionFunctions ) )
187 return '';
188
189 $extensionTypes = array(
190 'specialpage' => wfMsg( 'version-specialpages' ),
191 'parserhook' => wfMsg( 'version-parserhooks' ),
192 'variable' => wfMsg( 'version-variables' ),
193 'media' => wfMsg( 'version-mediahandlers' ),
194 'other' => wfMsg( 'version-other' ),
195 );
196 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
197
198 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
199 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-ext' ) );
200
201 foreach ( $extensionTypes as $type => $text ) {
202 if ( isset ( $wgExtensionCredits[$type] ) && count ( $wgExtensionCredits[$type] ) ) {
203 $out .= $this->openExtType( $text );
204
205 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
206
207 foreach ( $wgExtensionCredits[$type] as $extension ) {
208 $out .= $this->formatCredits( $extension );
209 }
210 }
211 }
212
213 if ( count( $wgExtensionFunctions ) ) {
214 $out .= $this->openExtType( wfMsg( 'version-extension-functions' ) );
215 $out .= '<tr><td colspan="4">' . $this->listToText( $wgExtensionFunctions ) . "</td></tr>\n";
216 }
217
218 if ( $cnt = count( $tags = $wgParser->getTags() ) ) {
219 for ( $i = 0; $i < $cnt; ++$i )
220 $tags[$i] = "&lt;{$tags[$i]}&gt;";
221 $out .= $this->openExtType( wfMsg( 'version-parser-extensiontags' ) );
222 $out .= '<tr><td colspan="4">' . $this->listToText( $tags ). "</td></tr>\n";
223 }
224
225 if( $cnt = count( $fhooks = $wgParser->getFunctionHooks() ) ) {
226 $out .= $this->openExtType( wfMsg( 'version-parser-function-hooks' ) );
227 $out .= '<tr><td colspan="4">' . $this->listToText( $fhooks ) . "</td></tr>\n";
228 }
229
230 if ( count( $wgSkinExtensionFunctions ) ) {
231 $out .= $this->openExtType( wfMsg( 'version-skin-extension-functions' ) );
232 $out .= '<tr><td colspan="4">' . $this->listToText( $wgSkinExtensionFunctions ) . "</td></tr>\n";
233 }
234 $out .= Xml::closeElement( 'table' );
235 return $out;
236 }
237
238 /** Callback to sort extensions by type */
239 function compare( $a, $b ) {
240 global $wgLang;
241 if( $a['name'] === $b['name'] ) {
242 return 0;
243 } else {
244 return $wgLang->lc( $a['name'] ) > $wgLang->lc( $b['name'] )
245 ? 1
246 : -1;
247 }
248 }
249
250 function formatCredits( $extension ) {
251 $name = isset( $extension['name'] ) ? $extension['name'] : '[no name]';
252 if ( isset( $extension['path'] ) ) {
253 $svnInfo = self::getSvnInfo( dirname($extension['path']) );
254 $directoryRev = isset( $svnInfo['directory-rev'] ) ? $svnInfo['directory-rev'] : null;
255 $checkoutRev = isset( $svnInfo['checkout-rev'] ) ? $svnInfo['checkout-rev'] : null;
256 $viewvcUrl = isset( $svnInfo['viewvc-url'] ) ? $svnInfo['viewvc-url'] : null;
257 } else {
258 $directoryRev = null;
259 $checkoutRev = null;
260 $viewvcUrl = null;
261 }
262
263 # Make main link (or just the name if there is no URL)
264 if ( isset( $extension['url'] ) ) {
265 $mainLink = "[{$extension['url']} $name]";
266 } else {
267 $mainLink = $name;
268 }
269 if ( isset( $extension['version'] ) ) {
270 $versionText = '<span class="mw-version-ext-version">' .
271 wfMsg( 'version-version', $extension['version'] ) .
272 '</span>';
273 } else {
274 $versionText = '';
275 }
276
277 # Make subversion text/link
278 if ( $checkoutRev ) {
279 $svnText = wfMsg( 'version-svn-revision', $directoryRev, $checkoutRev );
280 $svnText = isset( $viewvcUrl ) ? "[$viewvcUrl $svnText]" : $svnText;
281 } else {
282 $svnText = false;
283 }
284
285 # Make description text
286 $description = isset ( $extension['description'] ) ? $extension['description'] : '';
287 if( isset ( $extension['descriptionmsg'] ) ) {
288 # Look for a localized description
289 $descriptionMsg = $extension['descriptionmsg'];
290 if( is_array( $descriptionMsg ) ) {
291 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
292 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
293 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
294 $msg = wfMsg( $descriptionMsgKey, $descriptionMsg );
295 } else {
296 $msg = wfMsg( $descriptionMsg );
297 }
298 if ( !wfEmptyMsg( $descriptionMsg, $msg ) && $msg != '' ) {
299 $description = $msg;
300 }
301 }
302
303 if ( $svnText !== false ) {
304 $extNameVer = "<tr>
305 <td><em>$mainLink $versionText</em></td>
306 <td><em>$svnText</em></td>";
307 } else {
308 $extNameVer = "<tr>
309 <td colspan=\"2\"><em>$mainLink $versionText</em></td>";
310 }
311 $author = isset ( $extension['author'] ) ? $extension['author'] : array();
312 $extDescAuthor = "<td>$description</td>
313 <td>" . $this->listToText( (array)$author ) . "</td>
314 </tr>\n";
315 return $extNameVer . $extDescAuthor;
316 }
317
318 /**
319 * @return string
320 */
321 function wgHooks() {
322 global $wgHooks;
323
324 if ( count( $wgHooks ) ) {
325 $myWgHooks = $wgHooks;
326 ksort( $myWgHooks );
327
328 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-hooks' ), wfMsg( 'version-hooks' ) ) .
329 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) ) .
330 "<tr>
331 <th>" . wfMsg( 'version-hook-name' ) . "</th>
332 <th>" . wfMsg( 'version-hook-subscribedby' ) . "</th>
333 </tr>\n";
334
335 foreach ( $myWgHooks as $hook => $hooks )
336 $ret .= "<tr>
337 <td>$hook</td>
338 <td>" . $this->listToText( $hooks ) . "</td>
339 </tr>\n";
340
341 $ret .= Xml::closeElement( 'table' );
342 return $ret;
343 } else
344 return '';
345 }
346
347 private function openExtType($text, $name = null) {
348 $opt = array( 'colspan' => 4 );
349 $out = '';
350
351 if(!$this->firstExtOpened) {
352 // Insert a spacing line
353 $out .= '<tr class="sv-space">' . Xml::element( 'td', $opt ) . "</tr>\n";
354 }
355 $this->firstExtOpened = false;
356
357 if($name) { $opt['id'] = "sv-$name"; }
358
359 $out .= "<tr>" . Xml::element( 'th', $opt, $text) . "</tr>\n";
360 return $out;
361 }
362
363 /**
364 * @return string
365 */
366 function IPInfo() {
367 $ip = str_replace( '--', ' - ', htmlspecialchars( wfGetIP() ) );
368 return "<!-- visited from $ip -->\n" .
369 "<span style='display:none'>visited from $ip</span>";
370 }
371
372 /**
373 * @param array $list
374 * @return string
375 */
376 function listToText( $list ) {
377 $cnt = count( $list );
378
379 if ( $cnt == 1 ) {
380 // Enforce always returning a string
381 return (string)self::arrayToString( $list[0] );
382 } elseif ( $cnt == 0 ) {
383 return '';
384 } else {
385 global $wgLang;
386 sort( $list );
387 return $wgLang->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
388 }
389 }
390
391 /**
392 * @param mixed $list Will convert an array to string if given and return
393 * the paramater unaltered otherwise
394 * @return mixed
395 */
396 static function arrayToString( $list ) {
397 if( is_array( $list ) && count( $list ) == 1 )
398 $list = $list[0];
399 if( is_object( $list ) ) {
400 $class = get_class( $list );
401 return "($class)";
402 } elseif ( !is_array( $list ) ) {
403 return $list;
404 } else {
405 if( is_object( $list[0] ) )
406 $class = get_class( $list[0] );
407 else
408 $class = $list[0];
409 return "($class, {$list[1]})";
410 }
411 }
412
413 /**
414 * Get an associative array of information about a given path, from its .svn
415 * subdirectory. Returns false on error, such as if the directory was not
416 * checked out with subversion.
417 *
418 * Returned keys are:
419 * Required:
420 * checkout-rev The revision which was checked out
421 * Optional:
422 * directory-rev The revision when the directory was last modified
423 * url The subversion URL of the directory
424 * repo-url The base URL of the repository
425 * viewvc-url A ViewVC URL pointing to the checked-out revision
426 */
427 public static function getSvnInfo( $dir ) {
428 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
429 $entries = $dir . '/.svn/entries';
430
431 if( !file_exists( $entries ) ) {
432 return false;
433 }
434
435 $lines = file( $entries );
436 if ( !count( $lines ) ) {
437 return false;
438 }
439
440 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
441 if( preg_match( '/^<\?xml/', $lines[0] ) ) {
442 // subversion is release <= 1.3
443 if( !function_exists( 'simplexml_load_file' ) ) {
444 // We could fall back to expat... YUCK
445 return false;
446 }
447
448 // SimpleXml whines about the xmlns...
449 wfSuppressWarnings();
450 $xml = simplexml_load_file( $entries );
451 wfRestoreWarnings();
452
453 if( $xml ) {
454 foreach( $xml->entry as $entry ) {
455 if( $xml->entry[0]['name'] == '' ) {
456 // The directory entry should always have a revision marker.
457 if( $entry['revision'] ) {
458 return array( 'checkout-rev' => intval( $entry['revision'] ) );
459 }
460 }
461 }
462 }
463 return false;
464 }
465
466 // subversion is release 1.4 or above
467 if ( count( $lines ) < 11 ) {
468 return false;
469 }
470 $info = array(
471 'checkout-rev' => intval( trim( $lines[3] ) ),
472 'url' => trim( $lines[4] ),
473 'repo-url' => trim( $lines[5] ),
474 'directory-rev' => intval( trim( $lines[10] ) )
475 );
476 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
477 $viewvc = str_replace(
478 $info['repo-url'],
479 self::$viewvcUrls[$info['repo-url']],
480 $info['url']
481 );
482 $pathRelativeToRepo = substr( $info['url'], strlen( $info['repo-url'] ) );
483 if ( substr( $pathRelativeToRepo, 0, 6 ) == '/trunk' ) {
484 $viewvc .= '/?pathrev=';
485 } else {
486 // Avoids 404 error using pathrev when it does not found
487 $viewvc .= '/?revision=';
488 }
489 $viewvc .= urlencode( $info['checkout-rev'] );
490 $info['viewvc-url'] = $viewvc;
491 }
492 return $info;
493 }
494
495 /**
496 * Retrieve the revision number of a Subversion working directory.
497 *
498 * @param String $dir Directory of the svn checkout
499 * @return int revision number as int
500 */
501 public static function getSvnRevision( $dir ) {
502 $info = self::getSvnInfo( $dir );
503 if ( $info === false ) {
504 return false;
505 } elseif ( isset( $info['checkout-rev'] ) ) {
506 return $info['checkout-rev'];
507 } else {
508 return false;
509 }
510 }
511
512 /**#@-*/
513 }