Per Nikerabbit, follow-up to r77972: use a string instead of boolean for readability
[mediawiki.git] / includes / specials / SpecialVersion.php
blobc53569dc14c3b1ed09d747774e740032ed18874b
1 <?php
2 /**
3 * Implements Special:Version
5 * Copyright © 2005 Ævar Arnfjörð Bjarmason
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.
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.
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
22 * @file
23 * @ingroup SpecialPage
26 /**
27 * Give information about the version of MediaWiki, PHP, the DB and extensions
29 * @ingroup SpecialPage
31 class SpecialVersion extends SpecialPage {
33 protected $firstExtOpened = false;
35 protected static $extensionTypes = false;
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',
44 public function __construct(){
45 parent::__construct( 'Version' );
48 /**
49 * main()
51 public function execute( $par ) {
52 global $wgOut, $wgSpecialVersionShowHooks, $wgContLang;
54 $this->setHeaders();
55 $this->outputHeader();
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();
67 $wgOut->addWikiText( $text );
68 $wgOut->addHTML( $this->IPInfo() );
69 $wgOut->addHTML( '</div>' );
72 /**
73 * Returns wiki text showing the license information.
75 * @return string
77 private static function getMediaWikiCredits() {
78 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMsg( 'version-license' ) );
80 // This text is always left-to-right.
81 $ret .= '<div>';
82 $ret .= "__NOTOC__
83 " . self::getCopyrightAndAuthorList() . "\n
84 " . wfMsg( 'version-license-info' );
85 $ret .= '</div>';
87 return str_replace( "\t\t", '', $ret ) . "\n";
90 /**
91 * Get the "Mediawiki is copyright 2001-20xx by lots of cool guys" text
93 * @return String
95 public static function getCopyrightAndAuthorList() {
96 global $wgLang;
98 $authorList = array( 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
99 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
100 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
101 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
102 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
103 wfMsg( 'version-poweredby-others' )
106 return wfMsg( 'version-poweredby-credits', date( 'Y' ),
107 $wgLang->listToText( $authorList ) );
111 * Returns wiki text showing the third party software versions (apache, php, mysql).
113 * @return string
115 static function softwareInformation() {
116 $dbr = wfGetDB( DB_SLAVE );
118 // Put the software in an array of form 'name' => 'version'. All messages should
119 // be loaded here, so feel free to use wfMsg*() in the 'name'. Raw HTML or wikimarkup
120 // can be used.
121 $software = array();
122 $software['[http://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
123 $software['[http://www.php.net/ PHP]'] = phpversion() . " (" . php_sapi_name() . ")";
124 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
126 // Allow a hook to add/remove items.
127 wfRunHooks( 'SoftwareInfo', array( &$software ) );
129 $out = Xml::element( 'h2', array( 'id' => 'mw-version-software' ), wfMsg( 'version-software' ) ) .
130 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-software' ) ) .
131 "<tr>
132 <th>" . wfMsg( 'version-software-product' ) . "</th>
133 <th>" . wfMsg( 'version-software-version' ) . "</th>
134 </tr>\n";
136 foreach( $software as $name => $version ) {
137 $out .= "<tr>
138 <td>" . $name . "</td>
139 <td>" . $version . "</td>
140 </tr>\n";
143 return $out . Xml::closeElement( 'table' );
147 * Return a string of the MediaWiki version with SVN revision if available.
149 * @return mixed
151 public static function getVersion( $flags = '' ) {
152 global $wgVersion, $IP;
153 wfProfileIn( __METHOD__ );
155 $info = self::getSvnInfo( $IP );
156 if ( !$info ) {
157 $version = $wgVersion;
158 } elseif( $flags === 'nodb' ) {
159 $version = "$wgVersion (r{$info['checkout-rev']})";
160 } else {
161 $version = $wgVersion . ' ' .
162 wfMsg(
163 'version-svn-revision',
164 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
165 $info['checkout-rev']
169 wfProfileOut( __METHOD__ );
170 return $version;
174 * Return a wikitext-formatted string of the MediaWiki version with a link to
175 * the SVN revision if available.
177 * @return mixed
179 public static function getVersionLinked() {
180 global $wgVersion, $IP;
181 wfProfileIn( __METHOD__ );
183 $info = self::getSvnInfo( $IP );
185 if ( isset( $info['checkout-rev'] ) ) {
186 $linkText = wfMsg(
187 'version-svn-revision',
188 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
189 $info['checkout-rev']
192 if ( isset( $info['viewvc-url'] ) ) {
193 $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
194 } else {
195 $version = "$wgVersion $linkText";
197 } else {
198 $version = $wgVersion;
201 wfProfileOut( __METHOD__ );
202 return $version;
206 * Returns an array with the base extension types.
207 * Type is stored as array key, the message as array value.
209 * TODO: ideally this would return all extension types, including
210 * those added by SpecialVersionExtensionTypes. This is not possible
211 * since this hook is passing along $this though.
213 * @since 1.17
215 * @return array
217 public static function getExtensionTypes() {
218 if ( self::$extensionTypes === false ) {
219 self::$extensionTypes = array(
220 'specialpage' => wfMsg( 'version-specialpages' ),
221 'parserhook' => wfMsg( 'version-parserhooks' ),
222 'variable' => wfMsg( 'version-variables' ),
223 'media' => wfMsg( 'version-mediahandlers' ),
224 'skin' => wfMsg( 'version-skins' ),
225 'other' => wfMsg( 'version-other' ),
228 wfRunHooks( 'ExtensionTypes', array( &self::$extensionTypes ) );
231 return self::$extensionTypes;
235 * Returns the internationalized name for an extension type.
237 * @since 1.17
239 * @param $type String
241 * @return string
243 public static function getExtensionTypeName( $type ) {
244 $types = self::getExtensionTypes();
245 return $types[$type];
249 * Generate wikitext showing extensions name, URL, author and description.
251 * @return String: Wikitext
253 function getExtensionCredits() {
254 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunctions;
256 if ( !count( $wgExtensionCredits ) && !count( $wgExtensionFunctions ) && !count( $wgSkinExtensionFunctions ) ) {
257 return '';
260 $extensionTypes = self::getExtensionTypes();
263 * @deprecated as of 1.17, use hook ExtensionTypes instead.
265 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
267 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
268 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-ext' ) );
270 // Make sure the 'other' type is set to an array.
271 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
272 $wgExtensionCredits['other'] = array();
275 // Find all extensions that do not have a valid type and give them the type 'other'.
276 foreach ( $wgExtensionCredits as $type => $extensions ) {
277 if ( !array_key_exists( $type, $extensionTypes ) ) {
278 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
282 // Loop through the extension categories to display their extensions in the list.
283 foreach ( $extensionTypes as $type => $message ) {
284 if ( $type != 'other' ) {
285 $out .= $this->getExtensionCategory( $type, $message );
289 // We want the 'other' type to be last in the list.
290 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
292 if ( count( $wgExtensionFunctions ) ) {
293 $out .= $this->openExtType( wfMsg( 'version-extension-functions' ), 'extension-functions' );
294 $out .= '<tr><td colspan="4">' . $this->listToText( $wgExtensionFunctions ) . "</td></tr>\n";
297 $tags = $wgParser->getTags();
298 $cnt = count( $tags );
300 if ( $cnt ) {
301 for ( $i = 0; $i < $cnt; ++$i ) {
302 $tags[$i] = "&lt;{$tags[$i]}&gt;";
304 $out .= $this->openExtType( wfMsg( 'version-parser-extensiontags' ), 'parser-tags' );
305 $out .= '<tr><td colspan="4">' . $this->listToText( $tags ). "</td></tr>\n";
308 if( count( $fhooks = $wgParser->getFunctionHooks() ) ) {
309 $out .= $this->openExtType( wfMsg( 'version-parser-function-hooks' ), 'parser-function-hooks' );
310 $out .= '<tr><td colspan="4">' . $this->listToText( $fhooks ) . "</td></tr>\n";
313 if ( count( $wgSkinExtensionFunctions ) ) {
314 $out .= $this->openExtType( wfMsg( 'version-skin-extension-functions' ), 'skin-extension-functions' );
315 $out .= '<tr><td colspan="4">' . $this->listToText( $wgSkinExtensionFunctions ) . "</td></tr>\n";
318 $out .= Xml::closeElement( 'table' );
320 return $out;
324 * Creates and returns the HTML for a single extension category.
326 * @since 1.17
328 * @param $type String
329 * @param $message String
331 * @return string
333 protected function getExtensionCategory( $type, $message ) {
334 global $wgExtensionCredits;
336 $out = '';
338 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
339 $out .= $this->openExtType( $message, 'credits-' . $type );
341 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
343 foreach ( $wgExtensionCredits[$type] as $extension ) {
344 $out .= $this->getCreditsForExtension( $extension );
348 return $out;
352 * Callback to sort extensions by type.
354 function compare( $a, $b ) {
355 global $wgLang;
356 if( $a['name'] === $b['name'] ) {
357 return 0;
358 } else {
359 return $wgLang->lc( $a['name'] ) > $wgLang->lc( $b['name'] )
361 : -1;
366 * Creates and formats the creidts for a single extension and returns this.
368 * @param $extension Array
370 * @return string
372 function getCreditsForExtension( array $extension ) {
373 $name = isset( $extension['name'] ) ? $extension['name'] : '[no name]';
375 if ( isset( $extension['path'] ) ) {
376 $svnInfo = self::getSvnInfo( dirname($extension['path']) );
377 $directoryRev = isset( $svnInfo['directory-rev'] ) ? $svnInfo['directory-rev'] : null;
378 $checkoutRev = isset( $svnInfo['checkout-rev'] ) ? $svnInfo['checkout-rev'] : null;
379 $viewvcUrl = isset( $svnInfo['viewvc-url'] ) ? $svnInfo['viewvc-url'] : null;
380 } else {
381 $directoryRev = null;
382 $checkoutRev = null;
383 $viewvcUrl = null;
386 # Make main link (or just the name if there is no URL).
387 if ( isset( $extension['url'] ) ) {
388 $mainLink = "[{$extension['url']} $name]";
389 } else {
390 $mainLink = $name;
393 if ( isset( $extension['version'] ) ) {
394 $versionText = '<span class="mw-version-ext-version">' .
395 wfMsg( 'version-version', $extension['version'] ) .
396 '</span>';
397 } else {
398 $versionText = '';
401 # Make subversion text/link.
402 if ( $checkoutRev ) {
403 $svnText = wfMsg( 'version-svn-revision', $directoryRev, $checkoutRev );
404 $svnText = isset( $viewvcUrl ) ? "[$viewvcUrl $svnText]" : $svnText;
405 } else {
406 $svnText = false;
409 # Make description text.
410 $description = isset ( $extension['description'] ) ? $extension['description'] : '';
412 if( isset ( $extension['descriptionmsg'] ) ) {
413 # Look for a localized description.
414 $descriptionMsg = $extension['descriptionmsg'];
416 if( is_array( $descriptionMsg ) ) {
417 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
418 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
419 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
420 $msg = wfMsg( $descriptionMsgKey, $descriptionMsg );
421 } else {
422 $msg = wfMsg( $descriptionMsg );
424 if ( !wfEmptyMsg( $descriptionMsg, $msg ) && $msg != '' ) {
425 $description = $msg;
429 if ( $svnText !== false ) {
430 $extNameVer = "<tr>
431 <td><em>$mainLink $versionText</em></td>
432 <td><em>$svnText</em></td>";
433 } else {
434 $extNameVer = "<tr>
435 <td colspan=\"2\"><em>$mainLink $versionText</em></td>";
438 $author = isset ( $extension['author'] ) ? $extension['author'] : array();
439 $extDescAuthor = "<td>$description</td>
440 <td>" . $this->listToText( (array)$author, false ) . "</td>
441 </tr>\n";
443 return $extNameVer . $extDescAuthor;
447 * Generate wikitext showing hooks in $wgHooks.
449 * @return String: wikitext
451 private function getWgHooks() {
452 global $wgHooks;
454 if ( count( $wgHooks ) ) {
455 $myWgHooks = $wgHooks;
456 ksort( $myWgHooks );
458 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-hooks' ), wfMsg( 'version-hooks' ) ) .
459 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) ) .
460 "<tr>
461 <th>" . wfMsg( 'version-hook-name' ) . "</th>
462 <th>" . wfMsg( 'version-hook-subscribedby' ) . "</th>
463 </tr>\n";
465 foreach ( $myWgHooks as $hook => $hooks )
466 $ret .= "<tr>
467 <td>$hook</td>
468 <td>" . $this->listToText( $hooks ) . "</td>
469 </tr>\n";
471 $ret .= Xml::closeElement( 'table' );
472 return $ret;
473 } else
474 return '';
477 private function openExtType( $text, $name = null ) {
478 $opt = array( 'colspan' => 4 );
479 $out = '';
481 if( $this->firstExtOpened ) {
482 // Insert a spacing line
483 $out .= '<tr class="sv-space">' . Html::element( 'td', $opt ) . "</tr>\n";
485 $this->firstExtOpened = true;
487 if( $name ) {
488 $opt['id'] = "sv-$name";
491 $out .= "<tr>" . Xml::element( 'th', $opt, $text ) . "</tr>\n";
493 return $out;
497 * Get information about client's IP address.
499 * @return String: HTML fragment
501 private function IPInfo() {
502 $ip = str_replace( '--', ' - ', htmlspecialchars( wfGetIP() ) );
503 return "<!-- visited from $ip -->\n" .
504 "<span style='display:none'>visited from $ip</span>";
508 * Convert an array of items into a list for display.
510 * @param $list Array of elements to display
511 * @param $sort Boolean: whether to sort the items in $list
513 * @return String
515 function listToText( $list, $sort = true ) {
516 $cnt = count( $list );
518 if ( $cnt == 1 ) {
519 // Enforce always returning a string
520 return (string)self::arrayToString( $list[0] );
521 } elseif ( $cnt == 0 ) {
522 return '';
523 } else {
524 global $wgLang;
525 if ( $sort ) {
526 sort( $list );
528 return $wgLang->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
533 * Convert an array or object to a string for display.
535 * @param $list Mixed: will convert an array to string if given and return
536 * the paramater unaltered otherwise
538 * @return Mixed
540 static function arrayToString( $list ) {
541 if( is_array( $list ) && count( $list ) == 1 )
542 $list = $list[0];
543 if( is_object( $list ) ) {
544 $class = get_class( $list );
545 return "($class)";
546 } elseif ( !is_array( $list ) ) {
547 return $list;
548 } else {
549 if( is_object( $list[0] ) )
550 $class = get_class( $list[0] );
551 else
552 $class = $list[0];
553 return "($class, {$list[1]})";
558 * Get an associative array of information about a given path, from its .svn
559 * subdirectory. Returns false on error, such as if the directory was not
560 * checked out with subversion.
562 * Returned keys are:
563 * Required:
564 * checkout-rev The revision which was checked out
565 * Optional:
566 * directory-rev The revision when the directory was last modified
567 * url The subversion URL of the directory
568 * repo-url The base URL of the repository
569 * viewvc-url A ViewVC URL pointing to the checked-out revision
571 public static function getSvnInfo( $dir ) {
572 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
573 $entries = $dir . '/.svn/entries';
575 if( !file_exists( $entries ) ) {
576 return false;
579 $lines = file( $entries );
580 if ( !count( $lines ) ) {
581 return false;
584 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
585 if( preg_match( '/^<\?xml/', $lines[0] ) ) {
586 // subversion is release <= 1.3
587 if( !function_exists( 'simplexml_load_file' ) ) {
588 // We could fall back to expat... YUCK
589 return false;
592 // SimpleXml whines about the xmlns...
593 wfSuppressWarnings();
594 $xml = simplexml_load_file( $entries );
595 wfRestoreWarnings();
597 if( $xml ) {
598 foreach( $xml->entry as $entry ) {
599 if( $xml->entry[0]['name'] == '' ) {
600 // The directory entry should always have a revision marker.
601 if( $entry['revision'] ) {
602 return array( 'checkout-rev' => intval( $entry['revision'] ) );
608 return false;
611 // Subversion is release 1.4 or above.
612 if ( count( $lines ) < 11 ) {
613 return false;
616 $info = array(
617 'checkout-rev' => intval( trim( $lines[3] ) ),
618 'url' => trim( $lines[4] ),
619 'repo-url' => trim( $lines[5] ),
620 'directory-rev' => intval( trim( $lines[10] ) )
623 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
624 $viewvc = str_replace(
625 $info['repo-url'],
626 self::$viewvcUrls[$info['repo-url']],
627 $info['url']
630 $viewvc .= '/?pathrev=';
631 $viewvc .= urlencode( $info['checkout-rev'] );
632 $info['viewvc-url'] = $viewvc;
635 return $info;
639 * Retrieve the revision number of a Subversion working directory.
641 * @param $dir String: directory of the svn checkout
643 * @return Integer: revision number as int
645 public static function getSvnRevision( $dir ) {
646 $info = self::getSvnInfo( $dir );
648 if ( $info === false ) {
649 return false;
650 } elseif ( isset( $info['checkout-rev'] ) ) {
651 return $info['checkout-rev'];
652 } else {
653 return false;