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