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