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