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->setRobotPolicy( 'noindex,nofollow' );
48 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
49 $context->setOutput( $out );
51 self
::getHelp( $context, $modules, $params );
53 // Grab the output from the skin
55 $context->getOutput()->output();
56 $html = ob_get_clean();
58 $result = $this->getResult();
59 if ( $params['wrap'] ) {
61 'mime' => 'text/html',
62 'filename' => 'api-help.html',
65 ApiResult
::setSubelementsList( $data, 'help' );
66 $result->addValue( null, $this->getModuleName(), $data );
69 $result->addValue( null, 'text', $html, ApiResult
::NO_SIZE_CHECK
);
70 $result->addValue( null, 'mime', 'text/html', ApiResult
::NO_SIZE_CHECK
);
71 $result->addValue( null, 'filename', 'api-help.html', ApiResult
::NO_SIZE_CHECK
);
76 * Generate help for the specified modules
78 * Help is placed into the OutputPage object returned by
79 * $context->getOutput().
81 * Recognized options include:
82 * - headerlevel: (int) Header tag level
83 * - nolead: (bool) Skip the inclusion of api-help-lead
84 * - noheader: (bool) Skip the inclusion of the top-level section headers
85 * - submodules: (bool) Include help for submodules of the current module
86 * - recursivesubmodules: (bool) Include help for submodules recursively
87 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
88 * - toc: (bool) Include a table of contents
90 * @param IContextSource $context
91 * @param ApiBase[]|ApiBase $modules
92 * @param array $options Formatting options (described above)
94 public static function getHelp( IContextSource
$context, $modules, array $options ) {
95 if ( !is_array( $modules ) ) {
96 $modules = [ $modules ];
99 $out = $context->getOutput();
100 $out->addModuleStyles( [
104 if ( !empty( $options['toc'] ) ) {
105 $out->addModuleStyles( 'mediawiki.toc.styles' );
107 $out->setPageTitle( $context->msg( 'api-help-title' ) );
109 $services = MediaWikiServices
::getInstance();
110 $cache = $services->getMainWANObjectCache();
112 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain
&&
113 $options['recursivesubmodules'] &&
114 $context->getLanguage()->equals( $services->getContentLanguage() )
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 );
124 $out->addHTML( $cached );
129 if ( $out->getHTML() !== '' ) {
130 // Don't save to cache, there's someone else's content in the page
135 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
136 $options['submodules'] = $options['recursivesubmodules'] ||
!empty( $options['submodules'] );
139 if ( empty( $options['nolead'] ) ) {
140 $msg = $context->msg( 'api-help-lead' );
141 if ( !$msg->isDisabled() ) {
142 $out->addHTML( $msg->parseAsBlock() );
147 $html = self
::getHelpInternal( $context, $modules, $options, $haveModules );
148 if ( !empty( $options['toc'] ) && $haveModules ) {
149 $out->addHTML( Linker
::generateTOC( $haveModules, $context->getLanguage() ) );
151 $out->addHTML( $html );
153 $helptitle = $options['helptitle'] ??
null;
154 $html = self
::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
156 $out->addHTML( $html );
158 if ( $cacheKey !== null ) {
159 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
164 * Replace Special:ApiHelp links with links to api.php
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
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' );
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] )
189 $href = wfAppendQuery( wfScript( 'api' ), [
194 $node->setAttribute( 'href', $href );
195 $node->removeAttribute( 'title' );
199 return $formatter->getText();
203 * Wrap a message in HTML with a class.
205 * @param Message $msg
206 * @param string $class
210 private static function wrap( Message
$msg, $class, $tag = 'span' ) {
211 return Html
::rawElement( $tag, [ 'class' => $class ],
217 * Recursively-called function to actually construct the help
219 * @param IContextSource $context
220 * @param ApiBase[] $modules
221 * @param array $options
222 * @param array &$haveModules
225 private static function getHelpInternal( IContextSource
$context, array $modules,
226 array $options, &$haveModules
230 $level = empty( $options['headerlevel'] ) ?
2 : $options['headerlevel'];
231 if ( empty( $options['tocnumber'] ) ) {
232 $tocnumber = [ 2 => 0 ];
234 $tocnumber = &$options['tocnumber'];
237 foreach ( $modules as $module ) {
238 $tocnumber[$level]++
;
239 $path = $module->getModulePath();
240 $module->setContext( $context );
251 if ( empty( $options['noheader'] ) ||
!empty( $options['toc'] ) ) {
254 while ( isset( $haveModules[$anchor] ) ) {
255 $anchor = $path . '|' . ++
$i;
258 if ( $module->isMain() ) {
259 $headerContent = $context->msg( 'api-help-main-header' )->parse();
261 'class' => 'apihelp-header',
264 $name = $module->getModuleName();
265 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
267 if ( $module->getModulePrefix() !== '' ) {
268 $headerContent .= ' ' .
269 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
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
275 'class' => 'apihelp-header apihelp-module-name',
281 $headerAttr['id'] = $anchor;
283 $haveModules[$anchor] = [
284 'toclevel' => count( $tocnumber ),
287 'line' => $headerContent,
288 'number' => implode( '.', $tocnumber ),
291 if ( empty( $options['noheader'] ) ) {
292 $help['header'] .= Html
::element(
293 'h' . min( 6, $level ),
299 $haveModules[$path] = true;
304 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
305 $name = $m->getModuleName();
306 if ( $name === 'main_int' ) {
310 if ( count( $modules ) === 1 && $m === $modules[0] &&
311 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
313 $link = Html
::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
315 $link = SpecialPage
::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
316 $link = Html
::element( 'a',
317 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
322 array_unshift( $links, $link );
325 $help['header'] .= self
::wrap(
326 $context->msg( 'parentheses' )
327 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
328 'apihelp-linktrail', 'div'
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'
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" )
347 $sourceInfo = $module->getModuleSourceInfo();
349 if ( isset( $sourceInfo['namemsg'] ) ) {
350 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
352 // Probably English, so wrap it.
353 $extname = Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
355 $help['flags'] .= Html
::rawElement( 'li', null,
357 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
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'] )
367 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
368 $msg = $context->msg( 'api-help-license-noname', $link );
370 $msg = $context->msg( 'api-help-license-unknown' );
372 $help['flags'] .= Html
::rawElement( 'li', null,
373 self
::wrap( $msg, 'apihelp-license' )
376 $help['flags'] .= Html
::rawElement( 'li', null,
377 self
::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
379 $help['flags'] .= Html
::rawElement( 'li', null,
380 self
::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
383 $help['flags'] .= Html
::closeElement( 'ul' );
384 $help['flags'] .= Html
::closeElement( 'div' );
386 foreach ( $module->getFinalDescription() as $msg ) {
387 $msg->setContext( $context );
388 $help['description'] .= $msg->parseAsBlock();
391 $urls = $module->getHelpUrls();
393 $help['help-urls'] .= Html
::openElement( 'div',
394 [ 'class' => 'apihelp-block apihelp-help-urls' ]
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'
402 if ( !is_array( $urls ) ) {
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 )
411 $help['help-urls'] .= Html
::closeElement( 'ul' );
412 $help['help-urls'] .= Html
::closeElement( 'div' );
415 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
416 $dynamicParams = $module->dynamicParameterDocumentation();
418 if ( $params ||
$dynamicParams !== null ) {
419 $help['parameters'] .= Html
::openElement( 'div',
420 [ 'class' => 'apihelp-block apihelp-parameters' ]
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'
428 $help['parameters'] .= Html
::openElement( 'dl' );
430 $descriptions = $module->getFinalParamDescription();
432 foreach ( $params as $name => $settings ) {
433 if ( !is_array( $settings ) ) {
434 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
437 $help['parameters'] .= Html
::rawElement( 'dt', null,
438 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
443 if ( isset( $descriptions[$name] ) ) {
444 foreach ( $descriptions[$name] as $msg ) {
445 $msg->setContext( $context );
446 $description[] = $msg->parseAsBlock();
454 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
455 $info[] = $context->msg( 'api-help-param-required' )->parse();
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() )
471 if ( !empty( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] ) ) {
473 $msg = 'api-help-param-templated-var-first';
474 foreach ( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] as $k => $v ) {
475 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
476 $msg = 'api-help-param-templated-var';
478 $info[] = $context->msg( 'api-help-param-templated' )
479 ->numParams( count( $vars ) )
480 ->params( Message
::listParam( $vars ) )
484 // Type documentation
485 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
486 $dflt = $settings[ApiBase
::PARAM_DFLT
] ??
null;
487 if ( is_bool( $dflt ) ) {
488 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
489 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
490 $settings[ApiBase
::PARAM_TYPE
] = 'string';
491 } elseif ( is_int( $dflt ) ) {
492 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
495 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
496 $type = $settings[ApiBase
::PARAM_TYPE
];
497 $multi = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
498 $hintPipeSeparated = true;
499 $count = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
500 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] +
1
501 : ApiBase
::LIMIT_SML2 +
1;
503 if ( is_array( $type ) ) {
504 $count = count( $type );
505 $deprecatedValues = $settings[ApiBase
::PARAM_DEPRECATED_VALUES
] ??
[];
506 $links = $settings[ApiBase
::PARAM_VALUE_LINKS
] ??
[];
507 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
510 // We can't know whether this contains LTR or RTL text.
511 $attr['dir'] = 'auto';
513 if ( isset( $deprecatedValues[$v] ) ) {
514 $attr['class'] = 'apihelp-deprecated-value';
516 $ret = $attr ? Html
::element( 'span', $attr, $v ) : $v;
517 if ( isset( $links[$v] ) ) {
518 $ret = "[[{$links[$v]}|$ret]]";
522 $i = array_search( '', $type, true );
523 if ( $i === false ) {
524 $values = $context->getLanguage()->commaList( $values );
526 unset( $values[$i] );
527 $values = $context->msg( 'api-help-param-list-can-be-empty' )
528 ->numParams( count( $values ) )
529 ->params( $context->getLanguage()->commaList( $values ) )
532 $info[] = $context->msg( 'api-help-param-list' )
533 ->params( $multi ?
2 : 1 )
536 $hintPipeSeparated = false;
542 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
543 $map = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
546 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() . '+' );
548 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
549 $map[$submoduleName] = $prefix . $submoduleName;
551 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
556 $deprecatedSubmodules = [];
557 foreach ( $map as $v => $m ) {
558 $attrs = $defaultAttrs;
561 $submod = $module->getModuleFromPath( $m );
562 if ( $submod && $submod->isDeprecated() ) {
563 $arr = &$deprecatedSubmodules;
564 $attrs['class'] = 'apihelp-deprecated-value';
566 } catch ( ApiUsageException
$ex ) {
570 $v = Html
::element( 'span', $attrs, $v );
572 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
574 $submodules = array_merge( $submodules, $deprecatedSubmodules );
575 $count = count( $submodules );
576 $info[] = $context->msg( 'api-help-param-list' )
577 ->params( $multi ?
2 : 1 )
578 ->params( $context->getLanguage()->commaList( $submodules ) )
580 $hintPipeSeparated = false;
581 // No type message necessary, we have a list of values.
586 $namespaces = MWNamespace
::getValidNamespaces();
587 if ( isset( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] ) &&
588 is_array( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] )
590 $namespaces = array_merge( $namespaces, $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] );
593 $count = count( $namespaces );
594 $info[] = $context->msg( 'api-help-param-list' )
595 ->params( $multi ?
2 : 1 )
596 ->params( $context->getLanguage()->commaList( $namespaces ) )
598 $hintPipeSeparated = false;
599 // No type message necessary, we have a list of values.
604 $tags = ChangeTags
::listExplicitlyDefinedTags();
605 $count = count( $tags );
606 $info[] = $context->msg( 'api-help-param-list' )
607 ->params( $multi ?
2 : 1 )
608 ->params( $context->getLanguage()->commaList( $tags ) )
610 $hintPipeSeparated = false;
615 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
616 $info[] = $context->msg( 'api-help-param-limit2' )
617 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
618 ->numParams( $settings[ApiBase
::PARAM_MAX2
] )
621 $info[] = $context->msg( 'api-help-param-limit' )
622 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
628 // Possible messages:
629 // api-help-param-integer-min,
630 // api-help-param-integer-max,
631 // api-help-param-integer-minmax
634 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
636 $min = $settings[ApiBase
::PARAM_MIN
];
638 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
640 $max = $settings[ApiBase
::PARAM_MAX
];
642 if ( $suffix !== '' ) {
644 $context->msg( "api-help-param-integer-$suffix" )
645 ->params( $multi ?
2 : 1 )
646 ->numParams( $min, $max )
652 $info[] = $context->msg( 'api-help-param-upload' )
654 // No type message necessary, api-help-param-upload should handle it.
660 // Displaying a type message here would be useless.
666 // Add type. Messages for grep: api-help-param-type-limit
667 // api-help-param-type-integer api-help-param-type-boolean
668 // api-help-param-type-timestamp api-help-param-type-user
669 // api-help-param-type-password
670 if ( is_string( $type ) ) {
671 $msg = $context->msg( "api-help-param-type-$type" );
672 if ( !$msg->isDisabled() ) {
673 $info[] = $msg->params( $multi ?
2 : 1 )->parse();
679 $lowcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
680 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT1
]
681 : ApiBase
::LIMIT_SML1
;
682 $highcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
683 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
]
684 : ApiBase
::LIMIT_SML2
;
686 if ( $hintPipeSeparated ) {
687 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
689 if ( $count > $lowcount ) {
690 if ( $lowcount === $highcount ) {
691 $msg = $context->msg( 'api-help-param-multi-max-simple' )
692 ->numParams( $lowcount );
694 $msg = $context->msg( 'api-help-param-multi-max' )
695 ->numParams( $lowcount, $highcount );
697 $extra[] = $msg->parse();
700 $info[] = implode( ' ', $extra );
703 $allowAll = $settings[ApiBase
::PARAM_ALL
] ??
false;
704 if ( $allowAll ||
$settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
705 if ( $settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
706 $allSpecifier = ApiBase
::ALL_DEFAULT_STRING
;
708 $allSpecifier = ( is_string( $allowAll ) ?
$allowAll : ApiBase
::ALL_DEFAULT_STRING
);
710 $info[] = $context->msg( 'api-help-param-multi-all' )
711 ->params( $allSpecifier )
717 if ( isset( $settings[self
::PARAM_MAX_BYTES
] ) ) {
718 $info[] = $context->msg( 'api-help-param-maxbytes' )
719 ->numParams( $settings[self
::PARAM_MAX_BYTES
] );
721 if ( isset( $settings[self
::PARAM_MAX_CHARS
] ) ) {
722 $info[] = $context->msg( 'api-help-param-maxchars' )
723 ->numParams( $settings[self
::PARAM_MAX_CHARS
] );
727 $default = $settings[ApiBase
::PARAM_DFLT
] ??
null;
728 if ( $default === '' ) {
729 $info[] = $context->msg( 'api-help-param-default-empty' )
731 } elseif ( $default !== null && $default !== false ) {
732 // We can't know whether this contains LTR or RTL text.
733 $info[] = $context->msg( 'api-help-param-default' )
734 ->params( Html
::element( 'span', [ 'dir' => 'auto' ], $default ) )
738 if ( !array_filter( $description ) ) {
739 $description = [ self
::wrap(
740 $context->msg( 'api-help-param-no-description' ),
745 // Add "deprecated" flag
746 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
747 $help['parameters'] .= Html
::openElement( 'dd',
748 [ 'class' => 'info' ] );
749 $help['parameters'] .= self
::wrap(
750 $context->msg( 'api-help-param-deprecated' ),
751 'apihelp-deprecated', 'strong'
753 $help['parameters'] .= Html
::closeElement( 'dd' );
756 if ( $description ) {
757 $description = implode( '', $description );
758 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
759 $help['parameters'] .= Html
::rawElement( 'dd',
760 [ 'class' => 'description' ], $description );
763 foreach ( $info as $i ) {
764 $help['parameters'] .= Html
::rawElement( 'dd', [ 'class' => 'info' ], $i );
768 if ( $dynamicParams !== null ) {
769 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $context, [
770 $module->getModulePrefix(),
771 $module->getModuleName(),
772 $module->getModulePath()
774 $help['parameters'] .= Html
::element( 'dt', null, '*' );
775 $help['parameters'] .= Html
::rawElement( 'dd',
776 [ 'class' => 'description' ], $dynamicParams->parse() );
779 $help['parameters'] .= Html
::closeElement( 'dl' );
780 $help['parameters'] .= Html
::closeElement( 'div' );
783 $examples = $module->getExamplesMessages();
785 $help['examples'] .= Html
::openElement( 'div',
786 [ 'class' => 'apihelp-block apihelp-examples' ] );
787 $msg = $context->msg( 'api-help-examples' );
788 if ( !$msg->isDisabled() ) {
789 $help['examples'] .= self
::wrap(
790 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
794 $help['examples'] .= Html
::openElement( 'dl' );
795 foreach ( $examples as $qs => $msg ) {
796 $msg = ApiBase
::makeMessage( $msg, $context, [
797 $module->getModulePrefix(),
798 $module->getModuleName(),
799 $module->getModulePath()
802 $link = wfAppendQuery( wfScript( 'api' ), $qs );
803 $sandbox = SpecialPage
::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
804 $help['examples'] .= Html
::rawElement( 'dt', null, $msg->parse() );
805 $help['examples'] .= Html
::rawElement( 'dd', null,
806 Html
::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
807 Html
::rawElement( 'a', [ 'href' => $sandbox ],
808 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
811 $help['examples'] .= Html
::closeElement( 'dl' );
812 $help['examples'] .= Html
::closeElement( 'div' );
815 $subtocnumber = $tocnumber;
816 $subtocnumber[$level +
1] = 0;
818 'submodules' => $options['recursivesubmodules'],
819 'headerlevel' => $level +
1,
820 'tocnumber' => &$subtocnumber,
824 if ( $options['submodules'] && $module->getModuleManager() ) {
825 $manager = $module->getModuleManager();
827 foreach ( $groups as $group ) {
828 $names = $manager->getNames( $group );
830 foreach ( $names as $name ) {
831 $submodules[] = $manager->getModule( $name );
834 $help['submodules'] .= self
::getHelpInternal(
842 $module->modifyHelp( $help, $suboptions, $haveModules );
844 Hooks
::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
846 $out .= implode( "\n", $help );
852 public function shouldCheckMaxlag() {
856 public function isReadMode() {
860 public function getCustomPrinter() {
861 $params = $this->extractRequestParams();
862 if ( $params['wrap'] ) {
866 $main = $this->getMain();
867 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
868 return new ApiFormatRaw( $main, $errorPrinter );
871 public function getAllowedParams() {
874 ApiBase
::PARAM_DFLT
=> 'main',
875 ApiBase
::PARAM_ISMULTI
=> true,
877 'submodules' => false,
878 'recursivesubmodules' => false,
884 protected function getExamplesMessages() {
887 => 'apihelp-help-example-main',
888 'action=help&modules=query&submodules=1'
889 => 'apihelp-help-example-submodules',
890 'action=help&recursivesubmodules=1'
891 => 'apihelp-help-example-recursive',
892 'action=help&modules=help'
893 => 'apihelp-help-example-help',
894 'action=help&modules=query+info|query+categorymembers'
895 => 'apihelp-help-example-query',
899 public function getHelpUrls() {
901 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
902 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
903 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',