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