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