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