23use HtmlFormatter\HtmlFormatter;
37 foreach (
$params[
'modules'] as $path ) {
43 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin(
'apioutput' ) );
45 $context->setTitle( SpecialPage::getTitleFor(
'ApiHelp' ) );
47 $out->setCopyrightUrl(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
55 $html = ob_get_clean();
60 'mime' =>
'text/html',
61 'filename' =>
'api-help.html',
101 $out->addModuleStyles( [
106 $out->addModules(
'mediawiki.toc' );
110 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
116 if ( $cacheHelpTimeout > 0 ) {
121 $cached =
$cache->get( $cacheKey );
123 $out->addHTML( $cached );
128 if (
$out->getHTML() !==
'' ) {
134 $options[
'recursivesubmodules'] = !empty(
$options[
'recursivesubmodules'] );
138 if ( empty(
$options[
'nolead'] ) ) {
140 if ( !$msg->isDisabled() ) {
141 $out->addHTML( $msg->parseAsBlock() );
147 if ( !empty(
$options[
'toc'] ) && $haveModules ) {
157 if ( $cacheKey !==
null ) {
158 $cache->set( $cacheKey,
$out->getHTML(), $cacheHelpTimeout );
171 $formatter =
new HtmlFormatter(
$html );
172 $doc = $formatter->getDoc();
173 $xpath =
new DOMXPath( $doc );
174 $nodes = $xpath->query(
'//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
175 foreach ( $nodes as $node ) {
176 $href = $node->getAttribute(
'href' );
179 $href = rawurldecode( $href );
180 }
while ( $old !== $href );
181 if ( preg_match(
'!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
182 if ( isset( $localModules[$m[1]] ) ) {
183 $href = $m[2] ===
'' ?
'#' . $m[1] : $m[2];
184 } elseif ( $helptitle !==
null ) {
185 $href = Title::newFromText( str_replace(
'$1', $m[1], $helptitle ) . $m[2] )
193 $node->setAttribute(
'href', $href );
194 $node->removeAttribute(
'title' );
198 return $formatter->getText();
209 private static function wrap(
Message $msg, $class, $tag =
'span' ) {
210 return Html::rawElement( $tag, [
'class' => $class ],
230 if ( empty(
$options[
'tocnumber'] ) ) {
231 $tocnumber = [ 2 => 0 ];
233 $tocnumber = &
$options[
'tocnumber'];
237 $tocnumber[$level]++;
238 $path = $module->getModulePath();
253 while ( isset( $haveModules[$anchor] ) ) {
254 $anchor = $path .
'|' . ++$i;
257 if ( $module->isMain() ) {
258 $headerContent =
$context->
msg(
'api-help-main-header' )->parse();
260 'class' =>
'apihelp-header',
263 $name = $module->getModuleName();
264 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
266 if ( $module->getModulePrefix() !==
'' ) {
267 $headerContent .=
' ' .
268 $context->
msg(
'parentheses', $module->getModulePrefix() )->parse();
274 'class' =>
'apihelp-header apihelp-module-name',
280 $headerAttr[
'id'] = $anchor;
282 $haveModules[$anchor] = [
283 'toclevel' => count( $tocnumber ),
286 'line' => $headerContent,
287 'number' => implode(
'.', $tocnumber ),
290 if ( empty(
$options[
'noheader'] ) ) {
291 $help[
'header'] .= Html::element(
292 'h' . min( 6, $level ),
298 $haveModules[
$path] =
true;
303 for ( $m = $module; $m !==
null; $m = $m->getParent() ) {
304 $name = $m->getModuleName();
305 if ( $name ===
'main_int' ) {
310 !( !empty(
$options[
'submodules'] ) && $m->getModuleManager() )
312 $link = Html::element(
'b', [
'dir' =>
'ltr',
'lang' =>
'en' ], $name );
314 $link = SpecialPage::getTitleFor(
'ApiHelp', $m->getModulePath() )->getLocalURL();
315 $link = Html::element(
'a',
316 [
'href' =>
$link,
'class' =>
'apihelp-linktrail',
'dir' =>
'ltr',
'lang' =>
'en' ],
321 array_unshift( $links,
$link );
327 'apihelp-linktrail',
'div'
331 $flags = $module->getHelpFlags();
332 $help[
'flags'] .= Html::openElement(
'div',
333 [
'class' =>
'apihelp-block apihelp-flags' ] );
335 if ( !$msg->isDisabled() ) {
337 $msg->numParams( count( $flags ) ),
'apihelp-block-head',
'div'
340 $help[
'flags'] .= Html::openElement(
'ul' );
341 foreach ( $flags as $flag ) {
342 $help[
'flags'] .= Html::rawElement(
'li',
null,
343 self::wrap(
$context->
msg(
"api-help-flag-$flag" ),
"apihelp-flag-$flag" )
346 $sourceInfo = $module->getModuleSourceInfo();
348 if ( isset( $sourceInfo[
'namemsg'] ) ) {
349 $extname =
$context->
msg( $sourceInfo[
'namemsg'] )->text();
352 $extname = Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'name'] );
354 $help[
'flags'] .= Html::rawElement(
'li',
null,
356 $context->
msg(
'api-help-source', $extname, $sourceInfo[
'name'] ),
361 $link = SpecialPage::getTitleFor(
'Version',
'License/' . $sourceInfo[
'name'] );
362 if ( isset( $sourceInfo[
'license-name'] ) ) {
364 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $sourceInfo[
'license-name'] )
369 $msg =
$context->
msg(
'api-help-license-unknown' );
371 $help[
'flags'] .= Html::rawElement(
'li',
null,
372 self::wrap( $msg,
'apihelp-license' )
375 $help[
'flags'] .= Html::rawElement(
'li',
null,
376 self::wrap(
$context->
msg(
'api-help-source-unknown' ),
'apihelp-source' )
378 $help[
'flags'] .= Html::rawElement(
'li',
null,
379 self::wrap(
$context->
msg(
'api-help-license-unknown' ),
'apihelp-license' )
382 $help[
'flags'] .= Html::closeElement(
'ul' );
383 $help[
'flags'] .= Html::closeElement(
'div' );
385 foreach ( $module->getFinalDescription() as $msg ) {
387 $help[
'description'] .= $msg->parseAsBlock();
390 $urls = $module->getHelpUrls();
392 $help[
'help-urls'] .= Html::openElement(
'div',
393 [
'class' =>
'apihelp-block apihelp-help-urls' ]
396 if ( !$msg->isDisabled() ) {
398 $msg->numParams( count(
$urls ) ),
'apihelp-block-head',
'div'
401 if ( !is_array(
$urls ) ) {
404 $help[
'help-urls'] .= Html::openElement(
'ul' );
405 foreach (
$urls as $url ) {
406 $help[
'help-urls'] .= Html::rawElement(
'li',
null,
407 Html::element(
'a', [
'href' => $url,
'dir' =>
'ltr' ], $url )
410 $help[
'help-urls'] .= Html::closeElement(
'ul' );
411 $help[
'help-urls'] .= Html::closeElement(
'div' );
415 $dynamicParams = $module->dynamicParameterDocumentation();
417 if (
$params || $dynamicParams !==
null ) {
418 $help[
'parameters'] .= Html::openElement(
'div',
419 [
'class' =>
'apihelp-block apihelp-parameters' ]
422 if ( !$msg->isDisabled() ) {
424 $msg->numParams( count(
$params ) ),
'apihelp-block-head',
'div'
427 $help[
'parameters'] .= Html::openElement(
'dl' );
429 $descriptions = $module->getFinalParamDescription();
431 foreach (
$params as $name => $settings ) {
432 if ( !is_array( $settings ) ) {
436 $help[
'parameters'] .= Html::rawElement(
'dt',
null,
437 Html::element(
'span', [
'dir' =>
'ltr',
'lang' =>
'en' ], $module->encodeParamName( $name ) )
442 if ( isset( $descriptions[$name] ) ) {
443 foreach ( $descriptions[$name] as $msg ) {
445 $description[] = $msg->parseAsBlock();
454 $info[] =
$context->
msg(
'api-help-param-required' )->parse();
460 $tag = array_shift( $i );
461 $info[] =
$context->
msg(
"apihelp-{$path}-paraminfo-{$tag}" )
462 ->numParams( count( $i ) )
464 ->params( $module->getModulePrefix() )
474 if ( is_bool( $dflt ) ) {
476 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
478 } elseif ( is_int( $dflt ) ) {
485 $hintPipeSeparated =
true;
490 if ( is_array(
$type ) ) {
491 $count = count(
$type );
498 $values = array_map(
function ( $v ) use ( $links, $deprecatedValues ) {
502 $attr[
'dir'] =
'auto';
504 if ( isset( $deprecatedValues[$v] ) ) {
505 $attr[
'class'] =
'apihelp-deprecated-value';
507 $ret = $attr ? Html::element(
'span', $attr, $v ) : $v;
508 if ( isset( $links[$v] ) ) {
509 $ret =
"[[{$links[$v]}|$ret]]";
513 $i = array_search(
'',
$type,
true );
514 if ( $i ===
false ) {
517 unset( $values[$i] );
518 $values =
$context->
msg(
'api-help-param-list-can-be-empty' )
519 ->numParams( count( $values ) )
524 ->params( $multi ? 2 : 1 )
527 $hintPipeSeparated =
false;
537 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() .
'+' );
539 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
540 $map[$submoduleName] = $prefix . $submoduleName;
542 $defaultAttrs = [
'dir' =>
'ltr',
'lang' =>
'en' ];
547 $deprecatedSubmodules = [];
548 foreach ( $map as $v => $m ) {
549 $attrs = $defaultAttrs;
552 $submod = $module->getModuleFromPath( $m );
554 if ( $submod->isDeprecated() ) {
555 $arr = &$deprecatedSubmodules;
556 $attrs[
'class'] =
'apihelp-deprecated-value';
563 $v = Html::element(
'span', $attrs, $v );
565 $arr[] =
"[[Special:ApiHelp/{$m}|{$v}]]";
567 $submodules = array_merge( $submodules, $deprecatedSubmodules );
568 $count = count( $submodules );
570 ->params( $multi ? 2 : 1 )
573 $hintPipeSeparated =
false;
591 ->params( $multi ? 2 : 1 )
594 $hintPipeSeparated =
false;
601 $count = count( $tags );
603 ->params( $multi ? 2 : 1 )
606 $hintPipeSeparated =
false;
638 if ( $suffix !==
'' ) {
641 ->params( $multi ? 2 : 1 )
642 ->numParams( $min, $max )
666 if ( is_string(
$type ) ) {
667 $msg =
$context->
msg(
"api-help-param-type-$type" );
668 if ( !$msg->isDisabled() ) {
669 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
682 if ( $hintPipeSeparated ) {
683 $extra[] =
$context->
msg(
'api-help-param-multi-separate' )->parse();
685 if ( $count > $lowcount ) {
686 if ( $lowcount === $highcount ) {
687 $msg =
$context->
msg(
'api-help-param-multi-max-simple' )
688 ->numParams( $lowcount );
691 ->numParams( $lowcount, $highcount );
693 $extra[] = $msg->parse();
696 $info[] = implode(
' ', $extra );
706 $allSpecifier = ( is_string( $allowAll )
709 $info[] =
$context->
msg(
'api-help-param-multi-all' )
710 ->params( $allSpecifier )
716 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
717 $info[] =
$context->
msg(
'api-help-param-maxbytes' )
718 ->numParams( $settings[self::PARAM_MAX_BYTES] );
720 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
721 $info[] =
$context->
msg(
'api-help-param-maxchars' )
722 ->numParams( $settings[self::PARAM_MAX_CHARS] );
729 if ( $default ===
'' ) {
730 $info[] =
$context->
msg(
'api-help-param-default-empty' )
732 } elseif ( $default !==
null && $default !==
false ) {
734 $info[] =
$context->
msg(
'api-help-param-default' )
735 ->params( Html::element(
'span', [
'dir' =>
'auto' ], $default ) )
739 if ( !array_filter( $description ) ) {
748 $help[
'parameters'] .= Html::openElement(
'dd',
749 [
'class' =>
'info' ] );
752 'apihelp-deprecated',
'strong'
754 $help[
'parameters'] .= Html::closeElement(
'dd' );
757 if ( $description ) {
758 $description = implode(
'', $description );
759 $description = preg_replace(
'!\s*</([oud]l)>\s*<\1>\s*!',
"\n", $description );
760 $help[
'parameters'] .= Html::rawElement(
'dd',
761 [
'class' =>
'description' ], $description );
764 foreach ( $info as $i ) {
765 $help[
'parameters'] .= Html::rawElement(
'dd', [
'class' =>
'info' ], $i );
769 if ( $dynamicParams !==
null ) {
771 $module->getModulePrefix(),
772 $module->getModuleName(),
773 $module->getModulePath()
775 $help[
'parameters'] .= Html::element(
'dt',
null,
'*' );
776 $help[
'parameters'] .= Html::rawElement(
'dd',
777 [
'class' =>
'description' ], $dynamicParams->parse() );
780 $help[
'parameters'] .= Html::closeElement(
'dl' );
781 $help[
'parameters'] .= Html::closeElement(
'div' );
784 $examples = $module->getExamplesMessages();
786 $help[
'examples'] .= Html::openElement(
'div',
787 [
'class' =>
'apihelp-block apihelp-examples' ] );
789 if ( !$msg->isDisabled() ) {
791 $msg->numParams( count( $examples ) ),
'apihelp-block-head',
'div'
795 $help[
'examples'] .= Html::openElement(
'dl' );
796 foreach ( $examples as $qs => $msg ) {
798 $module->getModulePrefix(),
799 $module->getModuleName(),
800 $module->getModulePath()
804 $sandbox = SpecialPage::getTitleFor(
'ApiSandbox' )->getLocalURL() .
'#' . $qs;
805 $help[
'examples'] .= Html::rawElement(
'dt',
null, $msg->parse() );
806 $help[
'examples'] .= Html::rawElement(
'dd',
null,
807 Html::element(
'a', [
'href' =>
$link,
'dir' =>
'ltr' ],
"api.php?$qs" ) .
' ' .
808 Html::rawElement(
'a', [
'href' => $sandbox ],
809 $context->
msg(
'api-help-open-in-apisandbox' )->parse() )
812 $help[
'examples'] .= Html::closeElement(
'dl' );
813 $help[
'examples'] .= Html::closeElement(
'div' );
816 $subtocnumber = $tocnumber;
817 $subtocnumber[$level + 1] = 0;
819 'submodules' =>
$options[
'recursivesubmodules'],
820 'headerlevel' => $level + 1,
821 'tocnumber' => &$subtocnumber,
825 if (
$options[
'submodules'] && $module->getModuleManager() ) {
826 $manager = $module->getModuleManager();
828 foreach ( $groups as $group ) {
829 $names = $manager->getNames( $group );
831 foreach ( $names as $name ) {
832 $submodules[] = $manager->getModule( $name );
843 $module->modifyHelp(
$help, $suboptions, $haveModules );
845 Hooks::run(
'APIHelpModifyOutput', [ $module, &
$help, $suboptions, &$haveModules ] );
868 $errorPrinter = $main->createPrinterByName( $main->getParameter(
'format' ) );
878 'submodules' =>
false,
879 'recursivesubmodules' =>
false,
888 =>
'apihelp-help-example-main',
889 'action=help&modules=query&submodules=1'
890 =>
'apihelp-help-example-submodules',
891 'action=help&recursivesubmodules=1'
892 =>
'apihelp-help-example-recursive',
893 'action=help&modules=help'
894 =>
'apihelp-help-example-help',
895 'action=help&modules=query+info|query+categorymembers'
896 =>
'apihelp-help-example-query',
902 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
903 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
904 '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...
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
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.
getResult()
Get the result object.
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.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
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
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.