0ba056ac86896f5e4982004cbc35e068504985cf
[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;
52
53 $this->setHeaders();
54 $this->outputHeader();
55 $out = $this->getOutput();
56 $out->allowClickjacking();
57
58 if ( $par !== 'Credits' ) {
59 $text =
60 $this->getMediaWikiCredits() .
61 $this->softwareInformation() .
62 $this->getEntryPointInfo() .
63 $this->getExtensionCredits();
64 if ( $wgSpecialVersionShowHooks ) {
65 $text .= $this->getWgHooks();
66 }
67
68 $out->addWikiText( $text );
69 $out->addHTML( $this->IPInfo() );
70
71 if ( $this->getRequest()->getVal( 'easteregg' ) ) {
72 // TODO: put something interesting here
73 }
74 } else {
75 // Credits sub page
76
77 // Header
78 $out->addHTML( wfMessage( 'version-credits-summary' )->parseAsBlock() );
79
80 $wikiText = file_get_contents( $IP . '/CREDITS' );
81
82 // Take everything from the first section onwards, to remove the (not localized) header
83 $wikiText = substr( $wikiText, strpos( $wikiText, '==' ) );
84
85 $out->addWikiText( $wikiText );
86 }
87 }
88
89 /**
90 * Returns wiki text showing the license information.
91 *
92 * @return string
93 */
94 private static function getMediaWikiCredits() {
95 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-license' ), wfMessage( 'version-license' )->text() );
96
97 // This text is always left-to-right.
98 $ret .= '<div class="plainlinks">';
99 $ret .= "__NOTOC__
100 " . self::getCopyrightAndAuthorList() . "\n
101 " . wfMessage( 'version-license-info' )->text();
102 $ret .= '</div>';
103
104 return str_replace( "\t\t", '', $ret ) . "\n";
105 }
106
107 /**
108 * Get the "MediaWiki is copyright 2001-20xx by lots of cool guys" text
109 *
110 * @return String
111 */
112 public static function getCopyrightAndAuthorList() {
113 global $wgLang;
114
115 if ( defined( 'MEDIAWIKI_INSTALL' ) ) {
116 $othersLink = '[http://www.mediawiki.org/wiki/Special:Version/Credits ' . wfMessage( 'version-poweredby-others' )->text() . ']';
117 } else {
118 $othersLink = '[[Special:Version/Credits|' . wfMessage( 'version-poweredby-others' )->text() . ']]';
119 }
120
121 $authorList = array(
122 'Magnus Manske', 'Brion Vibber', 'Lee Daniel Crocker',
123 'Tim Starling', 'Erik Möller', 'Gabriel Wicke', 'Ævar Arnfjörð Bjarmason',
124 'Niklas Laxström', 'Domas Mituzas', 'Rob Church', 'Yuri Astrakhan',
125 'Aryeh Gregor', 'Aaron Schulz', 'Andrew Garrett', 'Raimond Spekking',
126 'Alexandre Emsenhuber', 'Siebrand Mazeland', 'Chad Horohoe',
127 'Roan Kattouw', 'Trevor Parscal', 'Bryan Tong Minh', 'Sam Reed',
128 'Victor Vasiliev', 'Rotem Liss', 'Platonides', 'Antoine Musso',
129 'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', $othersLink
130 );
131
132 return wfMessage( 'version-poweredby-credits', MWTimestamp::getLocalInstance()->format( 'Y' ),
133 $wgLang->listToText( $authorList ) )->text();
134 }
135
136 /**
137 * Returns wiki text showing the third party software versions (apache, php, mysql).
138 *
139 * @return string
140 */
141 static function softwareInformation() {
142 $dbr = wfGetDB( DB_SLAVE );
143
144 // Put the software in an array of form 'name' => 'version'. All messages should
145 // be loaded here, so feel free to use wfMessage in the 'name'. Raw HTML or
146 // wikimarkup can be used.
147 $software = array();
148 $software['[https://www.mediawiki.org/ MediaWiki]'] = self::getVersionLinked();
149 $software['[http://www.php.net/ PHP]'] = phpversion() . " (" . PHP_SAPI . ")";
150 $software[$dbr->getSoftwareLink()] = $dbr->getServerInfo();
151
152 // Allow a hook to add/remove items.
153 wfRunHooks( 'SoftwareInfo', array( &$software ) );
154
155 $out = Xml::element( 'h2', array( 'id' => 'mw-version-software' ), wfMessage( 'version-software' )->text() ) .
156 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-software' ) ) .
157 "<tr>
158 <th>" . wfMessage( 'version-software-product' )->text() . "</th>
159 <th>" . wfMessage( 'version-software-version' )->text() . "</th>
160 </tr>\n";
161
162 foreach ( $software as $name => $version ) {
163 $out .= "<tr>
164 <td>" . $name . "</td>
165 <td dir=\"ltr\">" . $version . "</td>
166 </tr>\n";
167 }
168
169 return $out . Xml::closeElement( 'table' );
170 }
171
172 /**
173 * Return a string of the MediaWiki version with SVN revision if available.
174 *
175 * @param $flags String
176 * @return mixed
177 */
178 public static function getVersion( $flags = '' ) {
179 global $wgVersion, $IP;
180 wfProfileIn( __METHOD__ );
181
182 $gitInfo = self::getGitHeadSha1( $IP );
183 $svnInfo = self::getSvnInfo( $IP );
184 if ( !$svnInfo && !$gitInfo ) {
185 $version = $wgVersion;
186 } elseif ( $gitInfo && $flags === 'nodb' ) {
187 $shortSha1 = substr( $gitInfo, 0, 7 );
188 $version = "$wgVersion ($shortSha1)";
189 } elseif ( $gitInfo ) {
190 $shortSha1 = substr( $gitInfo, 0, 7 );
191 $shortSha1 = wfMessage( 'parentheses' )->params( $shortSha1 )->escaped();
192 $version = "$wgVersion $shortSha1";
193 } elseif ( $flags === 'nodb' ) {
194 $version = "$wgVersion (r{$svnInfo['checkout-rev']})";
195 } else {
196 $version = $wgVersion . ' ' .
197 wfMessage(
198 'version-svn-revision',
199 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
200 $info['checkout-rev']
201 )->text();
202 }
203
204 wfProfileOut( __METHOD__ );
205 return $version;
206 }
207
208 /**
209 * Return a wikitext-formatted string of the MediaWiki version with a link to
210 * the SVN revision or the git SHA1 of head if available.
211 * Git is prefered over Svn
212 * The fallback is just $wgVersion
213 *
214 * @return mixed
215 */
216 public static function getVersionLinked() {
217 global $wgVersion;
218 wfProfileIn( __METHOD__ );
219
220 $gitVersion = self::getVersionLinkedGit();
221 if ( $gitVersion ) {
222 $v = $gitVersion;
223 } else {
224 $svnVersion = self::getVersionLinkedSvn();
225 if ( $svnVersion ) {
226 $v = $svnVersion;
227 } else {
228 $v = $wgVersion; // fallback
229 }
230 }
231
232 wfProfileOut( __METHOD__ );
233 return $v;
234 }
235
236 /**
237 * @return string wgVersion + a link to subversion revision of svn BASE
238 */
239 private static function getVersionLinkedSvn() {
240 global $IP;
241
242 $info = self::getSvnInfo( $IP );
243 if ( !isset( $info['checkout-rev'] ) ) {
244 return false;
245 }
246
247 $linkText = wfMessage(
248 'version-svn-revision',
249 isset( $info['directory-rev'] ) ? $info['directory-rev'] : '',
250 $info['checkout-rev']
251 )->text();
252
253 if ( isset( $info['viewvc-url'] ) ) {
254 $version = "[{$info['viewvc-url']} $linkText]";
255 } else {
256 $version = $linkText;
257 }
258
259 return self::getwgVersionLinked() . " $version";
260 }
261
262 /**
263 * @return string
264 */
265 private static function getwgVersionLinked() {
266 global $wgVersion;
267 $versionUrl = "";
268 if ( wfRunHooks( 'SpecialVersionVersionUrl', array( $wgVersion, &$versionUrl ) ) ) {
269 $versionParts = array();
270 preg_match( "/^(\d+\.\d+)/", $wgVersion, $versionParts );
271 $versionUrl = "https://www.mediawiki.org/wiki/MediaWiki_{$versionParts[1]}";
272 }
273 return "[$versionUrl $wgVersion]";
274 }
275
276 /**
277 * @since 1.22 Returns the HEAD date in addition to the sha1 and link
278 * @return bool|string wgVersion + HEAD sha1 stripped to the first 7 chars with link and date, or false on failure
279 */
280 private static function getVersionLinkedGit() {
281 global $IP, $wgLang;
282
283 $gitInfo = new GitInfo( $IP );
284 $headSHA1 = $gitInfo->getHeadSHA1();
285 if ( !$headSHA1 ) {
286 return false;
287 }
288
289 $shortSHA1 = '(' . substr( $headSHA1, 0, 7 ) . ')';
290
291 $gitHeadUrl = $gitInfo->getHeadViewUrl();
292 if ( $gitHeadUrl !== false ) {
293 $shortSHA1 = "[$gitHeadUrl $shortSHA1]";
294 }
295
296 $gitHeadCommitDate = $gitInfo->getHeadCommitDate();
297 if ( $gitHeadCommitDate ) {
298 $shortSHA1 .= "<br/>" . $wgLang->timeanddate( $gitHeadCommitDate, true );
299 }
300
301 return self::getwgVersionLinked() . " $shortSHA1";
302 }
303
304 /**
305 * Returns an array with the base extension types.
306 * Type is stored as array key, the message as array value.
307 *
308 * TODO: ideally this would return all extension types, including
309 * those added by SpecialVersionExtensionTypes. This is not possible
310 * since this hook is passing along $this though.
311 *
312 * @since 1.17
313 *
314 * @return array
315 */
316 public static function getExtensionTypes() {
317 if ( self::$extensionTypes === false ) {
318 self::$extensionTypes = array(
319 'specialpage' => wfMessage( 'version-specialpages' )->text(),
320 'parserhook' => wfMessage( 'version-parserhooks' )->text(),
321 'variable' => wfMessage( 'version-variables' )->text(),
322 'media' => wfMessage( 'version-mediahandlers' )->text(),
323 'antispam' => wfMessage( 'version-antispam' )->text(),
324 'skin' => wfMessage( 'version-skins' )->text(),
325 'api' => wfMessage( 'version-api' )->text(),
326 'other' => wfMessage( 'version-other' )->text(),
327 );
328
329 wfRunHooks( 'ExtensionTypes', array( &self::$extensionTypes ) );
330 }
331
332 return self::$extensionTypes;
333 }
334
335 /**
336 * Returns the internationalized name for an extension type.
337 *
338 * @since 1.17
339 *
340 * @param $type String
341 *
342 * @return string
343 */
344 public static function getExtensionTypeName( $type ) {
345 $types = self::getExtensionTypes();
346 return isset( $types[$type] ) ? $types[$type] : $types['other'];
347 }
348
349 /**
350 * Generate wikitext showing extensions name, URL, author and description.
351 *
352 * @return String: Wikitext
353 */
354 function getExtensionCredits() {
355 global $wgExtensionCredits, $wgExtensionFunctions, $wgParser;
356
357 if ( !count( $wgExtensionCredits ) && !count( $wgExtensionFunctions ) ) {
358 return '';
359 }
360
361 $extensionTypes = self::getExtensionTypes();
362
363 /**
364 * @deprecated as of 1.17, use hook ExtensionTypes instead.
365 */
366 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
367
368 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), $this->msg( 'version-extensions' )->text() ) .
369 Xml::openElement( 'table', array( 'class' => 'wikitable plainlinks', 'id' => 'sv-ext' ) );
370
371 // Make sure the 'other' type is set to an array.
372 if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
373 $wgExtensionCredits['other'] = array();
374 }
375
376 // Find all extensions that do not have a valid type and give them the type 'other'.
377 foreach ( $wgExtensionCredits as $type => $extensions ) {
378 if ( !array_key_exists( $type, $extensionTypes ) ) {
379 $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
380 }
381 }
382
383 // Loop through the extension categories to display their extensions in the list.
384 foreach ( $extensionTypes as $type => $message ) {
385 if ( $type != 'other' ) {
386 $out .= $this->getExtensionCategory( $type, $message );
387 }
388 }
389
390 // We want the 'other' type to be last in the list.
391 $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
392
393 $tags = $wgParser->getTags();
394 $cnt = count( $tags );
395
396 if ( $cnt ) {
397 for ( $i = 0; $i < $cnt; ++$i ) {
398 $tags[$i] = "&lt;{$tags[$i]}&gt;";
399 }
400 $out .= $this->openExtType( $this->msg( 'version-parser-extensiontags' )->text(), 'parser-tags' );
401 $out .= '<tr><td colspan="4">' . $this->listToText( $tags ) . "</td></tr>\n";
402 }
403
404 $fhooks = $wgParser->getFunctionHooks();
405 if ( count( $fhooks ) ) {
406 $out .= $this->openExtType( $this->msg( 'version-parser-function-hooks' )->text(), 'parser-function-hooks' );
407 $out .= '<tr><td colspan="4">' . $this->listToText( $fhooks ) . "</td></tr>\n";
408 }
409
410 $out .= Xml::closeElement( 'table' );
411
412 return $out;
413 }
414
415 /**
416 * Creates and returns the HTML for a single extension category.
417 *
418 * @since 1.17
419 *
420 * @param $type String
421 * @param $message String
422 *
423 * @return string
424 */
425 protected function getExtensionCategory( $type, $message ) {
426 global $wgExtensionCredits;
427
428 $out = '';
429
430 if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
431 $out .= $this->openExtType( $message, 'credits-' . $type );
432
433 usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
434
435 foreach ( $wgExtensionCredits[$type] as $extension ) {
436 $out .= $this->getCreditsForExtension( $extension );
437 }
438 }
439
440 return $out;
441 }
442
443 /**
444 * Callback to sort extensions by type.
445 * @param $a array
446 * @param $b array
447 * @return int
448 */
449 function compare( $a, $b ) {
450 if ( $a['name'] === $b['name'] ) {
451 return 0;
452 } else {
453 return $this->getLanguage()->lc( $a['name'] ) > $this->getLanguage()->lc( $b['name'] )
454 ? 1
455 : -1;
456 }
457 }
458
459 /**
460 * Creates and formats the credits for a single extension and returns this.
461 *
462 * @param $extension Array
463 *
464 * @return string
465 */
466 function getCreditsForExtension( array $extension ) {
467 global $wgLang;
468
469 $name = isset( $extension['name'] ) ? $extension['name'] : '[no name]';
470
471 $vcsText = false;
472
473 if ( isset( $extension['path'] ) ) {
474 $gitInfo = new GitInfo( dirname( $extension['path'] ) );
475 $gitHeadSHA1 = $gitInfo->getHeadSHA1();
476 if ( $gitHeadSHA1 !== false ) {
477 $vcsText = '(' . substr( $gitHeadSHA1, 0, 7 ) . ')';
478 $gitViewerUrl = $gitInfo->getHeadViewUrl();
479 if ( $gitViewerUrl !== false ) {
480 $vcsText = "[$gitViewerUrl $vcsText]";
481 }
482 $gitHeadCommitDate = $gitInfo->getHeadCommitDate();
483 if ( $gitHeadCommitDate ) {
484 $vcsText .= "<br/>" . $wgLang->timeanddate( $gitHeadCommitDate, true );
485 }
486 } else {
487 $svnInfo = self::getSvnInfo( dirname( $extension['path'] ) );
488 # Make subversion text/link.
489 if ( $svnInfo !== false ) {
490 $directoryRev = isset( $svnInfo['directory-rev'] ) ? $svnInfo['directory-rev'] : null;
491 $vcsText = $this->msg( 'version-svn-revision', $directoryRev, $svnInfo['checkout-rev'] )->text();
492 $vcsText = isset( $svnInfo['viewvc-url'] ) ? '[' . $svnInfo['viewvc-url'] . " $vcsText]" : $vcsText;
493 }
494 }
495 }
496
497 # Make main link (or just the name if there is no URL).
498 if ( isset( $extension['url'] ) ) {
499 $mainLink = "[{$extension['url']} $name]";
500 } else {
501 $mainLink = $name;
502 }
503
504 if ( isset( $extension['version'] ) ) {
505 $versionText = '<span class="mw-version-ext-version">' .
506 $this->msg( 'version-version', $extension['version'] )->text() .
507 '</span>';
508 } else {
509 $versionText = '';
510 }
511
512 # Make description text.
513 $description = isset( $extension['description'] ) ? $extension['description'] : '';
514
515 if ( isset( $extension['descriptionmsg'] ) ) {
516 # Look for a localized description.
517 $descriptionMsg = $extension['descriptionmsg'];
518
519 if ( is_array( $descriptionMsg ) ) {
520 $descriptionMsgKey = $descriptionMsg[0]; // Get the message key
521 array_shift( $descriptionMsg ); // Shift out the message key to get the parameters only
522 array_map( "htmlspecialchars", $descriptionMsg ); // For sanity
523 $description = $this->msg( $descriptionMsgKey, $descriptionMsg )->text();
524 } else {
525 $description = $this->msg( $descriptionMsg )->text();
526 }
527 }
528
529 if ( $vcsText !== false ) {
530 $extNameVer = "<tr>
531 <td><em>$mainLink $versionText</em></td>
532 <td><em>$vcsText</em></td>";
533 } else {
534 $extNameVer = "<tr>
535 <td colspan=\"2\"><em>$mainLink $versionText</em></td>";
536 }
537
538 $author = isset( $extension['author'] ) ? $extension['author'] : array();
539 $extDescAuthor = "<td>$description</td>
540 <td>" . $this->listAuthors( $author, false ) . "</td>
541 </tr>\n";
542
543 return $extNameVer . $extDescAuthor;
544 }
545
546 /**
547 * Generate wikitext showing hooks in $wgHooks.
548 *
549 * @return String: wikitext
550 */
551 private function getWgHooks() {
552 global $wgHooks;
553
554 if ( count( $wgHooks ) ) {
555 $myWgHooks = $wgHooks;
556 ksort( $myWgHooks );
557
558 $ret = Xml::element( 'h2', array( 'id' => 'mw-version-hooks' ), $this->msg( 'version-hooks' )->text() ) .
559 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-hooks' ) ) .
560 "<tr>
561 <th>" . $this->msg( 'version-hook-name' )->text() . "</th>
562 <th>" . $this->msg( 'version-hook-subscribedby' )->text() . "</th>
563 </tr>\n";
564
565 foreach ( $myWgHooks as $hook => $hooks ) {
566 $ret .= "<tr>
567 <td>$hook</td>
568 <td>" . $this->listToText( $hooks ) . "</td>
569 </tr>\n";
570 }
571
572 $ret .= Xml::closeElement( 'table' );
573 return $ret;
574 } else {
575 return '';
576 }
577 }
578
579 private function openExtType( $text, $name = null ) {
580 $opt = array( 'colspan' => 4 );
581 $out = '';
582
583 if ( $this->firstExtOpened ) {
584 // Insert a spacing line
585 $out .= '<tr class="sv-space">' . Html::element( 'td', $opt ) . "</tr>\n";
586 }
587 $this->firstExtOpened = true;
588
589 if ( $name ) {
590 $opt['id'] = "sv-$name";
591 }
592
593 $out .= "<tr>" . Xml::element( 'th', $opt, $text ) . "</tr>\n";
594
595 return $out;
596 }
597
598 /**
599 * Get information about client's IP address.
600 *
601 * @return String: HTML fragment
602 */
603 private function IPInfo() {
604 $ip = str_replace( '--', ' - ', htmlspecialchars( $this->getRequest()->getIP() ) );
605 return "<!-- visited from $ip -->\n<span style='display:none'>visited from $ip</span>";
606 }
607
608 /**
609 * Return a formatted unsorted list of authors
610 *
611 * @param $authors mixed: string or array of strings
612 * @return String: HTML fragment
613 */
614 function listAuthors( $authors ) {
615 $list = array();
616 foreach ( (array)$authors as $item ) {
617 if ( $item == '...' ) {
618 $list[] = $this->msg( 'version-poweredby-others' )->text();
619 } elseif ( substr( $item, -5 ) == ' ...]' ) {
620 $list[] = substr( $item, 0, -4 ) . $this->msg( 'version-poweredby-others' )->text() . "]";
621 } else {
622 $list[] = $item;
623 }
624 }
625 return $this->listToText( $list, false );
626 }
627
628 /**
629 * Convert an array of items into a list for display.
630 *
631 * @param array $list of elements to display
632 * @param $sort Boolean: whether to sort the items in $list
633 *
634 * @return String
635 */
636 function listToText( $list, $sort = true ) {
637 $cnt = count( $list );
638
639 if ( $cnt == 1 ) {
640 // Enforce always returning a string
641 return (string)self::arrayToString( $list[0] );
642 } elseif ( $cnt == 0 ) {
643 return '';
644 } else {
645 if ( $sort ) {
646 sort( $list );
647 }
648 return $this->getLanguage()->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
649 }
650 }
651
652 /**
653 * Convert an array or object to a string for display.
654 *
655 * @param $list Mixed: will convert an array to string if given and return
656 * the paramater unaltered otherwise
657 *
658 * @return Mixed
659 */
660 public static function arrayToString( $list ) {
661 if ( is_array( $list ) && count( $list ) == 1 ) {
662 $list = $list[0];
663 }
664 if ( is_object( $list ) ) {
665 $class = wfMessage( 'parentheses' )->params( get_class( $list ) )->escaped();
666 return $class;
667 } elseif ( !is_array( $list ) ) {
668 return $list;
669 } else {
670 if ( is_object( $list[0] ) ) {
671 $class = get_class( $list[0] );
672 } else {
673 $class = $list[0];
674 }
675 return wfMessage( 'parentheses' )->params( "$class, {$list[1]}" )->escaped();
676 }
677 }
678
679 /**
680 * Get an associative array of information about a given path, from its .svn
681 * subdirectory. Returns false on error, such as if the directory was not
682 * checked out with subversion.
683 *
684 * Returned keys are:
685 * Required:
686 * checkout-rev The revision which was checked out
687 * Optional:
688 * directory-rev The revision when the directory was last modified
689 * url The subversion URL of the directory
690 * repo-url The base URL of the repository
691 * viewvc-url A ViewVC URL pointing to the checked-out revision
692 * @param $dir string
693 * @return array|bool
694 */
695 public static function getSvnInfo( $dir ) {
696 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
697 $entries = $dir . '/.svn/entries';
698
699 if ( !file_exists( $entries ) ) {
700 return false;
701 }
702
703 $lines = file( $entries );
704 if ( !count( $lines ) ) {
705 return false;
706 }
707
708 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
709 if ( preg_match( '/^<\?xml/', $lines[0] ) ) {
710 // subversion is release <= 1.3
711 if ( !function_exists( 'simplexml_load_file' ) ) {
712 // We could fall back to expat... YUCK
713 return false;
714 }
715
716 // SimpleXml whines about the xmlns...
717 wfSuppressWarnings();
718 $xml = simplexml_load_file( $entries );
719 wfRestoreWarnings();
720
721 if ( $xml ) {
722 foreach ( $xml->entry as $entry ) {
723 if ( $xml->entry[0]['name'] == '' ) {
724 // The directory entry should always have a revision marker.
725 if ( $entry['revision'] ) {
726 return array( 'checkout-rev' => intval( $entry['revision'] ) );
727 }
728 }
729 }
730 }
731
732 return false;
733 }
734
735 // Subversion is release 1.4 or above.
736 if ( count( $lines ) < 11 ) {
737 return false;
738 }
739
740 $info = array(
741 'checkout-rev' => intval( trim( $lines[3] ) ),
742 'url' => trim( $lines[4] ),
743 'repo-url' => trim( $lines[5] ),
744 'directory-rev' => intval( trim( $lines[10] ) )
745 );
746
747 if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
748 $viewvc = str_replace(
749 $info['repo-url'],
750 self::$viewvcUrls[$info['repo-url']],
751 $info['url']
752 );
753
754 $viewvc .= '/?pathrev=';
755 $viewvc .= urlencode( $info['checkout-rev'] );
756 $info['viewvc-url'] = $viewvc;
757 }
758
759 return $info;
760 }
761
762 /**
763 * Retrieve the revision number of a Subversion working directory.
764 *
765 * @param string $dir directory of the svn checkout
766 *
767 * @return Integer: revision number as int
768 */
769 public static function getSvnRevision( $dir ) {
770 $info = self::getSvnInfo( $dir );
771
772 if ( $info === false ) {
773 return false;
774 } elseif ( isset( $info['checkout-rev'] ) ) {
775 return $info['checkout-rev'];
776 } else {
777 return false;
778 }
779 }
780
781 /**
782 * @param string $dir directory of the git checkout
783 * @return bool|String sha1 of commit HEAD points to
784 */
785 public static function getGitHeadSha1( $dir ) {
786 $repo = new GitInfo( $dir );
787 return $repo->getHeadSHA1();
788 }
789
790 /**
791 * Get the list of entry points and their URLs
792 * @return string Wikitext
793 */
794 public function getEntryPointInfo() {
795 global $wgArticlePath, $wgScriptPath;
796 $scriptPath = $wgScriptPath ? $wgScriptPath : "/";
797 $entryPoints = array(
798 'version-entrypoints-articlepath' => $wgArticlePath,
799 'version-entrypoints-scriptpath' => $scriptPath,
800 'version-entrypoints-index-php' => wfScript( 'index' ),
801 'version-entrypoints-api-php' => wfScript( 'api' ),
802 'version-entrypoints-load-php' => wfScript( 'load' ),
803 );
804
805 $language = $this->getLanguage();
806 $thAttribures = array(
807 'dir' => $language->getDir(),
808 'lang' => $language->getCode()
809 );
810 $out = Html::element( 'h2', array( 'id' => 'mw-version-entrypoints' ), $this->msg( 'version-entrypoints' )->text() ) .
811 Html::openElement( 'table',
812 array(
813 'class' => 'wikitable plainlinks',
814 'id' => 'mw-version-entrypoints-table',
815 'dir' => 'ltr',
816 'lang' => 'en'
817 )
818 ) .
819 Html::openElement( 'tr' ) .
820 Html::element( 'th', $thAttribures, $this->msg( 'version-entrypoints-header-entrypoint' )->text() ) .
821 Html::element( 'th', $thAttribures, $this->msg( 'version-entrypoints-header-url' )->text() ) .
822 Html::closeElement( 'tr' );
823
824 foreach ( $entryPoints as $message => $value ) {
825 $url = wfExpandUrl( $value, PROTO_RELATIVE );
826 $out .= Html::openElement( 'tr' ) .
827 // ->text() looks like it should be ->parse(), but this function
828 // returns wikitext, not HTML, boo
829 Html::rawElement( 'td', array(), $this->msg( $message )->text() ) .
830 Html::rawElement( 'td', array(), Html::rawElement( 'code', array(), "[$url $value]" ) ) .
831 Html::closeElement( 'tr' );
832 }
833
834 $out .= Html::closeElement( 'table' );
835 return $out;
836 }
837
838 protected function getGroupName() {
839 return 'wiki';
840 }
841
842 }