Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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->addWikiTextAsInterface( $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->addWikiTextAsInterface( $wikiText );
133 break;
134
135 default:
136 $out->addModuleStyles( 'mediawiki.special.version' );
137 $out->addWikiTextAsInterface(
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->addWikiTextAsInterface( $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['[https://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 ( defined( 'INTL_ICU_VERSION' ) ) {
237 $software['[http://site.icu-project.org/ ICU]'] = INTL_ICU_VERSION;
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 'editor' => wfMessage( 'version-editors' )->text(),
372 'parserhook' => wfMessage( 'version-parserhooks' )->text(),
373 'variable' => wfMessage( 'version-variables' )->text(),
374 'media' => wfMessage( 'version-mediahandlers' )->text(),
375 'antispam' => wfMessage( 'version-antispam' )->text(),
376 'skin' => wfMessage( 'version-skins' )->text(),
377 'api' => wfMessage( 'version-api' )->text(),
378 'other' => wfMessage( 'version-other' )->text(),
379 ];
380
381 Hooks::run( 'ExtensionTypes', [ &self::$extensionTypes ] );
382 }
383
384 return self::$extensionTypes;
385 }
386
387 /**
388 * Returns the internationalized name for an extension type.
389 *
390 * @since 1.17
391 *
392 * @param string $type
393 *
394 * @return string
395 */
396 public static function getExtensionTypeName( $type ) {
397 $types = self::getExtensionTypes();
398
399 return $types[$type] ?? $types['other'];
400 }
401
402 /**
403 * Generate wikitext showing the name, URL, author and description of each extension.
404 *
405 * @return string Wikitext
406 */
407 public function getExtensionCredits() {
408 global $wgExtensionCredits;
409
410 if (
411 count( $wgExtensionCredits ) === 0 ||
412 // Skins are displayed separately, see getSkinCredits()
413 ( count( $wgExtensionCredits ) === 1 && isset( $wgExtensionCredits['skin'] ) )
414 ) {
415 return '';
416 }
417
418 $extensionTypes = self::getExtensionTypes();
419
420 $out = Xml::element(
421 'h2',
422 [ 'id' => 'mw-version-ext' ],
423 $this->msg( 'version-extensions' )->text()
424 ) .
425 Xml::openElement( 'table', [ 'class' => 'wikitable plainlinks', 'id' => 'sv-ext' ] );
426
427 // Make sure the 'other' type is set to an array.
428 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
429 $wgExtensionCredits['other'] = [];
430 }
431
432 // Find all extensions that do not have a valid type and give them the type 'other'.
433 foreach ( $wgExtensionCredits as $type => $extensions ) {
434 if ( !array_key_exists( $type, $extensionTypes ) ) {
435 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
436 }
437 }
438
439 $this->firstExtOpened = false;
440 // Loop through the extension categories to display their extensions in the list.
441 foreach ( $extensionTypes as $type => $message ) {
442 // Skins have a separate section
443 if ( $type !== 'other' && $type !== 'skin' ) {
444 $out .= $this->getExtensionCategory( $type, $message );
445 }
446 }
447
448 // We want the 'other' type to be last in the list.
449 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
450
451 $out .= Xml::closeElement( 'table' );
452
453 return $out;
454 }
455
456 /**
457 * Generate wikitext showing the name, URL, author and description of each skin.
458 *
459 * @return string Wikitext
460 */
461 public function getSkinCredits() {
462 global $wgExtensionCredits;
463 if ( !isset( $wgExtensionCredits['skin'] ) || count( $wgExtensionCredits['skin'] ) === 0 ) {
464 return '';
465 }
466
467 $out = Xml::element(
468 'h2',
469 [ 'id' => 'mw-version-skin' ],
470 $this->msg( 'version-skins' )->text()
471 ) .
472 Xml::openElement( 'table', [ 'class' => 'wikitable plainlinks', 'id' => 'sv-skin' ] );
473
474 $this->firstExtOpened = false;
475 $out .= $this->getExtensionCategory( 'skin', null );
476
477 $out .= Xml::closeElement( 'table' );
478
479 return $out;
480 }
481
482 /**
483 * Generate an HTML table for external libraries that are installed
484 *
485 * @return string
486 */
487 protected function getExternalLibraries() {
488 global $IP;
489 $path = "$IP/vendor/composer/installed.json";
490 if ( !file_exists( $path ) ) {
491 return '';
492 }
493
494 $installed = new ComposerInstalled( $path );
495 $out = Html::element(
496 'h2',
497 [ 'id' => 'mw-version-libraries' ],
498 $this->msg( 'version-libraries' )->text()
499 );
500 $out .= Html::openElement(
501 'table',
502 [ 'class' => 'wikitable plainlinks', 'id' => 'sv-libraries' ]
503 );
504 $out .= Html::openElement( 'tr' )
505 . Html::element( 'th', [], $this->msg( 'version-libraries-library' )->text() )
506 . Html::element( 'th', [], $this->msg( 'version-libraries-version' )->text() )
507 . Html::element( 'th', [], $this->msg( 'version-libraries-license' )->text() )
508 . Html::element( 'th', [], $this->msg( 'version-libraries-description' )->text() )
509 . Html::element( 'th', [], $this->msg( 'version-libraries-authors' )->text() )
510 . Html::closeElement( 'tr' );
511
512 foreach ( $installed->getInstalledDependencies() as $name => $info ) {
513 if ( strpos( $info['type'], 'mediawiki-' ) === 0 ) {
514 // Skip any extensions or skins since they'll be listed
515 // in their proper section
516 continue;
517 }
518 $authors = array_map( function ( $arr ) {
519 // If a homepage is set, link to it
520 if ( isset( $arr['homepage'] ) ) {
521 return "[{$arr['homepage']} {$arr['name']}]";
522 }
523 return $arr['name'];
524 }, $info['authors'] );
525 $authors = $this->listAuthors( $authors, false, "$IP/vendor/$name" );
526
527 // We can safely assume that the libraries' names and descriptions
528 // are written in English and aren't going to be translated,
529 // so set appropriate lang and dir attributes
530 $out .= Html::openElement( 'tr' )
531 . Html::rawElement(
532 'td',
533 [],
534 Linker::makeExternalLink(
535 "https://packagist.org/packages/$name", $name,
536 true, '',
537 [ 'class' => 'mw-version-library-name' ]
538 )
539 )
540 . Html::element( 'td', [ 'dir' => 'auto' ], $info['version'] )
541 . Html::element( 'td', [ 'dir' => 'auto' ], $this->listToText( $info['licenses'] ) )
542 . Html::element( 'td', [ 'lang' => 'en', 'dir' => 'ltr' ], $info['description'] )
543 . Html::rawElement( 'td', [], $authors )
544 . Html::closeElement( 'tr' );
545 }
546 $out .= Html::closeElement( 'table' );
547
548 return $out;
549 }
550
551 /**
552 * Obtains a list of installed parser tags and the associated H2 header
553 *
554 * @return string HTML output
555 */
556 protected function getParserTags() {
557 global $wgParser;
558
559 $tags = $wgParser->getTags();
560
561 if ( count( $tags ) ) {
562 $out = Html::rawElement(
563 'h2',
564 [
565 'class' => 'mw-headline plainlinks',
566 'id' => 'mw-version-parser-extensiontags',
567 ],
568 Linker::makeExternalLink(
569 'https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Tag_extensions',
570 $this->msg( 'version-parser-extensiontags' )->parse(),
571 false /* msg()->parse() already escapes */
572 )
573 );
574
575 array_walk( $tags, function ( &$value ) {
576 // Bidirectional isolation improves readability in RTL wikis
577 $value = Html::element(
578 'bdi',
579 // Prevent < and > from slipping to another line
580 [
581 'style' => 'white-space: nowrap;',
582 ],
583 "<$value>"
584 );
585 } );
586
587 $out .= $this->listToText( $tags );
588 } else {
589 $out = '';
590 }
591
592 return $out;
593 }
594
595 /**
596 * Obtains a list of installed parser function hooks and the associated H2 header
597 *
598 * @return string HTML output
599 */
600 protected function getParserFunctionHooks() {
601 global $wgParser;
602
603 $fhooks = $wgParser->getFunctionHooks();
604 if ( count( $fhooks ) ) {
605 $out = Html::rawElement(
606 'h2',
607 [
608 'class' => 'mw-headline plainlinks',
609 'id' => 'mw-version-parser-function-hooks',
610 ],
611 Linker::makeExternalLink(
612 'https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Parser_functions',
613 $this->msg( 'version-parser-function-hooks' )->parse(),
614 false /* msg()->parse() already escapes */
615 )
616 );
617
618 $out .= $this->listToText( $fhooks );
619 } else {
620 $out = '';
621 }
622
623 return $out;
624 }
625
626 /**
627 * Creates and returns the HTML for a single extension category.
628 *
629 * @since 1.17
630 *
631 * @param string $type
632 * @param string $message
633 *
634 * @return string
635 */
636 protected function getExtensionCategory( $type, $message ) {
637 global $wgExtensionCredits;
638
639 $out = '';
640
641 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
642 $out .= $this->openExtType( $message, 'credits-' . $type );
643
644 usort( $wgExtensionCredits[$type], [ $this, 'compare' ] );
645
646 foreach ( $wgExtensionCredits[$type] as $extension ) {
647 $out .= $this->getCreditsForExtension( $type, $extension );
648 }
649 }
650
651 return $out;
652 }
653
654 /**
655 * Callback to sort extensions by type.
656 * @param array $a
657 * @param array $b
658 * @return int
659 */
660 public function compare( $a, $b ) {
661 return $this->getLanguage()->lc( $a['name'] ) <=> $this->getLanguage()->lc( $b['name'] );
662 }
663
664 /**
665 * Creates and formats a version line for a single extension.
666 *
667 * Information for five columns will be created. Parameters required in the
668 * $extension array for part rendering are indicated in ()
669 * - The name of (name), and URL link to (url), the extension
670 * - Official version number (version) and if available version control system
671 * revision (path), link, and date
672 * - If available the short name of the license (license-name) and a link
673 * to ((LICENSE)|(COPYING))(\.txt)? if it exists.
674 * - Description of extension (descriptionmsg or description)
675 * - List of authors (author) and link to a ((AUTHORS)|(CREDITS))(\.txt)? file if it exists
676 *
677 * @param string $type Category name of the extension
678 * @param array $extension
679 *
680 * @return string Raw HTML
681 */
682 public function getCreditsForExtension( $type, array $extension ) {
683 $out = $this->getOutput();
684
685 // We must obtain the information for all the bits and pieces!
686 // ... such as extension names and links
687 if ( isset( $extension['namemsg'] ) ) {
688 // Localized name of extension
689 $extensionName = $this->msg( $extension['namemsg'] )->text();
690 } elseif ( isset( $extension['name'] ) ) {
691 // Non localized version
692 $extensionName = $extension['name'];
693 } else {
694 $extensionName = $this->msg( 'version-no-ext-name' )->text();
695 }
696
697 if ( isset( $extension['url'] ) ) {
698 $extensionNameLink = Linker::makeExternalLink(
699 $extension['url'],
700 $extensionName,
701 true,
702 '',
703 [ 'class' => 'mw-version-ext-name' ]
704 );
705 } else {
706 $extensionNameLink = htmlspecialchars( $extensionName );
707 }
708
709 // ... and the version information
710 // If the extension path is set we will check that directory for GIT
711 // metadata in an attempt to extract date and vcs commit metadata.
712 $canonicalVersion = '&ndash;';
713 $extensionPath = null;
714 $vcsVersion = null;
715 $vcsLink = null;
716 $vcsDate = null;
717
718 if ( isset( $extension['version'] ) ) {
719 $canonicalVersion = $out->parseInlineAsInterface( $extension['version'] );
720 }
721
722 if ( isset( $extension['path'] ) ) {
723 global $IP;
724 $extensionPath = dirname( $extension['path'] );
725 if ( $this->coreId == '' ) {
726 wfDebug( 'Looking up core head id' );
727 $coreHeadSHA1 = self::getGitHeadSha1( $IP );
728 if ( $coreHeadSHA1 ) {
729 $this->coreId = $coreHeadSHA1;
730 }
731 }
732 $cache = wfGetCache( CACHE_ANYTHING );
733 $memcKey = $cache->makeKey(
734 'specialversion-ext-version-text', $extension['path'], $this->coreId
735 );
736 list( $vcsVersion, $vcsLink, $vcsDate ) = $cache->get( $memcKey );
737
738 if ( !$vcsVersion ) {
739 wfDebug( "Getting VCS info for extension {$extension['name']}" );
740 $gitInfo = new GitInfo( $extensionPath );
741 $vcsVersion = $gitInfo->getHeadSHA1();
742 if ( $vcsVersion !== false ) {
743 $vcsVersion = substr( $vcsVersion, 0, 7 );
744 $vcsLink = $gitInfo->getHeadViewUrl();
745 $vcsDate = $gitInfo->getHeadCommitDate();
746 }
747 $cache->set( $memcKey, [ $vcsVersion, $vcsLink, $vcsDate ], 60 * 60 * 24 );
748 } else {
749 wfDebug( "Pulled VCS info for extension {$extension['name']} from cache" );
750 }
751 }
752
753 $versionString = Html::rawElement(
754 'span',
755 [ 'class' => 'mw-version-ext-version' ],
756 $canonicalVersion
757 );
758
759 if ( $vcsVersion ) {
760 if ( $vcsLink ) {
761 $vcsVerString = Linker::makeExternalLink(
762 $vcsLink,
763 $this->msg( 'version-version', $vcsVersion ),
764 true,
765 '',
766 [ 'class' => 'mw-version-ext-vcs-version' ]
767 );
768 } else {
769 $vcsVerString = Html::element( 'span',
770 [ 'class' => 'mw-version-ext-vcs-version' ],
771 "({$vcsVersion})"
772 );
773 }
774 $versionString .= " {$vcsVerString}";
775
776 if ( $vcsDate ) {
777 $vcsTimeString = Html::element( 'span',
778 [ 'class' => 'mw-version-ext-vcs-timestamp' ],
779 $this->getLanguage()->timeanddate( $vcsDate, true )
780 );
781 $versionString .= " {$vcsTimeString}";
782 }
783 $versionString = Html::rawElement( 'span',
784 [ 'class' => 'mw-version-ext-meta-version' ],
785 $versionString
786 );
787 }
788
789 // ... and license information; if a license file exists we
790 // will link to it
791 $licenseLink = '';
792 if ( isset( $extension['name'] ) ) {
793 $licenseName = null;
794 if ( isset( $extension['license-name'] ) ) {
795 $licenseName = new HtmlArmor( $out->parseInlineAsInterface( $extension['license-name'] ) );
796 } elseif ( $this->getExtLicenseFileName( $extensionPath ) ) {
797 $licenseName = $this->msg( 'version-ext-license' )->text();
798 }
799 if ( $licenseName !== null ) {
800 $licenseLink = $this->getLinkRenderer()->makeLink(
801 $this->getPageTitle( 'License/' . $extension['name'] ),
802 $licenseName,
803 [
804 'class' => 'mw-version-ext-license',
805 'dir' => 'auto',
806 ]
807 );
808 }
809 }
810
811 // ... and generate the description; which can be a parameterized l10n message
812 // in the form array( <msgname>, <parameter>, <parameter>... ) or just a straight
813 // up string
814 if ( isset( $extension['descriptionmsg'] ) ) {
815 // Localized description of extension
816 $descriptionMsg = $extension['descriptionmsg'];
817
818 if ( is_array( $descriptionMsg ) ) {
819 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
820 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
821 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
822 $description = $this->msg( $descriptionMsgKey, $descriptionMsg )->text();
823 } else {
824 $description = $this->msg( $descriptionMsg )->text();
825 }
826 } elseif ( isset( $extension['description'] ) ) {
827 // Non localized version
828 $description = $extension['description'];
829 } else {
830 $description = '';
831 }
832 $description = $out->parseInlineAsInterface( $description );
833
834 // ... now get the authors for this extension
835 $authors = $extension['author'] ?? [];
836 $authors = $this->listAuthors( $authors, $extension['name'], $extensionPath );
837
838 // Finally! Create the table
839 $html = Html::openElement( 'tr', [
840 'class' => 'mw-version-ext',
841 'id' => Sanitizer::escapeIdForAttribute( 'mw-version-ext-' . $type . '-' . $extension['name'] )
842 ]
843 );
844
845 $html .= Html::rawElement( 'td', [], $extensionNameLink );
846 $html .= Html::rawElement( 'td', [], $versionString );
847 $html .= Html::rawElement( 'td', [], $licenseLink );
848 $html .= Html::rawElement( 'td', [ 'class' => 'mw-version-ext-description' ], $description );
849 $html .= Html::rawElement( 'td', [ 'class' => 'mw-version-ext-authors' ], $authors );
850
851 $html .= Html::closeElement( 'tr' );
852
853 return $html;
854 }
855
856 /**
857 * Generate wikitext showing hooks in $wgHooks.
858 *
859 * @return string Wikitext
860 */
861 private function getWgHooks() {
862 global $wgSpecialVersionShowHooks, $wgHooks;
863
864 if ( $wgSpecialVersionShowHooks && count( $wgHooks ) ) {
865 $myWgHooks = $wgHooks;
866 ksort( $myWgHooks );
867
868 $ret = [];
869 $ret[] = '== {{int:version-hooks}} ==';
870 $ret[] = Html::openElement( 'table', [ 'class' => 'wikitable', 'id' => 'sv-hooks' ] );
871 $ret[] = Html::openElement( 'tr' );
872 $ret[] = Html::element( 'th', [], $this->msg( 'version-hook-name' )->text() );
873 $ret[] = Html::element( 'th', [], $this->msg( 'version-hook-subscribedby' )->text() );
874 $ret[] = Html::closeElement( 'tr' );
875
876 foreach ( $myWgHooks as $hook => $hooks ) {
877 $ret[] = Html::openElement( 'tr' );
878 $ret[] = Html::element( 'td', [], $hook );
879 $ret[] = Html::element( 'td', [], $this->listToText( $hooks ) );
880 $ret[] = Html::closeElement( 'tr' );
881 }
882
883 $ret[] = Html::closeElement( 'table' );
884
885 return implode( "\n", $ret );
886 } else {
887 return '';
888 }
889 }
890
891 private function openExtType( $text = null, $name = null ) {
892 $out = '';
893
894 $opt = [ 'colspan' => 5 ];
895 if ( $this->firstExtOpened ) {
896 // Insert a spacing line
897 $out .= Html::rawElement( 'tr', [ 'class' => 'sv-space' ],
898 Html::element( 'td', $opt )
899 );
900 }
901 $this->firstExtOpened = true;
902
903 if ( $name ) {
904 $opt['id'] = "sv-$name";
905 }
906
907 if ( $text !== null ) {
908 $out .= Html::rawElement( 'tr', [],
909 Html::element( 'th', $opt, $text )
910 );
911 }
912
913 $firstHeadingMsg = ( $name === 'credits-skin' )
914 ? 'version-skin-colheader-name'
915 : 'version-ext-colheader-name';
916 $out .= Html::openElement( 'tr' );
917 $out .= Html::element( 'th', [ 'class' => 'mw-version-ext-col-label' ],
918 $this->msg( $firstHeadingMsg )->text() );
919 $out .= Html::element( 'th', [ 'class' => 'mw-version-ext-col-label' ],
920 $this->msg( 'version-ext-colheader-version' )->text() );
921 $out .= Html::element( 'th', [ 'class' => 'mw-version-ext-col-label' ],
922 $this->msg( 'version-ext-colheader-license' )->text() );
923 $out .= Html::element( 'th', [ 'class' => 'mw-version-ext-col-label' ],
924 $this->msg( 'version-ext-colheader-description' )->text() );
925 $out .= Html::element( 'th', [ 'class' => 'mw-version-ext-col-label' ],
926 $this->msg( 'version-ext-colheader-credits' )->text() );
927 $out .= Html::closeElement( 'tr' );
928
929 return $out;
930 }
931
932 /**
933 * Get information about client's IP address.
934 *
935 * @return string HTML fragment
936 */
937 private function IPInfo() {
938 $ip = str_replace( '--', ' - ', htmlspecialchars( $this->getRequest()->getIP() ) );
939
940 return "<!-- visited from $ip -->\n<span style='display:none'>visited from $ip</span>";
941 }
942
943 /**
944 * Return a formatted unsorted list of authors
945 *
946 * 'And Others'
947 * If an item in the $authors array is '...' it is assumed to indicate an
948 * 'and others' string which will then be linked to an ((AUTHORS)|(CREDITS))(\.txt)?
949 * file if it exists in $dir.
950 *
951 * Similarly an entry ending with ' ...]' is assumed to be a link to an
952 * 'and others' page.
953 *
954 * If no '...' string variant is found, but an authors file is found an
955 * 'and others' will be added to the end of the credits.
956 *
957 * @param string|array $authors
958 * @param string|bool $extName Name of the extension for link creation,
959 * false if no links should be created
960 * @param string $extDir Path to the extension root directory
961 *
962 * @return string HTML fragment
963 */
964 public function listAuthors( $authors, $extName, $extDir ) {
965 $hasOthers = false;
966 $linkRenderer = $this->getLinkRenderer();
967
968 $list = [];
969 foreach ( (array)$authors as $item ) {
970 if ( $item == '...' ) {
971 $hasOthers = true;
972
973 if ( $extName && $this->getExtAuthorsFileName( $extDir ) ) {
974 $text = $linkRenderer->makeLink(
975 $this->getPageTitle( "Credits/$extName" ),
976 $this->msg( 'version-poweredby-others' )->text()
977 );
978 } else {
979 $text = $this->msg( 'version-poweredby-others' )->escaped();
980 }
981 $list[] = $text;
982 } elseif ( substr( $item, -5 ) == ' ...]' ) {
983 $hasOthers = true;
984 $list[] = $this->getOutput()->parseInlineAsInterface(
985 substr( $item, 0, -4 ) . $this->msg( 'version-poweredby-others' )->text() . "]"
986 );
987 } else {
988 $list[] = $this->getOutput()->parseInlineAsInterface( $item );
989 }
990 }
991
992 if ( $extName && !$hasOthers && $this->getExtAuthorsFileName( $extDir ) ) {
993 $list[] = $text = $linkRenderer->makeLink(
994 $this->getPageTitle( "Credits/$extName" ),
995 $this->msg( 'version-poweredby-others' )->text()
996 );
997 }
998
999 return $this->listToText( $list, false );
1000 }
1001
1002 /**
1003 * Obtains the full path of an extensions authors or credits file if
1004 * one exists.
1005 *
1006 * @param string $extDir Path to the extensions root directory
1007 *
1008 * @since 1.23
1009 *
1010 * @return bool|string False if no such file exists, otherwise returns
1011 * a path to it.
1012 */
1013 public static function getExtAuthorsFileName( $extDir ) {
1014 if ( !$extDir ) {
1015 return false;
1016 }
1017
1018 foreach ( scandir( $extDir ) as $file ) {
1019 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1020 if ( preg_match( '/^((AUTHORS)|(CREDITS))(\.txt|\.wiki|\.mediawiki)?$/', $file ) &&
1021 is_readable( $fullPath ) &&
1022 is_file( $fullPath )
1023 ) {
1024 return $fullPath;
1025 }
1026 }
1027
1028 return false;
1029 }
1030
1031 /**
1032 * Obtains the full path of an extensions copying or license file if
1033 * one exists.
1034 *
1035 * @param string $extDir Path to the extensions root directory
1036 *
1037 * @since 1.23
1038 *
1039 * @return bool|string False if no such file exists, otherwise returns
1040 * a path to it.
1041 */
1042 public static function getExtLicenseFileName( $extDir ) {
1043 if ( !$extDir ) {
1044 return false;
1045 }
1046
1047 foreach ( scandir( $extDir ) as $file ) {
1048 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1049 if ( preg_match( '/^((COPYING)|(LICENSE))(\.txt)?$/', $file ) &&
1050 is_readable( $fullPath ) &&
1051 is_file( $fullPath )
1052 ) {
1053 return $fullPath;
1054 }
1055 }
1056
1057 return false;
1058 }
1059
1060 /**
1061 * Convert an array of items into a list for display.
1062 *
1063 * @param array $list List of elements to display
1064 * @param bool $sort Whether to sort the items in $list
1065 *
1066 * @return string
1067 */
1068 public function listToText( $list, $sort = true ) {
1069 if ( !count( $list ) ) {
1070 return '';
1071 }
1072 if ( $sort ) {
1073 sort( $list );
1074 }
1075
1076 return $this->getLanguage()
1077 ->listToText( array_map( [ __CLASS__, 'arrayToString' ], $list ) );
1078 }
1079
1080 /**
1081 * Convert an array or object to a string for display.
1082 *
1083 * @param mixed $list Will convert an array to string if given and return
1084 * the parameter unaltered otherwise
1085 *
1086 * @return mixed
1087 */
1088 public static function arrayToString( $list ) {
1089 if ( is_array( $list ) && count( $list ) == 1 ) {
1090 $list = $list[0];
1091 }
1092 if ( $list instanceof Closure ) {
1093 // Don't output stuff like "Closure$;1028376090#8$48499d94fe0147f7c633b365be39952b$"
1094 return 'Closure';
1095 } elseif ( is_object( $list ) ) {
1096 $class = wfMessage( 'parentheses' )->params( get_class( $list ) )->escaped();
1097
1098 return $class;
1099 } elseif ( !is_array( $list ) ) {
1100 return $list;
1101 } else {
1102 if ( is_object( $list[0] ) ) {
1103 $class = get_class( $list[0] );
1104 } else {
1105 $class = $list[0];
1106 }
1107
1108 return wfMessage( 'parentheses' )->params( "$class, {$list[1]}" )->escaped();
1109 }
1110 }
1111
1112 /**
1113 * @param string $dir Directory of the git checkout
1114 * @return bool|string Sha1 of commit HEAD points to
1115 */
1116 public static function getGitHeadSha1( $dir ) {
1117 $repo = new GitInfo( $dir );
1118
1119 return $repo->getHeadSHA1();
1120 }
1121
1122 /**
1123 * @param string $dir Directory of the git checkout
1124 * @return bool|string Branch currently checked out
1125 */
1126 public static function getGitCurrentBranch( $dir ) {
1127 $repo = new GitInfo( $dir );
1128 return $repo->getCurrentBranch();
1129 }
1130
1131 /**
1132 * Get the list of entry points and their URLs
1133 * @return string Wikitext
1134 */
1135 public function getEntryPointInfo() {
1136 global $wgArticlePath, $wgScriptPath;
1137 $scriptPath = $wgScriptPath ?: "/";
1138 $entryPoints = [
1139 'version-entrypoints-articlepath' => $wgArticlePath,
1140 'version-entrypoints-scriptpath' => $scriptPath,
1141 'version-entrypoints-index-php' => wfScript( 'index' ),
1142 'version-entrypoints-api-php' => wfScript( 'api' ),
1143 'version-entrypoints-load-php' => wfScript( 'load' ),
1144 ];
1145
1146 $language = $this->getLanguage();
1147 $thAttribures = [
1148 'dir' => $language->getDir(),
1149 'lang' => $language->getHtmlCode()
1150 ];
1151 $out = Html::element(
1152 'h2',
1153 [ 'id' => 'mw-version-entrypoints' ],
1154 $this->msg( 'version-entrypoints' )->text()
1155 ) .
1156 Html::openElement( 'table',
1157 [
1158 'class' => 'wikitable plainlinks',
1159 'id' => 'mw-version-entrypoints-table',
1160 'dir' => 'ltr',
1161 'lang' => 'en'
1162 ]
1163 ) .
1164 Html::openElement( 'tr' ) .
1165 Html::element(
1166 'th',
1167 $thAttribures,
1168 $this->msg( 'version-entrypoints-header-entrypoint' )->text()
1169 ) .
1170 Html::element(
1171 'th',
1172 $thAttribures,
1173 $this->msg( 'version-entrypoints-header-url' )->text()
1174 ) .
1175 Html::closeElement( 'tr' );
1176
1177 foreach ( $entryPoints as $message => $value ) {
1178 $url = wfExpandUrl( $value, PROTO_RELATIVE );
1179 $out .= Html::openElement( 'tr' ) .
1180 // ->plain() looks like it should be ->parse(), but this function
1181 // returns wikitext, not HTML, boo
1182 Html::rawElement( 'td', [], $this->msg( $message )->plain() ) .
1183 Html::rawElement( 'td', [], Html::rawElement( 'code', [], "[$url $value]" ) ) .
1184 Html::closeElement( 'tr' );
1185 }
1186
1187 $out .= Html::closeElement( 'table' );
1188
1189 return $out;
1190 }
1191
1192 protected function getGroupName() {
1193 return 'wiki';
1194 }
1195 }