ApiQuerySiteinfo.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Sep 25, 2006
  6. *
  7. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. use MediaWiki\MediaWikiServices;
  27. /**
  28. * A query action to return meta information about the wiki site.
  29. *
  30. * @ingroup API
  31. */
  32. class ApiQuerySiteinfo extends ApiQueryBase {
  33. public function __construct( ApiQuery $query, $moduleName ) {
  34. parent::__construct( $query, $moduleName, 'si' );
  35. }
  36. public function execute() {
  37. $params = $this->extractRequestParams();
  38. $done = [];
  39. $fit = false;
  40. foreach ( $params['prop'] as $p ) {
  41. switch ( $p ) {
  42. case 'general':
  43. $fit = $this->appendGeneralInfo( $p );
  44. break;
  45. case 'namespaces':
  46. $fit = $this->appendNamespaces( $p );
  47. break;
  48. case 'namespacealiases':
  49. $fit = $this->appendNamespaceAliases( $p );
  50. break;
  51. case 'specialpagealiases':
  52. $fit = $this->appendSpecialPageAliases( $p );
  53. break;
  54. case 'magicwords':
  55. $fit = $this->appendMagicWords( $p );
  56. break;
  57. case 'interwikimap':
  58. $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
  59. $fit = $this->appendInterwikiMap( $p, $filteriw );
  60. break;
  61. case 'dbrepllag':
  62. $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
  63. break;
  64. case 'statistics':
  65. $fit = $this->appendStatistics( $p );
  66. break;
  67. case 'usergroups':
  68. $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
  69. break;
  70. case 'libraries':
  71. $fit = $this->appendInstalledLibraries( $p );
  72. break;
  73. case 'extensions':
  74. $fit = $this->appendExtensions( $p );
  75. break;
  76. case 'fileextensions':
  77. $fit = $this->appendFileExtensions( $p );
  78. break;
  79. case 'rightsinfo':
  80. $fit = $this->appendRightsInfo( $p );
  81. break;
  82. case 'restrictions':
  83. $fit = $this->appendRestrictions( $p );
  84. break;
  85. case 'languages':
  86. $fit = $this->appendLanguages( $p );
  87. break;
  88. case 'languagevariants':
  89. $fit = $this->appendLanguageVariants( $p );
  90. break;
  91. case 'skins':
  92. $fit = $this->appendSkins( $p );
  93. break;
  94. case 'extensiontags':
  95. $fit = $this->appendExtensionTags( $p );
  96. break;
  97. case 'functionhooks':
  98. $fit = $this->appendFunctionHooks( $p );
  99. break;
  100. case 'showhooks':
  101. $fit = $this->appendSubscribedHooks( $p );
  102. break;
  103. case 'variables':
  104. $fit = $this->appendVariables( $p );
  105. break;
  106. case 'protocols':
  107. $fit = $this->appendProtocols( $p );
  108. break;
  109. case 'defaultoptions':
  110. $fit = $this->appendDefaultOptions( $p );
  111. break;
  112. case 'uploaddialog':
  113. $fit = $this->appendUploadDialog( $p );
  114. break;
  115. default:
  116. ApiBase::dieDebug( __METHOD__, "Unknown prop=$p" );
  117. }
  118. if ( !$fit ) {
  119. // Abuse siprop as a query-continue parameter
  120. // and set it to all unprocessed props
  121. $this->setContinueEnumParameter( 'prop', implode( '|',
  122. array_diff( $params['prop'], $done ) ) );
  123. break;
  124. }
  125. $done[] = $p;
  126. }
  127. }
  128. protected function appendGeneralInfo( $property ) {
  129. global $wgContLang;
  130. $config = $this->getConfig();
  131. $data = [];
  132. $mainPage = Title::newMainPage();
  133. $data['mainpage'] = $mainPage->getPrefixedText();
  134. $data['base'] = wfExpandUrl( $mainPage->getFullURL(), PROTO_CURRENT );
  135. $data['sitename'] = $config->get( 'Sitename' );
  136. // wgLogo can either be a relative or an absolute path
  137. // make sure we always return an absolute path
  138. $data['logo'] = wfExpandUrl( $config->get( 'Logo' ), PROTO_RELATIVE );
  139. $data['generator'] = "MediaWiki {$config->get( 'Version' )}";
  140. $data['phpversion'] = PHP_VERSION;
  141. $data['phpsapi'] = PHP_SAPI;
  142. if ( defined( 'HHVM_VERSION' ) ) {
  143. $data['hhvmversion'] = HHVM_VERSION;
  144. }
  145. $data['dbtype'] = $config->get( 'DBtype' );
  146. $data['dbversion'] = $this->getDB()->getServerVersion();
  147. $allowFrom = [ '' ];
  148. $allowException = true;
  149. if ( !$config->get( 'AllowExternalImages' ) ) {
  150. $data['imagewhitelistenabled'] = (bool)$config->get( 'EnableImageWhitelist' );
  151. $allowFrom = $config->get( 'AllowExternalImagesFrom' );
  152. $allowException = !empty( $allowFrom );
  153. }
  154. if ( $allowException ) {
  155. $data['externalimages'] = (array)$allowFrom;
  156. ApiResult::setIndexedTagName( $data['externalimages'], 'prefix' );
  157. }
  158. $data['langconversion'] = !$config->get( 'DisableLangConversion' );
  159. $data['titleconversion'] = !$config->get( 'DisableTitleConversion' );
  160. if ( $wgContLang->linkPrefixExtension() ) {
  161. $linkPrefixCharset = $wgContLang->linkPrefixCharset();
  162. $data['linkprefixcharset'] = $linkPrefixCharset;
  163. // For backwards compatibility
  164. $data['linkprefix'] = "/^((?>.*[^$linkPrefixCharset]|))(.+)$/sDu";
  165. } else {
  166. $data['linkprefixcharset'] = '';
  167. $data['linkprefix'] = '';
  168. }
  169. $linktrail = $wgContLang->linkTrail();
  170. $data['linktrail'] = $linktrail ?: '';
  171. $data['legaltitlechars'] = Title::legalChars();
  172. $data['invalidusernamechars'] = $config->get( 'InvalidUsernameCharacters' );
  173. $data['allunicodefixes'] = (bool)$config->get( 'AllUnicodeFixes' );
  174. $data['fixarabicunicode'] = (bool)$config->get( 'FixArabicUnicode' );
  175. $data['fixmalayalamunicode'] = (bool)$config->get( 'FixMalayalamUnicode' );
  176. global $IP;
  177. $git = SpecialVersion::getGitHeadSha1( $IP );
  178. if ( $git ) {
  179. $data['git-hash'] = $git;
  180. $data['git-branch'] =
  181. SpecialVersion::getGitCurrentBranch( $GLOBALS['IP'] );
  182. }
  183. // 'case-insensitive' option is reserved for future
  184. $data['case'] = $config->get( 'CapitalLinks' ) ? 'first-letter' : 'case-sensitive';
  185. $data['lang'] = $config->get( 'LanguageCode' );
  186. $fallbacks = [];
  187. foreach ( $wgContLang->getFallbackLanguages() as $code ) {
  188. $fallbacks[] = [ 'code' => $code ];
  189. }
  190. $data['fallback'] = $fallbacks;
  191. ApiResult::setIndexedTagName( $data['fallback'], 'lang' );
  192. if ( $wgContLang->hasVariants() ) {
  193. $variants = [];
  194. foreach ( $wgContLang->getVariants() as $code ) {
  195. $variants[] = [
  196. 'code' => $code,
  197. 'name' => $wgContLang->getVariantname( $code ),
  198. ];
  199. }
  200. $data['variants'] = $variants;
  201. ApiResult::setIndexedTagName( $data['variants'], 'lang' );
  202. }
  203. $data['rtl'] = $wgContLang->isRTL();
  204. $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
  205. $data['readonly'] = wfReadOnly();
  206. if ( $data['readonly'] ) {
  207. $data['readonlyreason'] = wfReadOnlyReason();
  208. }
  209. $data['writeapi'] = (bool)$config->get( 'EnableWriteAPI' );
  210. $data['maxarticlesize'] = $config->get( 'MaxArticleSize' ) * 1024;
  211. $tz = $config->get( 'Localtimezone' );
  212. $offset = $config->get( 'LocalTZoffset' );
  213. if ( is_null( $tz ) ) {
  214. $tz = 'UTC';
  215. $offset = 0;
  216. } elseif ( is_null( $offset ) ) {
  217. $offset = 0;
  218. }
  219. $data['timezone'] = $tz;
  220. $data['timeoffset'] = intval( $offset );
  221. $data['articlepath'] = $config->get( 'ArticlePath' );
  222. $data['scriptpath'] = $config->get( 'ScriptPath' );
  223. $data['script'] = $config->get( 'Script' );
  224. $data['variantarticlepath'] = $config->get( 'VariantArticlePath' );
  225. $data[ApiResult::META_BC_BOOLS][] = 'variantarticlepath';
  226. $data['server'] = $config->get( 'Server' );
  227. $data['servername'] = $config->get( 'ServerName' );
  228. $data['wikiid'] = wfWikiID();
  229. $data['time'] = wfTimestamp( TS_ISO_8601, time() );
  230. $data['misermode'] = (bool)$config->get( 'MiserMode' );
  231. $data['uploadsenabled'] = UploadBase::isEnabled();
  232. $data['maxuploadsize'] = UploadBase::getMaxUploadSize();
  233. $data['minuploadchunksize'] = (int)$config->get( 'MinUploadChunkSize' );
  234. $data['galleryoptions'] = $config->get( 'GalleryOptions' );
  235. $data['thumblimits'] = $config->get( 'ThumbLimits' );
  236. ApiResult::setArrayType( $data['thumblimits'], 'BCassoc' );
  237. ApiResult::setIndexedTagName( $data['thumblimits'], 'limit' );
  238. $data['imagelimits'] = [];
  239. ApiResult::setArrayType( $data['imagelimits'], 'BCassoc' );
  240. ApiResult::setIndexedTagName( $data['imagelimits'], 'limit' );
  241. foreach ( $config->get( 'ImageLimits' ) as $k => $limit ) {
  242. $data['imagelimits'][$k] = [ 'width' => $limit[0], 'height' => $limit[1] ];
  243. }
  244. $favicon = $config->get( 'Favicon' );
  245. if ( !empty( $favicon ) ) {
  246. // wgFavicon can either be a relative or an absolute path
  247. // make sure we always return an absolute path
  248. $data['favicon'] = wfExpandUrl( $favicon, PROTO_RELATIVE );
  249. }
  250. $data['centralidlookupprovider'] = $config->get( 'CentralIdLookupProvider' );
  251. $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
  252. $data['allcentralidlookupproviders'] = $providerIds;
  253. $data['interwikimagic'] = (bool)$config->get( 'InterwikiMagic' );
  254. $data['magiclinks'] = $config->get( 'EnableMagicLinks' );
  255. Hooks::run( 'APIQuerySiteInfoGeneralInfo', [ $this, &$data ] );
  256. return $this->getResult()->addValue( 'query', $property, $data );
  257. }
  258. protected function appendNamespaces( $property ) {
  259. global $wgContLang;
  260. $data = [
  261. ApiResult::META_TYPE => 'assoc',
  262. ];
  263. foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
  264. $data[$ns] = [
  265. 'id' => intval( $ns ),
  266. 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
  267. ];
  268. ApiResult::setContentValue( $data[$ns], 'name', $title );
  269. $canonical = MWNamespace::getCanonicalName( $ns );
  270. $data[$ns]['subpages'] = MWNamespace::hasSubpages( $ns );
  271. if ( $canonical ) {
  272. $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
  273. }
  274. $data[$ns]['content'] = MWNamespace::isContent( $ns );
  275. $data[$ns]['nonincludable'] = MWNamespace::isNonincludable( $ns );
  276. $contentmodel = MWNamespace::getNamespaceContentModel( $ns );
  277. if ( $contentmodel ) {
  278. $data[$ns]['defaultcontentmodel'] = $contentmodel;
  279. }
  280. }
  281. ApiResult::setArrayType( $data, 'assoc' );
  282. ApiResult::setIndexedTagName( $data, 'ns' );
  283. return $this->getResult()->addValue( 'query', $property, $data );
  284. }
  285. protected function appendNamespaceAliases( $property ) {
  286. global $wgContLang;
  287. $aliases = array_merge( $this->getConfig()->get( 'NamespaceAliases' ),
  288. $wgContLang->getNamespaceAliases() );
  289. $namespaces = $wgContLang->getNamespaces();
  290. $data = [];
  291. foreach ( $aliases as $title => $ns ) {
  292. if ( $namespaces[$ns] == $title ) {
  293. // Don't list duplicates
  294. continue;
  295. }
  296. $item = [
  297. 'id' => intval( $ns )
  298. ];
  299. ApiResult::setContentValue( $item, 'alias', strtr( $title, '_', ' ' ) );
  300. $data[] = $item;
  301. }
  302. sort( $data );
  303. ApiResult::setIndexedTagName( $data, 'ns' );
  304. return $this->getResult()->addValue( 'query', $property, $data );
  305. }
  306. protected function appendSpecialPageAliases( $property ) {
  307. global $wgContLang;
  308. $data = [];
  309. $aliases = $wgContLang->getSpecialPageAliases();
  310. foreach ( SpecialPageFactory::getNames() as $specialpage ) {
  311. if ( isset( $aliases[$specialpage] ) ) {
  312. $arr = [ 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] ];
  313. ApiResult::setIndexedTagName( $arr['aliases'], 'alias' );
  314. $data[] = $arr;
  315. }
  316. }
  317. ApiResult::setIndexedTagName( $data, 'specialpage' );
  318. return $this->getResult()->addValue( 'query', $property, $data );
  319. }
  320. protected function appendMagicWords( $property ) {
  321. global $wgContLang;
  322. $data = [];
  323. foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
  324. $caseSensitive = array_shift( $aliases );
  325. $arr = [ 'name' => $magicword, 'aliases' => $aliases ];
  326. $arr['case-sensitive'] = (bool)$caseSensitive;
  327. ApiResult::setIndexedTagName( $arr['aliases'], 'alias' );
  328. $data[] = $arr;
  329. }
  330. ApiResult::setIndexedTagName( $data, 'magicword' );
  331. return $this->getResult()->addValue( 'query', $property, $data );
  332. }
  333. protected function appendInterwikiMap( $property, $filter ) {
  334. $local = null;
  335. if ( $filter === 'local' ) {
  336. $local = 1;
  337. } elseif ( $filter === '!local' ) {
  338. $local = 0;
  339. } elseif ( $filter ) {
  340. ApiBase::dieDebug( __METHOD__, "Unknown filter=$filter" );
  341. }
  342. $params = $this->extractRequestParams();
  343. $langCode = isset( $params['inlanguagecode'] ) ? $params['inlanguagecode'] : '';
  344. $langNames = Language::fetchLanguageNames( $langCode );
  345. $getPrefixes = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes( $local );
  346. $extraLangPrefixes = $this->getConfig()->get( 'ExtraInterlanguageLinkPrefixes' );
  347. $localInterwikis = $this->getConfig()->get( 'LocalInterwikis' );
  348. $data = [];
  349. foreach ( $getPrefixes as $row ) {
  350. $prefix = $row['iw_prefix'];
  351. $val = [];
  352. $val['prefix'] = $prefix;
  353. if ( isset( $row['iw_local'] ) && $row['iw_local'] == '1' ) {
  354. $val['local'] = true;
  355. }
  356. if ( isset( $row['iw_trans'] ) && $row['iw_trans'] == '1' ) {
  357. $val['trans'] = true;
  358. }
  359. if ( isset( $langNames[$prefix] ) ) {
  360. $val['language'] = $langNames[$prefix];
  361. }
  362. if ( in_array( $prefix, $localInterwikis ) ) {
  363. $val['localinterwiki'] = true;
  364. }
  365. if ( in_array( $prefix, $extraLangPrefixes ) ) {
  366. $val['extralanglink'] = true;
  367. $linktext = wfMessage( "interlanguage-link-$prefix" );
  368. if ( !$linktext->isDisabled() ) {
  369. $val['linktext'] = $linktext->text();
  370. }
  371. $sitename = wfMessage( "interlanguage-link-sitename-$prefix" );
  372. if ( !$sitename->isDisabled() ) {
  373. $val['sitename'] = $sitename->text();
  374. }
  375. }
  376. $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT );
  377. $val['protorel'] = substr( $row['iw_url'], 0, 2 ) == '//';
  378. if ( isset( $row['iw_wikiid'] ) && $row['iw_wikiid'] !== '' ) {
  379. $val['wikiid'] = $row['iw_wikiid'];
  380. }
  381. if ( isset( $row['iw_api'] ) && $row['iw_api'] !== '' ) {
  382. $val['api'] = $row['iw_api'];
  383. }
  384. $data[] = $val;
  385. }
  386. ApiResult::setIndexedTagName( $data, 'iw' );
  387. return $this->getResult()->addValue( 'query', $property, $data );
  388. }
  389. protected function appendDbReplLagInfo( $property, $includeAll ) {
  390. $data = [];
  391. $lb = wfGetLB();
  392. $showHostnames = $this->getConfig()->get( 'ShowHostnames' );
  393. if ( $includeAll ) {
  394. if ( !$showHostnames ) {
  395. $this->dieWithError( 'apierror-siteinfo-includealldenied', 'includeAllDenied' );
  396. }
  397. $lags = $lb->getLagTimes();
  398. foreach ( $lags as $i => $lag ) {
  399. $data[] = [
  400. 'host' => $lb->getServerName( $i ),
  401. 'lag' => $lag
  402. ];
  403. }
  404. } else {
  405. list( , $lag, $index ) = $lb->getMaxLag();
  406. $data[] = [
  407. 'host' => $showHostnames
  408. ? $lb->getServerName( $index )
  409. : '',
  410. 'lag' => intval( $lag )
  411. ];
  412. }
  413. ApiResult::setIndexedTagName( $data, 'db' );
  414. return $this->getResult()->addValue( 'query', $property, $data );
  415. }
  416. protected function appendStatistics( $property ) {
  417. $data = [];
  418. $data['pages'] = intval( SiteStats::pages() );
  419. $data['articles'] = intval( SiteStats::articles() );
  420. $data['edits'] = intval( SiteStats::edits() );
  421. $data['images'] = intval( SiteStats::images() );
  422. $data['users'] = intval( SiteStats::users() );
  423. $data['activeusers'] = intval( SiteStats::activeUsers() );
  424. $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
  425. $data['jobs'] = intval( SiteStats::jobs() );
  426. Hooks::run( 'APIQuerySiteInfoStatisticsInfo', [ &$data ] );
  427. return $this->getResult()->addValue( 'query', $property, $data );
  428. }
  429. protected function appendUserGroups( $property, $numberInGroup ) {
  430. $config = $this->getConfig();
  431. $data = [];
  432. $result = $this->getResult();
  433. $allGroups = array_values( User::getAllGroups() );
  434. foreach ( $config->get( 'GroupPermissions' ) as $group => $permissions ) {
  435. $arr = [
  436. 'name' => $group,
  437. 'rights' => array_keys( $permissions, true ),
  438. ];
  439. if ( $numberInGroup ) {
  440. $autopromote = $config->get( 'Autopromote' );
  441. if ( $group == 'user' ) {
  442. $arr['number'] = SiteStats::users();
  443. // '*' and autopromote groups have no size
  444. } elseif ( $group !== '*' && !isset( $autopromote[$group] ) ) {
  445. $arr['number'] = SiteStats::numberingroup( $group );
  446. }
  447. }
  448. $groupArr = [
  449. 'add' => $config->get( 'AddGroups' ),
  450. 'remove' => $config->get( 'RemoveGroups' ),
  451. 'add-self' => $config->get( 'GroupsAddToSelf' ),
  452. 'remove-self' => $config->get( 'GroupsRemoveFromSelf' )
  453. ];
  454. foreach ( $groupArr as $type => $rights ) {
  455. if ( isset( $rights[$group] ) ) {
  456. if ( $rights[$group] === true ) {
  457. $groups = $allGroups;
  458. } else {
  459. $groups = array_intersect( $rights[$group], $allGroups );
  460. }
  461. if ( $groups ) {
  462. $arr[$type] = $groups;
  463. ApiResult::setArrayType( $arr[$type], 'BCarray' );
  464. ApiResult::setIndexedTagName( $arr[$type], 'group' );
  465. }
  466. }
  467. }
  468. ApiResult::setIndexedTagName( $arr['rights'], 'permission' );
  469. $data[] = $arr;
  470. }
  471. ApiResult::setIndexedTagName( $data, 'group' );
  472. return $result->addValue( 'query', $property, $data );
  473. }
  474. protected function appendFileExtensions( $property ) {
  475. $data = [];
  476. foreach ( array_unique( $this->getConfig()->get( 'FileExtensions' ) ) as $ext ) {
  477. $data[] = [ 'ext' => $ext ];
  478. }
  479. ApiResult::setIndexedTagName( $data, 'fe' );
  480. return $this->getResult()->addValue( 'query', $property, $data );
  481. }
  482. protected function appendInstalledLibraries( $property ) {
  483. global $IP;
  484. $path = "$IP/vendor/composer/installed.json";
  485. if ( !file_exists( $path ) ) {
  486. return true;
  487. }
  488. $data = [];
  489. $installed = new ComposerInstalled( $path );
  490. foreach ( $installed->getInstalledDependencies() as $name => $info ) {
  491. if ( strpos( $info['type'], 'mediawiki-' ) === 0 ) {
  492. // Skip any extensions or skins since they'll be listed
  493. // in their proper section
  494. continue;
  495. }
  496. $data[] = [
  497. 'name' => $name,
  498. 'version' => $info['version'],
  499. ];
  500. }
  501. ApiResult::setIndexedTagName( $data, 'library' );
  502. return $this->getResult()->addValue( 'query', $property, $data );
  503. }
  504. protected function appendExtensions( $property ) {
  505. $data = [];
  506. foreach ( $this->getConfig()->get( 'ExtensionCredits' ) as $type => $extensions ) {
  507. foreach ( $extensions as $ext ) {
  508. $ret = [];
  509. $ret['type'] = $type;
  510. if ( isset( $ext['name'] ) ) {
  511. $ret['name'] = $ext['name'];
  512. }
  513. if ( isset( $ext['namemsg'] ) ) {
  514. $ret['namemsg'] = $ext['namemsg'];
  515. }
  516. if ( isset( $ext['description'] ) ) {
  517. $ret['description'] = $ext['description'];
  518. }
  519. if ( isset( $ext['descriptionmsg'] ) ) {
  520. // Can be a string or [ key, param1, param2, ... ]
  521. if ( is_array( $ext['descriptionmsg'] ) ) {
  522. $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
  523. $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
  524. ApiResult::setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
  525. } else {
  526. $ret['descriptionmsg'] = $ext['descriptionmsg'];
  527. }
  528. }
  529. if ( isset( $ext['author'] ) ) {
  530. $ret['author'] = is_array( $ext['author'] ) ?
  531. implode( ', ', $ext['author'] ) : $ext['author'];
  532. }
  533. if ( isset( $ext['url'] ) ) {
  534. $ret['url'] = $ext['url'];
  535. }
  536. if ( isset( $ext['version'] ) ) {
  537. $ret['version'] = $ext['version'];
  538. }
  539. if ( isset( $ext['path'] ) ) {
  540. $extensionPath = dirname( $ext['path'] );
  541. $gitInfo = new GitInfo( $extensionPath );
  542. $vcsVersion = $gitInfo->getHeadSHA1();
  543. if ( $vcsVersion !== false ) {
  544. $ret['vcs-system'] = 'git';
  545. $ret['vcs-version'] = $vcsVersion;
  546. $ret['vcs-url'] = $gitInfo->getHeadViewUrl();
  547. $vcsDate = $gitInfo->getHeadCommitDate();
  548. if ( $vcsDate !== false ) {
  549. $ret['vcs-date'] = wfTimestamp( TS_ISO_8601, $vcsDate );
  550. }
  551. }
  552. if ( SpecialVersion::getExtLicenseFileName( $extensionPath ) ) {
  553. $ret['license-name'] = isset( $ext['license-name'] ) ? $ext['license-name'] : '';
  554. $ret['license'] = SpecialPage::getTitleFor(
  555. 'Version',
  556. "License/{$ext['name']}"
  557. )->getLinkURL();
  558. }
  559. if ( SpecialVersion::getExtAuthorsFileName( $extensionPath ) ) {
  560. $ret['credits'] = SpecialPage::getTitleFor(
  561. 'Version',
  562. "Credits/{$ext['name']}"
  563. )->getLinkURL();
  564. }
  565. }
  566. $data[] = $ret;
  567. }
  568. }
  569. ApiResult::setIndexedTagName( $data, 'ext' );
  570. return $this->getResult()->addValue( 'query', $property, $data );
  571. }
  572. protected function appendRightsInfo( $property ) {
  573. $config = $this->getConfig();
  574. $rightsPage = $config->get( 'RightsPage' );
  575. if ( is_string( $rightsPage ) ) {
  576. $title = Title::newFromText( $rightsPage );
  577. $url = wfExpandUrl( $title, PROTO_CURRENT );
  578. } else {
  579. $title = false;
  580. $url = $config->get( 'RightsUrl' );
  581. }
  582. $text = $config->get( 'RightsText' );
  583. if ( !$text && $title ) {
  584. $text = $title->getPrefixedText();
  585. }
  586. $data = [
  587. 'url' => $url ?: '',
  588. 'text' => $text ?: ''
  589. ];
  590. return $this->getResult()->addValue( 'query', $property, $data );
  591. }
  592. protected function appendRestrictions( $property ) {
  593. $config = $this->getConfig();
  594. $data = [
  595. 'types' => $config->get( 'RestrictionTypes' ),
  596. 'levels' => $config->get( 'RestrictionLevels' ),
  597. 'cascadinglevels' => $config->get( 'CascadingRestrictionLevels' ),
  598. 'semiprotectedlevels' => $config->get( 'SemiprotectedRestrictionLevels' ),
  599. ];
  600. ApiResult::setArrayType( $data['types'], 'BCarray' );
  601. ApiResult::setArrayType( $data['levels'], 'BCarray' );
  602. ApiResult::setArrayType( $data['cascadinglevels'], 'BCarray' );
  603. ApiResult::setArrayType( $data['semiprotectedlevels'], 'BCarray' );
  604. ApiResult::setIndexedTagName( $data['types'], 'type' );
  605. ApiResult::setIndexedTagName( $data['levels'], 'level' );
  606. ApiResult::setIndexedTagName( $data['cascadinglevels'], 'level' );
  607. ApiResult::setIndexedTagName( $data['semiprotectedlevels'], 'level' );
  608. return $this->getResult()->addValue( 'query', $property, $data );
  609. }
  610. public function appendLanguages( $property ) {
  611. $params = $this->extractRequestParams();
  612. $langCode = isset( $params['inlanguagecode'] ) ? $params['inlanguagecode'] : '';
  613. $langNames = Language::fetchLanguageNames( $langCode );
  614. $data = [];
  615. foreach ( $langNames as $code => $name ) {
  616. $lang = [ 'code' => $code ];
  617. ApiResult::setContentValue( $lang, 'name', $name );
  618. $data[] = $lang;
  619. }
  620. ApiResult::setIndexedTagName( $data, 'lang' );
  621. return $this->getResult()->addValue( 'query', $property, $data );
  622. }
  623. // Export information about which page languages will trigger
  624. // language conversion. (T153341)
  625. public function appendLanguageVariants( $property ) {
  626. $langNames = LanguageConverter::$languagesWithVariants;
  627. if ( $this->getConfig()->get( 'DisableLangConversion' ) ) {
  628. // Ensure result is empty if language conversion is disabled.
  629. $langNames = [];
  630. }
  631. sort( $langNames );
  632. $data = [];
  633. foreach ( $langNames as $langCode ) {
  634. $lang = Language::factory( $langCode );
  635. if ( $lang->getConverter() instanceof FakeConverter ) {
  636. // Only languages which do not return instances of
  637. // FakeConverter implement language conversion.
  638. continue;
  639. }
  640. $data[$langCode] = [];
  641. ApiResult::setIndexedTagName( $data[$langCode], 'variant' );
  642. ApiResult::setArrayType( $data[$langCode], 'kvp', 'code' );
  643. $variants = $lang->getVariants();
  644. sort( $variants );
  645. foreach ( $variants as $v ) {
  646. $fallbacks = $lang->getConverter()->getVariantFallbacks( $v );
  647. if ( !is_array( $fallbacks ) ) {
  648. $fallbacks = [ $fallbacks ];
  649. }
  650. $data[$langCode][$v] = [
  651. 'fallbacks' => $fallbacks,
  652. ];
  653. ApiResult::setIndexedTagName(
  654. $data[$langCode][$v]['fallbacks'], 'variant'
  655. );
  656. }
  657. }
  658. ApiResult::setIndexedTagName( $data, 'lang' );
  659. ApiResult::setArrayType( $data, 'kvp', 'code' );
  660. return $this->getResult()->addValue( 'query', $property, $data );
  661. }
  662. public function appendSkins( $property ) {
  663. $data = [];
  664. $allowed = Skin::getAllowedSkins();
  665. $default = Skin::normalizeKey( 'default' );
  666. foreach ( Skin::getSkinNames() as $name => $displayName ) {
  667. $msg = $this->msg( "skinname-{$name}" );
  668. $code = $this->getParameter( 'inlanguagecode' );
  669. if ( $code && Language::isValidCode( $code ) ) {
  670. $msg->inLanguage( $code );
  671. } else {
  672. $msg->inContentLanguage();
  673. }
  674. if ( $msg->exists() ) {
  675. $displayName = $msg->text();
  676. }
  677. $skin = [ 'code' => $name ];
  678. ApiResult::setContentValue( $skin, 'name', $displayName );
  679. if ( !isset( $allowed[$name] ) ) {
  680. $skin['unusable'] = true;
  681. }
  682. if ( $name === $default ) {
  683. $skin['default'] = true;
  684. }
  685. $data[] = $skin;
  686. }
  687. ApiResult::setIndexedTagName( $data, 'skin' );
  688. return $this->getResult()->addValue( 'query', $property, $data );
  689. }
  690. public function appendExtensionTags( $property ) {
  691. global $wgParser;
  692. $wgParser->firstCallInit();
  693. $tags = array_map( [ $this, 'formatParserTags' ], $wgParser->getTags() );
  694. ApiResult::setArrayType( $tags, 'BCarray' );
  695. ApiResult::setIndexedTagName( $tags, 't' );
  696. return $this->getResult()->addValue( 'query', $property, $tags );
  697. }
  698. public function appendFunctionHooks( $property ) {
  699. global $wgParser;
  700. $wgParser->firstCallInit();
  701. $hooks = $wgParser->getFunctionHooks();
  702. ApiResult::setArrayType( $hooks, 'BCarray' );
  703. ApiResult::setIndexedTagName( $hooks, 'h' );
  704. return $this->getResult()->addValue( 'query', $property, $hooks );
  705. }
  706. public function appendVariables( $property ) {
  707. $variables = MagicWord::getVariableIDs();
  708. ApiResult::setArrayType( $variables, 'BCarray' );
  709. ApiResult::setIndexedTagName( $variables, 'v' );
  710. return $this->getResult()->addValue( 'query', $property, $variables );
  711. }
  712. public function appendProtocols( $property ) {
  713. // Make a copy of the global so we don't try to set the _element key of it - T47130
  714. $protocols = array_values( $this->getConfig()->get( 'UrlProtocols' ) );
  715. ApiResult::setArrayType( $protocols, 'BCarray' );
  716. ApiResult::setIndexedTagName( $protocols, 'p' );
  717. return $this->getResult()->addValue( 'query', $property, $protocols );
  718. }
  719. public function appendDefaultOptions( $property ) {
  720. $options = User::getDefaultOptions();
  721. $options[ApiResult::META_BC_BOOLS] = array_keys( $options );
  722. return $this->getResult()->addValue( 'query', $property, $options );
  723. }
  724. public function appendUploadDialog( $property ) {
  725. $config = $this->getConfig()->get( 'UploadDialog' );
  726. return $this->getResult()->addValue( 'query', $property, $config );
  727. }
  728. private function formatParserTags( $item ) {
  729. return "<{$item}>";
  730. }
  731. public function appendSubscribedHooks( $property ) {
  732. $hooks = $this->getConfig()->get( 'Hooks' );
  733. $myWgHooks = $hooks;
  734. ksort( $myWgHooks );
  735. $data = [];
  736. foreach ( $myWgHooks as $name => $subscribers ) {
  737. $arr = [
  738. 'name' => $name,
  739. 'subscribers' => array_map( [ 'SpecialVersion', 'arrayToString' ], $subscribers ),
  740. ];
  741. ApiResult::setArrayType( $arr['subscribers'], 'array' );
  742. ApiResult::setIndexedTagName( $arr['subscribers'], 's' );
  743. $data[] = $arr;
  744. }
  745. ApiResult::setIndexedTagName( $data, 'hook' );
  746. return $this->getResult()->addValue( 'query', $property, $data );
  747. }
  748. public function getCacheMode( $params ) {
  749. // Messages for $wgExtraInterlanguageLinkPrefixes depend on user language
  750. if (
  751. count( $this->getConfig()->get( 'ExtraInterlanguageLinkPrefixes' ) ) &&
  752. !is_null( $params['prop'] ) &&
  753. in_array( 'interwikimap', $params['prop'] )
  754. ) {
  755. return 'anon-public-user-private';
  756. }
  757. return 'public';
  758. }
  759. public function getAllowedParams() {
  760. return [
  761. 'prop' => [
  762. ApiBase::PARAM_DFLT => 'general',
  763. ApiBase::PARAM_ISMULTI => true,
  764. ApiBase::PARAM_TYPE => [
  765. 'general',
  766. 'namespaces',
  767. 'namespacealiases',
  768. 'specialpagealiases',
  769. 'magicwords',
  770. 'interwikimap',
  771. 'dbrepllag',
  772. 'statistics',
  773. 'usergroups',
  774. 'libraries',
  775. 'extensions',
  776. 'fileextensions',
  777. 'rightsinfo',
  778. 'restrictions',
  779. 'languages',
  780. 'languagevariants',
  781. 'skins',
  782. 'extensiontags',
  783. 'functionhooks',
  784. 'showhooks',
  785. 'variables',
  786. 'protocols',
  787. 'defaultoptions',
  788. 'uploaddialog',
  789. ],
  790. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  791. ],
  792. 'filteriw' => [
  793. ApiBase::PARAM_TYPE => [
  794. 'local',
  795. '!local',
  796. ]
  797. ],
  798. 'showalldb' => false,
  799. 'numberingroup' => false,
  800. 'inlanguagecode' => null,
  801. ];
  802. }
  803. protected function getExamplesMessages() {
  804. return [
  805. 'action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics'
  806. => 'apihelp-query+siteinfo-example-simple',
  807. 'action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local'
  808. => 'apihelp-query+siteinfo-example-interwiki',
  809. 'action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb='
  810. => 'apihelp-query+siteinfo-example-replag',
  811. ];
  812. }
  813. public function getHelpUrls() {
  814. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Siteinfo';
  815. }
  816. }