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