45 parent::__construct( $main, $action );
46 $this->skinFactory = $skinFactory;
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 $context->setOutput( $out );
73 $html = ob_get_clean();
76 if ( $params[
'wrap'] ) {
78 'mime' =>
'text/html',
79 'filename' =>
'api-help.html',
87 'Types' => [
'AssocAsObject' => true ],
90 $errors = array_filter( [
91 'errors' => $this->
getResult()->getResultData( [
'errors' ], $transform ),
92 'warnings' => $this->
getResult()->getResultData( [
'warnings' ], $transform ),
95 $json = FormatJson::encode( $errors,
true, FormatJson::UTF8_OK );
98 $json = str_replace(
'--',
'-\u002D', $json );
99 $html =
"<!-- API warnings and errors:\n$json\n-->\n$html";
129 if ( !is_array( $modules ) ) {
130 $modules = [ $modules ];
134 $out->addModuleStyles( [
136 'mediawiki.apipretty',
138 $out->setPageTitleMsg( $context->
msg(
'api-help-title' ) );
141 $cache = $services->getMainWANObjectCache();
143 if ( count( $modules ) == 1 && $modules[0] instanceof
ApiMain &&
144 $options[
'recursivesubmodules'] &&
145 $context->
getLanguage()->equals( $services->getContentLanguage() )
148 if ( $cacheHelpTimeout > 0 ) {
150 $cacheKey = $cache->makeKey(
'apihelp', $modules[0]->
getModulePath(),
151 (
int)!empty( $options[
'toc'] ),
152 str_replace(
' ',
'_', SpecialVersion::getVersion(
'nodb' ) ) );
153 $cached = $cache->get( $cacheKey );
155 $out->addHTML( $cached );
160 if ( $out->getHTML() !==
'' ) {
166 $options[
'recursivesubmodules'] = !empty( $options[
'recursivesubmodules'] );
167 $options[
'submodules'] = $options[
'recursivesubmodules'] || !empty( $options[
'submodules'] );
170 if ( empty( $options[
'nolead'] ) ) {
171 $msg = $context->
msg(
'api-help-lead' );
172 if ( !$msg->isDisabled() ) {
173 $out->addHTML( $msg->parseAsBlock() );
178 $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
179 if ( !empty( $options[
'toc'] ) && $haveModules ) {
180 $out->addTOCPlaceholder(
new TOCData( ...array_values( $haveModules ) ) );
182 $out->addHTML( $html );
184 $helptitle = $options[
'helptitle'] ??
null;
187 $out->addHTML( $html );
189 if ( $cacheKey !==
null ) {
191 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
203 public static function fixHelpLinks( $html, $helptitle =
null, $localModules = [] ) {
206 static function ( SerializerNode $node ):
bool {
207 return $node->name ===
'a'
208 && isset( $node->attrs[
'href'] )
209 && !str_contains( $node->attrs[
'class'] ??
'',
'apihelp-linktrail' );
211 static function ( SerializerNode $node ) use ( $helptitle, $localModules ): SerializerNode {
212 $href = $node->attrs[
'href'];
216 $href = rawurldecode( $href );
217 }
while ( $old !== $href );
218 if ( preg_match(
'!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
219 if ( isset( $localModules[$m[1]] ) ) {
220 $href = $m[2] ===
'' ?
'#' . $m[1] : $m[2];
221 } elseif ( $helptitle !==
null ) {
222 $href = Title::newFromText( str_replace(
'$1', $m[1], $helptitle ) . $m[2] )
230 $node->attrs[
'href'] = $href;
231 unset( $node->attrs[
'title'] );
247 private static function wrap(
Message $msg, $class, $tag =
'span' ) {
248 return Html::rawElement( $tag, [
'class' => $class ],
262 private static function getHelpInternal(
IContextSource $context, array $modules,
263 array $options, &$haveModules
267 $level = empty( $options[
'headerlevel'] ) ? 2 : $options[
'headerlevel'];
268 if ( empty( $options[
'tocnumber'] ) ) {
269 $tocnumber = [ 2 => 0 ];
271 $tocnumber = &$options[
'tocnumber'];
274 foreach ( $modules as $module ) {
275 $paramValidator = $module->getMain()->getParamValidator();
276 $tocnumber[$level]++;
277 $path = $module->getModulePath();
278 $module->setContext( $context );
289 if ( empty( $options[
'noheader'] ) || !empty( $options[
'toc'] ) ) {
292 while ( isset( $haveModules[$anchor] ) ) {
293 $anchor =
$path .
'|' . ++$i;
296 if ( $module->isMain() ) {
297 $headerContent = $context->
msg(
'api-help-main-header' )->parse();
299 'class' =>
'apihelp-header',
302 $name = $module->getModuleName();
303 $headerContent = htmlspecialchars(
304 $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
"=$name"
306 if ( $module->getModulePrefix() !==
'' ) {
307 $headerContent .=
' ' .
308 $context->
msg(
'parentheses', $module->getModulePrefix() )->parse();
314 'class' => [
'apihelp-header',
'apihelp-module-name' ],
320 $headerAttr[
'id'] = $anchor;
322 $haveModules[$anchor] =
new SectionMetadata(
323 tocLevel: count( $tocnumber ),
325 line: $headerContent,
326 number: implode(
'.', $tocnumber ),
327 index: (string)( 1 + count( $haveModules ) ),
329 linkAnchor: Sanitizer::escapeIdForLink( $anchor ),
331 if ( empty( $options[
'noheader'] ) ) {
332 $help[
'header'] .= Html::rawElement(
333 'h' . min( 6, $level ),
339 $haveModules[
$path] =
true;
344 for ( $m = $module; $m !==
null; $m = $m->getParent() ) {
345 $name = $m->getModuleName();
346 if ( $name ===
'main_int' ) {
350 if ( count( $modules ) === 1 && $m === $modules[0] &&
351 !( !empty( $options[
'submodules'] ) && $m->getModuleManager() )
353 $link =
Html::element(
'b', [
'dir' =>
'ltr',
'lang' =>
'en' ], $name );
357 [
'href' => $link,
'class' =>
'apihelp-linktrail',
'dir' =>
'ltr',
'lang' =>
'en' ],
362 array_unshift( $links, $link );
365 $help[
'header'] .= self::wrap(
366 $context->
msg(
'parentheses' )
367 ->rawParams( $context->
getLanguage()->pipeList( $links ) ),
368 'apihelp-linktrail',
'div'
372 $flags = $module->getHelpFlags();
373 $help[
'flags'] .= Html::openElement(
'div',
374 [
'class' => [
'apihelp-block',
'apihelp-flags' ] ] );
375 $msg = $context->
msg(
'api-help-flags' );
376 if ( !$msg->isDisabled() ) {
377 $help[
'flags'] .= self::wrap(
378 $msg->numParams( count( $flags ) ),
'apihelp-block-head',
'div'
381 $help[
'flags'] .= Html::openElement(
'ul' );
382 foreach ( $flags as $flag ) {
383 $help[
'flags'] .= Html::rawElement(
'li', [],
390 self::wrap( $context->
msg(
"api-help-flag-$flag" ),
"apihelp-flag-$flag" )
393 $sourceInfo = $module->getModuleSourceInfo();
395 if ( isset( $sourceInfo[
'namemsg'] ) ) {
396 $extname = $context->
msg( $sourceInfo[
'namemsg'] )->text();
399 $extname =
Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'name'] );
401 $help[
'flags'] .= Html::rawElement(
'li', [],
403 $context->
msg(
'api-help-source', $extname, $sourceInfo[
'name'] ),
410 if ( isset( $sourceInfo[
'license-name'] ) ) {
411 $msg = $context->
msg(
'api-help-license', $linkText,
412 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'license-name'] )
414 } elseif ( ExtensionInfo::getLicenseFileNames( dirname( $sourceInfo[
'path'] ) ) ) {
415 $msg = $context->
msg(
'api-help-license-noname', $linkText );
417 $msg = $context->
msg(
'api-help-license-unknown' );
419 $help[
'flags'] .= Html::rawElement(
'li', [],
420 self::wrap( $msg,
'apihelp-license' )
423 $help[
'flags'] .= Html::rawElement(
'li', [],
424 self::wrap( $context->
msg(
'api-help-source-unknown' ),
'apihelp-source' )
426 $help[
'flags'] .= Html::rawElement(
'li', [],
427 self::wrap( $context->
msg(
'api-help-license-unknown' ),
'apihelp-license' )
430 $help[
'flags'] .= Html::closeElement(
'ul' );
431 $help[
'flags'] .= Html::closeElement(
'div' );
433 foreach ( $module->getFinalDescription() as $msg ) {
434 $msg->setContext( $context );
435 $help[
'description'] .= $msg->parseAsBlock();
438 $urls = $module->getHelpUrls();
440 if ( !is_array( $urls ) ) {
443 $help[
'help-urls'] .= Html::openElement(
'div',
444 [
'class' => [
'apihelp-block',
'apihelp-help-urls' ] ]
446 $msg = $context->
msg(
'api-help-help-urls' );
447 if ( !$msg->isDisabled() ) {
448 $help[
'help-urls'] .= self::wrap(
449 $msg->numParams( count( $urls ) ),
'apihelp-block-head',
'div'
452 $help[
'help-urls'] .= Html::openElement(
'ul' );
453 foreach ( $urls as
$url ) {
454 $help[
'help-urls'] .= Html::rawElement(
'li', [],
458 $help[
'help-urls'] .= Html::closeElement(
'ul' );
459 $help[
'help-urls'] .= Html::closeElement(
'div' );
463 $dynamicParams = $module->dynamicParameterDocumentation();
465 if ( $params || $dynamicParams !==
null ) {
466 $help[
'parameters'] .= Html::openElement(
'div',
467 [
'class' => [
'apihelp-block',
'apihelp-parameters' ] ]
469 $msg = $context->
msg(
'api-help-parameters' );
470 if ( !$msg->isDisabled() ) {
471 $help[
'parameters'] .= self::wrap(
472 $msg->numParams( count( $params ) ),
'apihelp-block-head',
'div'
474 if ( !$module->isMain() ) {
476 $help[
'parameters'] .= self::wrap(
477 $context->
msg(
'api-help-parameters-note' ),
'apihelp-block-header',
'div'
481 $help[
'parameters'] .= Html::openElement(
'dl' );
483 $descriptions = $module->getFinalParamDescription();
485 foreach ( $params as $name => $settings ) {
486 $settings = $paramValidator->normalizeSettings( $settings );
488 if ( $settings[ParamValidator::PARAM_TYPE] ===
'submodule' ) {
492 $encodedParamName = $module->encodeParamName( $name );
493 $paramNameAttribs = [
'dir' =>
'ltr',
'lang' =>
'en' ];
494 if ( isset( $anchor ) ) {
495 $paramNameAttribs[
'id'] =
"$anchor:$encodedParamName";
497 $help[
'parameters'] .= Html::rawElement(
'dt', [],
498 Html::element(
'span', $paramNameAttribs, $encodedParamName )
503 if ( isset( $descriptions[$name] ) ) {
504 foreach ( $descriptions[$name] as $msg ) {
505 $msg->setContext( $context );
506 $description[] = $msg->parseAsBlock();
509 if ( !array_filter( $description ) ) {
510 $description = [ self::wrap(
511 $context->
msg(
'api-help-param-no-description' ),
517 if ( !empty( $settings[ParamValidator::PARAM_DEPRECATED] ) ) {
518 $help[
'parameters'] .= Html::openElement(
'dd',
519 [
'class' =>
'info' ] );
520 $help[
'parameters'] .= self::wrap(
521 $context->
msg(
'api-help-param-deprecated' ),
522 'apihelp-deprecated',
'strong'
524 $help[
'parameters'] .= Html::closeElement(
'dd' );
527 if ( $description ) {
528 $description = implode(
'', $description );
529 $description = preg_replace(
'!\s*</([oud]l)>\s*<\1>\s*!',
"\n", $description );
530 $help[
'parameters'] .= Html::rawElement(
'dd',
531 [
'class' =>
'description' ], $description );
536 $paramHelp = $paramValidator->getHelpInfo( $module, $name, $settings, [] );
538 unset( $paramHelp[ParamValidator::PARAM_DEPRECATED] );
540 if ( isset( $paramHelp[ParamValidator::PARAM_REQUIRED] ) ) {
541 $paramHelp[ParamValidator::PARAM_REQUIRED]->setContext( $context );
542 $info[] = $paramHelp[ParamValidator::PARAM_REQUIRED];
543 unset( $paramHelp[ParamValidator::PARAM_REQUIRED] );
549 $tag = array_shift( $i );
550 $info[] = $context->
msg(
"apihelp-{$path}-paraminfo-{$tag}" )
551 ->numParams( count( $i ) )
552 ->params( $context->
getLanguage()->commaList( $i ) )
553 ->params( $module->getModulePrefix() )
561 $msg =
'api-help-param-templated-var-first';
563 $vars[] = $context->
msg( $msg, $k, $module->encodeParamName( $v ) );
564 $msg =
'api-help-param-templated-var';
566 $info[] = $context->
msg(
'api-help-param-templated' )
567 ->numParams( count( $vars ) )
573 foreach ( $paramHelp as $m ) {
574 $m->setContext( $context );
575 $info[] = $m->parse();
578 foreach ( $info as $i ) {
579 $help[
'parameters'] .= Html::rawElement(
'dd', [
'class' =>
'info' ], $i );
583 if ( $dynamicParams !==
null ) {
584 $dynamicParams = $context->
msg(
586 $module->getModulePrefix(),
587 $module->getModuleName(),
588 $module->getModulePath()
591 $help[
'parameters'] .= Html::rawElement(
'dd',
592 [
'class' =>
'description' ], $dynamicParams->parse() );
595 $help[
'parameters'] .= Html::closeElement(
'dl' );
596 $help[
'parameters'] .= Html::closeElement(
'div' );
599 $examples = $module->getExamplesMessages();
601 $help[
'examples'] .= Html::openElement(
'div',
602 [
'class' => [
'apihelp-block',
'apihelp-examples' ] ] );
603 $msg = $context->
msg(
'api-help-examples' );
604 if ( !$msg->isDisabled() ) {
605 $help[
'examples'] .= self::wrap(
606 $msg->numParams( count( $examples ) ),
'apihelp-block-head',
'div'
610 $help[
'examples'] .= Html::openElement(
'dl' );
611 foreach ( $examples as $qs => $msg ) {
612 $msg = $context->
msg(
614 $module->getModulePrefix(),
615 $module->getModuleName(),
616 $module->getModulePath()
621 $help[
'examples'] .= Html::rawElement(
'dt', [], $msg->parse() );
622 $help[
'examples'] .= Html::rawElement(
'dd', [],
627 ],
"api.php?$qs" ) .
' ' .
628 Html::rawElement(
'a', [
'href' => $sandbox ],
629 $context->
msg(
'api-help-open-in-apisandbox' )->parse() )
632 $help[
'examples'] .= Html::closeElement(
'dl' );
633 $help[
'examples'] .= Html::closeElement(
'div' );
636 $subtocnumber = $tocnumber;
637 $subtocnumber[$level + 1] = 0;
639 'submodules' => $options[
'recursivesubmodules'],
640 'headerlevel' => $level + 1,
641 'tocnumber' => &$subtocnumber,
645 if ( $options[
'submodules'] && $module->getModuleManager() ) {
646 $manager = $module->getModuleManager();
648 foreach ( $groups as $group ) {
649 $names = $manager->getNames( $group );
651 foreach ( $names as $name ) {
652 $submodules[] = $manager->getModule( $name );
655 $help[
'submodules'] .= self::getHelpInternal(
663 $module->modifyHelp( $help, $suboptions, $haveModules );
665 if ( $module->getHookContainer()->isRegistered(
'APIHelpModifyOutput' ) ) {
668 if ( !empty( $suboptions[
'toc'] ) ) {
669 $haveModules = array_map(
670 static fn ( $s )=>$s->toLegacy(), $haveModules
673 $module->getHookRunner()->onAPIHelpModifyOutput(
674 $module, $help, $suboptions, $haveModules
676 if ( !empty( $suboptions[
'toc'] ) ) {
677 $haveModules = array_map(
678 static fn ( $s )=>SectionMetadata::fromLegacy( $s ), $haveModules
683 $out .= implode(
"\n", $help );
702 if ( $params[
'wrap'] ) {
707 $errorPrinter = $main->createPrinterByName( $main->getParameter(
'format' ) );
715 ParamValidator::PARAM_DEFAULT =>
'main',
716 ParamValidator::PARAM_ISMULTI =>
true,
718 'submodules' =>
false,
719 'recursivesubmodules' =>
false,
729 =>
'apihelp-help-example-main',
730 'action=help&modules=query&submodules=1'
731 =>
'apihelp-help-example-submodules',
732 'action=help&recursivesubmodules=1'
733 =>
'apihelp-help-example-recursive',
734 'action=help&modules=help'
735 =>
'apihelp-help-example-help',
736 'action=help&modules=query+info|query+categorymembers'
737 =>
'apihelp-help-example-query',
744 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
745 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
746 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',