Revert r23471, r23474: restore Special:Version to working, non-ugly state
[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;
28
29 $wgOut->addHTML( '<div dir="ltr">' );
30 $wgOut->addWikiText(
31 $this->MediaWikiCredits() .
32 $this->extensionCredits() .
33 $this->wgHooks()
34 );
35 $wgOut->addHTML( $this->IPInfo() );
36 $wgOut->addHTML( '</div>' );
37 }
38
39 /**#@+
40 * @private
41 */
42
43 /**
44 * Return wiki text showing the licence information and third party
45 * software versions (apache, php, mysql).
46 * @static
47 */
48 function MediaWikiCredits() {
49 $version = self::getVersion();
50 $dbr = wfGetDB( DB_SLAVE );
51
52 $ret =
53 "__NOTOC__
54 This wiki is powered by '''[http://www.mediawiki.org/ MediaWiki]''',
55 copyright (C) 2001-2007 Magnus Manske, Brion Vibber, Lee Daniel Crocker,
56 Tim Starling, Erik Möller, Gabriel Wicke, Ævar Arnfjörð Bjarmason,
57 Niklas Laxström, Domas Mituzas, Rob Church and others.
58
59 MediaWiki is free software; you can redistribute it and/or modify
60 it under the terms of the GNU General Public License as published by
61 the Free Software Foundation; either version 2 of the License, or
62 (at your option) any later version.
63
64 MediaWiki is distributed in the hope that it will be useful,
65 but WITHOUT ANY WARRANTY; without even the implied warranty of
66 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 GNU General Public License for more details.
68
69 You should have received [{{SERVER}}{{SCRIPTPATH}}/COPYING a copy of the GNU General Public License]
70 along with this program; if not, write to the Free Software
71 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
72 or [http://www.gnu.org/copyleft/gpl.html read it online]
73
74 * [http://www.mediawiki.org/ MediaWiki]: $version
75 * [http://www.php.net/ PHP]: " . phpversion() . " (" . php_sapi_name() . ")
76 * " . $dbr->getSoftwareLink() . ": " . $dbr->getServerVersion();
77
78 return str_replace( "\t\t", '', $ret ) . "\n";
79 }
80
81 /** Return a string of the MediaWiki version with SVN revision if available */
82 public static function getVersion() {
83 global $wgVersion, $IP;
84 $svn = self::getSvnRevision( $IP );
85 return $svn ? "$wgVersion (r$svn)" : $wgVersion;
86 }
87
88 /** Generate wikitext showing extensions name, URL, author and description */
89 function extensionCredits() {
90 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser, $wgSkinExtensionFunction;
91
92 if ( ! count( $wgExtensionCredits ) && ! count( $wgExtensionFunctions ) && ! count( $wgSkinExtensionFunction ) )
93 return '';
94
95 $extensionTypes = array(
96 'specialpage' => 'Special pages',
97 'parserhook' => 'Parser hooks',
98 'variable' => 'Variables',
99 'other' => 'Other',
100 );
101 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
102
103 $out = "<h2>Extensions</h2>\n";
104 $out .= wfOpenElement('table', array('id' => 'sv-ext') );
105
106 foreach ( $extensionTypes as $type => $text ) {
107 if ( isset ( $wgExtensionCredits[$type] ) && count ( $wgExtensionCredits[$type] ) ) {
108 $out .= $this->openExtType( $text );
109
110 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
111
112 foreach ( $wgExtensionCredits[$type] as $extension ) {
113 $out .= $this->formatCredits(
114 isset ( $extension['name'] ) ? $extension['name'] : '',
115 isset ( $extension['version'] ) ? $extension['version'] : null,
116 isset ( $extension['author'] ) ? $extension['author'] : '',
117 isset ( $extension['url'] ) ? $extension['url'] : null,
118 isset ( $extension['description'] ) ? $extension['description'] : ''
119 );
120 }
121 }
122 }
123
124 if ( count( $wgExtensionFunctions ) ) {
125 $out .= $this->openExtType('Extension functions');
126 $out .= '<tr><td colspan="3">' . $this->listToText( $wgExtensionFunctions ) . "</td></tr>\n";
127 }
128
129 if ( $cnt = count( $tags = $wgParser->getTags() ) ) {
130 for ( $i = 0; $i < $cnt; ++$i )
131 $tags[$i] = "&lt;{$tags[$i]}&gt;";
132 $out .= $this->openExtType('Parser extension tags');
133 $out .= '<tr><td colspan="3">' . $this->listToText( $tags ). "</td></tr>\n";
134 }
135
136 if( $cnt = count( $fhooks = $wgParser->getFunctionHooks() ) ) {
137 $out .= $this->openExtType('Parser function hooks');
138 $out .= '<tr><td colspan="3">' . $this->listToText( $fhooks ) . "</td></tr>\n";
139 }
140
141 if ( count( $wgSkinExtensionFunction ) ) {
142 $out .= $this->openExtType('Skin extension functions');
143 $out .= '<tr><td colspan="3">' . $this->listToText( $wgSkinExtensionFunction ) . "</td></tr>\n";
144 }
145 $out .= wfCloseElement( 'table' );
146 return $out;
147 }
148
149 /** Callback to sort extensions by type */
150 function compare( $a, $b ) {
151 if ( $a['name'] === $b['name'] )
152 return 0;
153 else
154 return Language::lc( $a['name'] ) > Language::lc( $b['name'] ) ? 1 : -1;
155 }
156
157 function formatCredits( $name, $version = null, $author = null, $url = null, $description = null) {
158 $ret = '<tr><td>';
159 if ( isset( $url ) )
160 $ret .= "[$url ";
161 $ret .= "''$name";
162 if ( isset( $version ) )
163 $ret .= " (version $version)";
164 $ret .= "''";
165 if ( isset( $url ) )
166 $ret .= ']';
167 $ret .= '</td>';
168 $ret .= "<td>$description</td>";
169 $ret .= "<td>" . $this->listToText( (array)$author ) . "</td>";
170 $ret .= '</tr>';
171 return "$ret\n";
172 }
173
174 /**
175 * @return string
176 */
177 function wgHooks() {
178 global $wgHooks;
179
180 if ( count( $wgHooks ) ) {
181 $myWgHooks = $wgHooks;
182 ksort( $myWgHooks );
183
184 $ret = "<h2>Hooks</h2>\n"
185 . wfOpenElement('table', array('id' => 'sv-hooks') )
186 . "<tr><th>Hook name</th><th>Subscribed by</th></tr>\n";
187
188 foreach ($myWgHooks as $hook => $hooks)
189 $ret .= "<tr><td>$hook</td><td>" . $this->listToText( $hooks ) . "</td></tr>\n";
190
191 $ret .= '</table>';
192 return $ret;
193 } else
194 return '';
195 }
196
197 private function openExtType($text, $name = null) {
198 $opt = array( 'colspan' => 3 );
199 $out = '';
200
201 if(!$this->firstExtOpened) {
202 // Insert a spacing line
203 $out .= '<tr class="sv-space">' . wfElement( 'td', $opt ) . "</tr>\n";
204 }
205 $this->firstExtOpened = false;
206
207 if($name) { $opt['id'] = "sv-$name"; }
208
209 $out .= "<tr>" . wfElement( 'th', $opt, $text) . "</tr>\n";
210 return $out;
211 }
212
213 /**
214 * @static
215 *
216 * @return string
217 */
218 function IPInfo() {
219 $ip = str_replace( '--', ' - ', htmlspecialchars( wfGetIP() ) );
220 return "<!-- visited from $ip -->\n" .
221 "<span style='display:none'>visited from $ip</span>";
222 }
223
224 /**
225 * @param array $list
226 * @return string
227 */
228 function listToText( $list ) {
229 $cnt = count( $list );
230
231 if ( $cnt == 1 ) {
232 // Enforce always returning a string
233 return (string)$this->arrayToString( $list[0] );
234 } elseif ( $cnt == 0 ) {
235 return '';
236 } else {
237 $t = array_slice( $list, 0, $cnt - 1 );
238 $one = array_map( array( &$this, 'arrayToString' ), $t );
239 $two = $this->arrayToString( $list[$cnt - 1] );
240
241 return implode( ', ', $one ) . " and $two";
242 }
243 }
244
245 /**
246 * @static
247 *
248 * @param mixed $list Will convert an array to string if given and return
249 * the paramater unaltered otherwise
250 * @return mixed
251 */
252 function arrayToString( $list ) {
253 if( is_object( $list ) ) {
254 $class = get_class( $list );
255 return "($class)";
256 } elseif ( ! is_array( $list ) ) {
257 return $list;
258 } else {
259 $class = get_class( $list[0] );
260 return "($class, {$list[1]})";
261 }
262 }
263
264 /**
265 * Retrieve the revision number of a Subversion working directory.
266 *
267 * @param string $dir
268 * @return mixed revision number as int, or false if not a SVN checkout
269 */
270 public static function getSvnRevision( $dir ) {
271 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
272 $entries = $dir . '/.svn/entries';
273
274 if( !file_exists( $entries ) ) {
275 return false;
276 }
277
278 $content = file( $entries );
279
280 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
281 if( preg_match( '/^<\?xml/', $content[0] ) ) {
282 // subversion is release <= 1.3
283 if( !function_exists( 'simplexml_load_file' ) ) {
284 // We could fall back to expat... YUCK
285 return false;
286 }
287
288 // SimpleXml whines about the xmlns...
289 wfSuppressWarnings();
290 $xml = simplexml_load_file( $entries );
291 wfRestoreWarnings();
292
293 if( $xml ) {
294 foreach( $xml->entry as $entry ) {
295 if( $xml->entry[0]['name'] == '' ) {
296 // The directory entry should always have a revision marker.
297 if( $entry['revision'] ) {
298 return intval( $entry['revision'] );
299 }
300 }
301 }
302 }
303 return false;
304 } else {
305 // subversion is release 1.4
306 return intval( $content[3] );
307 }
308 }
309
310 /**#@-*/
311 }
312
313 /**#@-*/
314 ?>