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