Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiHelp.php
blobe347a9f2c526acd261262ddc8ad113d73b293d2b
1 <?php
2 /**
5 * Created on Aug 29, 2014
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 use HtmlFormatter\HtmlFormatter;
29 /**
30 * Class to output help for an API module
32 * @since 1.25 completely rewritten
33 * @ingroup API
35 class ApiHelp extends ApiBase {
36 public function execute() {
37 $params = $this->extractRequestParams();
38 $modules = [];
40 foreach ( $params['modules'] as $path ) {
41 $modules[] = $this->getModuleFromPath( $path );
44 // Get the help
45 $context = new DerivativeContext( $this->getMain()->getContext() );
46 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
47 $context->setLanguage( $this->getMain()->getLanguage() );
48 $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
49 $out = new OutputPage( $context );
50 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
51 $context->setOutput( $out );
53 self::getHelp( $context, $modules, $params );
55 // Grab the output from the skin
56 ob_start();
57 $context->getOutput()->output();
58 $html = ob_get_clean();
60 $result = $this->getResult();
61 if ( $params['wrap'] ) {
62 $data = [
63 'mime' => 'text/html',
64 'help' => $html,
66 ApiResult::setSubelementsList( $data, 'help' );
67 $result->addValue( null, $this->getModuleName(), $data );
68 } else {
69 $result->reset();
70 $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
71 $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
75 /**
76 * Generate help for the specified modules
78 * Help is placed into the OutputPage object returned by
79 * $context->getOutput().
81 * Recognized options include:
82 * - headerlevel: (int) Header tag level
83 * - nolead: (bool) Skip the inclusion of api-help-lead
84 * - noheader: (bool) Skip the inclusion of the top-level section headers
85 * - submodules: (bool) Include help for submodules of the current module
86 * - recursivesubmodules: (bool) Include help for submodules recursively
87 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
88 * - toc: (bool) Include a table of contents
90 * @param IContextSource $context
91 * @param ApiBase[]|ApiBase $modules
92 * @param array $options Formatting options (described above)
94 public static function getHelp( IContextSource $context, $modules, array $options ) {
95 global $wgContLang;
97 if ( !is_array( $modules ) ) {
98 $modules = [ $modules ];
101 $out = $context->getOutput();
102 $out->addModuleStyles( [
103 'mediawiki.hlist',
104 'mediawiki.apihelp',
105 ] );
106 if ( !empty( $options['toc'] ) ) {
107 $out->addModules( 'mediawiki.toc' );
109 $out->setPageTitle( $context->msg( 'api-help-title' ) );
111 $cache = ObjectCache::getMainWANInstance();
112 $cacheKey = null;
113 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain &&
114 $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
116 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
117 if ( $cacheHelpTimeout > 0 ) {
118 // Get help text from cache if present
119 $cacheKey = wfMemcKey( 'apihelp', $modules[0]->getModulePath(),
120 (int)!empty( $options['toc'] ),
121 str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) );
122 $cached = $cache->get( $cacheKey );
123 if ( $cached ) {
124 $out->addHTML( $cached );
125 return;
129 if ( $out->getHTML() !== '' ) {
130 // Don't save to cache, there's someone else's content in the page
131 // already
132 $cacheKey = null;
135 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
136 $options['submodules'] = $options['recursivesubmodules'] || !empty( $options['submodules'] );
138 // Prepend lead
139 if ( empty( $options['nolead'] ) ) {
140 $msg = $context->msg( 'api-help-lead' );
141 if ( !$msg->isDisabled() ) {
142 $out->addHTML( $msg->parseAsBlock() );
146 $haveModules = [];
147 $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
148 if ( !empty( $options['toc'] ) && $haveModules ) {
149 $out->addHTML( Linker::generateTOC( $haveModules, $context->getLanguage() ) );
151 $out->addHTML( $html );
153 $helptitle = isset( $options['helptitle'] ) ? $options['helptitle'] : null;
154 $html = self::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
155 $out->clearHTML();
156 $out->addHTML( $html );
158 if ( $cacheKey !== null ) {
159 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
164 * Replace Special:ApiHelp links with links to api.php
166 * @param string $html
167 * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
168 * @param array $localModules Keys are modules to link within the current page, values are ignored
169 * @return string
171 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
172 $formatter = new HtmlFormatter( $html );
173 $doc = $formatter->getDoc();
174 $xpath = new DOMXPath( $doc );
175 $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
176 foreach ( $nodes as $node ) {
177 $href = $node->getAttribute( 'href' );
178 do {
179 $old = $href;
180 $href = rawurldecode( $href );
181 } while ( $old !== $href );
182 if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
183 if ( isset( $localModules[$m[1]] ) ) {
184 $href = $m[2] === '' ? '#' . $m[1] : $m[2];
185 } elseif ( $helptitle !== null ) {
186 $href = Title::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
187 ->getFullURL();
188 } else {
189 $href = wfAppendQuery( wfScript( 'api' ), [
190 'action' => 'help',
191 'modules' => $m[1],
192 ] ) . $m[2];
194 $node->setAttribute( 'href', $href );
195 $node->removeAttribute( 'title' );
199 return $formatter->getText();
203 * Wrap a message in HTML with a class.
205 * @param Message $msg
206 * @param string $class
207 * @param string $tag
208 * @return string
210 private static function wrap( Message $msg, $class, $tag = 'span' ) {
211 return Html::rawElement( $tag, [ 'class' => $class ],
212 $msg->parse()
217 * Recursively-called function to actually construct the help
219 * @param IContextSource $context
220 * @param ApiBase[] $modules
221 * @param array $options
222 * @param array &$haveModules
223 * @return string
225 private static function getHelpInternal( IContextSource $context, array $modules,
226 array $options, &$haveModules
228 $out = '';
230 $level = empty( $options['headerlevel'] ) ? 2 : $options['headerlevel'];
231 if ( empty( $options['tocnumber'] ) ) {
232 $tocnumber = [ 2 => 0 ];
233 } else {
234 $tocnumber = &$options['tocnumber'];
237 foreach ( $modules as $module ) {
238 $tocnumber[$level]++;
239 $path = $module->getModulePath();
240 $module->setContext( $context );
241 $help = [
242 'header' => '',
243 'flags' => '',
244 'description' => '',
245 'help-urls' => '',
246 'parameters' => '',
247 'examples' => '',
248 'submodules' => '',
251 if ( empty( $options['noheader'] ) || !empty( $options['toc'] ) ) {
252 $anchor = $path;
253 $i = 1;
254 while ( isset( $haveModules[$anchor] ) ) {
255 $anchor = $path . '|' . ++$i;
258 if ( $module->isMain() ) {
259 $headerContent = $context->msg( 'api-help-main-header' )->parse();
260 $headerAttr = [
261 'class' => 'apihelp-header',
263 } else {
264 $name = $module->getModuleName();
265 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
266 "=$name";
267 if ( $module->getModulePrefix() !== '' ) {
268 $headerContent .= ' ' .
269 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
271 // Module names are always in English and not localized,
272 // so English language and direction must be set explicitly,
273 // otherwise parentheses will get broken in RTL wikis
274 $headerAttr = [
275 'class' => 'apihelp-header apihelp-module-name',
276 'dir' => 'ltr',
277 'lang' => 'en',
281 $headerAttr['id'] = $anchor;
283 $haveModules[$anchor] = [
284 'toclevel' => count( $tocnumber ),
285 'level' => $level,
286 'anchor' => $anchor,
287 'line' => $headerContent,
288 'number' => implode( '.', $tocnumber ),
289 'index' => false,
291 if ( empty( $options['noheader'] ) ) {
292 $help['header'] .= Html::element(
293 'h' . min( 6, $level ),
294 $headerAttr,
295 $headerContent
298 } else {
299 $haveModules[$path] = true;
302 $links = [];
303 $any = false;
304 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
305 $name = $m->getModuleName();
306 if ( $name === 'main_int' ) {
307 $name = 'main';
310 if ( count( $modules ) === 1 && $m === $modules[0] &&
311 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
313 $link = Html::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
314 } else {
315 $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
316 $link = Html::element( 'a',
317 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
318 $name
320 $any = true;
322 array_unshift( $links, $link );
324 if ( $any ) {
325 $help['header'] .= self::wrap(
326 $context->msg( 'parentheses' )
327 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
328 'apihelp-linktrail', 'div'
332 $flags = $module->getHelpFlags();
333 $help['flags'] .= Html::openElement( 'div',
334 [ 'class' => 'apihelp-block apihelp-flags' ] );
335 $msg = $context->msg( 'api-help-flags' );
336 if ( !$msg->isDisabled() ) {
337 $help['flags'] .= self::wrap(
338 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
341 $help['flags'] .= Html::openElement( 'ul' );
342 foreach ( $flags as $flag ) {
343 $help['flags'] .= Html::rawElement( 'li', null,
344 self::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
347 $sourceInfo = $module->getModuleSourceInfo();
348 if ( $sourceInfo ) {
349 if ( isset( $sourceInfo['namemsg'] ) ) {
350 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
351 } else {
352 // Probably English, so wrap it.
353 $extname = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
355 $help['flags'] .= Html::rawElement( 'li', null,
356 self::wrap(
357 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
358 'apihelp-source'
362 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
363 if ( isset( $sourceInfo['license-name'] ) ) {
364 $msg = $context->msg( 'api-help-license', $link,
365 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
367 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
368 $msg = $context->msg( 'api-help-license-noname', $link );
369 } else {
370 $msg = $context->msg( 'api-help-license-unknown' );
372 $help['flags'] .= Html::rawElement( 'li', null,
373 self::wrap( $msg, 'apihelp-license' )
375 } else {
376 $help['flags'] .= Html::rawElement( 'li', null,
377 self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
379 $help['flags'] .= Html::rawElement( 'li', null,
380 self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
383 $help['flags'] .= Html::closeElement( 'ul' );
384 $help['flags'] .= Html::closeElement( 'div' );
386 foreach ( $module->getFinalDescription() as $msg ) {
387 $msg->setContext( $context );
388 $help['description'] .= $msg->parseAsBlock();
391 $urls = $module->getHelpUrls();
392 if ( $urls ) {
393 $help['help-urls'] .= Html::openElement( 'div',
394 [ 'class' => 'apihelp-block apihelp-help-urls' ]
396 $msg = $context->msg( 'api-help-help-urls' );
397 if ( !$msg->isDisabled() ) {
398 $help['help-urls'] .= self::wrap(
399 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
402 if ( !is_array( $urls ) ) {
403 $urls = [ $urls ];
405 $help['help-urls'] .= Html::openElement( 'ul' );
406 foreach ( $urls as $url ) {
407 $help['help-urls'] .= Html::rawElement( 'li', null,
408 Html::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
411 $help['help-urls'] .= Html::closeElement( 'ul' );
412 $help['help-urls'] .= Html::closeElement( 'div' );
415 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
416 $dynamicParams = $module->dynamicParameterDocumentation();
417 $groups = [];
418 if ( $params || $dynamicParams !== null ) {
419 $help['parameters'] .= Html::openElement( 'div',
420 [ 'class' => 'apihelp-block apihelp-parameters' ]
422 $msg = $context->msg( 'api-help-parameters' );
423 if ( !$msg->isDisabled() ) {
424 $help['parameters'] .= self::wrap(
425 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
428 $help['parameters'] .= Html::openElement( 'dl' );
430 $descriptions = $module->getFinalParamDescription();
432 foreach ( $params as $name => $settings ) {
433 if ( !is_array( $settings ) ) {
434 $settings = [ ApiBase::PARAM_DFLT => $settings ];
437 $help['parameters'] .= Html::rawElement( 'dt', null,
438 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
441 // Add description
442 $description = [];
443 if ( isset( $descriptions[$name] ) ) {
444 foreach ( $descriptions[$name] as $msg ) {
445 $msg->setContext( $context );
446 $description[] = $msg->parseAsBlock();
450 // Add usage info
451 $info = [];
453 // Required?
454 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
455 $info[] = $context->msg( 'api-help-param-required' )->parse();
458 // Custom info?
459 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
460 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
461 $tag = array_shift( $i );
462 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
463 ->numParams( count( $i ) )
464 ->params( $context->getLanguage()->commaList( $i ) )
465 ->params( $module->getModulePrefix() )
466 ->parse();
470 // Type documentation
471 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
472 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
473 ? $settings[ApiBase::PARAM_DFLT]
474 : null;
475 if ( is_bool( $dflt ) ) {
476 $settings[ApiBase::PARAM_TYPE] = 'boolean';
477 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
478 $settings[ApiBase::PARAM_TYPE] = 'string';
479 } elseif ( is_int( $dflt ) ) {
480 $settings[ApiBase::PARAM_TYPE] = 'integer';
483 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
484 $type = $settings[ApiBase::PARAM_TYPE];
485 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
486 $hintPipeSeparated = true;
487 $count = ApiBase::LIMIT_SML2 + 1;
489 if ( is_array( $type ) ) {
490 $count = count( $type );
491 $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
492 ? $settings[ApiBase::PARAM_VALUE_LINKS]
493 : [];
494 $values = array_map( function ( $v ) use ( $links ) {
495 // We can't know whether this contains LTR or RTL text.
496 $ret = $v === '' ? $v : Html::element( 'span', [ 'dir' => 'auto' ], $v );
497 if ( isset( $links[$v] ) ) {
498 $ret = "[[{$links[$v]}|$ret]]";
500 return $ret;
501 }, $type );
502 $i = array_search( '', $type, true );
503 if ( $i === false ) {
504 $values = $context->getLanguage()->commaList( $values );
505 } else {
506 unset( $values[$i] );
507 $values = $context->msg( 'api-help-param-list-can-be-empty' )
508 ->numParams( count( $values ) )
509 ->params( $context->getLanguage()->commaList( $values ) )
510 ->parse();
512 $info[] = $context->msg( 'api-help-param-list' )
513 ->params( $multi ? 2 : 1 )
514 ->params( $values )
515 ->parse();
516 $hintPipeSeparated = false;
517 } else {
518 switch ( $type ) {
519 case 'submodule':
520 $groups[] = $name;
521 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
522 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
523 ksort( $map );
524 $submodules = [];
525 foreach ( $map as $v => $m ) {
526 $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
528 } else {
529 $submodules = $module->getModuleManager()->getNames( $name );
530 sort( $submodules );
531 $prefix = $module->isMain()
532 ? '' : ( $module->getModulePath() . '+' );
533 $submodules = array_map( function ( $name ) use ( $prefix ) {
534 $text = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
535 return "[[Special:ApiHelp/{$prefix}{$name}|{$text}]]";
536 }, $submodules );
538 $count = count( $submodules );
539 $info[] = $context->msg( 'api-help-param-list' )
540 ->params( $multi ? 2 : 1 )
541 ->params( $context->getLanguage()->commaList( $submodules ) )
542 ->parse();
543 $hintPipeSeparated = false;
544 // No type message necessary, we have a list of values.
545 $type = null;
546 break;
548 case 'namespace':
549 $namespaces = MWNamespace::getValidNamespaces();
550 if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
551 is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
553 $namespaces = array_merge( $namespaces, $settings[ApiBase::PARAM_EXTRA_NAMESPACES] );
555 sort( $namespaces );
556 $count = count( $namespaces );
557 $info[] = $context->msg( 'api-help-param-list' )
558 ->params( $multi ? 2 : 1 )
559 ->params( $context->getLanguage()->commaList( $namespaces ) )
560 ->parse();
561 $hintPipeSeparated = false;
562 // No type message necessary, we have a list of values.
563 $type = null;
564 break;
566 case 'tags':
567 $tags = ChangeTags::listExplicitlyDefinedTags();
568 $count = count( $tags );
569 $info[] = $context->msg( 'api-help-param-list' )
570 ->params( $multi ? 2 : 1 )
571 ->params( $context->getLanguage()->commaList( $tags ) )
572 ->parse();
573 $hintPipeSeparated = false;
574 $type = null;
575 break;
577 case 'limit':
578 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
579 $info[] = $context->msg( 'api-help-param-limit2' )
580 ->numParams( $settings[ApiBase::PARAM_MAX] )
581 ->numParams( $settings[ApiBase::PARAM_MAX2] )
582 ->parse();
583 } else {
584 $info[] = $context->msg( 'api-help-param-limit' )
585 ->numParams( $settings[ApiBase::PARAM_MAX] )
586 ->parse();
588 break;
590 case 'integer':
591 // Possible messages:
592 // api-help-param-integer-min,
593 // api-help-param-integer-max,
594 // api-help-param-integer-minmax
595 $suffix = '';
596 $min = $max = 0;
597 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
598 $suffix .= 'min';
599 $min = $settings[ApiBase::PARAM_MIN];
601 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
602 $suffix .= 'max';
603 $max = $settings[ApiBase::PARAM_MAX];
605 if ( $suffix !== '' ) {
606 $info[] =
607 $context->msg( "api-help-param-integer-$suffix" )
608 ->params( $multi ? 2 : 1 )
609 ->numParams( $min, $max )
610 ->parse();
612 break;
614 case 'upload':
615 $info[] = $context->msg( 'api-help-param-upload' )
616 ->parse();
617 // No type message necessary, api-help-param-upload should handle it.
618 $type = null;
619 break;
621 case 'string':
622 case 'text':
623 // Displaying a type message here would be useless.
624 $type = null;
625 break;
629 // Add type. Messages for grep: api-help-param-type-limit
630 // api-help-param-type-integer api-help-param-type-boolean
631 // api-help-param-type-timestamp api-help-param-type-user
632 // api-help-param-type-password
633 if ( is_string( $type ) ) {
634 $msg = $context->msg( "api-help-param-type-$type" );
635 if ( !$msg->isDisabled() ) {
636 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
640 if ( $multi ) {
641 $extra = [];
642 if ( $hintPipeSeparated ) {
643 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
645 if ( $count > ApiBase::LIMIT_SML1 ) {
646 $extra[] = $context->msg( 'api-help-param-multi-max' )
647 ->numParams( ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2 )
648 ->parse();
650 if ( $extra ) {
651 $info[] = implode( ' ', $extra );
654 $allowAll = isset( $settings[ApiBase::PARAM_ALL] )
655 ? $settings[ApiBase::PARAM_ALL]
656 : false;
657 if ( $allowAll || $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
658 if ( $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
659 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
660 } else {
661 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
663 $info[] = $context->msg( 'api-help-param-multi-all' )
664 ->params( $allSpecifier )
665 ->parse();
670 // Add default
671 $default = isset( $settings[ApiBase::PARAM_DFLT] )
672 ? $settings[ApiBase::PARAM_DFLT]
673 : null;
674 if ( $default === '' ) {
675 $info[] = $context->msg( 'api-help-param-default-empty' )
676 ->parse();
677 } elseif ( $default !== null && $default !== false ) {
678 // We can't know whether this contains LTR or RTL text.
679 $info[] = $context->msg( 'api-help-param-default' )
680 ->params( Html::element( 'span', [ 'dir' => 'auto' ], $default ) )
681 ->parse();
684 if ( !array_filter( $description ) ) {
685 $description = [ self::wrap(
686 $context->msg( 'api-help-param-no-description' ),
687 'apihelp-empty'
688 ) ];
691 // Add "deprecated" flag
692 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
693 $help['parameters'] .= Html::openElement( 'dd',
694 [ 'class' => 'info' ] );
695 $help['parameters'] .= self::wrap(
696 $context->msg( 'api-help-param-deprecated' ),
697 'apihelp-deprecated', 'strong'
699 $help['parameters'] .= Html::closeElement( 'dd' );
702 if ( $description ) {
703 $description = implode( '', $description );
704 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
705 $help['parameters'] .= Html::rawElement( 'dd',
706 [ 'class' => 'description' ], $description );
709 foreach ( $info as $i ) {
710 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
714 if ( $dynamicParams !== null ) {
715 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
716 $module->getModulePrefix(),
717 $module->getModuleName(),
718 $module->getModulePath()
719 ] );
720 $help['parameters'] .= Html::element( 'dt', null, '*' );
721 $help['parameters'] .= Html::rawElement( 'dd',
722 [ 'class' => 'description' ], $dynamicParams->parse() );
725 $help['parameters'] .= Html::closeElement( 'dl' );
726 $help['parameters'] .= Html::closeElement( 'div' );
729 $examples = $module->getExamplesMessages();
730 if ( $examples ) {
731 $help['examples'] .= Html::openElement( 'div',
732 [ 'class' => 'apihelp-block apihelp-examples' ] );
733 $msg = $context->msg( 'api-help-examples' );
734 if ( !$msg->isDisabled() ) {
735 $help['examples'] .= self::wrap(
736 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
740 $help['examples'] .= Html::openElement( 'dl' );
741 foreach ( $examples as $qs => $msg ) {
742 $msg = ApiBase::makeMessage( $msg, $context, [
743 $module->getModulePrefix(),
744 $module->getModuleName(),
745 $module->getModulePath()
746 ] );
748 $link = wfAppendQuery( wfScript( 'api' ), $qs );
749 $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
750 $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
751 $help['examples'] .= Html::rawElement( 'dd', null,
752 Html::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
753 Html::rawElement( 'a', [ 'href' => $sandbox ],
754 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
757 $help['examples'] .= Html::closeElement( 'dl' );
758 $help['examples'] .= Html::closeElement( 'div' );
761 $subtocnumber = $tocnumber;
762 $subtocnumber[$level + 1] = 0;
763 $suboptions = [
764 'submodules' => $options['recursivesubmodules'],
765 'headerlevel' => $level + 1,
766 'tocnumber' => &$subtocnumber,
767 'noheader' => false,
768 ] + $options;
770 if ( $options['submodules'] && $module->getModuleManager() ) {
771 $manager = $module->getModuleManager();
772 $submodules = [];
773 foreach ( $groups as $group ) {
774 $names = $manager->getNames( $group );
775 sort( $names );
776 foreach ( $names as $name ) {
777 $submodules[] = $manager->getModule( $name );
780 $help['submodules'] .= self::getHelpInternal(
781 $context,
782 $submodules,
783 $suboptions,
784 $haveModules
788 $module->modifyHelp( $help, $suboptions, $haveModules );
790 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
792 $out .= implode( "\n", $help );
795 return $out;
798 public function shouldCheckMaxlag() {
799 return false;
802 public function isReadMode() {
803 return false;
806 public function getCustomPrinter() {
807 $params = $this->extractRequestParams();
808 if ( $params['wrap'] ) {
809 return null;
812 $main = $this->getMain();
813 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
814 return new ApiFormatRaw( $main, $errorPrinter );
817 public function getAllowedParams() {
818 return [
819 'modules' => [
820 ApiBase::PARAM_DFLT => 'main',
821 ApiBase::PARAM_ISMULTI => true,
823 'submodules' => false,
824 'recursivesubmodules' => false,
825 'wrap' => false,
826 'toc' => false,
830 protected function getExamplesMessages() {
831 return [
832 'action=help'
833 => 'apihelp-help-example-main',
834 'action=help&modules=query&submodules=1'
835 => 'apihelp-help-example-submodules',
836 'action=help&recursivesubmodules=1'
837 => 'apihelp-help-example-recursive',
838 'action=help&modules=help'
839 => 'apihelp-help-example-help',
840 'action=help&modules=query+info|query+categorymembers'
841 => 'apihelp-help-example-query',
845 public function getHelpUrls() {
846 return [
847 'https://www.mediawiki.org/wiki/API:Main_page',
848 'https://www.mediawiki.org/wiki/API:FAQ',
849 'https://www.mediawiki.org/wiki/API:Quick_start_guide',