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