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