SpecialVersion.php 35 KB

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