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