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