Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialVersion.php
bloba7365bb2e5834c526f6d9aed95bfb2160c6f0f27
1 <?php
2 /**
3 * Implements Special:Version
5 * Copyright © 2005 Ævar Arnfjörð Bjarmason
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.
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.
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
22 * @file
23 * @ingroup SpecialPage
26 /**
27 * Give information about the version of MediaWiki, PHP, the DB and extensions
29 * @ingroup SpecialPage
31 class SpecialVersion extends SpecialPage {
32 protected $firstExtOpened = false;
34 /**
35 * Stores the current rev id/SHA hash of MediaWiki core
37 protected $coreId = '';
39 protected static $extensionTypes = false;
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',
47 public function __construct() {
48 parent::__construct( 'Version' );
51 /**
52 * main()
53 * @param string|null $par
55 public function execute( $par ) {
56 global $IP, $wgExtensionCredits;
58 $this->setHeaders();
59 $this->outputHeader();
60 $out = $this->getOutput();
61 $out->allowClickjacking();
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;
77 if ( !$extNode ) {
78 $out->setStatusCode( 404 );
80 } else {
81 $extName = 'MediaWiki';
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 );
100 $out->setPageTitle( $this->msg( 'version-credits-title', $extName ) );
101 $out->addWikiText( $wikiText );
102 break;
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 $wikiText = "<pre>$wikiText</pre>";
116 $out->setPageTitle( $this->msg( 'version-license-title', $extName ) );
117 $out->addWikiText( $wikiText );
118 break;
120 default:
121 $out->addModules( 'mediawiki.special.version' );
122 $out->addWikiText(
123 $this->getMediaWikiCredits() .
124 $this->softwareInformation() .
125 $this->getEntryPointInfo()
127 $out->addHtml(
128 $this->getSkinCredits() .
129 $this->getExtensionCredits() .
130 $this->getExternalLibraries() .
131 $this->getParserTags() .
132 $this->getParserFunctionHooks()
134 $out->addWikiText( $this->getWgHooks() );
135 $out->addHTML( $this->IPInfo() );
137 break;
142 * Returns wiki text showing the license information.
144 * @return string
146 private static function getMediaWikiCredits() {
147 $ret = Xml::element(
148 'h2',
149 array( 'id' => 'mw-version-license' ),
150 wfMessage( 'version-license' )->text()
153 // This text is always left-to-right.
154 $ret .= '<div class="plainlinks">';
155 $ret .= "__NOTOC__
156 " . self::getCopyrightAndAuthorList() . "\n
157 " . wfMessage( 'version-license-info' )->text();
158 $ret .= '</div>';
160 return str_replace( "\t\t", '', $ret ) . "\n";
164 * Get the "MediaWiki is copyright 2001-20xx by lots of cool guys" text
166 * @return string
168 public static function getCopyrightAndAuthorList() {
169 global $wgLang;
171 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
172 $othersLink = '[//www.mediawiki.org/wiki/Special:Version/Credits ' .
173 wfMessage( 'version-poweredby-others' )->text() . ']';
174 } else {
175 $othersLink = '[[Special:Version/Credits|' .
176 wfMessage( 'version-poweredby-others' )->text() . ']]';
179 $translatorsLink = '[//translatewiki.net/wiki/Translating:MediaWiki/Credits ' .
180 wfMessage( 'version-poweredby-translators' )->text() . ']';
182 $authorList = array(
183 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
184 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
185 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
186 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
187 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
188 'Roan Kattouw', 'Trevor Parscal', 'Bryan Tong Minh', 'Sam Reed',
189 'Victor Vasiliev', 'Rotem Liss', 'Platonides', 'Antoine Musso',
190 'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', 'Brad Jorsch',
191 $othersLink, $translatorsLink
194 return wfMessage( 'version-poweredby-credits', MWTimestamp::getLocalInstance()->format( 'Y' ),
195 $wgLang->listToText( $authorList ) )->text();
199 * Returns wiki text showing the third party software versions (apache, php, mysql).
201 * @return string
203 public static function softwareInformation() {
204 $dbr = wfGetDB( DB_SLAVE );
206 // Put the software in an array of form 'name' => 'version'. All messages should
207 // be loaded here, so feel free to use wfMessage in the 'name'. Raw HTML or
208 // wikimarkup can be used.
209 $software = array();
210 $software['[https://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
211 if ( wfIsHHVM() ) {
212 $software['[http://hhvm.com/ HHVM]'] = HHVM_VERSION . " (" . PHP_SAPI . ")";
213 } else {
214 $software['[https://php.net/ PHP]'] = PHP_VERSION . " (" . PHP_SAPI . ")";
216 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
218 // Allow a hook to add/remove items.
219 Hooks::run( 'SoftwareInfo', array( &$software ) );
221 $out = Xml::element(
222 'h2',
223 array( 'id' => 'mw-version-software' ),
224 wfMessage( 'version-software' )->text()
226 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-software' ) ) .
227 "<tr>
228 <th>" . wfMessage( 'version-software-product' )->text() . "</th>
229 <th>" . wfMessage( 'version-software-version' )->text() . "</th>
230 </tr>\n";
232 foreach ( $software as $name => $version ) {
233 $out .= "<tr>
234 <td>" . $name . "</td>
235 <td dir=\"ltr\">" . $version . "</td>
236 </tr>\n";
239 return $out . Xml::closeElement( 'table' );
243 * Return a string of the MediaWiki version with SVN revision if available.
245 * @param string $flags
246 * @return mixed
248 public static function getVersion( $flags = '' ) {
249 global $wgVersion, $IP;
251 $gitInfo = self::getGitHeadSha1( $IP );
252 $svnInfo = self::getSvnInfo( $IP );
253 if ( !$svnInfo && !$gitInfo ) {
254 $version = $wgVersion;
255 } elseif ( $gitInfo && $flags === 'nodb' ) {
256 $shortSha1 = substr( $gitInfo, 0, 7 );
257 $version = "$wgVersion ($shortSha1)";
258 } elseif ( $gitInfo ) {
259 $shortSha1 = substr( $gitInfo, 0, 7 );
260 $shortSha1 = wfMessage( 'parentheses' )->params( $shortSha1 )->escaped();
261 $version = "$wgVersion $shortSha1";
262 } elseif ( $flags === 'nodb' ) {
263 $version = "$wgVersion (r{$svnInfo['checkout-rev']})";
264 } else {
265 $version = $wgVersion . ' ' .
266 wfMessage(
267 'version-svn-revision',
268 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
269 $info['checkout-rev']
270 )->text();
273 return $version;
277 * Return a wikitext-formatted string of the MediaWiki version with a link to
278 * the SVN revision or the git SHA1 of head if available.
279 * Git is prefered over Svn
280 * The fallback is just $wgVersion
282 * @return mixed
284 public static function getVersionLinked() {
285 global $wgVersion;
287 $gitVersion = self::getVersionLinkedGit();
288 if ( $gitVersion ) {
289 $v = $gitVersion;
290 } else {
291 $svnVersion = self::getVersionLinkedSvn();
292 if ( $svnVersion ) {
293 $v = $svnVersion;
294 } else {
295 $v = $wgVersion; // fallback
299 return $v;
303 * @return string Global wgVersion + a link to subversion revision of svn BASE
305 private static function getVersionLinkedSvn() {
306 global $IP;
308 $info = self::getSvnInfo( $IP );
309 if ( !isset( $info['checkout-rev'] ) ) {
310 return false;
313 $linkText = wfMessage(
314 'version-svn-revision',
315 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
316 $info['checkout-rev']
317 )->text();
319 if ( isset( $info['viewvc-url'] ) ) {
320 $version = "[{$info['viewvc-url']} $linkText]";
321 } else {
322 $version = $linkText;
325 return self::getwgVersionLinked() . " $version";
329 * @return string
331 private static function getwgVersionLinked() {
332 global $wgVersion;
333 $versionUrl = "";
334 if ( Hooks::run( 'SpecialVersionVersionUrl', array( $wgVersion, &$versionUrl ) ) ) {
335 $versionParts = array();
336 preg_match( "/^(\d+\.\d+)/", $wgVersion, $versionParts );
337 $versionUrl = "https://www.mediawiki.org/wiki/MediaWiki_{$versionParts[1]}";
340 return "[$versionUrl $wgVersion]";
344 * @since 1.22 Returns the HEAD date in addition to the sha1 and link
345 * @return bool|string Global wgVersion + HEAD sha1 stripped to the first 7 chars
346 * with link and date, or false on failure
348 private static function getVersionLinkedGit() {
349 global $IP, $wgLang;
351 $gitInfo = new GitInfo( $IP );
352 $headSHA1 = $gitInfo->getHeadSHA1();
353 if ( !$headSHA1 ) {
354 return false;
357 $shortSHA1 = '(' . substr( $headSHA1, 0, 7 ) . ')';
359 $gitHeadUrl = $gitInfo->getHeadViewUrl();
360 if ( $gitHeadUrl !== false ) {
361 $shortSHA1 = "[$gitHeadUrl $shortSHA1]";
364 $gitHeadCommitDate = $gitInfo->getHeadCommitDate();
365 if ( $gitHeadCommitDate ) {
366 $shortSHA1 .= Html::element( 'br' ) . $wgLang->timeanddate( $gitHeadCommitDate, true );
369 return self::getwgVersionLinked() . " $shortSHA1";
373 * Returns an array with the base extension types.
374 * Type is stored as array key, the message as array value.
376 * TODO: ideally this would return all extension types.
378 * @since 1.17
380 * @return array
382 public static function getExtensionTypes() {
383 if ( self::$extensionTypes === false ) {
384 self::$extensionTypes = array(
385 'specialpage' => wfMessage( 'version-specialpages' )->text(),
386 'parserhook' => wfMessage( 'version-parserhooks' )->text(),
387 'variable' => wfMessage( 'version-variables' )->text(),
388 'media' => wfMessage( 'version-mediahandlers' )->text(),
389 'antispam' => wfMessage( 'version-antispam' )->text(),
390 'skin' => wfMessage( 'version-skins' )->text(),
391 'api' => wfMessage( 'version-api' )->text(),
392 'other' => wfMessage( 'version-other' )->text(),
395 Hooks::run( 'ExtensionTypes', array( &self::$extensionTypes ) );
398 return self::$extensionTypes;
402 * Returns the internationalized name for an extension type.
404 * @since 1.17
406 * @param string $type
408 * @return string
410 public static function getExtensionTypeName( $type ) {
411 $types = self::getExtensionTypes();
413 return isset( $types[$type] ) ? $types[$type] : $types['other'];
417 * Generate wikitext showing the name, URL, author and description of each extension.
419 * @return string Wikitext
421 public function getExtensionCredits() {
422 global $wgExtensionCredits;
424 if (
425 count( $wgExtensionCredits ) === 0 ||
426 // Skins are displayed separately, see getSkinCredits()
427 ( count( $wgExtensionCredits ) === 1 && isset( $wgExtensionCredits['skin'] ) )
429 return '';
432 $extensionTypes = self::getExtensionTypes();
434 $out = Xml::element(
435 'h2',
436 array( 'id' => 'mw-version-ext' ),
437 $this->msg( 'version-extensions' )->text()
439 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-ext' ) );
441 // Make sure the 'other' type is set to an array.
442 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
443 $wgExtensionCredits['other'] = array();
446 // Find all extensions that do not have a valid type and give them the type 'other'.
447 foreach ( $wgExtensionCredits as $type => $extensions ) {
448 if ( !array_key_exists( $type, $extensionTypes ) ) {
449 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
453 $this->firstExtOpened = false;
454 // Loop through the extension categories to display their extensions in the list.
455 foreach ( $extensionTypes as $type => $message ) {
456 // Skins have a separate section
457 if ( $type !== 'other' && $type !== 'skin' ) {
458 $out .= $this->getExtensionCategory( $type, $message );
462 // We want the 'other' type to be last in the list.
463 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
465 $out .= Xml::closeElement( 'table' );
467 return $out;
471 * Generate wikitext showing the name, URL, author and description of each skin.
473 * @return string Wikitext
475 public function getSkinCredits() {
476 global $wgExtensionCredits;
477 if ( !isset( $wgExtensionCredits['skin'] ) || count( $wgExtensionCredits['skin'] ) === 0 ) {
478 return '';
481 $out = Xml::element(
482 'h2',
483 array( 'id' => 'mw-version-skin' ),
484 $this->msg( 'version-skins' )->text()
486 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-skin' ) );
488 $this->firstExtOpened = false;
489 $out .= $this->getExtensionCategory( 'skin', null );
491 $out .= Xml::closeElement( 'table' );
493 return $out;
497 * Generate an HTML table for external libraries that are installed
499 * @return string
501 protected function getExternalLibraries() {
502 global $IP;
503 $path = "$IP/composer.lock";
504 if ( !file_exists( $path ) ) {
505 // Maybe they're using mediawiki/vendor?
506 $path = "$IP/vendor/composer.lock";
507 if ( !file_exists( $path ) ) {
508 return '';
512 $lock = new ComposerLock( $path );
513 $out = Html::element(
514 'h2',
515 array( 'id' => 'mw-version-libraries' ),
516 $this->msg( 'version-libraries' )->text()
518 $out .= Html::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-libraries' ) );
519 $out .= Html::openElement( 'tr' )
520 . Html::element( 'th', array(), $this->msg( 'version-libraries-library' )->text() )
521 . Html::element( 'th', array(), $this->msg( 'version-libraries-version' )->text() )
522 . Html::closeElement( 'tr' );
524 foreach ( $lock->getInstalledDependencies() as $name => $info ) {
525 if ( strpos( $info['type'], 'mediawiki-' ) === 0 ) {
526 // Skip any extensions or skins since they'll be listed
527 // in their proper section
528 continue;
530 $out .= Html::openElement( 'tr' )
531 . Html::rawElement( 'td', array(), Linker::makeExternalLink( "https://packagist.org/packages/$name", $name ) )
532 . Html::element( 'td', array(), $info['version'] )
533 . Html::closeElement( 'tr' );
535 $out .= Html::closeElement( 'table' );
537 return $out;
541 * Obtains a list of installed parser tags and the associated H2 header
543 * @return string HTML output
545 protected function getParserTags() {
546 global $wgParser;
548 $tags = $wgParser->getTags();
550 if ( count( $tags ) ) {
551 $out = Html::rawElement(
552 'h2',
553 array( 'class' => 'mw-headline plainlinks' ),
554 Linker::makeExternalLink(
555 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Tag_extensions',
556 $this->msg( 'version-parser-extensiontags' )->parse(),
557 false /* msg()->parse() already escapes */
561 array_walk( $tags, function ( &$value ) {
562 // Bidirectional isolation improves readability in RTL wikis
563 $value = Html::element(
564 'bdi',
565 // Prevent < and > from slipping to another line
566 array(
567 'style' => 'white-space: nowrap;',
569 "<$value>"
571 } );
573 $out .= $this->listToText( $tags );
574 } else {
575 $out = '';
578 return $out;
582 * Obtains a list of installed parser function hooks and the associated H2 header
584 * @return string HTML output
586 protected function getParserFunctionHooks() {
587 global $wgParser;
589 $fhooks = $wgParser->getFunctionHooks();
590 if ( count( $fhooks ) ) {
591 $out = Html::rawElement( 'h2', array( 'class' => 'mw-headline plainlinks' ), Linker::makeExternalLink(
592 '//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Parser_functions',
593 $this->msg( 'version-parser-function-hooks' )->parse(),
594 false /* msg()->parse() already escapes */
595 ) );
597 $out .= $this->listToText( $fhooks );
598 } else {
599 $out = '';
602 return $out;
606 * Creates and returns the HTML for a single extension category.
608 * @since 1.17
610 * @param string $type
611 * @param string $message
613 * @return string
615 protected function getExtensionCategory( $type, $message ) {
616 global $wgExtensionCredits;
618 $out = '';
620 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
621 $out .= $this->openExtType( $message, 'credits-' . $type );
623 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
625 foreach ( $wgExtensionCredits[$type] as $extension ) {
626 $out .= $this->getCreditsForExtension( $extension );
630 return $out;
634 * Callback to sort extensions by type.
635 * @param array $a
636 * @param array $b
637 * @return int
639 public function compare( $a, $b ) {
640 if ( $a['name'] === $b['name'] ) {
641 return 0;
642 } else {
643 return $this->getLanguage()->lc( $a['name'] ) > $this->getLanguage()->lc( $b['name'] )
645 : -1;
650 * Creates and formats a version line for a single extension.
652 * Information for five columns will be created. Parameters required in the
653 * $extension array for part rendering are indicated in ()
654 * - The name of (name), and URL link to (url), the extension
655 * - Official version number (version) and if available version control system
656 * revision (path), link, and date
657 * - If available the short name of the license (license-name) and a linke
658 * to ((LICENSE)|(COPYING))(\.txt)? if it exists.
659 * - Description of extension (descriptionmsg or description)
660 * - List of authors (author) and link to a ((AUTHORS)|(CREDITS))(\.txt)? file if it exists
662 * @param array $extension
664 * @return string Raw HTML
666 public function getCreditsForExtension( array $extension ) {
667 $out = $this->getOutput();
669 // We must obtain the information for all the bits and pieces!
670 // ... such as extension names and links
671 if ( isset( $extension['namemsg'] ) ) {
672 // Localized name of extension
673 $extensionName = $this->msg( $extension['namemsg'] )->text();
674 } elseif ( isset( $extension['name'] ) ) {
675 // Non localized version
676 $extensionName = $extension['name'];
677 } else {
678 $extensionName = $this->msg( 'version-no-ext-name' )->text();
681 if ( isset( $extension['url'] ) ) {
682 $extensionNameLink = Linker::makeExternalLink(
683 $extension['url'],
684 $extensionName,
685 true,
687 array( 'class' => 'mw-version-ext-name' )
689 } else {
690 $extensionNameLink = $extensionName;
693 // ... and the version information
694 // If the extension path is set we will check that directory for GIT and SVN
695 // metadata in an attempt to extract date and vcs commit metadata.
696 $canonicalVersion = '&ndash;';
697 $extensionPath = null;
698 $vcsVersion = null;
699 $vcsLink = null;
700 $vcsDate = null;
702 if ( isset( $extension['version'] ) ) {
703 $canonicalVersion = $out->parseInline( $extension['version'] );
706 if ( isset( $extension['path'] ) ) {
707 global $IP;
708 $extensionPath = dirname( $extension['path'] );
709 if ( $this->coreId == '' ) {
710 wfDebug( 'Looking up core head id' );
711 $coreHeadSHA1 = self::getGitHeadSha1( $IP );
712 if ( $coreHeadSHA1 ) {
713 $this->coreId = $coreHeadSHA1;
714 } else {
715 $svnInfo = self::getSvnInfo( $IP );
716 if ( $svnInfo !== false ) {
717 $this->coreId = $svnInfo['checkout-rev'];
721 $cache = wfGetCache( CACHE_ANYTHING );
722 $memcKey = wfMemcKey( 'specialversion-ext-version-text', $extension['path'], $this->coreId );
723 list( $vcsVersion, $vcsLink, $vcsDate ) = $cache->get( $memcKey );
725 if ( !$vcsVersion ) {
726 wfDebug( "Getting VCS info for extension {$extension['name']}" );
727 $gitInfo = new GitInfo( $extensionPath );
728 $vcsVersion = $gitInfo->getHeadSHA1();
729 if ( $vcsVersion !== false ) {
730 $vcsVersion = substr( $vcsVersion, 0, 7 );
731 $vcsLink = $gitInfo->getHeadViewUrl();
732 $vcsDate = $gitInfo->getHeadCommitDate();
733 } else {
734 $svnInfo = self::getSvnInfo( $extensionPath );
735 if ( $svnInfo !== false ) {
736 $vcsVersion = $this->msg( 'version-svn-revision', $svnInfo['checkout-rev'] )->text();
737 $vcsLink = isset( $svnInfo['viewvc-url'] ) ? $svnInfo['viewvc-url'] : '';
740 $cache->set( $memcKey, array( $vcsVersion, $vcsLink, $vcsDate ), 60 * 60 * 24 );
741 } else {
742 wfDebug( "Pulled VCS info for extension {$extension['name']} from cache" );
746 $versionString = Html::rawElement(
747 'span',
748 array( 'class' => 'mw-version-ext-version' ),
749 $canonicalVersion
752 if ( $vcsVersion ) {
753 if ( $vcsLink ) {
754 $vcsVerString = Linker::makeExternalLink(
755 $vcsLink,
756 $this->msg( 'version-version', $vcsVersion ),
757 true,
759 array( 'class' => 'mw-version-ext-vcs-version' )
761 } else {
762 $vcsVerString = Html::element( 'span',
763 array( 'class' => 'mw-version-ext-vcs-version' ),
764 "({$vcsVersion})"
767 $versionString .= " {$vcsVerString}";
769 if ( $vcsDate ) {
770 $vcsTimeString = Html::element( 'span',
771 array( 'class' => 'mw-version-ext-vcs-timestamp' ),
772 $this->getLanguage()->timeanddate( $vcsDate, true )
774 $versionString .= " {$vcsTimeString}";
776 $versionString = Html::rawElement( 'span',
777 array( 'class' => 'mw-version-ext-meta-version' ),
778 $versionString
782 // ... and license information; if a license file exists we
783 // will link to it
784 $licenseLink = '';
785 if ( isset( $extension['name'] ) ) {
786 $licenseName = null;
787 if ( isset( $extension['license-name'] ) ) {
788 $licenseName = $out->parseInline( $extension['license-name'] );
789 } elseif ( $this->getExtLicenseFileName( $extensionPath ) ) {
790 $licenseName = $this->msg( 'version-ext-license' );
792 if ( $licenseName !== null ) {
793 $licenseLink = Linker::link(
794 $this->getPageTitle( 'License/' . $extension['name'] ),
795 $licenseName,
796 array(
797 'class' => 'mw-version-ext-license',
798 'dir' => 'auto',
804 // ... and generate the description; which can be a parameterized l10n message
805 // in the form array( <msgname>, <parameter>, <parameter>... ) or just a straight
806 // up string
807 if ( isset( $extension['descriptionmsg'] ) ) {
808 // Localized description of extension
809 $descriptionMsg = $extension['descriptionmsg'];
811 if ( is_array( $descriptionMsg ) ) {
812 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
813 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
814 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
815 $description = $this->msg( $descriptionMsgKey, $descriptionMsg )->text();
816 } else {
817 $description = $this->msg( $descriptionMsg )->text();
819 } elseif ( isset( $extension['description'] ) ) {
820 // Non localized version
821 $description = $extension['description'];
822 } else {
823 $description = '';
825 $description = $out->parseInline( $description );
827 // ... now get the authors for this extension
828 $authors = isset( $extension['author'] ) ? $extension['author'] : array();
829 $authors = $this->listAuthors( $authors, $extension['name'], $extensionPath );
831 // Finally! Create the table
832 $html = Html::openElement( 'tr', array(
833 'class' => 'mw-version-ext',
834 'id' => "mw-version-ext-{$extension['name']}"
838 $html .= Html::rawElement( 'td', array(), $extensionNameLink );
839 $html .= Html::rawElement( 'td', array(), $versionString );
840 $html .= Html::rawElement( 'td', array(), $licenseLink );
841 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-description' ), $description );
842 $html .= Html::rawElement( 'td', array( 'class' => 'mw-version-ext-authors' ), $authors );
844 $html .= Html::closeElement( 'tr' );
846 return $html;
850 * Generate wikitext showing hooks in $wgHooks.
852 * @return string Wikitext
854 private function getWgHooks() {
855 global $wgSpecialVersionShowHooks, $wgHooks;
857 if ( $wgSpecialVersionShowHooks && count( $wgHooks ) ) {
858 $myWgHooks = $wgHooks;
859 ksort( $myWgHooks );
861 $ret = array();
862 $ret[] = '== {{int:version-hooks}} ==';
863 $ret[] = Html::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) );
864 $ret[] = Html::openElement( 'tr' );
865 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-name' )->text() );
866 $ret[] = Html::element( 'th', array(), $this->msg( 'version-hook-subscribedby' )->text() );
867 $ret[] = Html::closeElement( 'tr' );
869 foreach ( $myWgHooks as $hook => $hooks ) {
870 $ret[] = Html::openElement( 'tr' );
871 $ret[] = Html::element( 'td', array(), $hook );
872 $ret[] = Html::element( 'td', array(), $this->listToText( $hooks ) );
873 $ret[] = Html::closeElement( 'tr' );
876 $ret[] = Html::closeElement( 'table' );
878 return implode( "\n", $ret );
879 } else {
880 return '';
884 private function openExtType( $text = null, $name = null ) {
885 $out = '';
887 $opt = array( 'colspan' => 5 );
888 if ( $this->firstExtOpened ) {
889 // Insert a spacing line
890 $out .= Html::rawElement( 'tr', array( 'class' => 'sv-space' ),
891 Html::element( 'td', $opt )
894 $this->firstExtOpened = true;
896 if ( $name ) {
897 $opt['id'] = "sv-$name";
900 if ( $text !== null ) {
901 $out .= Html::rawElement( 'tr', array(),
902 Html::element( 'th', $opt, $text )
906 $firstHeadingMsg = ( $name === 'credits-skin' )
907 ? 'version-skin-colheader-name'
908 : 'version-ext-colheader-name';
909 $out .= Html::openElement( 'tr' );
910 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
911 $this->msg( $firstHeadingMsg )->text() );
912 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
913 $this->msg( 'version-ext-colheader-version' )->text() );
914 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
915 $this->msg( 'version-ext-colheader-license' )->text() );
916 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
917 $this->msg( 'version-ext-colheader-description' )->text() );
918 $out .= Html::element( 'th', array( 'class' => 'mw-version-ext-col-label' ),
919 $this->msg( 'version-ext-colheader-credits' )->text() );
920 $out .= Html::closeElement( 'tr' );
922 return $out;
926 * Get information about client's IP address.
928 * @return string HTML fragment
930 private function IPInfo() {
931 $ip = str_replace( '--', ' - ', htmlspecialchars( $this->getRequest()->getIP() ) );
933 return "<!-- visited from $ip -->\n<span style='display:none'>visited from $ip</span>";
937 * Return a formatted unsorted list of authors
939 * 'And Others'
940 * If an item in the $authors array is '...' it is assumed to indicate an
941 * 'and others' string which will then be linked to an ((AUTHORS)|(CREDITS))(\.txt)?
942 * file if it exists in $dir.
944 * Similarly an entry ending with ' ...]' is assumed to be a link to an
945 * 'and others' page.
947 * If no '...' string variant is found, but an authors file is found an
948 * 'and others' will be added to the end of the credits.
950 * @param string|array $authors
951 * @param string $extName Name of the extension for link creation
952 * @param string $extDir Path to the extension root directory
954 * @return string HTML fragment
956 public function listAuthors( $authors, $extName, $extDir ) {
957 $hasOthers = false;
959 $list = array();
960 foreach ( (array)$authors as $item ) {
961 if ( $item == '...' ) {
962 $hasOthers = true;
964 if ( $this->getExtAuthorsFileName( $extDir ) ) {
965 $text = Linker::link(
966 $this->getPageTitle( "Credits/$extName" ),
967 $this->msg( 'version-poweredby-others' )->escaped()
969 } else {
970 $text = $this->msg( 'version-poweredby-others' )->escaped();
972 $list[] = $text;
973 } elseif ( substr( $item, -5 ) == ' ...]' ) {
974 $hasOthers = true;
975 $list[] = $this->getOutput()->parseInline(
976 substr( $item, 0, -4 ) . $this->msg( 'version-poweredby-others' )->text() . "]"
978 } else {
979 $list[] = $this->getOutput()->parseInline( $item );
983 if ( !$hasOthers && $this->getExtAuthorsFileName( $extDir ) ) {
984 $list[] = $text = Linker::link(
985 $this->getPageTitle( "Credits/$extName" ),
986 $this->msg( 'version-poweredby-others' )->escaped()
990 return $this->listToText( $list, false );
994 * Obtains the full path of an extensions authors or credits file if
995 * one exists.
997 * @param string $extDir Path to the extensions root directory
999 * @since 1.23
1001 * @return bool|string False if no such file exists, otherwise returns
1002 * a path to it.
1004 public static function getExtAuthorsFileName( $extDir ) {
1005 if ( !$extDir ) {
1006 return false;
1009 foreach ( scandir( $extDir ) as $file ) {
1010 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1011 if ( preg_match( '/^((AUTHORS)|(CREDITS))(\.txt)?$/', $file ) &&
1012 is_readable( $fullPath ) &&
1013 is_file( $fullPath )
1015 return $fullPath;
1019 return false;
1023 * Obtains the full path of an extensions copying or license file if
1024 * one exists.
1026 * @param string $extDir Path to the extensions root directory
1028 * @since 1.23
1030 * @return bool|string False if no such file exists, otherwise returns
1031 * a path to it.
1033 public static function getExtLicenseFileName( $extDir ) {
1034 if ( !$extDir ) {
1035 return false;
1038 foreach ( scandir( $extDir ) as $file ) {
1039 $fullPath = $extDir . DIRECTORY_SEPARATOR . $file;
1040 if ( preg_match( '/^((COPYING)|(LICENSE))(\.txt)?$/', $file ) &&
1041 is_readable( $fullPath ) &&
1042 is_file( $fullPath )
1044 return $fullPath;
1048 return false;
1052 * Convert an array of items into a list for display.
1054 * @param array $list List of elements to display
1055 * @param bool $sort Whether to sort the items in $list
1057 * @return string
1059 public function listToText( $list, $sort = true ) {
1060 if ( !count( $list ) ) {
1061 return '';
1063 if ( $sort ) {
1064 sort( $list );
1067 return $this->getLanguage()
1068 ->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
1072 * Convert an array or object to a string for display.
1074 * @param mixed $list Will convert an array to string if given and return
1075 * the parameter unaltered otherwise
1077 * @return mixed
1079 public static function arrayToString( $list ) {
1080 if ( is_array( $list ) && count( $list ) == 1 ) {
1081 $list = $list[0];
1083 if ( is_object( $list ) ) {
1084 $class = wfMessage( 'parentheses' )->params( get_class( $list ) )->escaped();
1086 return $class;
1087 } elseif ( !is_array( $list ) ) {
1088 return $list;
1089 } else {
1090 if ( is_object( $list[0] ) ) {
1091 $class = get_class( $list[0] );
1092 } else {
1093 $class = $list[0];
1096 return wfMessage( 'parentheses' )->params( "$class, {$list[1]}" )->escaped();
1101 * Get an associative array of information about a given path, from its .svn
1102 * subdirectory. Returns false on error, such as if the directory was not
1103 * checked out with subversion.
1105 * Returned keys are:
1106 * Required:
1107 * checkout-rev The revision which was checked out
1108 * Optional:
1109 * directory-rev The revision when the directory was last modified
1110 * url The subversion URL of the directory
1111 * repo-url The base URL of the repository
1112 * viewvc-url A ViewVC URL pointing to the checked-out revision
1113 * @param string $dir
1114 * @return array|bool
1116 public static function getSvnInfo( $dir ) {
1117 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
1118 $entries = $dir . '/.svn/entries';
1120 if ( !file_exists( $entries ) ) {
1121 return false;
1124 $lines = file( $entries );
1125 if ( !count( $lines ) ) {
1126 return false;
1129 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
1130 if ( preg_match( '/^<\?xml/', $lines[0] ) ) {
1131 // subversion is release <= 1.3
1132 if ( !function_exists( 'simplexml_load_file' ) ) {
1133 // We could fall back to expat... YUCK
1134 return false;
1137 // SimpleXml whines about the xmlns...
1138 wfSuppressWarnings();
1139 $xml = simplexml_load_file( $entries );
1140 wfRestoreWarnings();
1142 if ( $xml ) {
1143 foreach ( $xml->entry as $entry ) {
1144 if ( $xml->entry[0]['name'] == '' ) {
1145 // The directory entry should always have a revision marker.
1146 if ( $entry['revision'] ) {
1147 return array( 'checkout-rev' => intval( $entry['revision'] ) );
1153 return false;
1156 // Subversion is release 1.4 or above.
1157 if ( count( $lines ) < 11 ) {
1158 return false;
1161 $info = array(
1162 'checkout-rev' => intval( trim( $lines[3] ) ),
1163 'url' => trim( $lines[4] ),
1164 'repo-url' => trim( $lines[5] ),
1165 'directory-rev' => intval( trim( $lines[10] ) )
1168 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
1169 $viewvc = str_replace(
1170 $info['repo-url'],
1171 self::$viewvcUrls[$info['repo-url']],
1172 $info['url']
1175 $viewvc .= '/?pathrev=';
1176 $viewvc .= urlencode( $info['checkout-rev'] );
1177 $info['viewvc-url'] = $viewvc;
1180 return $info;
1184 * Retrieve the revision number of a Subversion working directory.
1186 * @param string $dir Directory of the svn checkout
1188 * @return int Revision number
1190 public static function getSvnRevision( $dir ) {
1191 $info = self::getSvnInfo( $dir );
1193 if ( $info === false ) {
1194 return false;
1195 } elseif ( isset( $info['checkout-rev'] ) ) {
1196 return $info['checkout-rev'];
1197 } else {
1198 return false;
1203 * @param string $dir Directory of the git checkout
1204 * @return bool|string Sha1 of commit HEAD points to
1206 public static function getGitHeadSha1( $dir ) {
1207 $repo = new GitInfo( $dir );
1209 return $repo->getHeadSHA1();
1213 * @param string $dir Directory of the git checkout
1214 * @return bool|string Branch currently checked out
1216 public static function getGitCurrentBranch( $dir ) {
1217 $repo = new GitInfo( $dir );
1218 return $repo->getCurrentBranch();
1222 * Get the list of entry points and their URLs
1223 * @return string Wikitext
1225 public function getEntryPointInfo() {
1226 global $wgArticlePath, $wgScriptPath;
1227 $scriptPath = $wgScriptPath ? $wgScriptPath : "/";
1228 $entryPoints = array(
1229 'version-entrypoints-articlepath' => $wgArticlePath,
1230 'version-entrypoints-scriptpath' => $scriptPath,
1231 'version-entrypoints-index-php' => wfScript( 'index' ),
1232 'version-entrypoints-api-php' => wfScript( 'api' ),
1233 'version-entrypoints-load-php' => wfScript( 'load' ),
1236 $language = $this->getLanguage();
1237 $thAttribures = array(
1238 'dir' => $language->getDir(),
1239 'lang' => $language->getHtmlCode()
1241 $out = Html::element(
1242 'h2',
1243 array( 'id' => 'mw-version-entrypoints' ),
1244 $this->msg( 'version-entrypoints' )->text()
1246 Html::openElement( 'table',
1247 array(
1248 'class' => 'wikitable plainlinks',
1249 'id' => 'mw-version-entrypoints-table',
1250 'dir' => 'ltr',
1251 'lang' => 'en'
1254 Html::openElement( 'tr' ) .
1255 Html::element(
1256 'th',
1257 $thAttribures,
1258 $this->msg( 'version-entrypoints-header-entrypoint' )->text()
1260 Html::element(
1261 'th',
1262 $thAttribures,
1263 $this->msg( 'version-entrypoints-header-url' )->text()
1265 Html::closeElement( 'tr' );
1267 foreach ( $entryPoints as $message => $value ) {
1268 $url = wfExpandUrl( $value, PROTO_RELATIVE );
1269 $out .= Html::openElement( 'tr' ) .
1270 // ->text() looks like it should be ->parse(), but this function
1271 // returns wikitext, not HTML, boo
1272 Html::rawElement( 'td', array(), $this->msg( $message )->text() ) .
1273 Html::rawElement( 'td', array(), Html::rawElement( 'code', array(), "[$url $value]" ) ) .
1274 Html::closeElement( 'tr' );
1277 $out .= Html::closeElement( 'table' );
1279 return $out;
1282 protected function getGroupName() {
1283 return 'wiki';