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