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