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