46 parent::__construct( $main, $action );
53 foreach ( $params[
'modules'] as
$path ) {
59 $context->setSkin( $this->skinFactory->makeSkin(
'apioutput' ) );
63 $out->setRobotPolicy(
'noindex,nofollow' );
64 $out->setCopyrightUrl(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
65 $out->disallowUserJs();
66 $out->reduceAllowedModules( RL\Module::TYPE_SCRIPTS, RL\Module::ORIGIN_NONE );
67 $context->setOutput( $out );
71 if ( !$context->getRequest()->getValues() ) {
72 $params[
'toc'] =
true;
79 $context->getOutput()->output();
80 $html = ob_get_clean();
83 if ( $params[
'wrap'] ) {
85 'mime' =>
'text/html',
86 'filename' =>
'api-help.html',
94 'Types' => [
'AssocAsObject' => true ],
97 $errors = array_filter( [
98 'errors' => $this->
getResult()->getResultData( [
'errors' ], $transform ),
99 'warnings' => $this->
getResult()->getResultData( [
'warnings' ], $transform ),
102 $json = FormatJson::encode( $errors,
true, FormatJson::UTF8_OK );
105 $json = str_replace(
'--',
'-\u002D', $json );
106 $html =
"<!-- API warnings and errors:\n$json\n-->\n$html";
136 if ( !is_array( $modules ) ) {
137 $modules = [ $modules ];
140 $out = $context->getOutput();
141 $out->addModuleStyles( [
143 'mediawiki.apipretty',
145 $out->setPageTitleMsg( $context->msg(
'api-help-title' ) );
148 $cache = $services->getMainWANObjectCache();
150 if ( count( $modules ) == 1 && $modules[0] instanceof
ApiMain &&
151 $options[
'recursivesubmodules'] &&
152 $context->
getLanguage()->equals( $services->getContentLanguage() )
155 if ( $cacheHelpTimeout > 0 ) {
157 $cacheKey = $cache->makeKey(
'apihelp', $modules[0]->
getModulePath(),
158 (
int)!empty( $options[
'toc'] ),
159 str_replace(
' ',
'_', SpecialVersion::getVersion(
'nodb' ) ) );
160 $cached = $cache->get( $cacheKey );
162 $out->addHTML( $cached );
167 if ( $out->getHTML() !==
'' ) {
173 $options[
'recursivesubmodules'] = !empty( $options[
'recursivesubmodules'] );
174 $options[
'submodules'] = $options[
'recursivesubmodules'] || !empty( $options[
'submodules'] );
176 $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
178 if ( !empty( $options[
'toc'] ) && $haveModules ) {
179 $out->addTOCPlaceholder(
new TOCData( ...array_values( $haveModules ) ) );
183 if ( empty( $options[
'nolead'] ) ) {
184 $msg = $context->msg(
'api-help-lead' );
185 if ( !$msg->isDisabled() ) {
186 $out->addHTML( $msg->parseAsBlock() );
190 $out->addHTML( $html );
192 $helptitle = $options[
'helptitle'] ??
null;
195 $out->addHTML( $html );
197 if ( $cacheKey !==
null ) {
199 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
211 public static function fixHelpLinks( $html, $helptitle =
null, $localModules = [] ) {
214 static function ( SerializerNode $node ):
bool {
215 return $node->name ===
'a'
216 && isset( $node->attrs[
'href'] )
217 && !str_contains( $node->attrs[
'class'] ??
'',
'apihelp-linktrail' );
219 static function ( SerializerNode $node ) use ( $helptitle, $localModules ): SerializerNode {
220 $href = $node->attrs[
'href'];
224 $href = rawurldecode( $href );
225 }
while ( $old !== $href );
226 if ( preg_match(
'!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
227 if ( isset( $localModules[$m[1]] ) ) {
228 $href = $m[2] ===
'' ?
'#' . $m[1] : $m[2];
229 } elseif ( $helptitle !==
null ) {
230 $href = Title::newFromText( str_replace(
'$1', $m[1], $helptitle ) . $m[2] )
238 $node->attrs[
'href'] = $href;
239 unset( $node->attrs[
'title'] );
255 private static function wrap(
Message $msg, $class, $tag =
'span' ) {
256 return Html::rawElement( $tag, [
'class' => $class ],
270 private static function getHelpInternal(
IContextSource $context, array $modules,
271 array $options, &$haveModules
275 $level = empty( $options[
'headerlevel'] ) ? 2 : $options[
'headerlevel'];
276 if ( empty( $options[
'tocnumber'] ) ) {
277 $tocnumber = [ 2 => 0 ];
279 $tocnumber = &$options[
'tocnumber'];
282 foreach ( $modules as $module ) {
283 $paramValidator = $module->getMain()->getParamValidator();
284 $tocnumber[$level]++;
285 $path = $module->getModulePath();
286 $module->setContext( $context );
297 if ( empty( $options[
'noheader'] ) || !empty( $options[
'toc'] ) ) {
300 while ( isset( $haveModules[$anchor] ) ) {
301 $anchor =
$path .
'|' . ++$i;
304 if ( $module->isMain() ) {
305 $headerContent = $context->msg(
'api-help-main-header' )->parse();
307 'class' =>
'apihelp-header',
310 $name = $module->getModuleName();
311 $headerContent = htmlspecialchars(
312 $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
"=$name"
314 if ( $module->getModulePrefix() !==
'' ) {
315 $headerContent .=
' ' .
316 $context->msg(
'parentheses', $module->getModulePrefix() )->parse();
322 'class' => [
'apihelp-header',
'apihelp-module-name' ],
328 $headerAttr[
'id'] = $anchor;
330 $haveModules[$anchor] =
new SectionMetadata(
331 tocLevel: count( $tocnumber ),
333 line: $headerContent,
334 number: implode(
'.', $tocnumber ),
335 index: (
string)( 1 + count( $haveModules ) ),
337 linkAnchor: Sanitizer::escapeIdForLink( $anchor ),
339 if ( empty( $options[
'noheader'] ) ) {
340 $help[
'header'] .= Html::rawElement(
341 'h' . min( 6, $level ),
347 $haveModules[
$path] =
true;
352 for ( $m = $module; $m !==
null; $m = $m->getParent() ) {
353 $name = $m->getModuleName();
354 if ( $name ===
'main_int' ) {
358 if ( count( $modules ) === 1 && $m === $modules[0] &&
359 !( !empty( $options[
'submodules'] ) && $m->getModuleManager() )
361 $link =
Html::element(
'b', [
'dir' =>
'ltr',
'lang' =>
'en' ], $name );
365 [
'href' => $link,
'class' =>
'apihelp-linktrail',
'dir' =>
'ltr',
'lang' =>
'en' ],
370 array_unshift( $links, $link );
373 $help[
'header'] .= self::wrap(
374 $context->msg(
'parentheses' )
375 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
376 'apihelp-linktrail',
'div'
380 $flags = $module->getHelpFlags();
381 $help[
'flags'] .= Html::openElement(
'div',
382 [
'class' => [
'apihelp-block',
'apihelp-flags' ] ] );
383 $msg = $context->msg(
'api-help-flags' );
384 if ( !$msg->isDisabled() ) {
385 $help[
'flags'] .= self::wrap(
386 $msg->numParams( count( $flags ) ),
'apihelp-block-head',
'div'
389 $help[
'flags'] .= Html::openElement(
'ul' );
390 foreach ( $flags as $flag ) {
391 $help[
'flags'] .= Html::rawElement(
'li', [],
398 self::wrap( $context->msg(
"api-help-flag-$flag" ),
"apihelp-flag-$flag" )
401 $sourceInfo = $module->getModuleSourceInfo();
403 if ( isset( $sourceInfo[
'namemsg'] ) ) {
404 $extname = $context->msg( $sourceInfo[
'namemsg'] )->text();
407 $extname =
Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'name'] );
409 $help[
'flags'] .= Html::rawElement(
'li', [],
411 $context->msg(
'api-help-source', $extname, $sourceInfo[
'name'] ),
418 if ( isset( $sourceInfo[
'license-name'] ) ) {
419 $msg = $context->msg(
'api-help-license', $linkText,
420 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'license-name'] )
422 } elseif ( ExtensionInfo::getLicenseFileNames( dirname( $sourceInfo[
'path'] ) ) ) {
423 $msg = $context->msg(
'api-help-license-noname', $linkText );
425 $msg = $context->msg(
'api-help-license-unknown' );
427 $help[
'flags'] .= Html::rawElement(
'li', [],
428 self::wrap( $msg,
'apihelp-license' )
431 $help[
'flags'] .= Html::rawElement(
'li', [],
432 self::wrap( $context->msg(
'api-help-source-unknown' ),
'apihelp-source' )
434 $help[
'flags'] .= Html::rawElement(
'li', [],
435 self::wrap( $context->msg(
'api-help-license-unknown' ),
'apihelp-license' )
438 $help[
'flags'] .= Html::closeElement(
'ul' );
439 $help[
'flags'] .= Html::closeElement(
'div' );
441 foreach ( $module->getFinalDescription() as $msg ) {
442 $msg->setContext( $context );
443 $help[
'description'] .= $msg->parseAsBlock();
446 $urls = $module->getHelpUrls();
448 if ( !is_array( $urls ) ) {
451 $help[
'help-urls'] .= Html::openElement(
'div',
452 [
'class' => [
'apihelp-block',
'apihelp-help-urls' ] ]
454 $msg = $context->msg(
'api-help-help-urls' );
455 if ( !$msg->isDisabled() ) {
456 $help[
'help-urls'] .= self::wrap(
457 $msg->numParams( count( $urls ) ),
'apihelp-block-head',
'div'
460 $help[
'help-urls'] .= Html::openElement(
'ul' );
461 foreach ( $urls as
$url ) {
462 $help[
'help-urls'] .= Html::rawElement(
'li', [],
466 $help[
'help-urls'] .= Html::closeElement(
'ul' );
467 $help[
'help-urls'] .= Html::closeElement(
'div' );
471 $dynamicParams = $module->dynamicParameterDocumentation();
473 if ( $params || $dynamicParams !==
null ) {
474 $help[
'parameters'] .= Html::openElement(
'div',
475 [
'class' => [
'apihelp-block',
'apihelp-parameters' ] ]
477 $msg = $context->msg(
'api-help-parameters' );
478 if ( !$msg->isDisabled() ) {
479 $help[
'parameters'] .= self::wrap(
480 $msg->numParams( count( $params ) ),
'apihelp-block-head',
'div'
482 if ( !$module->isMain() ) {
484 $help[
'parameters'] .= self::wrap(
485 $context->msg(
'api-help-parameters-note' ),
'apihelp-block-header',
'div'
489 $help[
'parameters'] .= Html::openElement(
'dl' );
491 $descriptions = $module->getFinalParamDescription();
493 foreach ( $params as $name => $settings ) {
494 $settings = $paramValidator->normalizeSettings( $settings );
496 if ( $settings[ParamValidator::PARAM_TYPE] ===
'submodule' ) {
500 $encodedParamName = $module->encodeParamName( $name );
501 $paramNameAttribs = [
'dir' =>
'ltr',
'lang' =>
'en' ];
502 if ( isset( $anchor ) ) {
503 $paramNameAttribs[
'id'] =
"$anchor:$encodedParamName";
505 $help[
'parameters'] .= Html::rawElement(
'dt', [],
506 Html::element(
'span', $paramNameAttribs, $encodedParamName )
511 if ( isset( $descriptions[$name] ) ) {
512 foreach ( $descriptions[$name] as $msg ) {
513 $msg->setContext( $context );
514 $description[] = $msg->parseAsBlock();
517 if ( !array_filter( $description ) ) {
518 $description = [ self::wrap(
519 $context->msg(
'api-help-param-no-description' ),
525 if ( !empty( $settings[ParamValidator::PARAM_DEPRECATED] ) ) {
526 $help[
'parameters'] .= Html::openElement(
'dd',
527 [
'class' =>
'info' ] );
528 $help[
'parameters'] .= self::wrap(
529 $context->msg(
'api-help-param-deprecated' ),
530 'apihelp-deprecated',
'strong'
532 $help[
'parameters'] .= Html::closeElement(
'dd' );
535 if ( $description ) {
536 $description = implode(
'', $description );
537 $description = preg_replace(
'!\s*</([oud]l)>\s*<\1>\s*!',
"\n", $description );
538 $help[
'parameters'] .= Html::rawElement(
'dd',
539 [
'class' =>
'description' ], $description );
544 $paramHelp = $paramValidator->getHelpInfo( $module, $name, $settings, [] );
546 unset( $paramHelp[ParamValidator::PARAM_DEPRECATED] );
548 if ( isset( $paramHelp[ParamValidator::PARAM_REQUIRED] ) ) {
549 $paramHelp[ParamValidator::PARAM_REQUIRED]->setContext( $context );
550 $info[] = $paramHelp[ParamValidator::PARAM_REQUIRED];
551 unset( $paramHelp[ParamValidator::PARAM_REQUIRED] );
557 $tag = array_shift( $i );
558 $info[] = $context->msg(
"apihelp-{$path}-paraminfo-{$tag}" )
559 ->numParams( count( $i ) )
560 ->params( $context->getLanguage()->commaList( $i ) )
561 ->params( $module->getModulePrefix() )
569 $msg =
'api-help-param-templated-var-first';
571 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
572 $msg =
'api-help-param-templated-var';
574 $info[] = $context->msg(
'api-help-param-templated' )
575 ->numParams( count( $vars ) )
581 foreach ( $paramHelp as $m ) {
582 $m->setContext( $context );
583 $info[] = $m->parse();
586 foreach ( $info as $i ) {
587 $help[
'parameters'] .= Html::rawElement(
'dd', [
'class' =>
'info' ], $i );
591 if ( $dynamicParams !==
null ) {
592 $dynamicParams = $context->msg(
594 $module->getModulePrefix(),
595 $module->getModuleName(),
596 $module->getModulePath()
599 $help[
'parameters'] .= Html::rawElement(
'dd',
600 [
'class' =>
'description' ], $dynamicParams->parse() );
603 $help[
'parameters'] .= Html::closeElement(
'dl' );
604 $help[
'parameters'] .= Html::closeElement(
'div' );
607 $examples = $module->getExamplesMessages();
609 $help[
'examples'] .= Html::openElement(
'div',
610 [
'class' => [
'apihelp-block',
'apihelp-examples' ] ] );
611 $msg = $context->msg(
'api-help-examples' );
612 if ( !$msg->isDisabled() ) {
613 $help[
'examples'] .= self::wrap(
614 $msg->numParams( count( $examples ) ),
'apihelp-block-head',
'div'
618 $help[
'examples'] .= Html::openElement(
'dl' );
619 foreach ( $examples as $qs => $msg ) {
620 $msg = $context->msg(
622 $module->getModulePrefix(),
623 $module->getModuleName(),
624 $module->getModulePath()
629 $help[
'examples'] .= Html::rawElement(
'dt', [], $msg->parse() );
630 $help[
'examples'] .= Html::rawElement(
'dd', [],
635 ],
"api.php?$qs" ) .
' ' .
636 Html::rawElement(
'a', [
'href' => $sandbox ],
637 $context->msg(
'api-help-open-in-apisandbox' )->parse() )
640 $help[
'examples'] .= Html::closeElement(
'dl' );
641 $help[
'examples'] .= Html::closeElement(
'div' );
644 $subtocnumber = $tocnumber;
645 $subtocnumber[$level + 1] = 0;
647 'submodules' => $options[
'recursivesubmodules'],
648 'headerlevel' => $level + 1,
649 'tocnumber' => &$subtocnumber,
653 if ( $options[
'submodules'] && $module->getModuleManager() ) {
654 $manager = $module->getModuleManager();
656 foreach ( $groups as $group ) {
657 $names = $manager->getNames( $group );
659 foreach ( $names as $name ) {
660 $submodules[] = $manager->getModule( $name );
663 $help[
'submodules'] .= self::getHelpInternal(
672 $module instanceof ApiMain ||
673 MWDebug::detectDeprecatedOverride( $module, ApiBase::class,
'modifyHelp',
'1.47' )
675 $module->modifyHelp( $help, $suboptions, $haveModules );
678 if ( $module->getHookContainer()->isRegistered(
'APIHelpModifyOutput' ) ) {
680 if ( !empty( $suboptions[
'toc'] ) ) {
681 $haveModules = array_map(
682 static fn ( $s )=>$s->toLegacy(), $haveModules
685 $module->getHookRunner()->onAPIHelpModifyOutput(
686 $module, $help, $suboptions, $haveModules
688 if ( !empty( $suboptions[
'toc'] ) ) {
689 $haveModules = array_map(
690 static fn ( $s )=>SectionMetadata::fromLegacy( $s ), $haveModules
695 $out .= implode(
"\n", $help );
714 if ( $params[
'wrap'] ) {
719 $errorPrinter = $main->createPrinterByName( $main->getParameter(
'format' ) );
727 ParamValidator::PARAM_DEFAULT =>
'main',
728 ParamValidator::PARAM_ISMULTI =>
true,
730 'submodules' =>
false,
731 'recursivesubmodules' =>
false,
741 =>
'apihelp-help-example-main',
742 'action=help&modules=query&submodules=1'
743 =>
'apihelp-help-example-submodules',
744 'action=help&recursivesubmodules=1&toc'
745 =>
'apihelp-help-example-recursive',
746 'action=help&modules=help'
747 =>
'apihelp-help-example-help',
748 'action=help&modules=query+info|query+categorymembers'
749 =>
'apihelp-help-example-query',
756 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
757 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
758 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',