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
23 * @ingroup SpecialPage
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' );
51 public function execute( $par ) {
52 global $wgOut, $wgSpecialVersionShowHooks, $wgContLang;
55 $this->outputHeader();
56 $wgOut->allowClickjacking();
58 $wgOut->addHTML( Xml
::openElement( 'div',
59 array( 'dir' => $wgContLang->getDir() ) ) );
61 $this->getMediaWikiCredits() .
62 $this->softwareInformation() .
63 $this->getExtensionCredits();
64 if ( $wgSpecialVersionShowHooks ) {
65 $text .= $this->getWgHooks();
68 $wgOut->addWikiText( $text );
69 $wgOut->addHTML( $this->IPInfo() );
70 $wgOut->addHTML( '</div>' );
74 * Returns wiki text showing the license information.
78 private static function getMediaWikiCredits() {
79 $ret = Xml
::element( 'h2', array( 'id' => 'mw-version-license' ), wfMsg( 'version-license' ) );
81 // This text is always left-to-right.
84 " . self
::getCopyrightAndAuthorList() . "\n
85 " . wfMsg( 'version-license-info' );
88 return str_replace( "\t\t", '', $ret ) . "\n";
92 * Get the "MediaWiki is copyright 2001-20xx by lots of cool guys" text
96 public static function getCopyrightAndAuthorList() {
100 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
101 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
102 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
103 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
104 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
106 wfMsg( 'version-poweredby-others' )
109 return wfMsg( 'version-poweredby-credits', date( 'Y' ),
110 $wgLang->listToText( $authorList ) );
114 * Returns wiki text showing the third party software versions (apache, php, mysql).
118 static function softwareInformation() {
119 $dbr = wfGetDB( DB_SLAVE
);
121 // Put the software in an array of form 'name' => 'version'. All messages should
122 // be loaded here, so feel free to use wfMsg*() in the 'name'. Raw HTML or wikimarkup
125 $software['[http://www.mediawiki.org/ MediaWiki]'] = self
::getVersionLinked();
126 $software['[http://www.php.net/ PHP]'] = phpversion() . " (" . php_sapi_name() . ")";
127 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
129 // Allow a hook to add/remove items.
130 wfRunHooks( 'SoftwareInfo', array( &$software ) );
132 $out = Xml
::element( 'h2', array( 'id' => 'mw-version-software' ), wfMsg( 'version-software' ) ) .
133 Xml
::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-software' ) ) .
135 <th>" . wfMsg( 'version-software-product' ) . "</th>
136 <th>" . wfMsg( 'version-software-version' ) . "</th>
139 foreach( $software as $name => $version ) {
141 <td>" . $name . "</td>
142 <td>" . $version . "</td>
146 return $out . Xml
::closeElement( 'table' );
150 * Return a string of the MediaWiki version with SVN revision if available.
154 public static function getVersion( $flags = '' ) {
155 global $wgVersion, $IP;
156 wfProfileIn( __METHOD__
);
158 $info = self
::getSvnInfo( $IP );
160 $version = $wgVersion;
161 } elseif( $flags === 'nodb' ) {
162 $version = "$wgVersion (r{$info['checkout-rev']})";
164 $version = $wgVersion . ' ' .
166 'version-svn-revision',
167 isset( $info['directory-rev'] ) ?
$info['directory-rev'] : '',
168 $info['checkout-rev']
172 wfProfileOut( __METHOD__
);
177 * Return a wikitext-formatted string of the MediaWiki version with a link to
178 * the SVN revision if available.
182 public static function getVersionLinked() {
183 global $wgVersion, $IP;
184 wfProfileIn( __METHOD__
);
186 $info = self
::getSvnInfo( $IP );
188 if ( isset( $info['checkout-rev'] ) ) {
190 'version-svn-revision',
191 isset( $info['directory-rev'] ) ?
$info['directory-rev'] : '',
192 $info['checkout-rev']
195 if ( isset( $info['viewvc-url'] ) ) {
196 $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
198 $version = "$wgVersion $linkText";
201 $version = $wgVersion;
204 wfProfileOut( __METHOD__
);
209 * Returns an array with the base extension types.
210 * Type is stored as array key, the message as array value.
212 * TODO: ideally this would return all extension types, including
213 * those added by SpecialVersionExtensionTypes. This is not possible
214 * since this hook is passing along $this though.
220 public static function getExtensionTypes() {
221 if ( self
::$extensionTypes === false ) {
222 self
::$extensionTypes = array(
223 'specialpage' => wfMsg( 'version-specialpages' ),
224 'parserhook' => wfMsg( 'version-parserhooks' ),
225 'variable' => wfMsg( 'version-variables' ),
226 'media' => wfMsg( 'version-mediahandlers' ),
227 'antispam' => wfMsg( 'version-antispam' ),
228 'skin' => wfMsg( 'version-skins' ),
229 'other' => wfMsg( 'version-other' ),
232 wfRunHooks( 'ExtensionTypes', array( &self
::$extensionTypes ) );
235 return self
::$extensionTypes;
239 * Returns the internationalized name for an extension type.
243 * @param $type String
247 public static function getExtensionTypeName( $type ) {
248 $types = self
::getExtensionTypes();
249 return isset( $types[$type] ) ?
$types[$type] : $types['other'];
253 * Generate wikitext showing extensions name, URL, author and description.
255 * @return String: Wikitext
257 function getExtensionCredits() {
258 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunctions;
260 if ( !count( $wgExtensionCredits ) && !count( $wgExtensionFunctions ) && !count( $wgSkinExtensionFunctions ) ) {
264 $extensionTypes = self
::getExtensionTypes();
267 * @deprecated as of 1.17, use hook ExtensionTypes instead.
269 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
271 $out = Xml
::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
272 Xml
::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-ext' ) );
274 // Make sure the 'other' type is set to an array.
275 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
276 $wgExtensionCredits['other'] = array();
279 // Find all extensions that do not have a valid type and give them the type 'other'.
280 foreach ( $wgExtensionCredits as $type => $extensions ) {
281 if ( !array_key_exists( $type, $extensionTypes ) ) {
282 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
286 // Loop through the extension categories to display their extensions in the list.
287 foreach ( $extensionTypes as $type => $message ) {
288 if ( $type != 'other' ) {
289 $out .= $this->getExtensionCategory( $type, $message );
293 // We want the 'other' type to be last in the list.
294 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
296 if ( count( $wgExtensionFunctions ) ) {
297 $out .= $this->openExtType( wfMsg( 'version-extension-functions' ), 'extension-functions' );
298 $out .= '<tr><td colspan="4">' . $this->listToText( $wgExtensionFunctions ) . "</td></tr>\n";
301 $tags = $wgParser->getTags();
302 $cnt = count( $tags );
305 for ( $i = 0; $i < $cnt; ++
$i ) {
306 $tags[$i] = "<{$tags[$i]}>";
308 $out .= $this->openExtType( wfMsg( 'version-parser-extensiontags' ), 'parser-tags' );
309 $out .= '<tr><td colspan="4">' . $this->listToText( $tags ). "</td></tr>\n";
312 if( count( $fhooks = $wgParser->getFunctionHooks() ) ) {
313 $out .= $this->openExtType( wfMsg( 'version-parser-function-hooks' ), 'parser-function-hooks' );
314 $out .= '<tr><td colspan="4">' . $this->listToText( $fhooks ) . "</td></tr>\n";
317 if ( count( $wgSkinExtensionFunctions ) ) {
318 $out .= $this->openExtType( wfMsg( 'version-skin-extension-functions' ), 'skin-extension-functions' );
319 $out .= '<tr><td colspan="4">' . $this->listToText( $wgSkinExtensionFunctions ) . "</td></tr>\n";
322 $out .= Xml
::closeElement( 'table' );
328 * Creates and returns the HTML for a single extension category.
332 * @param $type String
333 * @param $message String
337 protected function getExtensionCategory( $type, $message ) {
338 global $wgExtensionCredits;
342 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
343 $out .= $this->openExtType( $message, 'credits-' . $type );
345 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
347 foreach ( $wgExtensionCredits[$type] as $extension ) {
348 $out .= $this->getCreditsForExtension( $extension );
356 * Callback to sort extensions by type.
358 function compare( $a, $b ) {
360 if( $a['name'] === $b['name'] ) {
363 return $wgLang->lc( $a['name'] ) > $wgLang->lc( $b['name'] )
370 * Creates and formats the creidts for a single extension and returns this.
372 * @param $extension Array
376 function getCreditsForExtension( array $extension ) {
377 $name = isset( $extension['name'] ) ?
$extension['name'] : '[no name]';
379 if ( isset( $extension['path'] ) ) {
380 $svnInfo = self
::getSvnInfo( dirname($extension['path']) );
381 $directoryRev = isset( $svnInfo['directory-rev'] ) ?
$svnInfo['directory-rev'] : null;
382 $checkoutRev = isset( $svnInfo['checkout-rev'] ) ?
$svnInfo['checkout-rev'] : null;
383 $viewvcUrl = isset( $svnInfo['viewvc-url'] ) ?
$svnInfo['viewvc-url'] : null;
385 $directoryRev = null;
390 # Make main link (or just the name if there is no URL).
391 if ( isset( $extension['url'] ) ) {
392 $mainLink = "[{$extension['url']} $name]";
397 if ( isset( $extension['version'] ) ) {
398 $versionText = '<span class="mw-version-ext-version">' .
399 wfMsg( 'version-version', $extension['version'] ) .
405 # Make subversion text/link.
406 if ( $checkoutRev ) {
407 $svnText = wfMsg( 'version-svn-revision', $directoryRev, $checkoutRev );
408 $svnText = isset( $viewvcUrl ) ?
"[$viewvcUrl $svnText]" : $svnText;
413 # Make description text.
414 $description = isset ( $extension['description'] ) ?
$extension['description'] : '';
416 if( isset ( $extension['descriptionmsg'] ) ) {
417 # Look for a localized description.
418 $descriptionMsg = $extension['descriptionmsg'];
420 if( is_array( $descriptionMsg ) ) {
421 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
422 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
423 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
424 $description = wfMsg( $descriptionMsgKey, $descriptionMsg );
426 $description = wfMsg( $descriptionMsg );
430 if ( $svnText !== false ) {
432 <td><em>$mainLink $versionText</em></td>
433 <td><em>$svnText</em></td>";
436 <td colspan=\"2\"><em>$mainLink $versionText</em></td>";
439 $author = isset ( $extension['author'] ) ?
$extension['author'] : array();
440 $extDescAuthor = "<td>$description</td>
441 <td>" . $this->listToText( (array)$author, false ) . "</td>
444 return $extNameVer . $extDescAuthor;
448 * Generate wikitext showing hooks in $wgHooks.
450 * @return String: wikitext
452 private function getWgHooks() {
455 if ( count( $wgHooks ) ) {
456 $myWgHooks = $wgHooks;
459 $ret = Xml
::element( 'h2', array( 'id' => 'mw-version-hooks' ), wfMsg( 'version-hooks' ) ) .
460 Xml
::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) ) .
462 <th>" . wfMsg( 'version-hook-name' ) . "</th>
463 <th>" . wfMsg( 'version-hook-subscribedby' ) . "</th>
466 foreach ( $myWgHooks as $hook => $hooks )
469 <td>" . $this->listToText( $hooks ) . "</td>
472 $ret .= Xml
::closeElement( 'table' );
478 private function openExtType( $text, $name = null ) {
479 $opt = array( 'colspan' => 4 );
482 if( $this->firstExtOpened
) {
483 // Insert a spacing line
484 $out .= '<tr class="sv-space">' . Html
::element( 'td', $opt ) . "</tr>\n";
486 $this->firstExtOpened
= true;
489 $opt['id'] = "sv-$name";
492 $out .= "<tr>" . Xml
::element( 'th', $opt, $text ) . "</tr>\n";
498 * Get information about client's IP address.
500 * @return String: HTML fragment
502 private function IPInfo() {
503 $ip = str_replace( '--', ' - ', htmlspecialchars( wfGetIP() ) );
504 return "<!-- visited from $ip -->\n" .
505 "<span style='display:none'>visited from $ip</span>";
509 * Convert an array of items into a list for display.
511 * @param $list Array of elements to display
512 * @param $sort Boolean: whether to sort the items in $list
516 function listToText( $list, $sort = true ) {
517 $cnt = count( $list );
520 // Enforce always returning a string
521 return (string)self
::arrayToString( $list[0] );
522 } elseif ( $cnt == 0 ) {
529 return $wgLang->listToText( array_map( array( __CLASS__
, 'arrayToString' ), $list ) );
534 * Convert an array or object to a string for display.
536 * @param $list Mixed: will convert an array to string if given and return
537 * the paramater unaltered otherwise
541 static function arrayToString( $list ) {
542 if( is_array( $list ) && count( $list ) == 1 )
544 if( is_object( $list ) ) {
545 $class = get_class( $list );
547 } elseif ( !is_array( $list ) ) {
550 if( is_object( $list[0] ) )
551 $class = get_class( $list[0] );
554 return "($class, {$list[1]})";
559 * Get an associative array of information about a given path, from its .svn
560 * subdirectory. Returns false on error, such as if the directory was not
561 * checked out with subversion.
565 * checkout-rev The revision which was checked out
567 * directory-rev The revision when the directory was last modified
568 * url The subversion URL of the directory
569 * repo-url The base URL of the repository
570 * viewvc-url A ViewVC URL pointing to the checked-out revision
572 public static function getSvnInfo( $dir ) {
573 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
574 $entries = $dir . '/.svn/entries';
576 if( !file_exists( $entries ) ) {
580 $lines = file( $entries );
581 if ( !count( $lines ) ) {
585 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
586 if( preg_match( '/^<\?xml/', $lines[0] ) ) {
587 // subversion is release <= 1.3
588 if( !function_exists( 'simplexml_load_file' ) ) {
589 // We could fall back to expat... YUCK
593 // SimpleXml whines about the xmlns...
594 wfSuppressWarnings();
595 $xml = simplexml_load_file( $entries );
599 foreach( $xml->entry
as $entry ) {
600 if( $xml->entry
[0]['name'] == '' ) {
601 // The directory entry should always have a revision marker.
602 if( $entry['revision'] ) {
603 return array( 'checkout-rev' => intval( $entry['revision'] ) );
612 // Subversion is release 1.4 or above.
613 if ( count( $lines ) < 11 ) {
618 'checkout-rev' => intval( trim( $lines[3] ) ),
619 'url' => trim( $lines[4] ),
620 'repo-url' => trim( $lines[5] ),
621 'directory-rev' => intval( trim( $lines[10] ) )
624 if ( isset( self
::$viewvcUrls[$info['repo-url']] ) ) {
625 $viewvc = str_replace(
627 self
::$viewvcUrls[$info['repo-url']],
631 $viewvc .= '/?pathrev=';
632 $viewvc .= urlencode( $info['checkout-rev'] );
633 $info['viewvc-url'] = $viewvc;
640 * Retrieve the revision number of a Subversion working directory.
642 * @param $dir String: directory of the svn checkout
644 * @return Integer: revision number as int
646 public static function getSvnRevision( $dir ) {
647 $info = self
::getSvnInfo( $dir );
649 if ( $info === false ) {
651 } elseif ( isset( $info['checkout-rev'] ) ) {
652 return $info['checkout-rev'];