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