* (bug 21826) Subsections of Special:Version now also have anchors
[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-2009 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 ) . "</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 * @return string
373 */
374 function listToText( $list ) {
375 $cnt = count( $list );
376
377 if ( $cnt == 1 ) {
378 // Enforce always returning a string
379 return (string)self::arrayToString( $list[0] );
380 } elseif ( $cnt == 0 ) {
381 return '';
382 } else {
383 global $wgLang;
384 sort( $list );
385 return $wgLang->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
386 }
387 }
388
389 /**
390 * @param mixed $list Will convert an array to string if given and return
391 * the paramater unaltered otherwise
392 * @return mixed
393 */
394 static function arrayToString( $list ) {
395 if( is_array( $list ) && count( $list ) == 1 )
396 $list = $list[0];
397 if( is_object( $list ) ) {
398 $class = get_class( $list );
399 return "($class)";
400 } elseif ( !is_array( $list ) ) {
401 return $list;
402 } else {
403 if( is_object( $list[0] ) )
404 $class = get_class( $list[0] );
405 else
406 $class = $list[0];
407 return "($class, {$list[1]})";
408 }
409 }
410
411 /**
412 * Get an associative array of information about a given path, from its .svn
413 * subdirectory. Returns false on error, such as if the directory was not
414 * checked out with subversion.
415 *
416 * Returned keys are:
417 * Required:
418 * checkout-rev The revision which was checked out
419 * Optional:
420 * directory-rev The revision when the directory was last modified
421 * url The subversion URL of the directory
422 * repo-url The base URL of the repository
423 * viewvc-url A ViewVC URL pointing to the checked-out revision
424 */
425 public static function getSvnInfo( $dir ) {
426 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
427 $entries = $dir . '/.svn/entries';
428
429 if( !file_exists( $entries ) ) {
430 return false;
431 }
432
433 $lines = file( $entries );
434 if ( !count( $lines ) ) {
435 return false;
436 }
437
438 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
439 if( preg_match( '/^<\?xml/', $lines[0] ) ) {
440 // subversion is release <= 1.3
441 if( !function_exists( 'simplexml_load_file' ) ) {
442 // We could fall back to expat... YUCK
443 return false;
444 }
445
446 // SimpleXml whines about the xmlns...
447 wfSuppressWarnings();
448 $xml = simplexml_load_file( $entries );
449 wfRestoreWarnings();
450
451 if( $xml ) {
452 foreach( $xml->entry as $entry ) {
453 if( $xml->entry[0]['name'] == '' ) {
454 // The directory entry should always have a revision marker.
455 if( $entry['revision'] ) {
456 return array( 'checkout-rev' => intval( $entry['revision'] ) );
457 }
458 }
459 }
460 }
461 return false;
462 }
463
464 // subversion is release 1.4 or above
465 if ( count( $lines ) < 11 ) {
466 return false;
467 }
468 $info = array(
469 'checkout-rev' => intval( trim( $lines[3] ) ),
470 'url' => trim( $lines[4] ),
471 'repo-url' => trim( $lines[5] ),
472 'directory-rev' => intval( trim( $lines[10] ) )
473 );
474 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
475 $viewvc = str_replace(
476 $info['repo-url'],
477 self::$viewvcUrls[$info['repo-url']],
478 $info['url']
479 );
480 $pathRelativeToRepo = substr( $info['url'], strlen( $info['repo-url'] ) );
481 $viewvc .= '/?pathrev=';
482 $viewvc .= urlencode( $info['checkout-rev'] );
483 $info['viewvc-url'] = $viewvc;
484 }
485 return $info;
486 }
487
488 /**
489 * Retrieve the revision number of a Subversion working directory.
490 *
491 * @param String $dir Directory of the svn checkout
492 * @return int revision number as int
493 */
494 public static function getSvnRevision( $dir ) {
495 $info = self::getSvnInfo( $dir );
496 if ( $info === false ) {
497 return false;
498 } elseif ( isset( $info['checkout-rev'] ) ) {
499 return $info['checkout-rev'];
500 } else {
501 return false;
502 }
503 }
504
505 /**#@-*/
506 }