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