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