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