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