ApiHelp.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Aug 29, 2014
  6. *
  7. * Copyright © 2014 Wikimedia Foundation and contributors
  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 HtmlFormatter\HtmlFormatter;
  27. use MediaWiki\MediaWikiServices;
  28. /**
  29. * Class to output help for an API module
  30. *
  31. * @since 1.25 completely rewritten
  32. * @ingroup API
  33. */
  34. class ApiHelp extends ApiBase {
  35. public function execute() {
  36. $params = $this->extractRequestParams();
  37. $modules = [];
  38. foreach ( $params['modules'] as $path ) {
  39. $modules[] = $this->getModuleFromPath( $path );
  40. }
  41. // Get the help
  42. $context = new DerivativeContext( $this->getMain()->getContext() );
  43. $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
  44. $context->setLanguage( $this->getMain()->getLanguage() );
  45. $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
  46. $out = new OutputPage( $context );
  47. $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
  48. $context->setOutput( $out );
  49. self::getHelp( $context, $modules, $params );
  50. // Grab the output from the skin
  51. ob_start();
  52. $context->getOutput()->output();
  53. $html = ob_get_clean();
  54. $result = $this->getResult();
  55. if ( $params['wrap'] ) {
  56. $data = [
  57. 'mime' => 'text/html',
  58. 'filename' => 'api-help.html',
  59. 'help' => $html,
  60. ];
  61. ApiResult::setSubelementsList( $data, 'help' );
  62. $result->addValue( null, $this->getModuleName(), $data );
  63. } else {
  64. $result->reset();
  65. $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
  66. $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
  67. $result->addValue( null, 'filename', 'api-help.html', ApiResult::NO_SIZE_CHECK );
  68. }
  69. }
  70. /**
  71. * Generate help for the specified modules
  72. *
  73. * Help is placed into the OutputPage object returned by
  74. * $context->getOutput().
  75. *
  76. * Recognized options include:
  77. * - headerlevel: (int) Header tag level
  78. * - nolead: (bool) Skip the inclusion of api-help-lead
  79. * - noheader: (bool) Skip the inclusion of the top-level section headers
  80. * - submodules: (bool) Include help for submodules of the current module
  81. * - recursivesubmodules: (bool) Include help for submodules recursively
  82. * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
  83. * - toc: (bool) Include a table of contents
  84. *
  85. * @param IContextSource $context
  86. * @param ApiBase[]|ApiBase $modules
  87. * @param array $options Formatting options (described above)
  88. */
  89. public static function getHelp( IContextSource $context, $modules, array $options ) {
  90. global $wgContLang;
  91. if ( !is_array( $modules ) ) {
  92. $modules = [ $modules ];
  93. }
  94. $out = $context->getOutput();
  95. $out->addModuleStyles( [
  96. 'mediawiki.hlist',
  97. 'mediawiki.apihelp',
  98. ] );
  99. if ( !empty( $options['toc'] ) ) {
  100. $out->addModules( 'mediawiki.toc' );
  101. }
  102. $out->setPageTitle( $context->msg( 'api-help-title' ) );
  103. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  104. $cacheKey = null;
  105. if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain &&
  106. $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
  107. ) {
  108. $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
  109. if ( $cacheHelpTimeout > 0 ) {
  110. // Get help text from cache if present
  111. $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
  112. (int)!empty( $options['toc'] ),
  113. str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) );
  114. $cached = $cache->get( $cacheKey );
  115. if ( $cached ) {
  116. $out->addHTML( $cached );
  117. return;
  118. }
  119. }
  120. }
  121. if ( $out->getHTML() !== '' ) {
  122. // Don't save to cache, there's someone else's content in the page
  123. // already
  124. $cacheKey = null;
  125. }
  126. $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
  127. $options['submodules'] = $options['recursivesubmodules'] || !empty( $options['submodules'] );
  128. // Prepend lead
  129. if ( empty( $options['nolead'] ) ) {
  130. $msg = $context->msg( 'api-help-lead' );
  131. if ( !$msg->isDisabled() ) {
  132. $out->addHTML( $msg->parseAsBlock() );
  133. }
  134. }
  135. $haveModules = [];
  136. $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
  137. if ( !empty( $options['toc'] ) && $haveModules ) {
  138. $out->addHTML( Linker::generateTOC( $haveModules, $context->getLanguage() ) );
  139. }
  140. $out->addHTML( $html );
  141. $helptitle = isset( $options['helptitle'] ) ? $options['helptitle'] : null;
  142. $html = self::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
  143. $out->clearHTML();
  144. $out->addHTML( $html );
  145. if ( $cacheKey !== null ) {
  146. $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
  147. }
  148. }
  149. /**
  150. * Replace Special:ApiHelp links with links to api.php
  151. *
  152. * @param string $html
  153. * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
  154. * @param array $localModules Keys are modules to link within the current page, values are ignored
  155. * @return string
  156. */
  157. public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
  158. $formatter = new HtmlFormatter( $html );
  159. $doc = $formatter->getDoc();
  160. $xpath = new DOMXPath( $doc );
  161. $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
  162. foreach ( $nodes as $node ) {
  163. $href = $node->getAttribute( 'href' );
  164. do {
  165. $old = $href;
  166. $href = rawurldecode( $href );
  167. } while ( $old !== $href );
  168. if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
  169. if ( isset( $localModules[$m[1]] ) ) {
  170. $href = $m[2] === '' ? '#' . $m[1] : $m[2];
  171. } elseif ( $helptitle !== null ) {
  172. $href = Title::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
  173. ->getFullURL();
  174. } else {
  175. $href = wfAppendQuery( wfScript( 'api' ), [
  176. 'action' => 'help',
  177. 'modules' => $m[1],
  178. ] ) . $m[2];
  179. }
  180. $node->setAttribute( 'href', $href );
  181. $node->removeAttribute( 'title' );
  182. }
  183. }
  184. return $formatter->getText();
  185. }
  186. /**
  187. * Wrap a message in HTML with a class.
  188. *
  189. * @param Message $msg
  190. * @param string $class
  191. * @param string $tag
  192. * @return string
  193. */
  194. private static function wrap( Message $msg, $class, $tag = 'span' ) {
  195. return Html::rawElement( $tag, [ 'class' => $class ],
  196. $msg->parse()
  197. );
  198. }
  199. /**
  200. * Recursively-called function to actually construct the help
  201. *
  202. * @param IContextSource $context
  203. * @param ApiBase[] $modules
  204. * @param array $options
  205. * @param array &$haveModules
  206. * @return string
  207. */
  208. private static function getHelpInternal( IContextSource $context, array $modules,
  209. array $options, &$haveModules
  210. ) {
  211. $out = '';
  212. $level = empty( $options['headerlevel'] ) ? 2 : $options['headerlevel'];
  213. if ( empty( $options['tocnumber'] ) ) {
  214. $tocnumber = [ 2 => 0 ];
  215. } else {
  216. $tocnumber = &$options['tocnumber'];
  217. }
  218. foreach ( $modules as $module ) {
  219. $tocnumber[$level]++;
  220. $path = $module->getModulePath();
  221. $module->setContext( $context );
  222. $help = [
  223. 'header' => '',
  224. 'flags' => '',
  225. 'description' => '',
  226. 'help-urls' => '',
  227. 'parameters' => '',
  228. 'examples' => '',
  229. 'submodules' => '',
  230. ];
  231. if ( empty( $options['noheader'] ) || !empty( $options['toc'] ) ) {
  232. $anchor = $path;
  233. $i = 1;
  234. while ( isset( $haveModules[$anchor] ) ) {
  235. $anchor = $path . '|' . ++$i;
  236. }
  237. if ( $module->isMain() ) {
  238. $headerContent = $context->msg( 'api-help-main-header' )->parse();
  239. $headerAttr = [
  240. 'class' => 'apihelp-header',
  241. ];
  242. } else {
  243. $name = $module->getModuleName();
  244. $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
  245. "=$name";
  246. if ( $module->getModulePrefix() !== '' ) {
  247. $headerContent .= ' ' .
  248. $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
  249. }
  250. // Module names are always in English and not localized,
  251. // so English language and direction must be set explicitly,
  252. // otherwise parentheses will get broken in RTL wikis
  253. $headerAttr = [
  254. 'class' => 'apihelp-header apihelp-module-name',
  255. 'dir' => 'ltr',
  256. 'lang' => 'en',
  257. ];
  258. }
  259. $headerAttr['id'] = $anchor;
  260. $haveModules[$anchor] = [
  261. 'toclevel' => count( $tocnumber ),
  262. 'level' => $level,
  263. 'anchor' => $anchor,
  264. 'line' => $headerContent,
  265. 'number' => implode( '.', $tocnumber ),
  266. 'index' => false,
  267. ];
  268. if ( empty( $options['noheader'] ) ) {
  269. $help['header'] .= Html::element(
  270. 'h' . min( 6, $level ),
  271. $headerAttr,
  272. $headerContent
  273. );
  274. }
  275. } else {
  276. $haveModules[$path] = true;
  277. }
  278. $links = [];
  279. $any = false;
  280. for ( $m = $module; $m !== null; $m = $m->getParent() ) {
  281. $name = $m->getModuleName();
  282. if ( $name === 'main_int' ) {
  283. $name = 'main';
  284. }
  285. if ( count( $modules ) === 1 && $m === $modules[0] &&
  286. !( !empty( $options['submodules'] ) && $m->getModuleManager() )
  287. ) {
  288. $link = Html::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
  289. } else {
  290. $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
  291. $link = Html::element( 'a',
  292. [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
  293. $name
  294. );
  295. $any = true;
  296. }
  297. array_unshift( $links, $link );
  298. }
  299. if ( $any ) {
  300. $help['header'] .= self::wrap(
  301. $context->msg( 'parentheses' )
  302. ->rawParams( $context->getLanguage()->pipeList( $links ) ),
  303. 'apihelp-linktrail', 'div'
  304. );
  305. }
  306. $flags = $module->getHelpFlags();
  307. $help['flags'] .= Html::openElement( 'div',
  308. [ 'class' => 'apihelp-block apihelp-flags' ] );
  309. $msg = $context->msg( 'api-help-flags' );
  310. if ( !$msg->isDisabled() ) {
  311. $help['flags'] .= self::wrap(
  312. $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
  313. );
  314. }
  315. $help['flags'] .= Html::openElement( 'ul' );
  316. foreach ( $flags as $flag ) {
  317. $help['flags'] .= Html::rawElement( 'li', null,
  318. self::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
  319. );
  320. }
  321. $sourceInfo = $module->getModuleSourceInfo();
  322. if ( $sourceInfo ) {
  323. if ( isset( $sourceInfo['namemsg'] ) ) {
  324. $extname = $context->msg( $sourceInfo['namemsg'] )->text();
  325. } else {
  326. // Probably English, so wrap it.
  327. $extname = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
  328. }
  329. $help['flags'] .= Html::rawElement( 'li', null,
  330. self::wrap(
  331. $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
  332. 'apihelp-source'
  333. )
  334. );
  335. $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
  336. if ( isset( $sourceInfo['license-name'] ) ) {
  337. $msg = $context->msg( 'api-help-license', $link,
  338. Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
  339. );
  340. } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
  341. $msg = $context->msg( 'api-help-license-noname', $link );
  342. } else {
  343. $msg = $context->msg( 'api-help-license-unknown' );
  344. }
  345. $help['flags'] .= Html::rawElement( 'li', null,
  346. self::wrap( $msg, 'apihelp-license' )
  347. );
  348. } else {
  349. $help['flags'] .= Html::rawElement( 'li', null,
  350. self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
  351. );
  352. $help['flags'] .= Html::rawElement( 'li', null,
  353. self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
  354. );
  355. }
  356. $help['flags'] .= Html::closeElement( 'ul' );
  357. $help['flags'] .= Html::closeElement( 'div' );
  358. foreach ( $module->getFinalDescription() as $msg ) {
  359. $msg->setContext( $context );
  360. $help['description'] .= $msg->parseAsBlock();
  361. }
  362. $urls = $module->getHelpUrls();
  363. if ( $urls ) {
  364. $help['help-urls'] .= Html::openElement( 'div',
  365. [ 'class' => 'apihelp-block apihelp-help-urls' ]
  366. );
  367. $msg = $context->msg( 'api-help-help-urls' );
  368. if ( !$msg->isDisabled() ) {
  369. $help['help-urls'] .= self::wrap(
  370. $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
  371. );
  372. }
  373. if ( !is_array( $urls ) ) {
  374. $urls = [ $urls ];
  375. }
  376. $help['help-urls'] .= Html::openElement( 'ul' );
  377. foreach ( $urls as $url ) {
  378. $help['help-urls'] .= Html::rawElement( 'li', null,
  379. Html::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
  380. );
  381. }
  382. $help['help-urls'] .= Html::closeElement( 'ul' );
  383. $help['help-urls'] .= Html::closeElement( 'div' );
  384. }
  385. $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
  386. $dynamicParams = $module->dynamicParameterDocumentation();
  387. $groups = [];
  388. if ( $params || $dynamicParams !== null ) {
  389. $help['parameters'] .= Html::openElement( 'div',
  390. [ 'class' => 'apihelp-block apihelp-parameters' ]
  391. );
  392. $msg = $context->msg( 'api-help-parameters' );
  393. if ( !$msg->isDisabled() ) {
  394. $help['parameters'] .= self::wrap(
  395. $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
  396. );
  397. }
  398. $help['parameters'] .= Html::openElement( 'dl' );
  399. $descriptions = $module->getFinalParamDescription();
  400. foreach ( $params as $name => $settings ) {
  401. if ( !is_array( $settings ) ) {
  402. $settings = [ ApiBase::PARAM_DFLT => $settings ];
  403. }
  404. $help['parameters'] .= Html::rawElement( 'dt', null,
  405. Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
  406. );
  407. // Add description
  408. $description = [];
  409. if ( isset( $descriptions[$name] ) ) {
  410. foreach ( $descriptions[$name] as $msg ) {
  411. $msg->setContext( $context );
  412. $description[] = $msg->parseAsBlock();
  413. }
  414. }
  415. // Add usage info
  416. $info = [];
  417. // Required?
  418. if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
  419. $info[] = $context->msg( 'api-help-param-required' )->parse();
  420. }
  421. // Custom info?
  422. if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
  423. foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
  424. $tag = array_shift( $i );
  425. $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
  426. ->numParams( count( $i ) )
  427. ->params( $context->getLanguage()->commaList( $i ) )
  428. ->params( $module->getModulePrefix() )
  429. ->parse();
  430. }
  431. }
  432. // Type documentation
  433. if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
  434. $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
  435. ? $settings[ApiBase::PARAM_DFLT]
  436. : null;
  437. if ( is_bool( $dflt ) ) {
  438. $settings[ApiBase::PARAM_TYPE] = 'boolean';
  439. } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
  440. $settings[ApiBase::PARAM_TYPE] = 'string';
  441. } elseif ( is_int( $dflt ) ) {
  442. $settings[ApiBase::PARAM_TYPE] = 'integer';
  443. }
  444. }
  445. if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
  446. $type = $settings[ApiBase::PARAM_TYPE];
  447. $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
  448. $hintPipeSeparated = true;
  449. $count = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
  450. ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2] + 1
  451. : ApiBase::LIMIT_SML2 + 1;
  452. if ( is_array( $type ) ) {
  453. $count = count( $type );
  454. $deprecatedValues = isset( $settings[ApiBase::PARAM_DEPRECATED_VALUES] )
  455. ? $settings[ApiBase::PARAM_DEPRECATED_VALUES]
  456. : [];
  457. $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
  458. ? $settings[ApiBase::PARAM_VALUE_LINKS]
  459. : [];
  460. $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
  461. $attr = [];
  462. if ( $v !== '' ) {
  463. // We can't know whether this contains LTR or RTL text.
  464. $attr['dir'] = 'auto';
  465. }
  466. if ( isset( $deprecatedValues[$v] ) ) {
  467. $attr['class'] = 'apihelp-deprecated-value';
  468. }
  469. $ret = $attr ? Html::element( 'span', $attr, $v ) : $v;
  470. if ( isset( $links[$v] ) ) {
  471. $ret = "[[{$links[$v]}|$ret]]";
  472. }
  473. return $ret;
  474. }, $type );
  475. $i = array_search( '', $type, true );
  476. if ( $i === false ) {
  477. $values = $context->getLanguage()->commaList( $values );
  478. } else {
  479. unset( $values[$i] );
  480. $values = $context->msg( 'api-help-param-list-can-be-empty' )
  481. ->numParams( count( $values ) )
  482. ->params( $context->getLanguage()->commaList( $values ) )
  483. ->parse();
  484. }
  485. $info[] = $context->msg( 'api-help-param-list' )
  486. ->params( $multi ? 2 : 1 )
  487. ->params( $values )
  488. ->parse();
  489. $hintPipeSeparated = false;
  490. } else {
  491. switch ( $type ) {
  492. case 'submodule':
  493. $groups[] = $name;
  494. if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
  495. $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
  496. $defaultAttrs = [];
  497. } else {
  498. $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
  499. $map = [];
  500. foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
  501. $map[$submoduleName] = $prefix . $submoduleName;
  502. }
  503. $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
  504. }
  505. ksort( $map );
  506. $submodules = [];
  507. $deprecatedSubmodules = [];
  508. foreach ( $map as $v => $m ) {
  509. $attrs = $defaultAttrs;
  510. $arr = &$submodules;
  511. try {
  512. $submod = $module->getModuleFromPath( $m );
  513. if ( $submod ) {
  514. if ( $submod->isDeprecated() ) {
  515. $arr = &$deprecatedSubmodules;
  516. $attrs['class'] = 'apihelp-deprecated-value';
  517. }
  518. }
  519. } catch ( ApiUsageException $ex ) {
  520. // Ignore
  521. }
  522. if ( $attrs ) {
  523. $v = Html::element( 'span', $attrs, $v );
  524. }
  525. $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
  526. }
  527. $submodules = array_merge( $submodules, $deprecatedSubmodules );
  528. $count = count( $submodules );
  529. $info[] = $context->msg( 'api-help-param-list' )
  530. ->params( $multi ? 2 : 1 )
  531. ->params( $context->getLanguage()->commaList( $submodules ) )
  532. ->parse();
  533. $hintPipeSeparated = false;
  534. // No type message necessary, we have a list of values.
  535. $type = null;
  536. break;
  537. case 'namespace':
  538. $namespaces = MWNamespace::getValidNamespaces();
  539. if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
  540. is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
  541. ) {
  542. $namespaces = array_merge( $namespaces, $settings[ApiBase::PARAM_EXTRA_NAMESPACES] );
  543. }
  544. sort( $namespaces );
  545. $count = count( $namespaces );
  546. $info[] = $context->msg( 'api-help-param-list' )
  547. ->params( $multi ? 2 : 1 )
  548. ->params( $context->getLanguage()->commaList( $namespaces ) )
  549. ->parse();
  550. $hintPipeSeparated = false;
  551. // No type message necessary, we have a list of values.
  552. $type = null;
  553. break;
  554. case 'tags':
  555. $tags = ChangeTags::listExplicitlyDefinedTags();
  556. $count = count( $tags );
  557. $info[] = $context->msg( 'api-help-param-list' )
  558. ->params( $multi ? 2 : 1 )
  559. ->params( $context->getLanguage()->commaList( $tags ) )
  560. ->parse();
  561. $hintPipeSeparated = false;
  562. $type = null;
  563. break;
  564. case 'limit':
  565. if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
  566. $info[] = $context->msg( 'api-help-param-limit2' )
  567. ->numParams( $settings[ApiBase::PARAM_MAX] )
  568. ->numParams( $settings[ApiBase::PARAM_MAX2] )
  569. ->parse();
  570. } else {
  571. $info[] = $context->msg( 'api-help-param-limit' )
  572. ->numParams( $settings[ApiBase::PARAM_MAX] )
  573. ->parse();
  574. }
  575. break;
  576. case 'integer':
  577. // Possible messages:
  578. // api-help-param-integer-min,
  579. // api-help-param-integer-max,
  580. // api-help-param-integer-minmax
  581. $suffix = '';
  582. $min = $max = 0;
  583. if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
  584. $suffix .= 'min';
  585. $min = $settings[ApiBase::PARAM_MIN];
  586. }
  587. if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
  588. $suffix .= 'max';
  589. $max = $settings[ApiBase::PARAM_MAX];
  590. }
  591. if ( $suffix !== '' ) {
  592. $info[] =
  593. $context->msg( "api-help-param-integer-$suffix" )
  594. ->params( $multi ? 2 : 1 )
  595. ->numParams( $min, $max )
  596. ->parse();
  597. }
  598. break;
  599. case 'upload':
  600. $info[] = $context->msg( 'api-help-param-upload' )
  601. ->parse();
  602. // No type message necessary, api-help-param-upload should handle it.
  603. $type = null;
  604. break;
  605. case 'string':
  606. case 'text':
  607. // Displaying a type message here would be useless.
  608. $type = null;
  609. break;
  610. }
  611. }
  612. // Add type. Messages for grep: api-help-param-type-limit
  613. // api-help-param-type-integer api-help-param-type-boolean
  614. // api-help-param-type-timestamp api-help-param-type-user
  615. // api-help-param-type-password
  616. if ( is_string( $type ) ) {
  617. $msg = $context->msg( "api-help-param-type-$type" );
  618. if ( !$msg->isDisabled() ) {
  619. $info[] = $msg->params( $multi ? 2 : 1 )->parse();
  620. }
  621. }
  622. if ( $multi ) {
  623. $extra = [];
  624. $lowcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT1] )
  625. ? $settings[ApiBase::PARAM_ISMULTI_LIMIT1]
  626. : ApiBase::LIMIT_SML1;
  627. $highcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
  628. ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2]
  629. : ApiBase::LIMIT_SML2;
  630. if ( $hintPipeSeparated ) {
  631. $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
  632. }
  633. if ( $count > $lowcount ) {
  634. if ( $lowcount === $highcount ) {
  635. $msg = $context->msg( 'api-help-param-multi-max-simple' )
  636. ->numParams( $lowcount );
  637. } else {
  638. $msg = $context->msg( 'api-help-param-multi-max' )
  639. ->numParams( $lowcount, $highcount );
  640. }
  641. $extra[] = $msg->parse();
  642. }
  643. if ( $extra ) {
  644. $info[] = implode( ' ', $extra );
  645. }
  646. $allowAll = isset( $settings[ApiBase::PARAM_ALL] )
  647. ? $settings[ApiBase::PARAM_ALL]
  648. : false;
  649. if ( $allowAll || $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
  650. if ( $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
  651. $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
  652. } else {
  653. $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
  654. }
  655. $info[] = $context->msg( 'api-help-param-multi-all' )
  656. ->params( $allSpecifier )
  657. ->parse();
  658. }
  659. }
  660. }
  661. // Add default
  662. $default = isset( $settings[ApiBase::PARAM_DFLT] )
  663. ? $settings[ApiBase::PARAM_DFLT]
  664. : null;
  665. if ( $default === '' ) {
  666. $info[] = $context->msg( 'api-help-param-default-empty' )
  667. ->parse();
  668. } elseif ( $default !== null && $default !== false ) {
  669. // We can't know whether this contains LTR or RTL text.
  670. $info[] = $context->msg( 'api-help-param-default' )
  671. ->params( Html::element( 'span', [ 'dir' => 'auto' ], $default ) )
  672. ->parse();
  673. }
  674. if ( !array_filter( $description ) ) {
  675. $description = [ self::wrap(
  676. $context->msg( 'api-help-param-no-description' ),
  677. 'apihelp-empty'
  678. ) ];
  679. }
  680. // Add "deprecated" flag
  681. if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
  682. $help['parameters'] .= Html::openElement( 'dd',
  683. [ 'class' => 'info' ] );
  684. $help['parameters'] .= self::wrap(
  685. $context->msg( 'api-help-param-deprecated' ),
  686. 'apihelp-deprecated', 'strong'
  687. );
  688. $help['parameters'] .= Html::closeElement( 'dd' );
  689. }
  690. if ( $description ) {
  691. $description = implode( '', $description );
  692. $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
  693. $help['parameters'] .= Html::rawElement( 'dd',
  694. [ 'class' => 'description' ], $description );
  695. }
  696. foreach ( $info as $i ) {
  697. $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
  698. }
  699. }
  700. if ( $dynamicParams !== null ) {
  701. $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
  702. $module->getModulePrefix(),
  703. $module->getModuleName(),
  704. $module->getModulePath()
  705. ] );
  706. $help['parameters'] .= Html::element( 'dt', null, '*' );
  707. $help['parameters'] .= Html::rawElement( 'dd',
  708. [ 'class' => 'description' ], $dynamicParams->parse() );
  709. }
  710. $help['parameters'] .= Html::closeElement( 'dl' );
  711. $help['parameters'] .= Html::closeElement( 'div' );
  712. }
  713. $examples = $module->getExamplesMessages();
  714. if ( $examples ) {
  715. $help['examples'] .= Html::openElement( 'div',
  716. [ 'class' => 'apihelp-block apihelp-examples' ] );
  717. $msg = $context->msg( 'api-help-examples' );
  718. if ( !$msg->isDisabled() ) {
  719. $help['examples'] .= self::wrap(
  720. $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
  721. );
  722. }
  723. $help['examples'] .= Html::openElement( 'dl' );
  724. foreach ( $examples as $qs => $msg ) {
  725. $msg = ApiBase::makeMessage( $msg, $context, [
  726. $module->getModulePrefix(),
  727. $module->getModuleName(),
  728. $module->getModulePath()
  729. ] );
  730. $link = wfAppendQuery( wfScript( 'api' ), $qs );
  731. $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
  732. $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
  733. $help['examples'] .= Html::rawElement( 'dd', null,
  734. Html::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
  735. Html::rawElement( 'a', [ 'href' => $sandbox ],
  736. $context->msg( 'api-help-open-in-apisandbox' )->parse() )
  737. );
  738. }
  739. $help['examples'] .= Html::closeElement( 'dl' );
  740. $help['examples'] .= Html::closeElement( 'div' );
  741. }
  742. $subtocnumber = $tocnumber;
  743. $subtocnumber[$level + 1] = 0;
  744. $suboptions = [
  745. 'submodules' => $options['recursivesubmodules'],
  746. 'headerlevel' => $level + 1,
  747. 'tocnumber' => &$subtocnumber,
  748. 'noheader' => false,
  749. ] + $options;
  750. if ( $options['submodules'] && $module->getModuleManager() ) {
  751. $manager = $module->getModuleManager();
  752. $submodules = [];
  753. foreach ( $groups as $group ) {
  754. $names = $manager->getNames( $group );
  755. sort( $names );
  756. foreach ( $names as $name ) {
  757. $submodules[] = $manager->getModule( $name );
  758. }
  759. }
  760. $help['submodules'] .= self::getHelpInternal(
  761. $context,
  762. $submodules,
  763. $suboptions,
  764. $haveModules
  765. );
  766. }
  767. $module->modifyHelp( $help, $suboptions, $haveModules );
  768. Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
  769. $out .= implode( "\n", $help );
  770. }
  771. return $out;
  772. }
  773. public function shouldCheckMaxlag() {
  774. return false;
  775. }
  776. public function isReadMode() {
  777. return false;
  778. }
  779. public function getCustomPrinter() {
  780. $params = $this->extractRequestParams();
  781. if ( $params['wrap'] ) {
  782. return null;
  783. }
  784. $main = $this->getMain();
  785. $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
  786. return new ApiFormatRaw( $main, $errorPrinter );
  787. }
  788. public function getAllowedParams() {
  789. return [
  790. 'modules' => [
  791. ApiBase::PARAM_DFLT => 'main',
  792. ApiBase::PARAM_ISMULTI => true,
  793. ],
  794. 'submodules' => false,
  795. 'recursivesubmodules' => false,
  796. 'wrap' => false,
  797. 'toc' => false,
  798. ];
  799. }
  800. protected function getExamplesMessages() {
  801. return [
  802. 'action=help'
  803. => 'apihelp-help-example-main',
  804. 'action=help&modules=query&submodules=1'
  805. => 'apihelp-help-example-submodules',
  806. 'action=help&recursivesubmodules=1'
  807. => 'apihelp-help-example-recursive',
  808. 'action=help&modules=help'
  809. => 'apihelp-help-example-help',
  810. 'action=help&modules=query+info|query+categorymembers'
  811. => 'apihelp-help-example-query',
  812. ];
  813. }
  814. public function getHelpUrls() {
  815. return [
  816. 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
  817. 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
  818. 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',
  819. ];
  820. }
  821. }