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