ApiQuerySiteinfo.php 29 KB

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