Get rid of PHP4-style constructors
[mediawiki.git] / includes / specials / SpecialVersion.php
blob82d2b7da2497d29939d2cd4e767880c6ae0c68bc
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 global $wgLang;
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( 'version-poweredby-others' )
87 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMsg( 'version-license' ) );
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>';
97 return str_replace( "\t\t", '', $ret ) . "\n";
101 * Returns wiki text showing the third party software versions (apache, php, mysql).
103 * @return string
105 static function softwareInformation() {
106 $dbr = wfGetDB( DB_SLAVE );
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();
116 // Allow a hook to add/remove items.
117 wfRunHooks( 'SoftwareInfo', array( &$software ) );
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";
126 foreach( $software as $name => $version ) {
127 $out .= "<tr>
128 <td>" . $name . "</td>
129 <td>" . $version . "</td>
130 </tr>\n";
133 return $out . Xml::closeElement( 'table' );
137 * Return a string of the MediaWiki version with SVN revision if available.
139 * @return mixed
141 public static function getVersion( $flags = '' ) {
142 global $wgVersion, $IP;
143 wfProfileIn( __METHOD__ );
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']
159 wfProfileOut( __METHOD__ );
160 return $version;
164 * Return a wikitext-formatted string of the MediaWiki version with a link to
165 * the SVN revision if available.
167 * @return mixed
169 public static function getVersionLinked() {
170 global $wgVersion, $IP;
171 wfProfileIn( __METHOD__ );
173 $info = self::getSvnInfo( $IP );
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']
182 if ( isset( $info['viewvc-url'] ) ) {
183 $version = "$wgVersion [{$info['viewvc-url']} $linkText]";
184 } else {
185 $version = "$wgVersion $linkText";
187 } else {
188 $version = $wgVersion;
191 wfProfileOut( __METHOD__ );
192 return $version;
196 * Returns an array with the base extension types.
197 * Type is stored as array key, the message as array value.
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.
203 * @since 1.17
205 * @return array
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' ),
217 wfRunHooks( 'ExtensionTypes', array( &self::$extensionTypes ) );
220 return self::$extensionTypes;
224 * Returns the internationalized name for an extension type.
226 * @since 1.17
228 * @param $type String
230 * @return string
232 public static function getExtensionTypeName( $type ) {
233 $types = self::getExtensionTypes();
234 return $types[$type];
238 * Generate wikitext showing extensions name, URL, author and description.
240 * @return String: Wikitext
242 function getExtensionCredits() {
243 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunctions;
245 if ( !count( $wgExtensionCredits ) && !count( $wgExtensionFunctions ) && !count( $wgSkinExtensionFunctions ) ) {
246 return '';
249 $extensionTypes = self::getExtensionTypes();
252 * @deprecated as of 1.17, use hook ExtensionTypes instead.
254 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
256 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
257 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-ext' ) );
259 // Make sure the 'other' type is set to an array.
260 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
261 $wgExtensionCredits['other'] = array();
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 );
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 );
278 // We want the 'other' type to be last in the list.
279 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
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";
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";
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";
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";
303 $out .= Xml::closeElement( 'table' );
305 return $out;
309 * Creates and returns the HTML for a single extension category.
311 * @since 1.17
313 * @param $type String
314 * @param $message String
316 * @return string
318 protected function getExtensionCategory( $type, $message ) {
319 global $wgExtensionCredits;
321 $out = '';
323 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
324 $out .= $this->openExtType( $message, 'credits-' . $type );
326 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
328 foreach ( $wgExtensionCredits[$type] as $extension ) {
329 $out .= $this->getCreditsForExtension( $extension );
333 return $out;
337 * Callback to sort extensions by type.
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'] )
346 : -1;
351 * Creates and formats the creidts for a single extension and returns this.
353 * @param $extension Array
355 * @return string
357 function getCreditsForExtension( array $extension ) {
358 $name = isset( $extension['name'] ) ? $extension['name'] : '[no name]';
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;
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;
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 = '';
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;
394 # Make description text.
395 $description = isset ( $extension['description'] ) ? $extension['description'] : '';
397 if( isset ( $extension['descriptionmsg'] ) ) {
398 # Look for a localized description.
399 $descriptionMsg = $extension['descriptionmsg'];
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 );
409 if ( !wfEmptyMsg( $descriptionMsg, $msg ) && $msg != '' ) {
410 $description = $msg;
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>";
423 $author = isset ( $extension['author'] ) ? $extension['author'] : array();
424 $extDescAuthor = "<td>$description</td>
425 <td>" . $this->listToText( (array)$author, false ) . "</td>
426 </tr>\n";
428 return $extNameVer . $extDescAuthor;
432 * Generate wikitext showing hooks in $wgHooks.
434 * @return String: wikitext
436 private function getWgHooks() {
437 global $wgHooks;
439 if ( count( $wgHooks ) ) {
440 $myWgHooks = $wgHooks;
441 ksort( $myWgHooks );
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";
450 foreach ( $myWgHooks as $hook => $hooks )
451 $ret .= "<tr>
452 <td>$hook</td>
453 <td>" . $this->listToText( $hooks ) . "</td>
454 </tr>\n";
456 $ret .= Xml::closeElement( 'table' );
457 return $ret;
458 } else
459 return '';
462 private function openExtType( $text, $name = null ) {
463 $opt = array( 'colspan' => 4 );
464 $out = '';
466 if( $this->firstExtOpened ) {
467 // Insert a spacing line
468 $out .= '<tr class="sv-space">' . Html::element( 'td', $opt ) . "</tr>\n";
470 $this->firstExtOpened = true;
472 if( $name ) {
473 $opt['id'] = "sv-$name";
476 $out .= "<tr>" . Xml::element( 'th', $opt, $text ) . "</tr>\n";
478 return $out;
482 * Get information about client's IP address.
484 * @return String: HTML fragment
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>";
493 * Convert an array of items into a list for display.
495 * @param $list Array of elements to display
496 * @param $sort Boolean: whether to sort the items in $list
498 * @return String
500 function listToText( $list, $sort = true ) {
501 $cnt = count( $list );
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 );
513 return $wgLang->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
518 * Convert an array or object to a string for display.
520 * @param $list Mixed: will convert an array to string if given and return
521 * the paramater unaltered otherwise
523 * @return Mixed
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]})";
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.
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
556 public static function getSvnInfo( $dir ) {
557 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
558 $entries = $dir . '/.svn/entries';
560 if( !file_exists( $entries ) ) {
561 return false;
564 $lines = file( $entries );
565 if ( !count( $lines ) ) {
566 return false;
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;
577 // SimpleXml whines about the xmlns...
578 wfSuppressWarnings();
579 $xml = simplexml_load_file( $entries );
580 wfRestoreWarnings();
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'] ) );
593 return false;
596 // Subversion is release 1.4 or above.
597 if ( count( $lines ) < 11 ) {
598 return false;
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] ) )
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']
615 $viewvc .= '/?pathrev=';
616 $viewvc .= urlencode( $info['checkout-rev'] );
617 $info['viewvc-url'] = $viewvc;
620 return $info;
624 * Retrieve the revision number of a Subversion working directory.
626 * @param $dir String: directory of the svn checkout
628 * @return Integer: revision number as int
630 public static function getSvnRevision( $dir ) {
631 $info = self::getSvnInfo( $dir );
633 if ( $info === false ) {
634 return false;
635 } elseif ( isset( $info['checkout-rev'] ) ) {
636 return $info['checkout-rev'];
637 } else {
638 return false;