23use HtmlFormatter\HtmlFormatter;
37 foreach (
$params[
'modules'] as $path ) {
43 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin(
'apioutput' ) );
45 $context->setTitle( SpecialPage::getTitleFor(
'ApiHelp' ) );
47 $out->setRobotPolicy(
'noindex,nofollow' );
48 $out->setCopyrightUrl(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
56 $html = ob_get_clean();
61 'mime' =>
'text/html',
62 'filename' =>
'api-help.html',
100 $out->addModuleStyles( [
105 $out->addModules(
'mediawiki.toc' );
106 $out->addModuleStyles(
'mediawiki.toc.styles' );
110 $services = MediaWikiServices::getInstance();
118 if ( $cacheHelpTimeout > 0 ) {
123 $cached =
$cache->get( $cacheKey );
125 $out->addHTML( $cached );
130 if (
$out->getHTML() !==
'' ) {
136 $options[
'recursivesubmodules'] = !empty(
$options[
'recursivesubmodules'] );
140 if ( empty(
$options[
'nolead'] ) ) {
142 if ( !$msg->isDisabled() ) {
143 $out->addHTML( $msg->parseAsBlock() );
149 if ( !empty(
$options[
'toc'] ) && $haveModules ) {
154 $helptitle =
$options[
'helptitle'] ??
null;
159 if ( $cacheKey !==
null ) {
160 $cache->set( $cacheKey,
$out->getHTML(), $cacheHelpTimeout );
173 $formatter =
new HtmlFormatter(
$html );
174 $doc = $formatter->getDoc();
175 $xpath =
new DOMXPath( $doc );
176 $nodes = $xpath->query(
'//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
177 foreach ( $nodes as $node ) {
178 $href = $node->getAttribute(
'href' );
181 $href = rawurldecode( $href );
182 }
while ( $old !== $href );
183 if ( preg_match(
'!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
184 if ( isset( $localModules[$m[1]] ) ) {
185 $href = $m[2] ===
'' ?
'#' . $m[1] : $m[2];
186 } elseif ( $helptitle !==
null ) {
187 $href = Title::newFromText( str_replace(
'$1', $m[1], $helptitle ) . $m[2] )
195 $node->setAttribute(
'href', $href );
196 $node->removeAttribute(
'title' );
200 return $formatter->getText();
211 private static function wrap(
Message $msg, $class, $tag =
'span' ) {
212 return Html::rawElement( $tag, [
'class' => $class ],
232 if ( empty(
$options[
'tocnumber'] ) ) {
233 $tocnumber = [ 2 => 0 ];
235 $tocnumber = &
$options[
'tocnumber'];
239 $tocnumber[$level]++;
240 $path = $module->getModulePath();
255 while ( isset( $haveModules[$anchor] ) ) {
256 $anchor = $path .
'|' . ++$i;
259 if ( $module->isMain() ) {
260 $headerContent =
$context->
msg(
'api-help-main-header' )->parse();
262 'class' =>
'apihelp-header',
265 $name = $module->getModuleName();
266 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
268 if ( $module->getModulePrefix() !==
'' ) {
269 $headerContent .=
' ' .
270 $context->
msg(
'parentheses', $module->getModulePrefix() )->parse();
276 'class' =>
'apihelp-header apihelp-module-name',
282 $headerAttr[
'id'] = $anchor;
284 $haveModules[$anchor] = [
285 'toclevel' => count( $tocnumber ),
288 'line' => $headerContent,
289 'number' => implode(
'.', $tocnumber ),
292 if ( empty(
$options[
'noheader'] ) ) {
293 $help[
'header'] .= Html::element(
294 'h' . min( 6, $level ),
300 $haveModules[
$path] =
true;
305 for ( $m = $module; $m !==
null; $m = $m->getParent() ) {
306 $name = $m->getModuleName();
307 if ( $name ===
'main_int' ) {
312 !( !empty(
$options[
'submodules'] ) && $m->getModuleManager() )
314 $link = Html::element(
'b', [
'dir' =>
'ltr',
'lang' =>
'en' ], $name );
316 $link = SpecialPage::getTitleFor(
'ApiHelp', $m->getModulePath() )->getLocalURL();
317 $link = Html::element(
'a',
318 [
'href' =>
$link,
'class' =>
'apihelp-linktrail',
'dir' =>
'ltr',
'lang' =>
'en' ],
323 array_unshift( $links,
$link );
329 'apihelp-linktrail',
'div'
333 $flags = $module->getHelpFlags();
334 $help[
'flags'] .= Html::openElement(
'div',
335 [
'class' =>
'apihelp-block apihelp-flags' ] );
337 if ( !$msg->isDisabled() ) {
339 $msg->numParams( count( $flags ) ),
'apihelp-block-head',
'div'
342 $help[
'flags'] .= Html::openElement(
'ul' );
343 foreach ( $flags as $flag ) {
344 $help[
'flags'] .= Html::rawElement(
'li',
null,
345 self::wrap(
$context->
msg(
"api-help-flag-$flag" ),
"apihelp-flag-$flag" )
348 $sourceInfo = $module->getModuleSourceInfo();
350 if ( isset( $sourceInfo[
'namemsg'] ) ) {
351 $extname =
$context->
msg( $sourceInfo[
'namemsg'] )->text();
354 $extname = Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'name'] );
356 $help[
'flags'] .= Html::rawElement(
'li',
null,
358 $context->
msg(
'api-help-source', $extname, $sourceInfo[
'name'] ),
363 $link = SpecialPage::getTitleFor(
'Version',
'License/' . $sourceInfo[
'name'] );
364 if ( isset( $sourceInfo[
'license-name'] ) ) {
366 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'license-name'] )
371 $msg =
$context->
msg(
'api-help-license-unknown' );
373 $help[
'flags'] .= Html::rawElement(
'li',
null,
374 self::wrap( $msg,
'apihelp-license' )
377 $help[
'flags'] .= Html::rawElement(
'li',
null,
378 self::wrap(
$context->
msg(
'api-help-source-unknown' ),
'apihelp-source' )
380 $help[
'flags'] .= Html::rawElement(
'li',
null,
381 self::wrap(
$context->
msg(
'api-help-license-unknown' ),
'apihelp-license' )
384 $help[
'flags'] .= Html::closeElement(
'ul' );
385 $help[
'flags'] .= Html::closeElement(
'div' );
387 foreach ( $module->getFinalDescription() as $msg ) {
389 $help[
'description'] .= $msg->parseAsBlock();
392 $urls = $module->getHelpUrls();
394 $help[
'help-urls'] .= Html::openElement(
'div',
395 [
'class' =>
'apihelp-block apihelp-help-urls' ]
398 if ( !$msg->isDisabled() ) {
400 $msg->numParams( count(
$urls ) ),
'apihelp-block-head',
'div'
403 if ( !is_array(
$urls ) ) {
406 $help[
'help-urls'] .= Html::openElement(
'ul' );
407 foreach (
$urls as $url ) {
408 $help[
'help-urls'] .= Html::rawElement(
'li',
null,
409 Html::element(
'a', [
'href' => $url,
'dir' =>
'ltr' ], $url )
412 $help[
'help-urls'] .= Html::closeElement(
'ul' );
413 $help[
'help-urls'] .= Html::closeElement(
'div' );
417 $dynamicParams = $module->dynamicParameterDocumentation();
419 if (
$params || $dynamicParams !==
null ) {
420 $help[
'parameters'] .= Html::openElement(
'div',
421 [
'class' =>
'apihelp-block apihelp-parameters' ]
424 if ( !$msg->isDisabled() ) {
426 $msg->numParams( count(
$params ) ),
'apihelp-block-head',
'div'
429 $help[
'parameters'] .= Html::openElement(
'dl' );
431 $descriptions = $module->getFinalParamDescription();
433 foreach (
$params as $name => $settings ) {
434 if ( !is_array( $settings ) ) {
438 $help[
'parameters'] .= Html::rawElement(
'dt',
null,
439 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $module->encodeParamName( $name ) )
444 if ( isset( $descriptions[$name] ) ) {
445 foreach ( $descriptions[$name] as $msg ) {
447 $description[] = $msg->parseAsBlock();
456 $info[] =
$context->
msg(
'api-help-param-required' )->parse();
462 $tag = array_shift( $i );
463 $info[] =
$context->
msg(
"apihelp-{$path}-paraminfo-{$tag}" )
464 ->numParams( count( $i ) )
466 ->params( $module->getModulePrefix() )
474 $msg =
'api-help-param-templated-var-first';
477 $msg =
'api-help-param-templated-var';
479 $info[] =
$context->
msg(
'api-help-param-templated' )
480 ->numParams( count(
$vars ) )
481 ->params( Message::listParam(
$vars ) )
488 if ( is_bool( $dflt ) ) {
490 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
492 } elseif ( is_int( $dflt ) ) {
499 $hintPipeSeparated =
true;
504 if ( is_array(
$type ) ) {
505 $count = count(
$type );
508 $values = array_map(
function ( $v ) use ( $links, $deprecatedValues ) {
512 $attr[
'dir'] =
'auto';
514 if ( isset( $deprecatedValues[$v] ) ) {
515 $attr[
'class'] =
'apihelp-deprecated-value';
517 $ret = $attr ? Html::element(
'span', $attr, $v ) : $v;
518 if ( isset( $links[$v] ) ) {
519 $ret =
"[[{$links[$v]}|$ret]]";
523 $i = array_search(
'',
$type,
true );
524 if ( $i ===
false ) {
527 unset( $values[$i] );
528 $values =
$context->
msg(
'api-help-param-list-can-be-empty' )
529 ->numParams( count( $values ) )
534 ->params( $multi ? 2 : 1 )
537 $hintPipeSeparated =
false;
547 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() .
'+' );
549 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
550 $map[$submoduleName] = $prefix . $submoduleName;
552 $defaultAttrs = [
'dir' =>
'ltr',
'lang' =>
'en' ];
557 $deprecatedSubmodules = [];
558 foreach ( $map as $v => $m ) {
559 $attrs = $defaultAttrs;
562 $submod = $module->getModuleFromPath( $m );
564 if ( $submod->isDeprecated() ) {
565 $arr = &$deprecatedSubmodules;
566 $attrs[
'class'] =
'apihelp-deprecated-value';
573 $v = Html::element(
'span', $attrs, $v );
575 $arr[] =
"[[Special:ApiHelp/{$m}|{$v}]]";
577 $submodules = array_merge( $submodules, $deprecatedSubmodules );
578 $count = count( $submodules );
580 ->params( $multi ? 2 : 1 )
583 $hintPipeSeparated =
false;
598 ->params( $multi ? 2 : 1 )
601 $hintPipeSeparated =
false;
607 $tags = ChangeTags::listExplicitlyDefinedTags();
608 $count = count( $tags );
610 ->params( $multi ? 2 : 1 )
613 $hintPipeSeparated =
false;
645 if ( $suffix !==
'' ) {
648 ->params( $multi ? 2 : 1 )
649 ->numParams( $min, $max )
673 if ( is_string(
$type ) ) {
674 $msg =
$context->
msg(
"api-help-param-type-$type" );
675 if ( !$msg->isDisabled() ) {
676 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
689 if ( $hintPipeSeparated ) {
690 $extra[] =
$context->
msg(
'api-help-param-multi-separate' )->parse();
692 if ( $count > $lowcount ) {
693 if ( $lowcount === $highcount ) {
694 $msg =
$context->
msg(
'api-help-param-multi-max-simple' )
695 ->numParams( $lowcount );
698 ->numParams( $lowcount, $highcount );
700 $extra[] = $msg->parse();
703 $info[] = implode(
' ', $extra );
713 $info[] =
$context->
msg(
'api-help-param-multi-all' )
714 ->params( $allSpecifier )
720 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
721 $info[] =
$context->
msg(
'api-help-param-maxbytes' )
722 ->numParams( $settings[self::PARAM_MAX_BYTES] );
724 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
725 $info[] =
$context->
msg(
'api-help-param-maxchars' )
726 ->numParams( $settings[self::PARAM_MAX_CHARS] );
731 if ( $default ===
'' ) {
732 $info[] =
$context->
msg(
'api-help-param-default-empty' )
734 } elseif ( $default !==
null && $default !==
false ) {
736 $info[] =
$context->
msg(
'api-help-param-default' )
737 ->params( Html::element(
'span', [
'dir' =>
'auto' ], $default ) )
741 if ( !array_filter( $description ) ) {
750 $help[
'parameters'] .= Html::openElement(
'dd',
751 [
'class' =>
'info' ] );
754 'apihelp-deprecated',
'strong'
756 $help[
'parameters'] .= Html::closeElement(
'dd' );
759 if ( $description ) {
760 $description = implode(
'', $description );
761 $description = preg_replace(
'!\s*</([oud]l)>\s*<\1>\s*!',
"\n", $description );
762 $help[
'parameters'] .= Html::rawElement(
'dd',
763 [
'class' =>
'description' ], $description );
766 foreach ( $info as $i ) {
767 $help[
'parameters'] .= Html::rawElement(
'dd', [
'class' =>
'info' ], $i );
771 if ( $dynamicParams !==
null ) {
773 $module->getModulePrefix(),
774 $module->getModuleName(),
775 $module->getModulePath()
777 $help[
'parameters'] .= Html::element(
'dt',
null,
'*' );
778 $help[
'parameters'] .= Html::rawElement(
'dd',
779 [
'class' =>
'description' ], $dynamicParams->parse() );
782 $help[
'parameters'] .= Html::closeElement(
'dl' );
783 $help[
'parameters'] .= Html::closeElement(
'div' );
786 $examples = $module->getExamplesMessages();
788 $help[
'examples'] .= Html::openElement(
'div',
789 [
'class' =>
'apihelp-block apihelp-examples' ] );
791 if ( !$msg->isDisabled() ) {
793 $msg->numParams( count( $examples ) ),
'apihelp-block-head',
'div'
797 $help[
'examples'] .= Html::openElement(
'dl' );
798 foreach ( $examples as $qs => $msg ) {
800 $module->getModulePrefix(),
801 $module->getModuleName(),
802 $module->getModulePath()
806 $sandbox = SpecialPage::getTitleFor(
'ApiSandbox' )->getLocalURL() .
'#' . $qs;
807 $help[
'examples'] .= Html::rawElement(
'dt',
null, $msg->parse() );
808 $help[
'examples'] .= Html::rawElement(
'dd',
null,
809 Html::element(
'a', [
'href' =>
$link,
'dir' =>
'ltr' ],
"api.php?$qs" ) .
' ' .
810 Html::rawElement(
'a', [
'href' => $sandbox ],
811 $context->
msg(
'api-help-open-in-apisandbox' )->parse() )
814 $help[
'examples'] .= Html::closeElement(
'dl' );
815 $help[
'examples'] .= Html::closeElement(
'div' );
818 $subtocnumber = $tocnumber;
819 $subtocnumber[$level + 1] = 0;
821 'submodules' =>
$options[
'recursivesubmodules'],
822 'headerlevel' => $level + 1,
823 'tocnumber' => &$subtocnumber,
827 if (
$options[
'submodules'] && $module->getModuleManager() ) {
828 $manager = $module->getModuleManager();
830 foreach ( $groups as $group ) {
831 $names = $manager->getNames( $group );
833 foreach ( $names as $name ) {
834 $submodules[] = $manager->getModule( $name );
845 $module->modifyHelp(
$help, $suboptions, $haveModules );
847 Hooks::run(
'APIHelpModifyOutput', [ $module, &
$help, $suboptions, &$haveModules ] );
870 $errorPrinter = $main->createPrinterByName( $main->getParameter(
'format' ) );
880 'submodules' =>
false,
881 'recursivesubmodules' =>
false,
890 =>
'apihelp-help-example-main',
891 'action=help&modules=query&submodules=1'
892 =>
'apihelp-help-example-submodules',
893 'action=help&recursivesubmodules=1'
894 =>
'apihelp-help-example-recursive',
895 'action=help&modules=help'
896 =>
'apihelp-help-example-help',
897 'action=help&modules=query+info|query+categorymembers'
898 =>
'apihelp-help-example-query',
904 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
905 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
906 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
This abstract class implements many basic API functions, and is the base of all API classes.
const PARAM_REQUIRED
(boolean) Is the parameter required?
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
const PARAM_SUBMODULE_MAP
(string[]) When PARAM_TYPE is 'submodule', map parameter values to submodule paths.
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
getModuleFromPath( $path)
Get a module from its module path.
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
const PARAM_DEPRECATED_VALUES
(array) When PARAM_TYPE is an array, this indicates which of the values are deprecated.
const PARAM_ISMULTI_LIMIT1
(integer) Maximum number of values, for normal users.
getMain()
Get the main module.
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
const PARAM_HELP_MSG_INFO
(array) Specify additional information tags for the parameter.
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
const PARAM_VALUE_LINKS
(string[]) When PARAM_TYPE is an array, this may be an array mapping those values to page titles whic...
const PARAM_ISMULTI_LIMIT2
(integer) Maximum number of values, for users with the apihighimits right.
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
const LIMIT_SML2
Slow query, apihighlimits limit.
const PARAM_TEMPLATE_VARS
(array) Indicate that this is a templated parameter, and specify replacements.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
getModulePath()
Get the path to this module.
const PARAM_EXTRA_NAMESPACES
(int[]) When PARAM_TYPE is 'namespace', include these as additional possible values.
const LIMIT_SML1
Slow query, standard limit.
const PARAM_ALL
(boolean|string) When PARAM_TYPE has a defined set of values and PARAM_ISMULTI is true,...
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
getModuleName()
Get the name of the module being executed by this instance.
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Class to output help for an API module.
static wrap(Message $msg, $class, $tag='span')
Wrap a message in HTML with a class.
isReadMode()
Indicates whether this module requires read rights.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
static fixHelpLinks( $html, $helptitle=null, $localModules=[])
Replace Special:ApiHelp links with links to api.php.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
static getHelpInternal(IContextSource $context, array $modules, array $options, &$haveModules)
Recursively-called function to actually construct the help.
static getHelp(IContextSource $context, $modules, array $options)
Generate help for the specified modules.
shouldCheckMaxlag()
Indicates if this module needs maxlag to be checked.
getCustomPrinter()
If the module may only be used with a certain format module, it should override this method to return...
This is the main API class, used for both external and internal processing.
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
static setSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as subelements rather than attributes.
Exception used to abort API execution with an error.
getContext()
Get the base IContextSource object.
An IContextSource implementation which will inherit context from another source but allow individual ...
static generateTOC( $tree, $lang=false)
Generate a table of contents from a section tree.
The Message class provides methods which fulfil two basic services:
This class should be covered by a general architecture document which does not exist as of January 20...
static getExtLicenseFileName( $extDir)
Obtains the full path of an extensions copying or license file if one exists.
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
namespace and then decline to actually register it & $namespaces
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Allows to change the fields on the form that will be generated $name
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
msg( $key)
This is the method for getting translated interface messages.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))