Merge "Fix documentation of wfDebugLog()"
[lhc/web/wiklou.git] / includes / specials / SpecialVersion.php
1 <?php
2 /**
3 * Implements Special:Version
4 *
5 * Copyright © 2005 Ævar Arnfjörð Bjarmason
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Give information about the version of MediaWiki, PHP, the DB and extensions
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialVersion extends SpecialPage {
32
33 protected $firstExtOpened = false;
34
35 protected static $extensionTypes = false;
36
37 protected static $viewvcUrls = array(
38 'svn+ssh://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
39 'http://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
40 'https://svn.wikimedia.org/svnroot/mediawiki' => 'https://svn.wikimedia.org/viewvc/mediawiki',
41 );
42
43 public function __construct() {
44 parent::__construct( 'Version' );
45 }
46
47 /**
48 * main()
49 */
50 public function execute( $par ) {
51 global $IP, $wgExtensionCredits;
52
53 $this->setHeaders();
54 $this->outputHeader();
55 $out = $this->getOutput();
56 $out->allowClickjacking();
57
58 // Explode the sub page information into useful bits
59 $parts = explode( '/', (string)$par );
60 $extNode = null;
61 if ( isset( $parts[1] ) ) {
62 $extName = str_replace( '_', ' ', $parts[1] );
63 // Find it!
64 foreach ( $wgExtensionCredits as $group => $extensions ) {
65 foreach ( $extensions as $ext ) {
66 if ( isset( $ext['name'] ) && ( $ext['name'] === $extName ) ) {
67 $extNode = &$ext;
68 break 2;
69 }
70 }
71 }
72 if ( !$extNode ) {
73 $out->setStatusCode( 404 );
74 }
75 } else {
76 $extName = 'MediaWiki';
77 }
78
79 // Now figure out what to do
80 switch ( strtolower( $parts[0] ) ) {
81 case 'credits':
82 $wikiText = '{{int:version-credits-not-found}}';
83 if ( $extName === 'MediaWiki' ) {
84 $wikiText = file_get_contents( $IP . '/CREDITS' );
85 } elseif ( ( $extNode !== null ) && isset( $extNode['path'] ) ) {
86 $file = $this->getExtAuthorsFileName( dirname( $extNode['path'] ) );
87 if ( $file ) {
88 $wikiText = file_get_contents( $file );
89 if ( substr( $file, -4 ) === '.txt' ) {
90 $wikiText = Html::element( 'pre', array(), $wikiText );
91 }
92 }
93 }
94
95 $out->setPageTitle( $this->msg( 'version-credits-title', $extName ) );
96 $out->addWikiText( $wikiText );
97 break;
98
99 case 'license':
100 $wikiText = '{{int:version-license-not-found}}';
101 if ( $extName === 'MediaWiki' ) {
102 $wikiText = file_get_contents( $IP . '/COPYING' );
103 } elseif ( ( $extNode !== null ) && isset( $extNode['path'] ) ) {
104 $file = $this->getExtLicenseFileName( dirname( $extNode['path'] ) );
105 if ( $file ) {
106 $wikiText = file_get_contents( $file );
107 if ( !isset( $extNode['license-name'] ) ) {
108 // If the developer did not explicitly set license-name they probably
109 // are unaware that we're now sucking this file in and thus it's probably
110 // not wikitext friendly.
111 $wikiText = "<pre>$wikiText</pre>";
112 }
113 }
114 }
115
116 $out->setPageTitle( $this->msg( 'version-license-title', $extName ) );
117 $out->addWikiText( $wikiText );
118 break;
119
120 default:
121 $out->addModules( 'mediawiki.special.version' );
122 $out->addWikiText(
123 $this->getMediaWikiCredits() .
124 $this->softwareInformation() .
125 $this->getEntryPointInfo()
126 );
127 $out->addHtml(
128 $this->getExtensionCredits() .
129 $this->getParserTags() .
130 $this->getParserFunctionHooks()
131 );
132 $out->addWikiText( $this->getWgHooks() );
133 $out->addHTML( $this->IPInfo() );
134
135 break;
136 }
137 }
138
139 /**
140 * Returns wiki text showing the license information.
141 *
142 * @return string
143 */
144 private static function getMediaWikiCredits() {
145 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMessage( 'version-license' )->text() );
146
147 // This text is always left-to-right.
148 $ret .= '<div class="plainlinks">';
149 $ret .= "__NOTOC__
150 " . self::getCopyrightAndAuthorList() . "\n
151 " . wfMessage( 'version-license-info' )->text();
152 $ret .= '</div>';
153
154 return str_replace( "\t\t", '', $ret ) . "\n";
155 }
156
157 /**
158 * Get the "MediaWiki is copyright 2001-20xx by lots of cool guys" text
159 *
160 * @return String
161 */
162 public static function getCopyrightAndAuthorList() {
163 global $wgLang;
164
165 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
166 $othersLink = '[//www.mediawiki.org/wiki/Special:Version/Credits ' . wfMessage( 'version-poweredby-others' )->text() . ']';
167 } else {
168 $othersLink = '[[Special:Version/Credits|' . wfMessage( 'version-poweredby-others' )->text() . ']]';
169 }
170
171 $translatorsLink = '[//translatewiki.net/wiki/Translating:MediaWiki/Credits ' . wfMessage( 'version-poweredby-translators' )->text() . ']';
172
173 $authorList = array(
174 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
175 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
176 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
177 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
178 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
179 'Roan Kattouw', 'Trevor Parscal', 'Bryan Tong Minh', 'Sam Reed',
180 'Victor Vasiliev', 'Rotem Liss', 'Platonides', 'Antoine Musso',
181 'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', $othersLink,
182 $translatorsLink
183 );
184
185 return wfMessage( 'version-poweredby-credits', MWTimestamp::getLocalInstance()->format( 'Y' ),
186 $wgLang->listToText( $authorList ) )->text();
187 }
188
189 /**
190 * Returns wiki text showing the third party software versions (apache, php, mysql).
191 *
192 * @return string
193 */
194 static function softwareInformation() {
195 $dbr = wfGetDB( DB_SLAVE );
196
197 // Put the software in an array of form 'name' => 'version'. All messages should
198 // be loaded here, so feel free to use wfMessage in the 'name'. Raw HTML or
199 // wikimarkup can be used.
200 $software = array();
201 $software['[https://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
202 $software['[http://www.php.net/ PHP]'] = phpversion() . " (" . PHP_SAPI . ")";
203 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
204
205 // Allow a hook to add/remove items.
206 wfRunHooks( 'SoftwareInfo', array( &$software ) );
207
208 $out = Xml::element( 'h2', array( 'id' => 'mw-version-software' ), wfMessage( 'version-software' )->text() ) .
209 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-software' ) ) .
210 "<tr>
211 <th>" . wfMessage( 'version-software-product' )->text() . "</th>
212 <th>" . wfMessage( 'version-software-version' )->text() . "</th>
213 </tr>\n";
214
215 foreach ( $software as $name => $version ) {
216 $out .= "<tr>
217 <td>" . $name . "</td>
218 <td dir=\"ltr\">" . $version . "</td>
219 </tr>\n";
220 }
221
222 return $out . Xml::closeElement( 'table' );
223 }
224
225 /**
226 * Return a string of the MediaWiki version with SVN revision if available.
227 *
228 * @param $flags String
229 * @return mixed
230 */
231 public static function getVersion( $flags = '' ) {
232 global $wgVersion, $IP;
233 wfProfileIn( __METHOD__ );
234
235 $gitInfo = self::getGitHeadSha1( $IP );
236 $svnInfo = self::getSvnInfo( $IP );
237 if ( !$svnInfo && !$gitInfo ) {
238 $version = $wgVersion;
239 } elseif ( $gitInfo && $flags === 'nodb' ) {
240 $shortSha1 = substr( $gitInfo, 0, 7 );
241 $version = "$wgVersion ($shortSha1)";
242 } elseif ( $gitInfo ) {
243 $shortSha1 = substr( $gitInfo, 0, 7 );
244 $shortSha1 = wfMessage( 'parentheses' )->params( $shortSha1 )->escaped();
245 $version = "$wgVersion $shortSha1";
246 } elseif ( $flags === 'nodb' ) {
247 $version = "$wgVersion (r{$svnInfo['checkout-rev']})";
248 } else {
249 $version = $wgVersion . ' ' .
250 wfMessage(
251 'version-svn-revision',
252 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
253 $info['checkout-rev']
254 )->text();
255 }
256
257 wfProfileOut( __METHOD__ );
258
259 return $version;
260 }
261
262 /**
263 * Return a wikitext-formatted string of the MediaWiki version with a link to
264 * the SVN revision or the git SHA1 of head if available.
265 * Git is prefered over Svn
266 * The fallback is just $wgVersion
267 *
268 * @return mixed
269 */
270 public static function getVersionLinked() {
271 global $wgVersion;
272 wfProfileIn( __METHOD__ );
273
274 $gitVersion = self::getVersionLinkedGit();
275 if ( $gitVersion ) {
276 $v = $gitVersion;
277 } else {
278 $svnVersion = self::getVersionLinkedSvn();
279 if ( $svnVersion ) {
280 $v = $svnVersion;
281 } else {
282 $v = $wgVersion; // fallback
283 }
284 }
285
286 wfProfileOut( __METHOD__ );
287
288 return $v;
289 }
290
291 /**
292 * @return string wgVersion + a link to subversion revision of svn BASE
293 */
294 private static function getVersionLinkedSvn() {
295 global $IP;
296
297 $info = self::getSvnInfo( $IP );
298 if ( !isset( $info['checkout-rev'] ) ) {
299 return false;
300 }
301
302 $linkText = wfMessage(
303 'version-svn-revision',
304 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
305 $info['checkout-rev']
306 )->text();
307
308 if ( isset( $info['viewvc-url'] ) ) {
309 $version = "[{$info['viewvc-url']} $linkText]";
310 } else {
311 $version = $linkText;
312 }
313
314 return self::getwgVersionLinked() . " $version";
315 }
316
317 /**
318 * @return string
319 */
320 private static function getwgVersionLinked() {
321 global $wgVersion;
322 $versionUrl = "";
323 if ( wfRunHooks( 'SpecialVersionVersionUrl', array( $wgVersion, &$versionUrl ) ) ) {
324 $versionParts = array();
325 preg_match( "/^(\d+\.\d+)/", $wgVersion, $versionParts );
326 $versionUrl = "https://www.mediawiki.org/wiki/MediaWiki_{$versionParts[1]}";
327 }
328
329 return "[$versionUrl $wgVersion]";
330 }
331
332 /**
333 * @since 1.22 Returns the HEAD date in addition to the sha1 and link
334 * @return bool|string wgVersion + HEAD sha1 stripped to the first 7 chars with link and date, or false on failure
335 */
336 private static function getVersionLinkedGit() {
337 global $IP, $wgLang;
338
339 $gitInfo = new GitInfo( $IP );
340 $headSHA1 = $gitInfo->getHeadSHA1();
341 if ( !$headSHA1 ) {
342 return false;
343 }
344
345 $shortSHA1 = '(' . substr( $headSHA1, 0, 7 ) . ')';
346
347 $gitHeadUrl = $gitInfo->getHeadViewUrl();
348 if ( $gitHeadUrl !== false ) {
349 $shortSHA1 = "[$gitHeadUrl $shortSHA1]";
350 }
351
352 $gitHeadCommitDate = $gitInfo->getHeadCommitDate();
353 if ( $gitHeadCommitDate ) {
354 $shortSHA1 .= Html::element( 'br' ) . $wgLang->timeanddate( $gitHeadCommitDate, true );
355 }
356
357 return self::getwgVersionLinked() . " $shortSHA1";
358 }
359
360 /**
361 * Returns an array with the base extension types.
362 * Type is stored as array key, the message as array value.
363 *
364 * TODO: ideally this would return all extension types, including
365 * those added by SpecialVersionExtensionTypes. This is not possible
366 * since this hook is passing along $this though.
367 *
368 * @since 1.17
369 *
370 * @return array
371 */
372 public static function getExtensionTypes() {
373 if ( self::$extensionTypes === false ) {
374 self::$extensionTypes = array(
375 'specialpage' => wfMessage( 'version-specialpages' )->text(),
376 'parserhook' => wfMessage( 'version-parserhooks' )->text(),
377 'variable' => wfMessage( 'version-variables' )->text(),
378 'media' => wfMessage( 'version-mediahandlers' )->text(),
379 'antispam' => wfMessage( 'version-antispam' )->text(),
380 'skin' => wfMessage( 'version-skins' )->text(),
381 'api' => wfMessage( 'version-api' )->text(),
382 'other' => wfMessage( 'version-other' )->text(),
383 );
384
385 wfRunHooks( 'ExtensionTypes', array( &self::$extensionTypes ) );
386 }
387
388 return self::$extensionTypes;
389 }
390
391 /**
392 * Returns the internationalized name for an extension type.
393 *
394 * @since 1.17
395 *
396 * @param $type String
397 *
398 * @return string
399 */
400 public static function getExtensionTypeName( $type ) {
401 $types = self::getExtensionTypes();
402
403 return isset( $types[$type] ) ? $types[$type] : $types['other'];
404 }
405
406 /**
407 * Generate wikitext showing extensions name, URL, author and description.
408 *
409 * @return String: Wikitext
410 */
411 function getExtensionCredits() {
412 global $wgExtensionCredits;
413
414 if ( !count( $wgExtensionCredits ) ) {
415 return '';
416 }
417
418 $extensionTypes = self::getExtensionTypes();
419
420 /**
421 * @deprecated as of 1.17, use hook ExtensionTypes instead.
422 */
423 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
424
425 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), $this->msg( 'version-extensions' )->text() ) .
426 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-ext' ) );
427
428 // Make sure the 'other' type is set to an array.
429 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
430 $wgExtensionCredits['other'] = array();
431 }
432
433 // Find all extensions that do not have a valid type and give them the type 'other'.
434 foreach ( $wgExtensionCredits as $type => $extensions ) {
435 if ( !array_key_exists( $type, $extensionTypes ) ) {
436 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
437 }
438 }
439
440 // Loop through the extension categories to display their extensions in the list.
441 foreach ( $extensionTypes as $type => $message ) {
442 if ( $type != 'other' ) {
443 $out .= $this->getExtensionCategory( $type, $message );
444 }
445 }
446
447 // We want the 'other' type to be last in the list.
448 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
449
450 $out .= Xml::closeElement( 'table' );
451
452 return $out;
453 }
454
455 /**
456 * Obtains a list of installed parser tags and the associated H2 header
457 *
458 * @return string HTML output
459 */
460 protected function getParserTags() {
461 global $wgParser;
462
463 $tags = $wgParser->getTags();
464
465 if ( count( $tags ) ) {
466 $out = Html::rawElement(
467 'h2',
468 array( 'class' => 'mw-headline' ),
469 Linker::makeExternalLink(
470 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Tag_extensions',
471 $this->msg( 'version-parser-extensiontags' )->parse(),
472 false /* msg()->parse() already escapes */
473 )
474 );
475
476 array_walk( $tags, function ( &$value ) {
477 $value = '&lt;' . htmlentities( $value ) . '&gt;';
478 } );
479 $out .= $this->listToText( $tags );
480 } else {
481 $out = '';
482 }
483
484 return $out;
485 }
486
487 /**
488 * Obtains a list of installed parser function hooks and the associated H2 header
489 *
490 * @return string HTML output
491 */
492 protected function getParserFunctionHooks() {
493 global $wgParser;
494
495 $fhooks = $wgParser->getFunctionHooks();
496 if ( count( $fhooks ) ) {
497 $out = Html::rawElement( 'h2', array( 'class' => 'mw-headline' ), Linker::makeExternalLink(
498 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Parser_functions',
499 $this->msg( 'version-parser-function-hooks' )->parse(),
500 false /* msg()->parse() already escapes */
501 ) );
502
503 $out .= $this->listToText( $fhooks );
504 } else {
505 $out = '';
506 }
507
508 return $out;
509 }
510
511 /**
512 * Creates and returns the HTML for a single extension category.
513 *
514 * @since 1.17
515 *
516 * @param $type String
517 * @param $message String
518 *
519 * @return string
520 */
521 protected function getExtensionCategory( $type, $message ) {
522 global $wgExtensionCredits;
523
524 $out = '';
525
526 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
527 $out .= $this->openExtType( $message, 'credits-' . $type );
528
529 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
530
531 foreach ( $wgExtensionCredits[$type] as $extension ) {
532 $out .= $this->getCreditsForExtension( $extension );
533 }
534 }
535
536 return $out;
537 }
538
539 /**
540 * Callback to sort extensions by type.
541 * @param $a array
542 * @param $b array
543 * @return int
544 */
545 function compare( $a, $b ) {
546 if ( $a['name'] === $b['name'] ) {
547 return 0;
548 } else {
549 return $this->getLanguage()->lc( $a['name'] ) > $this->getLanguage()->lc( $b['name'] )
550 ? 1
551 : -1;
552 }
553 }
554
555 /**
556 * Creates and formats a version line for a single extension.
557 *
558 * Information for five columns will be created. Parameters required in the
559 * $extension array for part rendering are indicated in ()
560 * - The name of (name), and URL link to (url), the extension
561 * - Official version number (version) and if available version control system
562 * revision (path), link, and date
563 * - If available the short name of the license (license-name) and a linke
564 * to ((LICENSE)|(COPYING))(\.txt)? if it exists.
565 * - Description of extension (descriptionmsg or description)
566 * - List of authors (author) and link to a ((AUTHORS)|(CREDITS))(\.txt)? file if it exists
567 *
568 * @param $extension Array
569 *
570 * @return string raw HTML
571 */
572 function getCreditsForExtension( array $extension ) {
573 $out = $this->getOutput();
574
575 // We must obtain the information for all the bits and pieces!
576 // ... such as extension names and links
577 $extensionName = isset( $extension['name'] ) ? $extension['name'] : '[no name]';
578 if ( isset( $extension['url'] ) ) {
579 $extensionNameLink = Linker::makeExternalLink(
580 $extension['url'],
581 $extensionName,
582 true,
583 '',
584 array( 'class' => 'mw-version-ext-name' )
585 );
586 } else {
587 $extensionNameLink = $extensionName;
588 }
589
590 // ... and the version information
591 // If the extension path is set we will check that directory for GIT and SVN
592 // metadata in an attempt to extract date and vcs commit metadata.
593 $canonicalVersion = '&ndash;';
594 $extensionPath = null;
595 $vcsVersion = null;
596 $vcsLink = null;
597 $vcsDate = null;
598
599 if ( isset( $extension['version'] ) ) {
600 $canonicalVersion = $out->parseInline( $extension['version'] );
601 }
602
603 if ( isset( $extension['path'] ) ) {
604 $extensionPath = dirname( $extension['path'] );
605 $gitInfo = new GitInfo( $extensionPath );
606 $vcsVersion = $gitInfo->getHeadSHA1();
607 if ( $vcsVersion !== false ) {
608 $vcsVersion = substr( $vcsVersion, 0, 7 );
609 $vcsLink = $gitInfo->getHeadViewUrl();
610 $vcsDate = $gitInfo->getHeadCommitDate();
611 } else {
612 $svnInfo = self::getSvnInfo( $extensionPath );
613 if ( $svnInfo !== false ) {
614 $vcsVersion = $this->msg( 'version-svn-revision', $svnInfo['checkout-rev'] )->text();
615 $vcsLink = isset( $svnInfo['viewvc-url'] ) ? $svnInfo['viewvc-url'] : '';
616 }
617 }
618 }
619
620 $versionString = Html::rawElement( 'span', array( 'class' => 'mw-version-ext-version' ), $canonicalVersion );
621 if ( $vcsVersion ) {
622 if ( $vcsLink ) {
623 $vcsVerString = Linker::makeExternalLink(
624 $vcsLink,
625 $this->msg( 'version-version', $vcsVersion ),
626 true,
627 '',
628 array( 'class' => 'mw-version-ext-vcs-version' )
629 );
630 } else {
631 $vcsVerString = Html::element( 'span',
632 array( 'class' => 'mw-version-ext-vcs-version' ),
633 "({$vcsVersion})"
634 );
635 }
636 $versionString .= " {$vcsVerString}";
637
638 if ( $vcsDate ) {
639 $vcsTimeString = Html::element( 'span',
640 array( 'class' => 'mw-version-ext-vcs-timestamp' ),
641 $this->getLanguage()->timeanddate( $vcsDate )
642 );
643 $versionString .= " {$vcsTimeString}";
644 }
645 $versionString = Html::rawElement( 'span',
646 array( 'class' => 'mw-version-ext-meta-version' ),
647 $versionString
648 );
649 }
650
651 // ... and license information; if a license file exists we
652 // will link to it
653 $licenseLink = '';
654 if ( isset( $extension['license-name'] ) ) {
655 $licenseLink = Linker::link(
656 $this->getPageTitle( 'License/' . $extensionName ),
657 $out->parseInline( $extension['license-name'] ),
658 array( 'class' => 'mw-version-ext-license' )
659 );
660 } elseif ( $this->getExtLicenseFileName( $extensionPath ) ) {
661 $licenseLink = Linker::link(
662 $this->getPageTitle( 'License/' . $extensionName ),
663 $this->msg( 'version-ext-license' ),
664 array( 'class' => 'mw-version-ext-license' )
665 );
666 }
667
668 // ... and generate the description; which can be a parameterized l10n message
669 // in the form array( <msgname>, <parameter>, <parameter>... ) or just a straight
670 // up string
671 if ( isset( $extension['descriptionmsg'] ) ) {
672 // Localized description of extension
673 $descriptionMsg = $extension['descriptionmsg'];
674
675 if ( is_array( $descriptionMsg ) ) {
676 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
677 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
678 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
679 $description = $this->msg( $descriptionMsgKey, $descriptionMsg )->text();
680 } else {
681 $description = $this->msg( $descriptionMsg )->text();
682 }
683 } elseif ( isset( $extension['description'] ) ) {
684 // Non localized version
685 $description = $extension['description'];
686 } else {
687 $description = '';
688 }
689 $description = $out->parseInline( $description );
690
691 // ... now get the authors for this extension
692 $authors = isset( $extension['author'] ) ? $extension['author'] : array();
693 $authors = $this->listAuthors( $authors, $extensionName, $extensionPath );
694
695 // Finally! Create the table
696 $html = Html::openElement( 'tr', array(
697 'class' => 'mw-version-ext',
698 'id' => "mw-version-ext-{$extensionName}"
699 )
700 );
701
702 $html .= Html::rawElement( 'td', array(), $extensionNameLink );
703 $html .= Html::rawElement( 'td', array(), $versionString );
704 $html .= Html::rawElement( 'td', array(), $licenseLink );
705 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-description' ), $description );
706 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-authors' ), $authors );
707
708 $html .= Html::closeElement( 'td' );
709
710 return $html;
711 }
712
713 /**
714 * Generate wikitext showing hooks in $wgHooks.
715 *
716 * @return String: wikitext
717 */
718 private function getWgHooks() {
719 global $wgSpecialVersionShowHooks, $wgHooks;
720
721 if ( $wgSpecialVersionShowHooks && count( $wgHooks ) ) {
722 $myWgHooks = $wgHooks;
723 ksort( $myWgHooks );
724
725 $ret = array();
726 $ret[] = '== {{int:version-hooks}} ==';
727 $ret[] = Html::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) );
728 $ret[] = Html::openElement( 'tr' );
729 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-name' )->text() );
730 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-subscribedby' )->text() );
731 $ret[] = Html::closeElement( 'tr' );
732
733 foreach ( $myWgHooks as $hook => $hooks ) {
734 $ret[] = Html::openElement( 'tr' );
735 $ret[] = Html::element( 'td', array(), $hook );
736 $ret[] = Html::element( 'td', array(), $this->listToText( $hooks ) );
737 $ret[] = Html::closeElement( 'tr' );
738 }
739
740 $ret[] = Html::closeElement( 'table' );
741
742 return implode( "\n", $ret );
743 } else {
744 return '';
745 }
746 }
747
748 private function openExtType( $text, $name = null ) {
749 $out = '';
750
751 $opt = array( 'colspan' => 5 );
752 if ( $this->firstExtOpened ) {
753 // Insert a spacing line
754 $out .= Html::rawElement( 'tr', array( 'class' => 'sv-space' ),
755 Html::element( 'td', $opt )
756 );
757 }
758 $this->firstExtOpened = true;
759
760 if ( $name ) {
761 $opt['id'] = "sv-$name";
762 }
763
764 $out .= Html::rawElement( 'tr', array(),
765 Html::element( 'th', $opt, $text )
766 );
767
768 $out .= Html::openElement( 'tr' );
769 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
770 $this->msg( 'version-ext-colheader-name' )->text() );
771 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
772 $this->msg( 'version-ext-colheader-version' )->text() );
773 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
774 $this->msg( 'version-ext-colheader-license' )->text() );
775 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
776 $this->msg( 'version-ext-colheader-description' )->text() );
777 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
778 $this->msg( 'version-ext-colheader-credits' )->text() );
779 $out .= Html::closeElement( 'tr' );
780
781 return $out;
782 }
783
784 /**
785 * Get information about client's IP address.
786 *
787 * @return String: HTML fragment
788 */
789 private function IPInfo() {
790 $ip = str_replace( '--', ' - ', htmlspecialchars( $this->getRequest()->getIP() ) );
791
792 return "<!-- visited from $ip -->\n<span style='display:none'>visited from $ip</span>";
793 }
794
795 /**
796 * Return a formatted unsorted list of authors
797 *
798 * 'And Others'
799 * If an item in the $authors array is '...' it is assumed to indicate an
800 * 'and others' string which will then be linked to an ((AUTHORS)|(CREDITS))(\.txt)?
801 * file if it exists in $dir.
802 *
803 * Similarly an entry ending with ' ...]' is assumed to be a link to an
804 * 'and others' page.
805 *
806 * If no '...' string variant is found, but an authors file is found an
807 * 'and others' will be added to the end of the credits.
808 *
809 * @param $authors mixed: string or array of strings
810 * @param $extName string: name of the extension for link creation
811 * @param $extDir string: path to the extension root directory
812 *
813 * @return String: HTML fragment
814 */
815 function listAuthors( $authors, $extName, $extDir ) {
816 $hasOthers = false;
817
818 $list = array();
819 foreach ( (array)$authors as $item ) {
820 if ( $item == '...' ) {
821 $hasOthers = true;
822
823 if ( $this->getExtAuthorsFileName( $extDir ) ) {
824 $text = Linker::link(
825 $this->getPageTitle( "Credits/$extName" ),
826 $this->msg( 'version-poweredby-others' )->text()
827 );
828 } else {
829 $text = $this->msg( 'version-poweredby-others' )->text();
830 }
831 $list[] = $text;
832 } elseif ( substr( $item, -5 ) == ' ...]' ) {
833 $hasOthers = true;
834 $list[] = $this->getOutput()->parseInline(
835 substr( $item, 0, -4 ) . $this->msg( 'version-poweredby-others' )->text() . "]"
836 );
837 } else {
838 $list[] = $this->getOutput()->parseInline( $item );
839 }
840 }
841
842 if ( !$hasOthers && $this->getExtAuthorsFileName( $extDir ) ) {
843 $list[] = $text = Linker::link(
844 $this->getPageTitle( "Credits/$extName" ),
845 $this->msg( 'version-poweredby-others' )->text()
846 );
847 }
848
849 return $this->listToText( $list, false );
850 }
851
852 /**
853 * Obtains the full path of an extensions authors or credits file if
854 * one exists.
855 *
856 * @param string $extDir Path to the extensions root directory
857 *
858 * @since 1.23
859 *
860 * @return bool|string False if no such file exists, otherwise returns
861 * a path to it.
862 */
863 public static function getExtAuthorsFileName( $extDir ) {
864 if ( !$extDir ) {
865 return false;
866 }
867
868 foreach ( scandir( $extDir ) as $file ) {
869 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
870 if ( preg_match( '/^((AUTHORS)|(CREDITS))(\.txt)?$/', $file ) &&
871 is_readable( $fullPath ) &&
872 is_file( $fullPath )
873 ) {
874 return $fullPath;
875 }
876 }
877
878 return false;
879 }
880
881 /**
882 * Obtains the full path of an extensions copying or license file if
883 * one exists.
884 *
885 * @param string $extDir Path to the extensions root directory
886 *
887 * @since 1.23
888 *
889 * @return bool|string False if no such file exists, otherwise returns
890 * a path to it.
891 */
892 public static function getExtLicenseFileName( $extDir ) {
893 if ( !$extDir ) {
894 return false;
895 }
896
897 foreach ( scandir( $extDir ) as $file ) {
898 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
899 if ( preg_match( '/^((COPYING)|(LICENSE))(\.txt)?$/', $file ) &&
900 is_readable( $fullPath ) &&
901 is_file( $fullPath )
902 ) {
903 return $fullPath;
904 }
905 }
906
907 return false;
908 }
909
910 /**
911 * Convert an array of items into a list for display.
912 *
913 * @param array $list of elements to display
914 * @param $sort Boolean: whether to sort the items in $list
915 *
916 * @return String
917 */
918 function listToText( $list, $sort = true ) {
919 $cnt = count( $list );
920
921 if ( $cnt == 1 ) {
922 // Enforce always returning a string
923 return (string)self::arrayToString( $list[0] );
924 } elseif ( $cnt == 0 ) {
925 return '';
926 } else {
927 if ( $sort ) {
928 sort( $list );
929 }
930
931 return $this->getLanguage()->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
932 }
933 }
934
935 /**
936 * Convert an array or object to a string for display.
937 *
938 * @param $list Mixed: will convert an array to string if given and return
939 * the paramater unaltered otherwise
940 *
941 * @return Mixed
942 */
943 public static function arrayToString( $list ) {
944 if ( is_array( $list ) && count( $list ) == 1 ) {
945 $list = $list[0];
946 }
947 if ( is_object( $list ) ) {
948 $class = wfMessage( 'parentheses' )->params( get_class( $list ) )->escaped();
949
950 return $class;
951 } elseif ( !is_array( $list ) ) {
952 return $list;
953 } else {
954 if ( is_object( $list[0] ) ) {
955 $class = get_class( $list[0] );
956 } else {
957 $class = $list[0];
958 }
959
960 return wfMessage( 'parentheses' )->params( "$class, {$list[1]}" )->escaped();
961 }
962 }
963
964 /**
965 * Get an associative array of information about a given path, from its .svn
966 * subdirectory. Returns false on error, such as if the directory was not
967 * checked out with subversion.
968 *
969 * Returned keys are:
970 * Required:
971 * checkout-rev The revision which was checked out
972 * Optional:
973 * directory-rev The revision when the directory was last modified
974 * url The subversion URL of the directory
975 * repo-url The base URL of the repository
976 * viewvc-url A ViewVC URL pointing to the checked-out revision
977 * @param $dir string
978 * @return array|bool
979 */
980 public static function getSvnInfo( $dir ) {
981 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
982 $entries = $dir . '/.svn/entries';
983
984 if ( !file_exists( $entries ) ) {
985 return false;
986 }
987
988 $lines = file( $entries );
989 if ( !count( $lines ) ) {
990 return false;
991 }
992
993 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
994 if ( preg_match( '/^<\?xml/', $lines[0] ) ) {
995 // subversion is release <= 1.3
996 if ( !function_exists( 'simplexml_load_file' ) ) {
997 // We could fall back to expat... YUCK
998 return false;
999 }
1000
1001 // SimpleXml whines about the xmlns...
1002 wfSuppressWarnings();
1003 $xml = simplexml_load_file( $entries );
1004 wfRestoreWarnings();
1005
1006 if ( $xml ) {
1007 foreach ( $xml->entry as $entry ) {
1008 if ( $xml->entry[0]['name'] == '' ) {
1009 // The directory entry should always have a revision marker.
1010 if ( $entry['revision'] ) {
1011 return array( 'checkout-rev' => intval( $entry['revision'] ) );
1012 }
1013 }
1014 }
1015 }
1016
1017 return false;
1018 }
1019
1020 // Subversion is release 1.4 or above.
1021 if ( count( $lines ) < 11 ) {
1022 return false;
1023 }
1024
1025 $info = array(
1026 'checkout-rev' => intval( trim( $lines[3] ) ),
1027 'url' => trim( $lines[4] ),
1028 'repo-url' => trim( $lines[5] ),
1029 'directory-rev' => intval( trim( $lines[10] ) )
1030 );
1031
1032 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
1033 $viewvc = str_replace(
1034 $info['repo-url'],
1035 self::$viewvcUrls[$info['repo-url']],
1036 $info['url']
1037 );
1038
1039 $viewvc .= '/?pathrev=';
1040 $viewvc .= urlencode( $info['checkout-rev'] );
1041 $info['viewvc-url'] = $viewvc;
1042 }
1043
1044 return $info;
1045 }
1046
1047 /**
1048 * Retrieve the revision number of a Subversion working directory.
1049 *
1050 * @param string $dir directory of the svn checkout
1051 *
1052 * @return Integer: revision number as int
1053 */
1054 public static function getSvnRevision( $dir ) {
1055 $info = self::getSvnInfo( $dir );
1056
1057 if ( $info === false ) {
1058 return false;
1059 } elseif ( isset( $info['checkout-rev'] ) ) {
1060 return $info['checkout-rev'];
1061 } else {
1062 return false;
1063 }
1064 }
1065
1066 /**
1067 * @param string $dir directory of the git checkout
1068 * @return bool|String sha1 of commit HEAD points to
1069 */
1070 public static function getGitHeadSha1( $dir ) {
1071 $repo = new GitInfo( $dir );
1072
1073 return $repo->getHeadSHA1();
1074 }
1075
1076 /**
1077 * Get the list of entry points and their URLs
1078 * @return string Wikitext
1079 */
1080 public function getEntryPointInfo() {
1081 global $wgArticlePath, $wgScriptPath;
1082 $scriptPath = $wgScriptPath ? $wgScriptPath : "/";
1083 $entryPoints = array(
1084 'version-entrypoints-articlepath' => $wgArticlePath,
1085 'version-entrypoints-scriptpath' => $scriptPath,
1086 'version-entrypoints-index-php' => wfScript( 'index' ),
1087 'version-entrypoints-api-php' => wfScript( 'api' ),
1088 'version-entrypoints-load-php' => wfScript( 'load' ),
1089 );
1090
1091 $language = $this->getLanguage();
1092 $thAttribures = array(
1093 'dir' => $language->getDir(),
1094 'lang' => $language->getCode()
1095 );
1096 $out = Html::element( 'h2', array( 'id' => 'mw-version-entrypoints' ), $this->msg( 'version-entrypoints' )->text() ) .
1097 Html::openElement( 'table',
1098 array(
1099 'class' => 'wikitable plainlinks',
1100 'id' => 'mw-version-entrypoints-table',
1101 'dir' => 'ltr',
1102 'lang' => 'en'
1103 )
1104 ) .
1105 Html::openElement( 'tr' ) .
1106 Html::element( 'th', $thAttribures, $this->msg( 'version-entrypoints-header-entrypoint' )->text() ) .
1107 Html::element( 'th', $thAttribures, $this->msg( 'version-entrypoints-header-url' )->text() ) .
1108 Html::closeElement( 'tr' );
1109
1110 foreach ( $entryPoints as $message => $value ) {
1111 $url = wfExpandUrl( $value, PROTO_RELATIVE );
1112 $out .= Html::openElement( 'tr' ) .
1113 // ->text() looks like it should be ->parse(), but this function
1114 // returns wikitext, not HTML, boo
1115 Html::rawElement( 'td', array(), $this->msg( $message )->text() ) .
1116 Html::rawElement( 'td', array(), Html::rawElement( 'code', array(), "[$url $value]" ) ) .
1117 Html::closeElement( 'tr' );
1118 }
1119
1120 $out .= Html::closeElement( 'table' );
1121
1122 return $out;
1123 }
1124
1125 protected function getGroupName() {
1126 return 'wiki';
1127 }
1128 }