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