3 * Copyright © 2014 Wikimedia Foundation and contributors
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.
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.
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
23 use HtmlFormatter\HtmlFormatter
;
24 use MediaWiki\MediaWikiServices
;
27 * Class to output help for an API module
29 * @since 1.25 completely rewritten
32 class ApiHelp
extends ApiBase
{
33 public function execute() {
34 $params = $this->extractRequestParams();
37 foreach ( $params['modules'] as $path ) {
38 $modules[] = $this->getModuleFromPath( $path );
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 );
50 self
::getHelp( $context, $modules, $params );
52 // Grab the output from the skin
54 $context->getOutput()->output();
55 $html = ob_get_clean();
57 $result = $this->getResult();
58 if ( $params['wrap'] ) {
60 'mime' => 'text/html',
61 'filename' => 'api-help.html',
64 ApiResult
::setSubelementsList( $data, 'help' );
65 $result->addValue( null, $this->getModuleName(), $data );
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
);
75 * Generate help for the specified modules
77 * Help is placed into the OutputPage object returned by
78 * $context->getOutput().
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
89 * @param IContextSource $context
90 * @param ApiBase[]|ApiBase $modules
91 * @param array $options Formatting options (described above)
93 public static function getHelp( IContextSource
$context, $modules, array $options ) {
96 if ( !is_array( $modules ) ) {
97 $modules = [ $modules ];
100 $out = $context->getOutput();
101 $out->addModuleStyles( [
105 if ( !empty( $options['toc'] ) ) {
106 $out->addModules( 'mediawiki.toc' );
108 $out->setPageTitle( $context->msg( 'api-help-title' ) );
110 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
112 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain
&&
113 $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
115 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
116 if ( $cacheHelpTimeout > 0 ) {
117 // Get help text from cache if present
118 $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
119 (int)!empty( $options['toc'] ),
120 str_replace( ' ', '_', SpecialVersion
::getVersion( 'nodb' ) ) );
121 $cached = $cache->get( $cacheKey );
123 $out->addHTML( $cached );
128 if ( $out->getHTML() !== '' ) {
129 // Don't save to cache, there's someone else's content in the page
134 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
135 $options['submodules'] = $options['recursivesubmodules'] ||
!empty( $options['submodules'] );
138 if ( empty( $options['nolead'] ) ) {
139 $msg = $context->msg( 'api-help-lead' );
140 if ( !$msg->isDisabled() ) {
141 $out->addHTML( $msg->parseAsBlock() );
146 $html = self
::getHelpInternal( $context, $modules, $options, $haveModules );
147 if ( !empty( $options['toc'] ) && $haveModules ) {
148 $out->addHTML( Linker
::generateTOC( $haveModules, $context->getLanguage() ) );
150 $out->addHTML( $html );
152 $helptitle = $options['helptitle'] ??
null;
153 $html = self
::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
155 $out->addHTML( $html );
157 if ( $cacheKey !== null ) {
158 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
163 * Replace Special:ApiHelp links with links to api.php
165 * @param string $html
166 * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
167 * @param array $localModules Keys are modules to link within the current page, values are ignored
170 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
171 $formatter = new HtmlFormatter( $html );
172 $doc = $formatter->getDoc();
173 $xpath = new DOMXPath( $doc );
174 $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
175 foreach ( $nodes as $node ) {
176 $href = $node->getAttribute( 'href' );
179 $href = rawurldecode( $href );
180 } while ( $old !== $href );
181 if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
182 if ( isset( $localModules[$m[1]] ) ) {
183 $href = $m[2] === '' ?
'#' . $m[1] : $m[2];
184 } elseif ( $helptitle !== null ) {
185 $href = Title
::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
188 $href = wfAppendQuery( wfScript( 'api' ), [
193 $node->setAttribute( 'href', $href );
194 $node->removeAttribute( 'title' );
198 return $formatter->getText();
202 * Wrap a message in HTML with a class.
204 * @param Message $msg
205 * @param string $class
209 private static function wrap( Message
$msg, $class, $tag = 'span' ) {
210 return Html
::rawElement( $tag, [ 'class' => $class ],
216 * Recursively-called function to actually construct the help
218 * @param IContextSource $context
219 * @param ApiBase[] $modules
220 * @param array $options
221 * @param array &$haveModules
224 private static function getHelpInternal( IContextSource
$context, array $modules,
225 array $options, &$haveModules
229 $level = empty( $options['headerlevel'] ) ?
2 : $options['headerlevel'];
230 if ( empty( $options['tocnumber'] ) ) {
231 $tocnumber = [ 2 => 0 ];
233 $tocnumber = &$options['tocnumber'];
236 foreach ( $modules as $module ) {
237 $tocnumber[$level]++
;
238 $path = $module->getModulePath();
239 $module->setContext( $context );
250 if ( empty( $options['noheader'] ) ||
!empty( $options['toc'] ) ) {
253 while ( isset( $haveModules[$anchor] ) ) {
254 $anchor = $path . '|' . ++
$i;
257 if ( $module->isMain() ) {
258 $headerContent = $context->msg( 'api-help-main-header' )->parse();
260 'class' => 'apihelp-header',
263 $name = $module->getModuleName();
264 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
266 if ( $module->getModulePrefix() !== '' ) {
267 $headerContent .= ' ' .
268 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
270 // Module names are always in English and not localized,
271 // so English language and direction must be set explicitly,
272 // otherwise parentheses will get broken in RTL wikis
274 'class' => 'apihelp-header apihelp-module-name',
280 $headerAttr['id'] = $anchor;
282 $haveModules[$anchor] = [
283 'toclevel' => count( $tocnumber ),
286 'line' => $headerContent,
287 'number' => implode( '.', $tocnumber ),
290 if ( empty( $options['noheader'] ) ) {
291 $help['header'] .= Html
::element(
292 'h' . min( 6, $level ),
298 $haveModules[$path] = true;
303 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
304 $name = $m->getModuleName();
305 if ( $name === 'main_int' ) {
309 if ( count( $modules ) === 1 && $m === $modules[0] &&
310 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
312 $link = Html
::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
314 $link = SpecialPage
::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
315 $link = Html
::element( 'a',
316 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
321 array_unshift( $links, $link );
324 $help['header'] .= self
::wrap(
325 $context->msg( 'parentheses' )
326 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
327 'apihelp-linktrail', 'div'
331 $flags = $module->getHelpFlags();
332 $help['flags'] .= Html
::openElement( 'div',
333 [ 'class' => 'apihelp-block apihelp-flags' ] );
334 $msg = $context->msg( 'api-help-flags' );
335 if ( !$msg->isDisabled() ) {
336 $help['flags'] .= self
::wrap(
337 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
340 $help['flags'] .= Html
::openElement( 'ul' );
341 foreach ( $flags as $flag ) {
342 $help['flags'] .= Html
::rawElement( 'li', null,
343 self
::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
346 $sourceInfo = $module->getModuleSourceInfo();
348 if ( isset( $sourceInfo['namemsg'] ) ) {
349 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
351 // Probably English, so wrap it.
352 $extname = Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
354 $help['flags'] .= Html
::rawElement( 'li', null,
356 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
361 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
362 if ( isset( $sourceInfo['license-name'] ) ) {
363 $msg = $context->msg( 'api-help-license', $link,
364 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
366 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
367 $msg = $context->msg( 'api-help-license-noname', $link );
369 $msg = $context->msg( 'api-help-license-unknown' );
371 $help['flags'] .= Html
::rawElement( 'li', null,
372 self
::wrap( $msg, 'apihelp-license' )
375 $help['flags'] .= Html
::rawElement( 'li', null,
376 self
::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
378 $help['flags'] .= Html
::rawElement( 'li', null,
379 self
::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
382 $help['flags'] .= Html
::closeElement( 'ul' );
383 $help['flags'] .= Html
::closeElement( 'div' );
385 foreach ( $module->getFinalDescription() as $msg ) {
386 $msg->setContext( $context );
387 $help['description'] .= $msg->parseAsBlock();
390 $urls = $module->getHelpUrls();
392 $help['help-urls'] .= Html
::openElement( 'div',
393 [ 'class' => 'apihelp-block apihelp-help-urls' ]
395 $msg = $context->msg( 'api-help-help-urls' );
396 if ( !$msg->isDisabled() ) {
397 $help['help-urls'] .= self
::wrap(
398 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
401 if ( !is_array( $urls ) ) {
404 $help['help-urls'] .= Html
::openElement( 'ul' );
405 foreach ( $urls as $url ) {
406 $help['help-urls'] .= Html
::rawElement( 'li', null,
407 Html
::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
410 $help['help-urls'] .= Html
::closeElement( 'ul' );
411 $help['help-urls'] .= Html
::closeElement( 'div' );
414 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
415 $dynamicParams = $module->dynamicParameterDocumentation();
417 if ( $params ||
$dynamicParams !== null ) {
418 $help['parameters'] .= Html
::openElement( 'div',
419 [ 'class' => 'apihelp-block apihelp-parameters' ]
421 $msg = $context->msg( 'api-help-parameters' );
422 if ( !$msg->isDisabled() ) {
423 $help['parameters'] .= self
::wrap(
424 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
427 $help['parameters'] .= Html
::openElement( 'dl' );
429 $descriptions = $module->getFinalParamDescription();
431 foreach ( $params as $name => $settings ) {
432 if ( !is_array( $settings ) ) {
433 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
436 $help['parameters'] .= Html
::rawElement( 'dt', null,
437 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
442 if ( isset( $descriptions[$name] ) ) {
443 foreach ( $descriptions[$name] as $msg ) {
444 $msg->setContext( $context );
445 $description[] = $msg->parseAsBlock();
453 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
454 $info[] = $context->msg( 'api-help-param-required' )->parse();
458 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
459 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
460 $tag = array_shift( $i );
461 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
462 ->numParams( count( $i ) )
463 ->params( $context->getLanguage()->commaList( $i ) )
464 ->params( $module->getModulePrefix() )
470 if ( !empty( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] ) ) {
472 $msg = 'api-help-param-templated-var-first';
473 foreach ( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] as $k => $v ) {
474 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
475 $msg = 'api-help-param-templated-var';
477 $info[] = $context->msg( 'api-help-param-templated' )
478 ->numParams( count( $vars ) )
479 ->params( Message
::listParam( $vars ) )
483 // Type documentation
484 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
485 $dflt = $settings[ApiBase
::PARAM_DFLT
] ??
null;
486 if ( is_bool( $dflt ) ) {
487 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
488 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
489 $settings[ApiBase
::PARAM_TYPE
] = 'string';
490 } elseif ( is_int( $dflt ) ) {
491 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
494 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
495 $type = $settings[ApiBase
::PARAM_TYPE
];
496 $multi = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
497 $hintPipeSeparated = true;
498 $count = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
499 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] +
1
500 : ApiBase
::LIMIT_SML2 +
1;
502 if ( is_array( $type ) ) {
503 $count = count( $type );
504 $deprecatedValues = $settings[ApiBase
::PARAM_DEPRECATED_VALUES
] ??
[];
505 $links = $settings[ApiBase
::PARAM_VALUE_LINKS
] ??
[];
506 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
509 // We can't know whether this contains LTR or RTL text.
510 $attr['dir'] = 'auto';
512 if ( isset( $deprecatedValues[$v] ) ) {
513 $attr['class'] = 'apihelp-deprecated-value';
515 $ret = $attr ? Html
::element( 'span', $attr, $v ) : $v;
516 if ( isset( $links[$v] ) ) {
517 $ret = "[[{$links[$v]}|$ret]]";
521 $i = array_search( '', $type, true );
522 if ( $i === false ) {
523 $values = $context->getLanguage()->commaList( $values );
525 unset( $values[$i] );
526 $values = $context->msg( 'api-help-param-list-can-be-empty' )
527 ->numParams( count( $values ) )
528 ->params( $context->getLanguage()->commaList( $values ) )
531 $info[] = $context->msg( 'api-help-param-list' )
532 ->params( $multi ?
2 : 1 )
535 $hintPipeSeparated = false;
541 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
542 $map = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
545 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() . '+' );
547 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
548 $map[$submoduleName] = $prefix . $submoduleName;
550 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
555 $deprecatedSubmodules = [];
556 foreach ( $map as $v => $m ) {
557 $attrs = $defaultAttrs;
560 $submod = $module->getModuleFromPath( $m );
562 if ( $submod->isDeprecated() ) {
563 $arr = &$deprecatedSubmodules;
564 $attrs['class'] = 'apihelp-deprecated-value';
567 } catch ( ApiUsageException
$ex ) {
571 $v = Html
::element( 'span', $attrs, $v );
573 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
575 $submodules = array_merge( $submodules, $deprecatedSubmodules );
576 $count = count( $submodules );
577 $info[] = $context->msg( 'api-help-param-list' )
578 ->params( $multi ?
2 : 1 )
579 ->params( $context->getLanguage()->commaList( $submodules ) )
581 $hintPipeSeparated = false;
582 // No type message necessary, we have a list of values.
587 $namespaces = MWNamespace
::getValidNamespaces();
588 if ( isset( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] ) &&
589 is_array( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] )
591 $namespaces = array_merge( $namespaces, $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] );
594 $count = count( $namespaces );
595 $info[] = $context->msg( 'api-help-param-list' )
596 ->params( $multi ?
2 : 1 )
597 ->params( $context->getLanguage()->commaList( $namespaces ) )
599 $hintPipeSeparated = false;
600 // No type message necessary, we have a list of values.
605 $tags = ChangeTags
::listExplicitlyDefinedTags();
606 $count = count( $tags );
607 $info[] = $context->msg( 'api-help-param-list' )
608 ->params( $multi ?
2 : 1 )
609 ->params( $context->getLanguage()->commaList( $tags ) )
611 $hintPipeSeparated = false;
616 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
617 $info[] = $context->msg( 'api-help-param-limit2' )
618 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
619 ->numParams( $settings[ApiBase
::PARAM_MAX2
] )
622 $info[] = $context->msg( 'api-help-param-limit' )
623 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
629 // Possible messages:
630 // api-help-param-integer-min,
631 // api-help-param-integer-max,
632 // api-help-param-integer-minmax
635 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
637 $min = $settings[ApiBase
::PARAM_MIN
];
639 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
641 $max = $settings[ApiBase
::PARAM_MAX
];
643 if ( $suffix !== '' ) {
645 $context->msg( "api-help-param-integer-$suffix" )
646 ->params( $multi ?
2 : 1 )
647 ->numParams( $min, $max )
653 $info[] = $context->msg( 'api-help-param-upload' )
655 // No type message necessary, api-help-param-upload should handle it.
661 // Displaying a type message here would be useless.
667 // Add type. Messages for grep: api-help-param-type-limit
668 // api-help-param-type-integer api-help-param-type-boolean
669 // api-help-param-type-timestamp api-help-param-type-user
670 // api-help-param-type-password
671 if ( is_string( $type ) ) {
672 $msg = $context->msg( "api-help-param-type-$type" );
673 if ( !$msg->isDisabled() ) {
674 $info[] = $msg->params( $multi ?
2 : 1 )->parse();
680 $lowcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
681 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT1
]
682 : ApiBase
::LIMIT_SML1
;
683 $highcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
684 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
]
685 : ApiBase
::LIMIT_SML2
;
687 if ( $hintPipeSeparated ) {
688 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
690 if ( $count > $lowcount ) {
691 if ( $lowcount === $highcount ) {
692 $msg = $context->msg( 'api-help-param-multi-max-simple' )
693 ->numParams( $lowcount );
695 $msg = $context->msg( 'api-help-param-multi-max' )
696 ->numParams( $lowcount, $highcount );
698 $extra[] = $msg->parse();
701 $info[] = implode( ' ', $extra );
704 $allowAll = $settings[ApiBase
::PARAM_ALL
] ??
false;
705 if ( $allowAll ||
$settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
706 if ( $settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
707 $allSpecifier = ApiBase
::ALL_DEFAULT_STRING
;
709 $allSpecifier = ( is_string( $allowAll ) ?
$allowAll : ApiBase
::ALL_DEFAULT_STRING
);
711 $info[] = $context->msg( 'api-help-param-multi-all' )
712 ->params( $allSpecifier )
718 if ( isset( $settings[self
::PARAM_MAX_BYTES
] ) ) {
719 $info[] = $context->msg( 'api-help-param-maxbytes' )
720 ->numParams( $settings[self
::PARAM_MAX_BYTES
] );
722 if ( isset( $settings[self
::PARAM_MAX_CHARS
] ) ) {
723 $info[] = $context->msg( 'api-help-param-maxchars' )
724 ->numParams( $settings[self
::PARAM_MAX_CHARS
] );
728 $default = $settings[ApiBase
::PARAM_DFLT
] ??
null;
729 if ( $default === '' ) {
730 $info[] = $context->msg( 'api-help-param-default-empty' )
732 } elseif ( $default !== null && $default !== false ) {
733 // We can't know whether this contains LTR or RTL text.
734 $info[] = $context->msg( 'api-help-param-default' )
735 ->params( Html
::element( 'span', [ 'dir' => 'auto' ], $default ) )
739 if ( !array_filter( $description ) ) {
740 $description = [ self
::wrap(
741 $context->msg( 'api-help-param-no-description' ),
746 // Add "deprecated" flag
747 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
748 $help['parameters'] .= Html
::openElement( 'dd',
749 [ 'class' => 'info' ] );
750 $help['parameters'] .= self
::wrap(
751 $context->msg( 'api-help-param-deprecated' ),
752 'apihelp-deprecated', 'strong'
754 $help['parameters'] .= Html
::closeElement( 'dd' );
757 if ( $description ) {
758 $description = implode( '', $description );
759 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
760 $help['parameters'] .= Html
::rawElement( 'dd',
761 [ 'class' => 'description' ], $description );
764 foreach ( $info as $i ) {
765 $help['parameters'] .= Html
::rawElement( 'dd', [ 'class' => 'info' ], $i );
769 if ( $dynamicParams !== null ) {
770 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $context, [
771 $module->getModulePrefix(),
772 $module->getModuleName(),
773 $module->getModulePath()
775 $help['parameters'] .= Html
::element( 'dt', null, '*' );
776 $help['parameters'] .= Html
::rawElement( 'dd',
777 [ 'class' => 'description' ], $dynamicParams->parse() );
780 $help['parameters'] .= Html
::closeElement( 'dl' );
781 $help['parameters'] .= Html
::closeElement( 'div' );
784 $examples = $module->getExamplesMessages();
786 $help['examples'] .= Html
::openElement( 'div',
787 [ 'class' => 'apihelp-block apihelp-examples' ] );
788 $msg = $context->msg( 'api-help-examples' );
789 if ( !$msg->isDisabled() ) {
790 $help['examples'] .= self
::wrap(
791 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
795 $help['examples'] .= Html
::openElement( 'dl' );
796 foreach ( $examples as $qs => $msg ) {
797 $msg = ApiBase
::makeMessage( $msg, $context, [
798 $module->getModulePrefix(),
799 $module->getModuleName(),
800 $module->getModulePath()
803 $link = wfAppendQuery( wfScript( 'api' ), $qs );
804 $sandbox = SpecialPage
::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
805 $help['examples'] .= Html
::rawElement( 'dt', null, $msg->parse() );
806 $help['examples'] .= Html
::rawElement( 'dd', null,
807 Html
::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
808 Html
::rawElement( 'a', [ 'href' => $sandbox ],
809 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
812 $help['examples'] .= Html
::closeElement( 'dl' );
813 $help['examples'] .= Html
::closeElement( 'div' );
816 $subtocnumber = $tocnumber;
817 $subtocnumber[$level +
1] = 0;
819 'submodules' => $options['recursivesubmodules'],
820 'headerlevel' => $level +
1,
821 'tocnumber' => &$subtocnumber,
825 if ( $options['submodules'] && $module->getModuleManager() ) {
826 $manager = $module->getModuleManager();
828 foreach ( $groups as $group ) {
829 $names = $manager->getNames( $group );
831 foreach ( $names as $name ) {
832 $submodules[] = $manager->getModule( $name );
835 $help['submodules'] .= self
::getHelpInternal(
843 $module->modifyHelp( $help, $suboptions, $haveModules );
845 Hooks
::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
847 $out .= implode( "\n", $help );
853 public function shouldCheckMaxlag() {
857 public function isReadMode() {
861 public function getCustomPrinter() {
862 $params = $this->extractRequestParams();
863 if ( $params['wrap'] ) {
867 $main = $this->getMain();
868 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
869 return new ApiFormatRaw( $main, $errorPrinter );
872 public function getAllowedParams() {
875 ApiBase
::PARAM_DFLT
=> 'main',
876 ApiBase
::PARAM_ISMULTI
=> true,
878 'submodules' => false,
879 'recursivesubmodules' => false,
885 protected function getExamplesMessages() {
888 => 'apihelp-help-example-main',
889 'action=help&modules=query&submodules=1'
890 => 'apihelp-help-example-submodules',
891 'action=help&recursivesubmodules=1'
892 => 'apihelp-help-example-recursive',
893 'action=help&modules=help'
894 => 'apihelp-help-example-help',
895 'action=help&modules=query+info|query+categorymembers'
896 => 'apihelp-help-example-query',
900 public function getHelpUrls() {
902 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
903 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
904 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',