Merge "Don't check namespace in SpecialWantedtemplates"
[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 protected $firstExtOpened = false;
33
34 /**
35 * Stores the current rev id/SHA hash of MediaWiki core
36 */
37 protected $coreId = '';
38
39 protected static $extensionTypes = false;
40
41 protected static $viewvcUrls = array(
42 'svn+ssh://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
43 'http://svn.wikimedia.org/svnroot/mediawiki' => 'http://svn.wikimedia.org/viewvc/mediawiki',
44 'https://svn.wikimedia.org/svnroot/mediawiki' => 'https://svn.wikimedia.org/viewvc/mediawiki',
45 );
46
47 public function __construct() {
48 parent::__construct( 'Version' );
49 }
50
51 /**
52 * main()
53 * @param string|null $par
54 */
55 public function execute( $par ) {
56 global $IP, $wgExtensionCredits;
57
58 $this->setHeaders();
59 $this->outputHeader();
60 $out = $this->getOutput();
61 $out->allowClickjacking();
62
63 // Explode the sub page information into useful bits
64 $parts = explode( '/', (string)$par );
65 $extNode = null;
66 if ( isset( $parts[1] ) ) {
67 $extName = str_replace( '_', ' ', $parts[1] );
68 // Find it!
69 foreach ( $wgExtensionCredits as $group => $extensions ) {
70 foreach ( $extensions as $ext ) {
71 if ( isset( $ext['name'] ) && ( $ext['name'] === $extName ) ) {
72 $extNode = &$ext;
73 break 2;
74 }
75 }
76 }
77 if ( !$extNode ) {
78 $out->setStatusCode( 404 );
79 }
80 } else {
81 $extName = 'MediaWiki';
82 }
83
84 // Now figure out what to do
85 switch ( strtolower( $parts[0] ) ) {
86 case 'credits':
87 $wikiText = '{{int:version-credits-not-found}}';
88 if ( $extName === 'MediaWiki' ) {
89 $wikiText = file_get_contents( $IP . '/CREDITS' );
90 } elseif ( ( $extNode !== null ) && isset( $extNode['path'] ) ) {
91 $file = $this->getExtAuthorsFileName( dirname( $extNode['path'] ) );
92 if ( $file ) {
93 $wikiText = file_get_contents( $file );
94 if ( substr( $file, -4 ) === '.txt' ) {
95 $wikiText = Html::element(
96 'pre',
97 array(
98 'lang' => 'en',
99 'dir' => 'ltr',
100 ),
101 $wikiText
102 );
103 }
104 }
105 }
106
107 $out->setPageTitle( $this->msg( 'version-credits-title', $extName ) );
108 $out->addWikiText( $wikiText );
109 break;
110
111 case 'license':
112 $wikiText = '{{int:version-license-not-found}}';
113 if ( $extName === 'MediaWiki' ) {
114 $wikiText = file_get_contents( $IP . '/COPYING' );
115 } elseif ( ( $extNode !== null ) && isset( $extNode['path'] ) ) {
116 $file = $this->getExtLicenseFileName( dirname( $extNode['path'] ) );
117 if ( $file ) {
118 $wikiText = file_get_contents( $file );
119 $wikiText = Html::element(
120 'pre',
121 array(
122 'lang' => 'en',
123 'dir' => 'ltr',
124 ),
125 $wikiText
126 );
127 }
128 }
129
130 $out->setPageTitle( $this->msg( 'version-license-title', $extName ) );
131 $out->addWikiText( $wikiText );
132 break;
133
134 default:
135 $out->addModules( 'mediawiki.special.version' );
136 $out->addWikiText(
137 $this->getMediaWikiCredits() .
138 $this->softwareInformation() .
139 $this->getEntryPointInfo()
140 );
141 $out->addHtml(
142 $this->getSkinCredits() .
143 $this->getExtensionCredits() .
144 $this->getExternalLibraries() .
145 $this->getParserTags() .
146 $this->getParserFunctionHooks()
147 );
148 $out->addWikiText( $this->getWgHooks() );
149 $out->addHTML( $this->IPInfo() );
150
151 break;
152 }
153 }
154
155 /**
156 * Returns wiki text showing the license information.
157 *
158 * @return string
159 */
160 private static function getMediaWikiCredits() {
161 $ret = Xml::element(
162 'h2',
163 array( 'id' => 'mw-version-license' ),
164 wfMessage( 'version-license' )->text()
165 );
166
167 // This text is always left-to-right.
168 $ret .= '<div class="plainlinks">';
169 $ret .= "__NOTOC__
170 " . self::getCopyrightAndAuthorList() . "\n
171 " . wfMessage( 'version-license-info' )->text();
172 $ret .= '</div>';
173
174 return str_replace( "\t\t", '', $ret ) . "\n";
175 }
176
177 /**
178 * Get the "MediaWiki is copyright 2001-20xx by lots of cool guys" text
179 *
180 * @return string
181 */
182 public static function getCopyrightAndAuthorList() {
183 global $wgLang;
184
185 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
186 $othersLink = '[//www.mediawiki.org/wiki/Special:Version/Credits ' .
187 wfMessage( 'version-poweredby-others' )->text() . ']';
188 } else {
189 $othersLink = '[[Special:Version/Credits|' .
190 wfMessage( 'version-poweredby-others' )->text() . ']]';
191 }
192
193 $translatorsLink = '[//translatewiki.net/wiki/Translating:MediaWiki/Credits ' .
194 wfMessage( 'version-poweredby-translators' )->text() . ']';
195
196 $authorList = array(
197 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
198 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
199 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
200 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
201 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
202 'Roan Kattouw', 'Trevor Parscal', 'Bryan Tong Minh', 'Sam Reed',
203 'Victor Vasiliev', 'Rotem Liss', 'Platonides', 'Antoine Musso',
204 'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', 'Brad Jorsch',
205 $othersLink, $translatorsLink
206 );
207
208 return wfMessage( 'version-poweredby-credits', MWTimestamp::getLocalInstance()->format( 'Y' ),
209 $wgLang->listToText( $authorList ) )->text();
210 }
211
212 /**
213 * Returns wiki text showing the third party software versions (apache, php, mysql).
214 *
215 * @return string
216 */
217 public static function softwareInformation() {
218 $dbr = wfGetDB( DB_SLAVE );
219
220 // Put the software in an array of form 'name' => 'version'. All messages should
221 // be loaded here, so feel free to use wfMessage in the 'name'. Raw HTML or
222 // wikimarkup can be used.
223 $software = array();
224 $software['[https://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
225 if ( wfIsHHVM() ) {
226 $software['[http://hhvm.com/ HHVM]'] = HHVM_VERSION . " (" . PHP_SAPI . ")";
227 } else {
228 $software['[https://php.net/ PHP]'] = PHP_VERSION . " (" . PHP_SAPI . ")";
229 }
230 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
231
232 if ( IcuCollation::getICUVersion() ) {
233 $software['[http://site.icu-project.org/ ICU]'] = IcuCollation::getICUVersion();
234 }
235
236 // Allow a hook to add/remove items.
237 Hooks::run( 'SoftwareInfo', array( &$software ) );
238
239 $out = Xml::element(
240 'h2',
241 array( 'id' => 'mw-version-software' ),
242 wfMessage( 'version-software' )->text()
243 ) .
244 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-software' ) ) .
245 "<tr>
246 <th>" . wfMessage( 'version-software-product' )->text() . "</th>
247 <th>" . wfMessage( 'version-software-version' )->text() . "</th>
248 </tr>\n";
249
250 foreach ( $software as $name => $version ) {
251 $out .= "<tr>
252 <td>" . $name . "</td>
253 <td dir=\"ltr\">" . $version . "</td>
254 </tr>\n";
255 }
256
257 return $out . Xml::closeElement( 'table' );
258 }
259
260 /**
261 * Return a string of the MediaWiki version with SVN revision if available.
262 *
263 * @param string $flags
264 * @return mixed
265 */
266 public static function getVersion( $flags = '' ) {
267 global $wgVersion, $IP;
268
269 $gitInfo = self::getGitHeadSha1( $IP );
270 $svnInfo = self::getSvnInfo( $IP );
271 if ( !$svnInfo && !$gitInfo ) {
272 $version = $wgVersion;
273 } elseif ( $gitInfo && $flags === 'nodb' ) {
274 $shortSha1 = substr( $gitInfo, 0, 7 );
275 $version = "$wgVersion ($shortSha1)";
276 } elseif ( $gitInfo ) {
277 $shortSha1 = substr( $gitInfo, 0, 7 );
278 $shortSha1 = wfMessage( 'parentheses' )->params( $shortSha1 )->escaped();
279 $version = "$wgVersion $shortSha1";
280 } elseif ( $flags === 'nodb' ) {
281 $version = "$wgVersion (r{$svnInfo['checkout-rev']})";
282 } else {
283 $version = $wgVersion . ' ' .
284 wfMessage(
285 'version-svn-revision',
286 isset( $svnInfo['directory-rev'] ) ? $svnInfo['directory-rev'] : '',
287 isset( $svnInfo['checkout-rev'] ) ? $svnInfo['checkout-rev'] : ''
288 )->text();
289 }
290
291 return $version;
292 }
293
294 /**
295 * Return a wikitext-formatted string of the MediaWiki version with a link to
296 * the SVN revision or the git SHA1 of head if available.
297 * Git is prefered over Svn
298 * The fallback is just $wgVersion
299 *
300 * @return mixed
301 */
302 public static function getVersionLinked() {
303 global $wgVersion;
304
305 $gitVersion = self::getVersionLinkedGit();
306 if ( $gitVersion ) {
307 $v = $gitVersion;
308 } else {
309 $svnVersion = self::getVersionLinkedSvn();
310 if ( $svnVersion ) {
311 $v = $svnVersion;
312 } else {
313 $v = $wgVersion; // fallback
314 }
315 }
316
317 return $v;
318 }
319
320 /**
321 * @return string Global wgVersion + a link to subversion revision of svn BASE
322 */
323 private static function getVersionLinkedSvn() {
324 global $IP;
325
326 $info = self::getSvnInfo( $IP );
327 if ( !isset( $info['checkout-rev'] ) ) {
328 return false;
329 }
330
331 $linkText = wfMessage(
332 'version-svn-revision',
333 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
334 $info['checkout-rev']
335 )->text();
336
337 if ( isset( $info['viewvc-url'] ) ) {
338 $version = "[{$info['viewvc-url']} $linkText]";
339 } else {
340 $version = $linkText;
341 }
342
343 return self::getwgVersionLinked() . " $version";
344 }
345
346 /**
347 * @return string
348 */
349 private static function getwgVersionLinked() {
350 global $wgVersion;
351 $versionUrl = "";
352 if ( Hooks::run( 'SpecialVersionVersionUrl', array( $wgVersion, &$versionUrl ) ) ) {
353 $versionParts = array();
354 preg_match( "/^(\d+\.\d+)/", $wgVersion, $versionParts );
355 $versionUrl = "https://www.mediawiki.org/wiki/MediaWiki_{$versionParts[1]}";
356 }
357
358 return "[$versionUrl $wgVersion]";
359 }
360
361 /**
362 * @since 1.22 Returns the HEAD date in addition to the sha1 and link
363 * @return bool|string Global wgVersion + HEAD sha1 stripped to the first 7 chars
364 * with link and date, or false on failure
365 */
366 private static function getVersionLinkedGit() {
367 global $IP, $wgLang;
368
369 $gitInfo = new GitInfo( $IP );
370 $headSHA1 = $gitInfo->getHeadSHA1();
371 if ( !$headSHA1 ) {
372 return false;
373 }
374
375 $shortSHA1 = '(' . substr( $headSHA1, 0, 7 ) . ')';
376
377 $gitHeadUrl = $gitInfo->getHeadViewUrl();
378 if ( $gitHeadUrl !== false ) {
379 $shortSHA1 = "[$gitHeadUrl $shortSHA1]";
380 }
381
382 $gitHeadCommitDate = $gitInfo->getHeadCommitDate();
383 if ( $gitHeadCommitDate ) {
384 $shortSHA1 .= Html::element( 'br' ) . $wgLang->timeanddate( $gitHeadCommitDate, true );
385 }
386
387 return self::getwgVersionLinked() . " $shortSHA1";
388 }
389
390 /**
391 * Returns an array with the base extension types.
392 * Type is stored as array key, the message as array value.
393 *
394 * TODO: ideally this would return all extension types.
395 *
396 * @since 1.17
397 *
398 * @return array
399 */
400 public static function getExtensionTypes() {
401 if ( self::$extensionTypes === false ) {
402 self::$extensionTypes = array(
403 'specialpage' => wfMessage( 'version-specialpages' )->text(),
404 'parserhook' => wfMessage( 'version-parserhooks' )->text(),
405 'variable' => wfMessage( 'version-variables' )->text(),
406 'media' => wfMessage( 'version-mediahandlers' )->text(),
407 'antispam' => wfMessage( 'version-antispam' )->text(),
408 'skin' => wfMessage( 'version-skins' )->text(),
409 'api' => wfMessage( 'version-api' )->text(),
410 'other' => wfMessage( 'version-other' )->text(),
411 );
412
413 Hooks::run( 'ExtensionTypes', array( &self::$extensionTypes ) );
414 }
415
416 return self::$extensionTypes;
417 }
418
419 /**
420 * Returns the internationalized name for an extension type.
421 *
422 * @since 1.17
423 *
424 * @param string $type
425 *
426 * @return string
427 */
428 public static function getExtensionTypeName( $type ) {
429 $types = self::getExtensionTypes();
430
431 return isset( $types[$type] ) ? $types[$type] : $types['other'];
432 }
433
434 /**
435 * Generate wikitext showing the name, URL, author and description of each extension.
436 *
437 * @return string Wikitext
438 */
439 public function getExtensionCredits() {
440 global $wgExtensionCredits;
441
442 if (
443 count( $wgExtensionCredits ) === 0 ||
444 // Skins are displayed separately, see getSkinCredits()
445 ( count( $wgExtensionCredits ) === 1 && isset( $wgExtensionCredits['skin'] ) )
446 ) {
447 return '';
448 }
449
450 $extensionTypes = self::getExtensionTypes();
451
452 $out = Xml::element(
453 'h2',
454 array( 'id' => 'mw-version-ext' ),
455 $this->msg( 'version-extensions' )->text()
456 ) .
457 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-ext' ) );
458
459 // Make sure the 'other' type is set to an array.
460 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
461 $wgExtensionCredits['other'] = array();
462 }
463
464 // Find all extensions that do not have a valid type and give them the type 'other'.
465 foreach ( $wgExtensionCredits as $type => $extensions ) {
466 if ( !array_key_exists( $type, $extensionTypes ) ) {
467 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
468 }
469 }
470
471 $this->firstExtOpened = false;
472 // Loop through the extension categories to display their extensions in the list.
473 foreach ( $extensionTypes as $type => $message ) {
474 // Skins have a separate section
475 if ( $type !== 'other' && $type !== 'skin' ) {
476 $out .= $this->getExtensionCategory( $type, $message );
477 }
478 }
479
480 // We want the 'other' type to be last in the list.
481 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
482
483 $out .= Xml::closeElement( 'table' );
484
485 return $out;
486 }
487
488 /**
489 * Generate wikitext showing the name, URL, author and description of each skin.
490 *
491 * @return string Wikitext
492 */
493 public function getSkinCredits() {
494 global $wgExtensionCredits;
495 if ( !isset( $wgExtensionCredits['skin'] ) || count( $wgExtensionCredits['skin'] ) === 0 ) {
496 return '';
497 }
498
499 $out = Xml::element(
500 'h2',
501 array( 'id' => 'mw-version-skin' ),
502 $this->msg( 'version-skins' )->text()
503 ) .
504 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-skin' ) );
505
506 $this->firstExtOpened = false;
507 $out .= $this->getExtensionCategory( 'skin', null );
508
509 $out .= Xml::closeElement( 'table' );
510
511 return $out;
512 }
513
514 /**
515 * Generate an HTML table for external libraries that are installed
516 *
517 * @return string
518 */
519 protected function getExternalLibraries() {
520 global $IP;
521 $path = "$IP/composer.lock";
522 if ( !file_exists( $path ) ) {
523 // Maybe they're using mediawiki/vendor?
524 $path = "$IP/vendor/composer.lock";
525 if ( !file_exists( $path ) ) {
526 return '';
527 }
528 }
529
530 $lock = new ComposerLock( $path );
531 $out = Html::element(
532 'h2',
533 array( 'id' => 'mw-version-libraries' ),
534 $this->msg( 'version-libraries' )->text()
535 );
536 $out .= Html::openElement(
537 'table',
538 array( 'class' => 'wikitable plainlinks', 'id' => 'sv-libraries' )
539 );
540 $out .= Html::openElement( 'tr' )
541 . Html::element( 'th', array(), $this->msg( 'version-libraries-library' )->text() )
542 . Html::element( 'th', array(), $this->msg( 'version-libraries-version' )->text() )
543 . Html::element( 'th', array(), $this->msg( 'version-libraries-license' )->text() )
544 . Html::element( 'th', array(), $this->msg( 'version-libraries-description' )->text() )
545 . Html::element( 'th', array(), $this->msg( 'version-libraries-authors' )->text() )
546 . Html::closeElement( 'tr' );
547
548 foreach ( $lock->getInstalledDependencies() as $name => $info ) {
549 if ( strpos( $info['type'], 'mediawiki-' ) === 0 ) {
550 // Skip any extensions or skins since they'll be listed
551 // in their proper section
552 continue;
553 }
554 $authors = array_map( function( $arr ) {
555 // If a homepage is set, link to it
556 if ( isset( $arr['homepage'] ) ) {
557 return "[{$arr['homepage']} {$arr['name']}]";
558 }
559 return $arr['name'];
560 }, $info['authors'] );
561 $authors = $this->listAuthors( $authors, false, "$IP/vendor/$name" );
562
563 // We can safely assume that the libraries' names and descriptions
564 // are written in English and aren't going to be translated,
565 // so set appropriate lang and dir attributes
566 $out .= Html::openElement( 'tr' )
567 . Html::rawElement(
568 'td',
569 array(),
570 Linker::makeExternalLink(
571 "https://packagist.org/packages/$name", $name,
572 true, '',
573 array( 'class' => 'mw-version-library-name' )
574 )
575 )
576 . Html::element( 'td', array( 'dir' => 'auto' ), $info['version'] )
577 . Html::element( 'td', array( 'dir' => 'auto' ), $this->listToText( $info['licenses'] ) )
578 . Html::element( 'td', array( 'lang' => 'en', 'dir' => 'ltr' ), $info['description'] )
579 . Html::rawElement( 'td', array(), $authors )
580 . Html::closeElement( 'tr' );
581 }
582 $out .= Html::closeElement( 'table' );
583
584 return $out;
585 }
586
587 /**
588 * Obtains a list of installed parser tags and the associated H2 header
589 *
590 * @return string HTML output
591 */
592 protected function getParserTags() {
593 global $wgParser;
594
595 $tags = $wgParser->getTags();
596
597 if ( count( $tags ) ) {
598 $out = Html::rawElement(
599 'h2',
600 array(
601 'class' => 'mw-headline plainlinks',
602 'id' => 'mw-version-parser-extensiontags',
603 ),
604 Linker::makeExternalLink(
605 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Tag_extensions',
606 $this->msg( 'version-parser-extensiontags' )->parse(),
607 false /* msg()->parse() already escapes */
608 )
609 );
610
611 array_walk( $tags, function ( &$value ) {
612 // Bidirectional isolation improves readability in RTL wikis
613 $value = Html::element(
614 'bdi',
615 // Prevent < and > from slipping to another line
616 array(
617 'style' => 'white-space: nowrap;',
618 ),
619 "<$value>"
620 );
621 } );
622
623 $out .= $this->listToText( $tags );
624 } else {
625 $out = '';
626 }
627
628 return $out;
629 }
630
631 /**
632 * Obtains a list of installed parser function hooks and the associated H2 header
633 *
634 * @return string HTML output
635 */
636 protected function getParserFunctionHooks() {
637 global $wgParser;
638
639 $fhooks = $wgParser->getFunctionHooks();
640 if ( count( $fhooks ) ) {
641 $out = Html::rawElement(
642 'h2',
643 array(
644 'class' => 'mw-headline plainlinks',
645 'id' => 'mw-version-parser-function-hooks',
646 ),
647 Linker::makeExternalLink(
648 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Parser_functions',
649 $this->msg( 'version-parser-function-hooks' )->parse(),
650 false /* msg()->parse() already escapes */
651 )
652 );
653
654 $out .= $this->listToText( $fhooks );
655 } else {
656 $out = '';
657 }
658
659 return $out;
660 }
661
662 /**
663 * Creates and returns the HTML for a single extension category.
664 *
665 * @since 1.17
666 *
667 * @param string $type
668 * @param string $message
669 *
670 * @return string
671 */
672 protected function getExtensionCategory( $type, $message ) {
673 global $wgExtensionCredits;
674
675 $out = '';
676
677 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
678 $out .= $this->openExtType( $message, 'credits-' . $type );
679
680 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
681
682 foreach ( $wgExtensionCredits[$type] as $extension ) {
683 $out .= $this->getCreditsForExtension( $extension );
684 }
685 }
686
687 return $out;
688 }
689
690 /**
691 * Callback to sort extensions by type.
692 * @param array $a
693 * @param array $b
694 * @return int
695 */
696 public function compare( $a, $b ) {
697 if ( $a['name'] === $b['name'] ) {
698 return 0;
699 } else {
700 return $this->getLanguage()->lc( $a['name'] ) > $this->getLanguage()->lc( $b['name'] )
701 ? 1
702 : -1;
703 }
704 }
705
706 /**
707 * Creates and formats a version line for a single extension.
708 *
709 * Information for five columns will be created. Parameters required in the
710 * $extension array for part rendering are indicated in ()
711 * - The name of (name), and URL link to (url), the extension
712 * - Official version number (version) and if available version control system
713 * revision (path), link, and date
714 * - If available the short name of the license (license-name) and a linke
715 * to ((LICENSE)|(COPYING))(\.txt)? if it exists.
716 * - Description of extension (descriptionmsg or description)
717 * - List of authors (author) and link to a ((AUTHORS)|(CREDITS))(\.txt)? file if it exists
718 *
719 * @param array $extension
720 *
721 * @return string Raw HTML
722 */
723 public function getCreditsForExtension( array $extension ) {
724 $out = $this->getOutput();
725
726 // We must obtain the information for all the bits and pieces!
727 // ... such as extension names and links
728 if ( isset( $extension['namemsg'] ) ) {
729 // Localized name of extension
730 $extensionName = $this->msg( $extension['namemsg'] )->text();
731 } elseif ( isset( $extension['name'] ) ) {
732 // Non localized version
733 $extensionName = $extension['name'];
734 } else {
735 $extensionName = $this->msg( 'version-no-ext-name' )->text();
736 }
737
738 if ( isset( $extension['url'] ) ) {
739 $extensionNameLink = Linker::makeExternalLink(
740 $extension['url'],
741 $extensionName,
742 true,
743 '',
744 array( 'class' => 'mw-version-ext-name' )
745 );
746 } else {
747 $extensionNameLink = $extensionName;
748 }
749
750 // ... and the version information
751 // If the extension path is set we will check that directory for GIT and SVN
752 // metadata in an attempt to extract date and vcs commit metadata.
753 $canonicalVersion = '&ndash;';
754 $extensionPath = null;
755 $vcsVersion = null;
756 $vcsLink = null;
757 $vcsDate = null;
758
759 if ( isset( $extension['version'] ) ) {
760 $canonicalVersion = $out->parseInline( $extension['version'] );
761 }
762
763 if ( isset( $extension['path'] ) ) {
764 global $IP;
765 $extensionPath = dirname( $extension['path'] );
766 if ( $this->coreId == '' ) {
767 wfDebug( 'Looking up core head id' );
768 $coreHeadSHA1 = self::getGitHeadSha1( $IP );
769 if ( $coreHeadSHA1 ) {
770 $this->coreId = $coreHeadSHA1;
771 } else {
772 $svnInfo = self::getSvnInfo( $IP );
773 if ( $svnInfo !== false ) {
774 $this->coreId = $svnInfo['checkout-rev'];
775 }
776 }
777 }
778 $cache = wfGetCache( CACHE_ANYTHING );
779 $memcKey = wfMemcKey( 'specialversion-ext-version-text', $extension['path'], $this->coreId );
780 list( $vcsVersion, $vcsLink, $vcsDate ) = $cache->get( $memcKey );
781
782 if ( !$vcsVersion ) {
783 wfDebug( "Getting VCS info for extension {$extension['name']}" );
784 $gitInfo = new GitInfo( $extensionPath );
785 $vcsVersion = $gitInfo->getHeadSHA1();
786 if ( $vcsVersion !== false ) {
787 $vcsVersion = substr( $vcsVersion, 0, 7 );
788 $vcsLink = $gitInfo->getHeadViewUrl();
789 $vcsDate = $gitInfo->getHeadCommitDate();
790 } else {
791 $svnInfo = self::getSvnInfo( $extensionPath );
792 if ( $svnInfo !== false ) {
793 $vcsVersion = $this->msg( 'version-svn-revision', $svnInfo['checkout-rev'] )->text();
794 $vcsLink = isset( $svnInfo['viewvc-url'] ) ? $svnInfo['viewvc-url'] : '';
795 }
796 }
797 $cache->set( $memcKey, array( $vcsVersion, $vcsLink, $vcsDate ), 60 * 60 * 24 );
798 } else {
799 wfDebug( "Pulled VCS info for extension {$extension['name']} from cache" );
800 }
801 }
802
803 $versionString = Html::rawElement(
804 'span',
805 array( 'class' => 'mw-version-ext-version' ),
806 $canonicalVersion
807 );
808
809 if ( $vcsVersion ) {
810 if ( $vcsLink ) {
811 $vcsVerString = Linker::makeExternalLink(
812 $vcsLink,
813 $this->msg( 'version-version', $vcsVersion ),
814 true,
815 '',
816 array( 'class' => 'mw-version-ext-vcs-version' )
817 );
818 } else {
819 $vcsVerString = Html::element( 'span',
820 array( 'class' => 'mw-version-ext-vcs-version' ),
821 "({$vcsVersion})"
822 );
823 }
824 $versionString .= " {$vcsVerString}";
825
826 if ( $vcsDate ) {
827 $vcsTimeString = Html::element( 'span',
828 array( 'class' => 'mw-version-ext-vcs-timestamp' ),
829 $this->getLanguage()->timeanddate( $vcsDate, true )
830 );
831 $versionString .= " {$vcsTimeString}";
832 }
833 $versionString = Html::rawElement( 'span',
834 array( 'class' => 'mw-version-ext-meta-version' ),
835 $versionString
836 );
837 }
838
839 // ... and license information; if a license file exists we
840 // will link to it
841 $licenseLink = '';
842 if ( isset( $extension['name'] ) ) {
843 $licenseName = null;
844 if ( isset( $extension['license-name'] ) ) {
845 $licenseName = $out->parseInline( $extension['license-name'] );
846 } elseif ( $this->getExtLicenseFileName( $extensionPath ) ) {
847 $licenseName = $this->msg( 'version-ext-license' );
848 }
849 if ( $licenseName !== null ) {
850 $licenseLink = Linker::link(
851 $this->getPageTitle( 'License/' . $extension['name'] ),
852 $licenseName,
853 array(
854 'class' => 'mw-version-ext-license',
855 'dir' => 'auto',
856 )
857 );
858 }
859 }
860
861 // ... and generate the description; which can be a parameterized l10n message
862 // in the form array( <msgname>, <parameter>, <parameter>... ) or just a straight
863 // up string
864 if ( isset( $extension['descriptionmsg'] ) ) {
865 // Localized description of extension
866 $descriptionMsg = $extension['descriptionmsg'];
867
868 if ( is_array( $descriptionMsg ) ) {
869 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
870 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
871 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
872 $description = $this->msg( $descriptionMsgKey, $descriptionMsg )->text();
873 } else {
874 $description = $this->msg( $descriptionMsg )->text();
875 }
876 } elseif ( isset( $extension['description'] ) ) {
877 // Non localized version
878 $description = $extension['description'];
879 } else {
880 $description = '';
881 }
882 $description = $out->parseInline( $description );
883
884 // ... now get the authors for this extension
885 $authors = isset( $extension['author'] ) ? $extension['author'] : array();
886 $authors = $this->listAuthors( $authors, $extension['name'], $extensionPath );
887
888 // Finally! Create the table
889 $html = Html::openElement( 'tr', array(
890 'class' => 'mw-version-ext',
891 'id' => Sanitizer::escapeId( 'mw-version-ext-' . $extension['name'] )
892 )
893 );
894
895 $html .= Html::rawElement( 'td', array(), $extensionNameLink );
896 $html .= Html::rawElement( 'td', array(), $versionString );
897 $html .= Html::rawElement( 'td', array(), $licenseLink );
898 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-description' ), $description );
899 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-authors' ), $authors );
900
901 $html .= Html::closeElement( 'tr' );
902
903 return $html;
904 }
905
906 /**
907 * Generate wikitext showing hooks in $wgHooks.
908 *
909 * @return string Wikitext
910 */
911 private function getWgHooks() {
912 global $wgSpecialVersionShowHooks, $wgHooks;
913
914 if ( $wgSpecialVersionShowHooks && count( $wgHooks ) ) {
915 $myWgHooks = $wgHooks;
916 ksort( $myWgHooks );
917
918 $ret = array();
919 $ret[] = '== {{int:version-hooks}} ==';
920 $ret[] = Html::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) );
921 $ret[] = Html::openElement( 'tr' );
922 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-name' )->text() );
923 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-subscribedby' )->text() );
924 $ret[] = Html::closeElement( 'tr' );
925
926 foreach ( $myWgHooks as $hook => $hooks ) {
927 $ret[] = Html::openElement( 'tr' );
928 $ret[] = Html::element( 'td', array(), $hook );
929 $ret[] = Html::element( 'td', array(), $this->listToText( $hooks ) );
930 $ret[] = Html::closeElement( 'tr' );
931 }
932
933 $ret[] = Html::closeElement( 'table' );
934
935 return implode( "\n", $ret );
936 } else {
937 return '';
938 }
939 }
940
941 private function openExtType( $text = null, $name = null ) {
942 $out = '';
943
944 $opt = array( 'colspan' => 5 );
945 if ( $this->firstExtOpened ) {
946 // Insert a spacing line
947 $out .= Html::rawElement( 'tr', array( 'class' => 'sv-space' ),
948 Html::element( 'td', $opt )
949 );
950 }
951 $this->firstExtOpened = true;
952
953 if ( $name ) {
954 $opt['id'] = "sv-$name";
955 }
956
957 if ( $text !== null ) {
958 $out .= Html::rawElement( 'tr', array(),
959 Html::element( 'th', $opt, $text )
960 );
961 }
962
963 $firstHeadingMsg = ( $name === 'credits-skin' )
964 ? 'version-skin-colheader-name'
965 : 'version-ext-colheader-name';
966 $out .= Html::openElement( 'tr' );
967 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
968 $this->msg( $firstHeadingMsg )->text() );
969 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
970 $this->msg( 'version-ext-colheader-version' )->text() );
971 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
972 $this->msg( 'version-ext-colheader-license' )->text() );
973 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
974 $this->msg( 'version-ext-colheader-description' )->text() );
975 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
976 $this->msg( 'version-ext-colheader-credits' )->text() );
977 $out .= Html::closeElement( 'tr' );
978
979 return $out;
980 }
981
982 /**
983 * Get information about client's IP address.
984 *
985 * @return string HTML fragment
986 */
987 private function IPInfo() {
988 $ip = str_replace( '--', ' - ', htmlspecialchars( $this->getRequest()->getIP() ) );
989
990 return "<!-- visited from $ip -->\n<span style='display:none'>visited from $ip</span>";
991 }
992
993 /**
994 * Return a formatted unsorted list of authors
995 *
996 * 'And Others'
997 * If an item in the $authors array is '...' it is assumed to indicate an
998 * 'and others' string which will then be linked to an ((AUTHORS)|(CREDITS))(\.txt)?
999 * file if it exists in $dir.
1000 *
1001 * Similarly an entry ending with ' ...]' is assumed to be a link to an
1002 * 'and others' page.
1003 *
1004 * If no '...' string variant is found, but an authors file is found an
1005 * 'and others' will be added to the end of the credits.
1006 *
1007 * @param string|array $authors
1008 * @param string|bool $extName Name of the extension for link creation,
1009 * false if no links should be created
1010 * @param string $extDir Path to the extension root directory
1011 *
1012 * @return string HTML fragment
1013 */
1014 public function listAuthors( $authors, $extName, $extDir ) {
1015 $hasOthers = false;
1016
1017 $list = array();
1018 foreach ( (array)$authors as $item ) {
1019 if ( $item == '...' ) {
1020 $hasOthers = true;
1021
1022 if ( $extName && $this->getExtAuthorsFileName( $extDir ) ) {
1023 $text = Linker::link(
1024 $this->getPageTitle( "Credits/$extName" ),
1025 $this->msg( 'version-poweredby-others' )->escaped()
1026 );
1027 } else {
1028 $text = $this->msg( 'version-poweredby-others' )->escaped();
1029 }
1030 $list[] = $text;
1031 } elseif ( substr( $item, -5 ) == ' ...]' ) {
1032 $hasOthers = true;
1033 $list[] = $this->getOutput()->parseInline(
1034 substr( $item, 0, -4 ) . $this->msg( 'version-poweredby-others' )->text() . "]"
1035 );
1036 } else {
1037 $list[] = $this->getOutput()->parseInline( $item );
1038 }
1039 }
1040
1041 if ( $extName && !$hasOthers && $this->getExtAuthorsFileName( $extDir ) ) {
1042 $list[] = $text = Linker::link(
1043 $this->getPageTitle( "Credits/$extName" ),
1044 $this->msg( 'version-poweredby-others' )->escaped()
1045 );
1046 }
1047
1048 return $this->listToText( $list, false );
1049 }
1050
1051 /**
1052 * Obtains the full path of an extensions authors or credits file if
1053 * one exists.
1054 *
1055 * @param string $extDir Path to the extensions root directory
1056 *
1057 * @since 1.23
1058 *
1059 * @return bool|string False if no such file exists, otherwise returns
1060 * a path to it.
1061 */
1062 public static function getExtAuthorsFileName( $extDir ) {
1063 if ( !$extDir ) {
1064 return false;
1065 }
1066
1067 foreach ( scandir( $extDir ) as $file ) {
1068 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1069 if ( preg_match( '/^((AUTHORS)|(CREDITS))(\.txt)?$/', $file ) &&
1070 is_readable( $fullPath ) &&
1071 is_file( $fullPath )
1072 ) {
1073 return $fullPath;
1074 }
1075 }
1076
1077 return false;
1078 }
1079
1080 /**
1081 * Obtains the full path of an extensions copying or license file if
1082 * one exists.
1083 *
1084 * @param string $extDir Path to the extensions root directory
1085 *
1086 * @since 1.23
1087 *
1088 * @return bool|string False if no such file exists, otherwise returns
1089 * a path to it.
1090 */
1091 public static function getExtLicenseFileName( $extDir ) {
1092 if ( !$extDir ) {
1093 return false;
1094 }
1095
1096 foreach ( scandir( $extDir ) as $file ) {
1097 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1098 if ( preg_match( '/^((COPYING)|(LICENSE))(\.txt)?$/', $file ) &&
1099 is_readable( $fullPath ) &&
1100 is_file( $fullPath )
1101 ) {
1102 return $fullPath;
1103 }
1104 }
1105
1106 return false;
1107 }
1108
1109 /**
1110 * Convert an array of items into a list for display.
1111 *
1112 * @param array $list List of elements to display
1113 * @param bool $sort Whether to sort the items in $list
1114 *
1115 * @return string
1116 */
1117 public function listToText( $list, $sort = true ) {
1118 if ( !count( $list ) ) {
1119 return '';
1120 }
1121 if ( $sort ) {
1122 sort( $list );
1123 }
1124
1125 return $this->getLanguage()
1126 ->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
1127 }
1128
1129 /**
1130 * Convert an array or object to a string for display.
1131 *
1132 * @param mixed $list Will convert an array to string if given and return
1133 * the parameter unaltered otherwise
1134 *
1135 * @return mixed
1136 */
1137 public static function arrayToString( $list ) {
1138 if ( is_array( $list ) && count( $list ) == 1 ) {
1139 $list = $list[0];
1140 }
1141 if ( $list instanceof Closure ) {
1142 // Don't output stuff like "Closure$;1028376090#8$48499d94fe0147f7c633b365be39952b$"
1143 return 'Closure';
1144 } elseif ( is_object( $list ) ) {
1145 $class = wfMessage( 'parentheses' )->params( get_class( $list ) )->escaped();
1146
1147 return $class;
1148 } elseif ( !is_array( $list ) ) {
1149 return $list;
1150 } else {
1151 if ( is_object( $list[0] ) ) {
1152 $class = get_class( $list[0] );
1153 } else {
1154 $class = $list[0];
1155 }
1156
1157 return wfMessage( 'parentheses' )->params( "$class, {$list[1]}" )->escaped();
1158 }
1159 }
1160
1161 /**
1162 * Get an associative array of information about a given path, from its .svn
1163 * subdirectory. Returns false on error, such as if the directory was not
1164 * checked out with subversion.
1165 *
1166 * Returned keys are:
1167 * Required:
1168 * checkout-rev The revision which was checked out
1169 * Optional:
1170 * directory-rev The revision when the directory was last modified
1171 * url The subversion URL of the directory
1172 * repo-url The base URL of the repository
1173 * viewvc-url A ViewVC URL pointing to the checked-out revision
1174 * @param string $dir
1175 * @return array|bool
1176 */
1177 public static function getSvnInfo( $dir ) {
1178 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
1179 $entries = $dir . '/.svn/entries';
1180
1181 if ( !file_exists( $entries ) ) {
1182 return false;
1183 }
1184
1185 $lines = file( $entries );
1186 if ( !count( $lines ) ) {
1187 return false;
1188 }
1189
1190 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
1191 if ( preg_match( '/^<\?xml/', $lines[0] ) ) {
1192 // subversion is release <= 1.3
1193 if ( !function_exists( 'simplexml_load_file' ) ) {
1194 // We could fall back to expat... YUCK
1195 return false;
1196 }
1197
1198 // SimpleXml whines about the xmlns...
1199 MediaWiki\suppressWarnings();
1200 $xml = simplexml_load_file( $entries );
1201 MediaWiki\restoreWarnings();
1202
1203 if ( $xml ) {
1204 foreach ( $xml->entry as $entry ) {
1205 if ( $xml->entry[0]['name'] == '' ) {
1206 // The directory entry should always have a revision marker.
1207 if ( $entry['revision'] ) {
1208 return array( 'checkout-rev' => intval( $entry['revision'] ) );
1209 }
1210 }
1211 }
1212 }
1213
1214 return false;
1215 }
1216
1217 // Subversion is release 1.4 or above.
1218 if ( count( $lines ) < 11 ) {
1219 return false;
1220 }
1221
1222 $info = array(
1223 'checkout-rev' => intval( trim( $lines[3] ) ),
1224 'url' => trim( $lines[4] ),
1225 'repo-url' => trim( $lines[5] ),
1226 'directory-rev' => intval( trim( $lines[10] ) )
1227 );
1228
1229 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
1230 $viewvc = str_replace(
1231 $info['repo-url'],
1232 self::$viewvcUrls[$info['repo-url']],
1233 $info['url']
1234 );
1235
1236 $viewvc .= '/?pathrev=';
1237 $viewvc .= urlencode( $info['checkout-rev'] );
1238 $info['viewvc-url'] = $viewvc;
1239 }
1240
1241 return $info;
1242 }
1243
1244 /**
1245 * Retrieve the revision number of a Subversion working directory.
1246 *
1247 * @param string $dir Directory of the svn checkout
1248 *
1249 * @return int Revision number
1250 */
1251 public static function getSvnRevision( $dir ) {
1252 $info = self::getSvnInfo( $dir );
1253
1254 if ( $info === false ) {
1255 return false;
1256 } elseif ( isset( $info['checkout-rev'] ) ) {
1257 return $info['checkout-rev'];
1258 } else {
1259 return false;
1260 }
1261 }
1262
1263 /**
1264 * @param string $dir Directory of the git checkout
1265 * @return bool|string Sha1 of commit HEAD points to
1266 */
1267 public static function getGitHeadSha1( $dir ) {
1268 $repo = new GitInfo( $dir );
1269
1270 return $repo->getHeadSHA1();
1271 }
1272
1273 /**
1274 * @param string $dir Directory of the git checkout
1275 * @return bool|string Branch currently checked out
1276 */
1277 public static function getGitCurrentBranch( $dir ) {
1278 $repo = new GitInfo( $dir );
1279 return $repo->getCurrentBranch();
1280 }
1281
1282 /**
1283 * Get the list of entry points and their URLs
1284 * @return string Wikitext
1285 */
1286 public function getEntryPointInfo() {
1287 global $wgArticlePath, $wgScriptPath;
1288 $scriptPath = $wgScriptPath ? $wgScriptPath : "/";
1289 $entryPoints = array(
1290 'version-entrypoints-articlepath' => $wgArticlePath,
1291 'version-entrypoints-scriptpath' => $scriptPath,
1292 'version-entrypoints-index-php' => wfScript( 'index' ),
1293 'version-entrypoints-api-php' => wfScript( 'api' ),
1294 'version-entrypoints-load-php' => wfScript( 'load' ),
1295 );
1296
1297 $language = $this->getLanguage();
1298 $thAttribures = array(
1299 'dir' => $language->getDir(),
1300 'lang' => $language->getHtmlCode()
1301 );
1302 $out = Html::element(
1303 'h2',
1304 array( 'id' => 'mw-version-entrypoints' ),
1305 $this->msg( 'version-entrypoints' )->text()
1306 ) .
1307 Html::openElement( 'table',
1308 array(
1309 'class' => 'wikitable plainlinks',
1310 'id' => 'mw-version-entrypoints-table',
1311 'dir' => 'ltr',
1312 'lang' => 'en'
1313 )
1314 ) .
1315 Html::openElement( 'tr' ) .
1316 Html::element(
1317 'th',
1318 $thAttribures,
1319 $this->msg( 'version-entrypoints-header-entrypoint' )->text()
1320 ) .
1321 Html::element(
1322 'th',
1323 $thAttribures,
1324 $this->msg( 'version-entrypoints-header-url' )->text()
1325 ) .
1326 Html::closeElement( 'tr' );
1327
1328 foreach ( $entryPoints as $message => $value ) {
1329 $url = wfExpandUrl( $value, PROTO_RELATIVE );
1330 $out .= Html::openElement( 'tr' ) .
1331 // ->text() looks like it should be ->parse(), but this function
1332 // returns wikitext, not HTML, boo
1333 Html::rawElement( 'td', array(), $this->msg( $message )->text() ) .
1334 Html::rawElement( 'td', array(), Html::rawElement( 'code', array(), "[$url $value]" ) ) .
1335 Html::closeElement( 'tr' );
1336 }
1337
1338 $out .= Html::closeElement( 'table' );
1339
1340 return $out;
1341 }
1342
1343 protected function getGroupName() {
1344 return 'wiki';
1345 }
1346 }