60 parent::__construct( $main, $action );
61 $this->skinFactory = $skinFactory;
74 $context->setSkin( $this->skinFactory->makeSkin(
'apioutput' ) );
78 $out->setRobotPolicy(
'noindex,nofollow' );
79 $out->setCopyrightUrl(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
80 $out->disallowUserJs();
81 $context->setOutput( $out );
88 $html = ob_get_clean();
93 'mime' =>
'text/html',
94 'filename' =>
'api-help.html',
102 'Types' => [
'AssocAsObject' => true ],
105 $errors = array_filter( [
106 'errors' => $this->
getResult()->getResultData( [
'errors' ], $transform ),
107 'warnings' => $this->
getResult()->getResultData( [
'warnings' ], $transform ),
110 $json = FormatJson::encode( $errors,
true, FormatJson::UTF8_OK );
113 $json = str_replace(
'--',
'-\u002D', $json );
114 $html =
"<!-- API warnings and errors:\n$json\n-->\n$html";
144 if ( !is_array( $modules ) ) {
145 $modules = [ $modules ];
149 $out->addModuleStyles( [
151 'mediawiki.apipretty',
153 $out->setPageTitleMsg( $context->
msg(
'api-help-title' ) );
156 $cache = $services->getMainWANObjectCache();
158 if ( count( $modules ) == 1 && $modules[0] instanceof
ApiMain &&
159 $options[
'recursivesubmodules'] &&
160 $context->
getLanguage()->equals( $services->getContentLanguage() )
163 if ( $cacheHelpTimeout > 0 ) {
165 $cacheKey = $cache->makeKey(
'apihelp', $modules[0]->
getModulePath(),
166 (
int)!empty( $options[
'toc'] ),
167 str_replace(
' ',
'_', SpecialVersion::getVersion(
'nodb' ) ) );
168 $cached = $cache->get( $cacheKey );
170 $out->addHTML( $cached );
175 if ( $out->getHTML() !==
'' ) {
181 $options[
'recursivesubmodules'] = !empty( $options[
'recursivesubmodules'] );
182 $options[
'submodules'] = $options[
'recursivesubmodules'] || !empty( $options[
'submodules'] );
185 if ( empty( $options[
'nolead'] ) ) {
186 $msg = $context->
msg(
'api-help-lead' );
187 if ( !$msg->isDisabled() ) {
188 $out->addHTML( $msg->parseAsBlock() );
193 $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
194 if ( !empty( $options[
'toc'] ) && $haveModules ) {
196 $pout->
setTOCData( TOCData::fromLegacy( array_values( $haveModules ) ) );
197 $pout->setOutputFlag( ParserOutputFlags::SHOW_TOC );
198 $pout->setText( Parser::TOC_PLACEHOLDER );
199 $out->addParserOutput( $pout );
201 $out->addHTML( $html );
203 $helptitle = $options[
'helptitle'] ??
null;
206 $out->addHTML( $html );
208 if ( $cacheKey !==
null ) {
210 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
222 public static function fixHelpLinks( $html, $helptitle =
null, $localModules = [] ) {
225 static function ( SerializerNode $node ):
bool {
226 return $node->name ===
'a'
227 && isset( $node->attrs[
'href'] )
228 && !str_contains( $node->attrs[
'class'] ??
'',
'apihelp-linktrail' );
230 static function ( SerializerNode $node ) use ( $helptitle, $localModules ): SerializerNode {
231 $href = $node->attrs[
'href'];
235 $href = rawurldecode( $href );
236 }
while ( $old !== $href );
237 if ( preg_match(
'!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
238 if ( isset( $localModules[$m[1]] ) ) {
239 $href = $m[2] ===
'' ?
'#' . $m[1] : $m[2];
240 } elseif ( $helptitle !==
null ) {
241 $href = Title::newFromText( str_replace(
'$1', $m[1], $helptitle ) . $m[2] )
249 $node->attrs[
'href'] = $href;
250 unset( $node->attrs[
'title'] );
266 private static function wrap(
Message $msg, $class, $tag =
'span' ) {
267 return Html::rawElement( $tag, [
'class' => $class ],
281 private static function getHelpInternal(
IContextSource $context, array $modules,
282 array $options, &$haveModules
286 $level = empty( $options[
'headerlevel'] ) ? 2 : $options[
'headerlevel'];
287 if ( empty( $options[
'tocnumber'] ) ) {
288 $tocnumber = [ 2 => 0 ];
290 $tocnumber = &$options[
'tocnumber'];
293 foreach ( $modules as $module ) {
294 $paramValidator = $module->getMain()->getParamValidator();
295 $tocnumber[$level]++;
296 $path = $module->getModulePath();
297 $module->setContext( $context );
308 if ( empty( $options[
'noheader'] ) || !empty( $options[
'toc'] ) ) {
311 while ( isset( $haveModules[$anchor] ) ) {
312 $anchor =
$path .
'|' . ++$i;
315 if ( $module->isMain() ) {
316 $headerContent = $context->
msg(
'api-help-main-header' )->parse();
318 'class' =>
'apihelp-header',
321 $name = $module->getModuleName();
322 $headerContent = htmlspecialchars(
323 $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
"=$name"
325 if ( $module->getModulePrefix() !==
'' ) {
326 $headerContent .=
' ' .
327 $context->
msg(
'parentheses', $module->getModulePrefix() )->parse();
333 'class' => [
'apihelp-header',
'apihelp-module-name' ],
339 $headerAttr[
'id'] = $anchor;
342 $haveModules[$anchor] = [
343 'toclevel' => count( $tocnumber ),
346 'line' => $headerContent,
347 'number' => implode(
'.', $tocnumber ),
350 if ( empty( $options[
'noheader'] ) ) {
351 $help[
'header'] .= Html::rawElement(
352 'h' . min( 6, $level ),
358 $haveModules[
$path] =
true;
363 for ( $m = $module; $m !==
null; $m = $m->getParent() ) {
364 $name = $m->getModuleName();
365 if ( $name ===
'main_int' ) {
369 if ( count( $modules ) === 1 && $m === $modules[0] &&
370 !( !empty( $options[
'submodules'] ) && $m->getModuleManager() )
372 $link =
Html::element(
'b', [
'dir' =>
'ltr',
'lang' =>
'en' ], $name );
376 [
'href' => $link,
'class' =>
'apihelp-linktrail',
'dir' =>
'ltr',
'lang' =>
'en' ],
381 array_unshift( $links, $link );
384 $help[
'header'] .= self::wrap(
385 $context->
msg(
'parentheses' )
386 ->rawParams( $context->
getLanguage()->pipeList( $links ) ),
387 'apihelp-linktrail',
'div'
391 $flags = $module->getHelpFlags();
392 $help[
'flags'] .= Html::openElement(
'div',
393 [
'class' => [
'apihelp-block',
'apihelp-flags' ] ] );
394 $msg = $context->
msg(
'api-help-flags' );
395 if ( !$msg->isDisabled() ) {
396 $help[
'flags'] .= self::wrap(
397 $msg->numParams( count( $flags ) ),
'apihelp-block-head',
'div'
400 $help[
'flags'] .= Html::openElement(
'ul' );
401 foreach ( $flags as $flag ) {
402 $help[
'flags'] .= Html::rawElement(
'li', [],
409 self::wrap( $context->
msg(
"api-help-flag-$flag" ),
"apihelp-flag-$flag" )
412 $sourceInfo = $module->getModuleSourceInfo();
414 if ( isset( $sourceInfo[
'namemsg'] ) ) {
415 $extname = $context->
msg( $sourceInfo[
'namemsg'] )->text();
418 $extname =
Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'name'] );
420 $help[
'flags'] .= Html::rawElement(
'li', [],
422 $context->
msg(
'api-help-source', $extname, $sourceInfo[
'name'] ),
428 if ( isset( $sourceInfo[
'license-name'] ) ) {
429 $msg = $context->
msg(
'api-help-license', $link,
430 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'license-name'] )
432 } elseif ( ExtensionInfo::getLicenseFileNames( dirname( $sourceInfo[
'path'] ) ) ) {
433 $msg = $context->
msg(
'api-help-license-noname', $link );
435 $msg = $context->
msg(
'api-help-license-unknown' );
437 $help[
'flags'] .= Html::rawElement(
'li', [],
438 self::wrap( $msg,
'apihelp-license' )
441 $help[
'flags'] .= Html::rawElement(
'li', [],
442 self::wrap( $context->
msg(
'api-help-source-unknown' ),
'apihelp-source' )
444 $help[
'flags'] .= Html::rawElement(
'li', [],
445 self::wrap( $context->
msg(
'api-help-license-unknown' ),
'apihelp-license' )
448 $help[
'flags'] .= Html::closeElement(
'ul' );
449 $help[
'flags'] .= Html::closeElement(
'div' );
451 foreach ( $module->getFinalDescription() as $msg ) {
452 $msg->setContext( $context );
453 $help[
'description'] .= $msg->parseAsBlock();
456 $urls = $module->getHelpUrls();
458 if ( !is_array( $urls ) ) {
461 $help[
'help-urls'] .= Html::openElement(
'div',
462 [
'class' => [
'apihelp-block',
'apihelp-help-urls' ] ]
464 $msg = $context->
msg(
'api-help-help-urls' );
465 if ( !$msg->isDisabled() ) {
466 $help[
'help-urls'] .= self::wrap(
467 $msg->numParams( count( $urls ) ),
'apihelp-block-head',
'div'
470 $help[
'help-urls'] .= Html::openElement(
'ul' );
471 foreach ( $urls as
$url ) {
472 $help[
'help-urls'] .= Html::rawElement(
'li', [],
476 $help[
'help-urls'] .= Html::closeElement(
'ul' );
477 $help[
'help-urls'] .= Html::closeElement(
'div' );
481 $dynamicParams = $module->dynamicParameterDocumentation();
483 if (
$params || $dynamicParams !==
null ) {
484 $help[
'parameters'] .= Html::openElement(
'div',
485 [
'class' => [
'apihelp-block',
'apihelp-parameters' ] ]
487 $msg = $context->
msg(
'api-help-parameters' );
488 if ( !$msg->isDisabled() ) {
489 $help[
'parameters'] .= self::wrap(
490 $msg->numParams( count(
$params ) ),
'apihelp-block-head',
'div'
492 if ( !$module->isMain() ) {
494 $help[
'parameters'] .= self::wrap(
495 $context->
msg(
'api-help-parameters-note' ),
'apihelp-block-header',
'div'
499 $help[
'parameters'] .= Html::openElement(
'dl' );
501 $descriptions = $module->getFinalParamDescription();
503 foreach (
$params as $name => $settings ) {
504 $settings = $paramValidator->normalizeSettings( $settings );
506 if ( $settings[ParamValidator::PARAM_TYPE] ===
'submodule' ) {
510 $encodedParamName = $module->encodeParamName( $name );
511 $paramNameAttribs = [
'dir' =>
'ltr',
'lang' =>
'en' ];
512 if ( isset( $anchor ) ) {
513 $paramNameAttribs[
'id'] =
"$anchor:$encodedParamName";
515 $help[
'parameters'] .= Html::rawElement(
'dt', [],
516 Html::element(
'span', $paramNameAttribs, $encodedParamName )
521 if ( isset( $descriptions[$name] ) ) {
522 foreach ( $descriptions[$name] as $msg ) {
523 $msg->setContext( $context );
524 $description[] = $msg->parseAsBlock();
527 if ( !array_filter( $description ) ) {
528 $description = [ self::wrap(
529 $context->
msg(
'api-help-param-no-description' ),
535 if ( !empty( $settings[ParamValidator::PARAM_DEPRECATED] ) ) {
536 $help[
'parameters'] .= Html::openElement(
'dd',
537 [
'class' =>
'info' ] );
538 $help[
'parameters'] .= self::wrap(
539 $context->
msg(
'api-help-param-deprecated' ),
540 'apihelp-deprecated',
'strong'
542 $help[
'parameters'] .= Html::closeElement(
'dd' );
545 if ( $description ) {
546 $description = implode(
'', $description );
547 $description = preg_replace(
'!\s*</([oud]l)>\s*<\1>\s*!',
"\n", $description );
548 $help[
'parameters'] .= Html::rawElement(
'dd',
549 [
'class' =>
'description' ], $description );
554 $paramHelp = $paramValidator->getHelpInfo( $module, $name, $settings, [] );
556 unset( $paramHelp[ParamValidator::PARAM_DEPRECATED] );
558 if ( isset( $paramHelp[ParamValidator::PARAM_REQUIRED] ) ) {
559 $paramHelp[ParamValidator::PARAM_REQUIRED]->setContext( $context );
560 $info[] = $paramHelp[ParamValidator::PARAM_REQUIRED];
561 unset( $paramHelp[ParamValidator::PARAM_REQUIRED] );
567 $tag = array_shift( $i );
568 $info[] = $context->
msg(
"apihelp-{$path}-paraminfo-{$tag}" )
569 ->numParams( count( $i ) )
570 ->params( $context->
getLanguage()->commaList( $i ) )
571 ->params( $module->getModulePrefix() )
579 $msg =
'api-help-param-templated-var-first';
581 $vars[] = $context->
msg( $msg, $k, $module->encodeParamName( $v ) );
582 $msg =
'api-help-param-templated-var';
584 $info[] = $context->
msg(
'api-help-param-templated' )
585 ->numParams( count( $vars ) )
591 foreach ( $paramHelp as $m ) {
592 $m->setContext( $context );
593 $info[] = $m->parse();
596 foreach ( $info as $i ) {
597 $help[
'parameters'] .= Html::rawElement(
'dd', [
'class' =>
'info' ], $i );
601 if ( $dynamicParams !==
null ) {
602 $dynamicParams = $context->
msg(
604 $module->getModulePrefix(),
605 $module->getModuleName(),
606 $module->getModulePath()
609 $help[
'parameters'] .= Html::rawElement(
'dd',
610 [
'class' =>
'description' ], $dynamicParams->parse() );
613 $help[
'parameters'] .= Html::closeElement(
'dl' );
614 $help[
'parameters'] .= Html::closeElement(
'div' );
617 $examples = $module->getExamplesMessages();
619 $help[
'examples'] .= Html::openElement(
'div',
620 [
'class' => [
'apihelp-block',
'apihelp-examples' ] ] );
621 $msg = $context->
msg(
'api-help-examples' );
622 if ( !$msg->isDisabled() ) {
623 $help[
'examples'] .= self::wrap(
624 $msg->numParams( count( $examples ) ),
'apihelp-block-head',
'div'
628 $help[
'examples'] .= Html::openElement(
'dl' );
629 foreach ( $examples as $qs => $msg ) {
630 $msg = $context->
msg(
632 $module->getModulePrefix(),
633 $module->getModuleName(),
634 $module->getModulePath()
639 $help[
'examples'] .= Html::rawElement(
'dt', [], $msg->parse() );
640 $help[
'examples'] .= Html::rawElement(
'dd', [],
645 ],
"api.php?$qs" ) .
' ' .
646 Html::rawElement(
'a', [
'href' => $sandbox ],
647 $context->
msg(
'api-help-open-in-apisandbox' )->parse() )
650 $help[
'examples'] .= Html::closeElement(
'dl' );
651 $help[
'examples'] .= Html::closeElement(
'div' );
654 $subtocnumber = $tocnumber;
655 $subtocnumber[$level + 1] = 0;
657 'submodules' => $options[
'recursivesubmodules'],
658 'headerlevel' => $level + 1,
659 'tocnumber' => &$subtocnumber,
664 if ( $options[
'submodules'] && $module->getModuleManager() ) {
665 $manager = $module->getModuleManager();
667 foreach ( $groups as $group ) {
668 $names = $manager->getNames( $group );
670 foreach ( $names as $name ) {
671 $submodules[] = $manager->getModule( $name );
674 $help[
'submodules'] .= self::getHelpInternal(
682 $module->modifyHelp( $help, $suboptions, $haveModules );
684 $module->getHookRunner()->onAPIHelpModifyOutput( $module, $help,
685 $suboptions, $haveModules );
687 $out .= implode(
"\n", $help );
708 $errorPrinter = $main->createPrinterByName( $main->getParameter(
'format' ) );
715 ParamValidator::PARAM_DEFAULT =>
'main',
716 ParamValidator::PARAM_ISMULTI =>
true,
718 'submodules' =>
false,
719 'recursivesubmodules' =>
false,
728 =>
'apihelp-help-example-main',
729 'action=help&modules=query&submodules=1'
730 =>
'apihelp-help-example-submodules',
731 'action=help&recursivesubmodules=1'
732 =>
'apihelp-help-example-recursive',
733 'action=help&modules=help'
734 =>
'apihelp-help-example-help',
735 'action=help&modules=query+info|query+categorymembers'
736 =>
'apihelp-help-example-query',
742 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
743 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
744 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',