* Reorganised the includes directory, creating subdirectories db, parser and specials
[lhc/web/wiklou.git] / includes / specials / Version.php
1 <?php
2 /**#@+
3 * Give information about the version of MediaWiki, PHP, the DB and extensions
4 *
5 * @file
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
13 /**
14 * constructor
15 */
16 function wfSpecialVersion() {
17 $version = new SpecialVersion;
18 $version->execute();
19 }
20
21 /**
22 * @ingroup SpecialPage
23 */
24 class SpecialVersion {
25 private $firstExtOpened = true;
26
27 /**
28 * main()
29 */
30 function execute() {
31 global $wgOut, $wgMessageCache, $wgSpecialVersionShowHooks;
32 $wgMessageCache->loadAllMessages();
33
34 $wgOut->addHTML( '<div dir="ltr">' );
35 $text =
36 $this->MediaWikiCredits() .
37 $this->softwareInformation() .
38 $this->extensionCredits();
39 if ( $wgSpecialVersionShowHooks ) {
40 $text .= $this->wgHooks();
41 }
42 $wgOut->addWikiText( $text );
43 $wgOut->addHTML( $this->IPInfo() );
44 $wgOut->addHTML( '</div>' );
45 }
46
47 /**#@+
48 * @private
49 */
50
51 /**
52 * @return wiki text showing the license information
53 */
54 static function MediaWikiCredits() {
55 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMsg( 'version-license' ) ) .
56 "__NOTOC__
57 This wiki is powered by '''[http://www.mediawiki.org/ MediaWiki]''',
58 copyright (C) 2001-2008 Magnus Manske, Brion Vibber, Lee Daniel Crocker,
59 Tim Starling, Erik Möller, Gabriel Wicke, Ævar Arnfjörð Bjarmason,
60 Niklas Laxström, Domas Mituzas, Rob Church, Yuri Astrakhan and others.
61
62 MediaWiki is free software; you can redistribute it and/or modify
63 it under the terms of the GNU General Public License as published by
64 the Free Software Foundation; either version 2 of the License, or
65 (at your option) any later version.
66
67 MediaWiki is distributed in the hope that it will be useful,
68 but WITHOUT ANY WARRANTY; without even the implied warranty of
69 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70 GNU General Public License for more details.
71
72 You should have received [{{SERVER}}{{SCRIPTPATH}}/COPYING a copy of the GNU General Public License]
73 along with this program; if not, write to the Free Software
74 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
75 or [http://www.gnu.org/licenses/old-licenses/gpl-2.0.html read it online].
76 ";
77
78 return str_replace( "\t\t", '', $ret ) . "\n";
79 }
80
81 /**
82 * @return wiki text showing the third party software versions (apache, php, mysql).
83 */
84 static function softwareInformation() {
85 $dbr = wfGetDB( DB_SLAVE );
86
87 return Xml::element( 'h2', array( 'id' => 'mw-version-software' ), wfMsg( 'version-software' ) ) .
88 Xml::openElement( 'table', array( 'id' => 'sv-software' ) ) .
89 "<tr>
90 <th>" . wfMsg( 'version-software-product' ) . "</th>
91 <th>" . wfMsg( 'version-software-version' ) . "</th>
92 </tr>\n
93 <tr>
94 <td>[http://www.mediawiki.org/ MediaWiki]</td>
95 <td>" . self::getVersionLinked() . "</td>
96 </tr>\n
97 <tr>
98 <td>[http://www.php.net/ PHP]</td>
99 <td>" . phpversion() . " (" . php_sapi_name() . ")</td>
100 </tr>\n
101 <tr>
102 <td>" . $dbr->getSoftwareLink() . "</td>
103 <td>" . $dbr->getServerVersion() . "</td>
104 </tr>\n" .
105 Xml::closeElement( 'table' );
106 }
107
108 /**
109 * Return a string of the MediaWiki version with SVN revision if available
110 *
111 * @return mixed
112 */
113 public static function getVersion() {
114 global $wgVersion, $IP;
115 wfProfileIn( __METHOD__ );
116 $svn = self::getSvnRevision( $IP );
117 $version = $svn ? "$wgVersion (r$svn)" : $wgVersion;
118 wfProfileOut( __METHOD__ );
119 return $version;
120 }
121
122 /**
123 * Return a string of the MediaWiki version with a link to SVN revision if
124 * available
125 *
126 * @return mixed
127 */
128 public static function getVersionLinked() {
129 global $wgVersion, $IP;
130 wfProfileIn( __METHOD__ );
131 $svn = self::getSvnRevision( $IP );
132 $viewvc = 'http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/?pathrev=';
133 $version = $svn ? "$wgVersion ([{$viewvc}{$svn} r$svn])" : $wgVersion;
134 wfProfileOut( __METHOD__ );
135 return $version;
136 }
137
138 /** Generate wikitext showing extensions name, URL, author and description */
139 function extensionCredits() {
140 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunctions;
141
142 if ( ! count( $wgExtensionCredits ) && ! count( $wgExtensionFunctions ) && ! count( $wgSkinExtensionFunctions ) )
143 return '';
144
145 $extensionTypes = array(
146 'specialpage' => wfMsg( 'version-specialpages' ),
147 'parserhook' => wfMsg( 'version-parserhooks' ),
148 'variable' => wfMsg( 'version-variables' ),
149 'media' => wfMsg( 'version-mediahandlers' ),
150 'other' => wfMsg( 'version-other' ),
151 );
152 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
153
154 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
155 Xml::openElement( 'table', array( 'id' => 'sv-ext' ) );
156
157 foreach ( $extensionTypes as $type => $text ) {
158 if ( isset ( $wgExtensionCredits[$type] ) && count ( $wgExtensionCredits[$type] ) ) {
159 $out .= $this->openExtType( $text );
160
161 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
162
163 foreach ( $wgExtensionCredits[$type] as $extension ) {
164 if ( isset( $extension['version'] ) ) {
165 $version = $extension['version'];
166 } elseif ( isset( $extension['svn-revision'] ) &&
167 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
168 $extension['svn-revision'], $m ) )
169 {
170 $version = 'r' . $m[1];
171 } else {
172 $version = null;
173 }
174
175 $out .= $this->formatCredits(
176 isset ( $extension['name'] ) ? $extension['name'] : '',
177 $version,
178 isset ( $extension['author'] ) ? $extension['author'] : '',
179 isset ( $extension['url'] ) ? $extension['url'] : null,
180 isset ( $extension['description'] ) ? $extension['description'] : '',
181 isset ( $extension['descriptionmsg'] ) ? $extension['descriptionmsg'] : ''
182 );
183 }
184 }
185 }
186
187 if ( count( $wgExtensionFunctions ) ) {
188 $out .= $this->openExtType( wfMsg( 'version-extension-functions' ) );
189 $out .= '<tr><td colspan="3">' . $this->listToText( $wgExtensionFunctions ) . "</td></tr>\n";
190 }
191
192 if ( $cnt = count( $tags = $wgParser->getTags() ) ) {
193 for ( $i = 0; $i < $cnt; ++$i )
194 $tags[$i] = "&lt;{$tags[$i]}&gt;";
195 $out .= $this->openExtType( wfMsg( 'version-parser-extensiontags' ) );
196 $out .= '<tr><td colspan="3">' . $this->listToText( $tags ). "</td></tr>\n";
197 }
198
199 if( $cnt = count( $fhooks = $wgParser->getFunctionHooks() ) ) {
200 $out .= $this->openExtType( wfMsg( 'version-parser-function-hooks' ) );
201 $out .= '<tr><td colspan="3">' . $this->listToText( $fhooks ) . "</td></tr>\n";
202 }
203
204 if ( count( $wgSkinExtensionFunctions ) ) {
205 $out .= $this->openExtType( wfMsg( 'version-skin-extension-functions' ) );
206 $out .= '<tr><td colspan="3">' . $this->listToText( $wgSkinExtensionFunctions ) . "</td></tr>\n";
207 }
208 $out .= Xml::closeElement( 'table' );
209 return $out;
210 }
211
212 /** Callback to sort extensions by type */
213 function compare( $a, $b ) {
214 global $wgLang;
215 if( $a['name'] === $b['name'] ) {
216 return 0;
217 } else {
218 return $wgLang->lc( $a['name'] ) > $wgLang->lc( $b['name'] )
219 ? 1
220 : -1;
221 }
222 }
223
224 function formatCredits( $name, $version = null, $author = null, $url = null, $description = null, $descriptionMsg = null ) {
225 $extension = isset( $url ) ? "[$url $name]" : $name;
226 $version = isset( $version ) ? "(" . wfMsg( 'version-version' ) . " $version)" : '';
227
228 # Look for a localized description
229 if( isset( $descriptionMsg ) ) {
230 $msg = wfMsg( $descriptionMsg );
231 if ( !wfEmptyMsg( $descriptionMsg, $msg ) && $msg != '' ) {
232 $description = $msg;
233 }
234 }
235
236 return "<tr>
237 <td><em>$extension $version</em></td>
238 <td>$description</td>
239 <td>" . $this->listToText( (array)$author ) . "</td>
240 </tr>\n";
241 }
242
243 /**
244 * @return string
245 */
246 function wgHooks() {
247 global $wgHooks;
248
249 if ( count( $wgHooks ) ) {
250 $myWgHooks = $wgHooks;
251 ksort( $myWgHooks );
252
253 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-hooks' ), wfMsg( 'version-hooks' ) ) .
254 Xml::openElement( 'table', array( 'id' => 'sv-hooks' ) ) .
255 "<tr>
256 <th>" . wfMsg( 'version-hook-name' ) . "</th>
257 <th>" . wfMsg( 'version-hook-subscribedby' ) . "</th>
258 </tr>\n";
259
260 foreach ( $myWgHooks as $hook => $hooks )
261 $ret .= "<tr>
262 <td>$hook</td>
263 <td>" . $this->listToText( $hooks ) . "</td>
264 </tr>\n";
265
266 $ret .= Xml::closeElement( 'table' );
267 return $ret;
268 } else
269 return '';
270 }
271
272 private function openExtType($text, $name = null) {
273 $opt = array( 'colspan' => 3 );
274 $out = '';
275
276 if(!$this->firstExtOpened) {
277 // Insert a spacing line
278 $out .= '<tr class="sv-space">' . Xml::element( 'td', $opt ) . "</tr>\n";
279 }
280 $this->firstExtOpened = false;
281
282 if($name) { $opt['id'] = "sv-$name"; }
283
284 $out .= "<tr>" . Xml::element( 'th', $opt, $text) . "</tr>\n";
285 return $out;
286 }
287
288 /**
289 * @static
290 *
291 * @return string
292 */
293 function IPInfo() {
294 $ip = str_replace( '--', ' - ', htmlspecialchars( wfGetIP() ) );
295 return "<!-- visited from $ip -->\n" .
296 "<span style='display:none'>visited from $ip</span>";
297 }
298
299 /**
300 * @param array $list
301 * @return string
302 */
303 function listToText( $list ) {
304 $cnt = count( $list );
305
306 if ( $cnt == 1 ) {
307 // Enforce always returning a string
308 return (string)$this->arrayToString( $list[0] );
309 } elseif ( $cnt == 0 ) {
310 return '';
311 } else {
312 sort( $list );
313 $t = array_slice( $list, 0, $cnt - 1 );
314 $one = array_map( array( &$this, 'arrayToString' ), $t );
315 $two = $this->arrayToString( $list[$cnt - 1] );
316 $and = wfMsg( 'and' );
317
318 return implode( ', ', $one ) . " $and $two";
319 }
320 }
321
322 /**
323 * @static
324 *
325 * @param mixed $list Will convert an array to string if given and return
326 * the paramater unaltered otherwise
327 * @return mixed
328 */
329 function arrayToString( $list ) {
330 if( is_object( $list ) ) {
331 $class = get_class( $list );
332 return "($class)";
333 } elseif ( ! is_array( $list ) ) {
334 return $list;
335 } else {
336 $class = get_class( $list[0] );
337 return "($class, {$list[1]})";
338 }
339 }
340
341 /**
342 * Retrieve the revision number of a Subversion working directory.
343 *
344 * @param string $dir
345 * @return mixed revision number as int, or false if not a SVN checkout
346 */
347 public static function getSvnRevision( $dir ) {
348 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
349 $entries = $dir . '/.svn/entries';
350
351 if( !file_exists( $entries ) ) {
352 return false;
353 }
354
355 $content = file( $entries );
356
357 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
358 if( preg_match( '/^<\?xml/', $content[0] ) ) {
359 // subversion is release <= 1.3
360 if( !function_exists( 'simplexml_load_file' ) ) {
361 // We could fall back to expat... YUCK
362 return false;
363 }
364
365 // SimpleXml whines about the xmlns...
366 wfSuppressWarnings();
367 $xml = simplexml_load_file( $entries );
368 wfRestoreWarnings();
369
370 if( $xml ) {
371 foreach( $xml->entry as $entry ) {
372 if( $xml->entry[0]['name'] == '' ) {
373 // The directory entry should always have a revision marker.
374 if( $entry['revision'] ) {
375 return intval( $entry['revision'] );
376 }
377 }
378 }
379 }
380 return false;
381 } else {
382 // subversion is release 1.4
383 return intval( $content[3] );
384 }
385 }
386
387 /**#@-*/
388 }
389
390 /**#@-*/