Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / includes / api / ApiHelp.php
1 <?php
2 /**
3 * Copyright © 2014 Wikimedia Foundation and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use HtmlFormatter\HtmlFormatter;
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Class to output help for an API module
28 *
29 * @since 1.25 completely rewritten
30 * @ingroup API
31 */
32 class ApiHelp extends ApiBase {
33 public function execute() {
34 $params = $this->extractRequestParams();
35 $modules = [];
36
37 foreach ( $params['modules'] as $path ) {
38 $modules[] = $this->getModuleFromPath( $path );
39 }
40
41 // Get the help
42 $context = new DerivativeContext( $this->getMain()->getContext() );
43 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
44 $context->setLanguage( $this->getMain()->getLanguage() );
45 $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
46 $out = new OutputPage( $context );
47 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
48 $context->setOutput( $out );
49
50 self::getHelp( $context, $modules, $params );
51
52 // Grab the output from the skin
53 ob_start();
54 $context->getOutput()->output();
55 $html = ob_get_clean();
56
57 $result = $this->getResult();
58 if ( $params['wrap'] ) {
59 $data = [
60 'mime' => 'text/html',
61 'filename' => 'api-help.html',
62 'help' => $html,
63 ];
64 ApiResult::setSubelementsList( $data, 'help' );
65 $result->addValue( null, $this->getModuleName(), $data );
66 } else {
67 $result->reset();
68 $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
69 $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
70 $result->addValue( null, 'filename', 'api-help.html', ApiResult::NO_SIZE_CHECK );
71 }
72 }
73
74 /**
75 * Generate help for the specified modules
76 *
77 * Help is placed into the OutputPage object returned by
78 * $context->getOutput().
79 *
80 * Recognized options include:
81 * - headerlevel: (int) Header tag level
82 * - nolead: (bool) Skip the inclusion of api-help-lead
83 * - noheader: (bool) Skip the inclusion of the top-level section headers
84 * - submodules: (bool) Include help for submodules of the current module
85 * - recursivesubmodules: (bool) Include help for submodules recursively
86 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
87 * - toc: (bool) Include a table of contents
88 *
89 * @param IContextSource $context
90 * @param ApiBase[]|ApiBase $modules
91 * @param array $options Formatting options (described above)
92 */
93 public static function getHelp( IContextSource $context, $modules, array $options ) {
94 global $wgContLang;
95
96 if ( !is_array( $modules ) ) {
97 $modules = [ $modules ];
98 }
99
100 $out = $context->getOutput();
101 $out->addModuleStyles( [
102 'mediawiki.hlist',
103 'mediawiki.apihelp',
104 ] );
105 if ( !empty( $options['toc'] ) ) {
106 $out->addModules( 'mediawiki.toc' );
107 }
108 $out->setPageTitle( $context->msg( 'api-help-title' ) );
109
110 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
111 $cacheKey = null;
112 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain &&
113 $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
114 ) {
115 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
116 if ( $cacheHelpTimeout > 0 ) {
117 // Get help text from cache if present
118 $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
119 (int)!empty( $options['toc'] ),
120 str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) );
121 $cached = $cache->get( $cacheKey );
122 if ( $cached ) {
123 $out->addHTML( $cached );
124 return;
125 }
126 }
127 }
128 if ( $out->getHTML() !== '' ) {
129 // Don't save to cache, there's someone else's content in the page
130 // already
131 $cacheKey = null;
132 }
133
134 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
135 $options['submodules'] = $options['recursivesubmodules'] || !empty( $options['submodules'] );
136
137 // Prepend lead
138 if ( empty( $options['nolead'] ) ) {
139 $msg = $context->msg( 'api-help-lead' );
140 if ( !$msg->isDisabled() ) {
141 $out->addHTML( $msg->parseAsBlock() );
142 }
143 }
144
145 $haveModules = [];
146 $html = self::getHelpInternal( $context, $modules, $options, $haveModules );
147 if ( !empty( $options['toc'] ) && $haveModules ) {
148 $out->addHTML( Linker::generateTOC( $haveModules, $context->getLanguage() ) );
149 }
150 $out->addHTML( $html );
151
152 $helptitle = $options['helptitle'] ?? null;
153 $html = self::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
154 $out->clearHTML();
155 $out->addHTML( $html );
156
157 if ( $cacheKey !== null ) {
158 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
159 }
160 }
161
162 /**
163 * Replace Special:ApiHelp links with links to api.php
164 *
165 * @param string $html
166 * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
167 * @param array $localModules Keys are modules to link within the current page, values are ignored
168 * @return string
169 */
170 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
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' );
177 do {
178 $old = $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] )
186 ->getFullURL();
187 } else {
188 $href = wfAppendQuery( wfScript( 'api' ), [
189 'action' => 'help',
190 'modules' => $m[1],
191 ] ) . $m[2];
192 }
193 $node->setAttribute( 'href', $href );
194 $node->removeAttribute( 'title' );
195 }
196 }
197
198 return $formatter->getText();
199 }
200
201 /**
202 * Wrap a message in HTML with a class.
203 *
204 * @param Message $msg
205 * @param string $class
206 * @param string $tag
207 * @return string
208 */
209 private static function wrap( Message $msg, $class, $tag = 'span' ) {
210 return Html::rawElement( $tag, [ 'class' => $class ],
211 $msg->parse()
212 );
213 }
214
215 /**
216 * Recursively-called function to actually construct the help
217 *
218 * @param IContextSource $context
219 * @param ApiBase[] $modules
220 * @param array $options
221 * @param array &$haveModules
222 * @return string
223 */
224 private static function getHelpInternal( IContextSource $context, array $modules,
225 array $options, &$haveModules
226 ) {
227 $out = '';
228
229 $level = empty( $options['headerlevel'] ) ? 2 : $options['headerlevel'];
230 if ( empty( $options['tocnumber'] ) ) {
231 $tocnumber = [ 2 => 0 ];
232 } else {
233 $tocnumber = &$options['tocnumber'];
234 }
235
236 foreach ( $modules as $module ) {
237 $tocnumber[$level]++;
238 $path = $module->getModulePath();
239 $module->setContext( $context );
240 $help = [
241 'header' => '',
242 'flags' => '',
243 'description' => '',
244 'help-urls' => '',
245 'parameters' => '',
246 'examples' => '',
247 'submodules' => '',
248 ];
249
250 if ( empty( $options['noheader'] ) || !empty( $options['toc'] ) ) {
251 $anchor = $path;
252 $i = 1;
253 while ( isset( $haveModules[$anchor] ) ) {
254 $anchor = $path . '|' . ++$i;
255 }
256
257 if ( $module->isMain() ) {
258 $headerContent = $context->msg( 'api-help-main-header' )->parse();
259 $headerAttr = [
260 'class' => 'apihelp-header',
261 ];
262 } else {
263 $name = $module->getModuleName();
264 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
265 "=$name";
266 if ( $module->getModulePrefix() !== '' ) {
267 $headerContent .= ' ' .
268 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
269 }
270 // Module names are always in English and not localized,
271 // so English language and direction must be set explicitly,
272 // otherwise parentheses will get broken in RTL wikis
273 $headerAttr = [
274 'class' => 'apihelp-header apihelp-module-name',
275 'dir' => 'ltr',
276 'lang' => 'en',
277 ];
278 }
279
280 $headerAttr['id'] = $anchor;
281
282 $haveModules[$anchor] = [
283 'toclevel' => count( $tocnumber ),
284 'level' => $level,
285 'anchor' => $anchor,
286 'line' => $headerContent,
287 'number' => implode( '.', $tocnumber ),
288 'index' => false,
289 ];
290 if ( empty( $options['noheader'] ) ) {
291 $help['header'] .= Html::element(
292 'h' . min( 6, $level ),
293 $headerAttr,
294 $headerContent
295 );
296 }
297 } else {
298 $haveModules[$path] = true;
299 }
300
301 $links = [];
302 $any = false;
303 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
304 $name = $m->getModuleName();
305 if ( $name === 'main_int' ) {
306 $name = 'main';
307 }
308
309 if ( count( $modules ) === 1 && $m === $modules[0] &&
310 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
311 ) {
312 $link = Html::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
313 } else {
314 $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
315 $link = Html::element( 'a',
316 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
317 $name
318 );
319 $any = true;
320 }
321 array_unshift( $links, $link );
322 }
323 if ( $any ) {
324 $help['header'] .= self::wrap(
325 $context->msg( 'parentheses' )
326 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
327 'apihelp-linktrail', 'div'
328 );
329 }
330
331 $flags = $module->getHelpFlags();
332 $help['flags'] .= Html::openElement( 'div',
333 [ 'class' => 'apihelp-block apihelp-flags' ] );
334 $msg = $context->msg( 'api-help-flags' );
335 if ( !$msg->isDisabled() ) {
336 $help['flags'] .= self::wrap(
337 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
338 );
339 }
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" )
344 );
345 }
346 $sourceInfo = $module->getModuleSourceInfo();
347 if ( $sourceInfo ) {
348 if ( isset( $sourceInfo['namemsg'] ) ) {
349 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
350 } else {
351 // Probably English, so wrap it.
352 $extname = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
353 }
354 $help['flags'] .= Html::rawElement( 'li', null,
355 self::wrap(
356 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
357 'apihelp-source'
358 )
359 );
360
361 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
362 if ( isset( $sourceInfo['license-name'] ) ) {
363 $msg = $context->msg( 'api-help-license', $link,
364 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
365 );
366 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
367 $msg = $context->msg( 'api-help-license-noname', $link );
368 } else {
369 $msg = $context->msg( 'api-help-license-unknown' );
370 }
371 $help['flags'] .= Html::rawElement( 'li', null,
372 self::wrap( $msg, 'apihelp-license' )
373 );
374 } else {
375 $help['flags'] .= Html::rawElement( 'li', null,
376 self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
377 );
378 $help['flags'] .= Html::rawElement( 'li', null,
379 self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
380 );
381 }
382 $help['flags'] .= Html::closeElement( 'ul' );
383 $help['flags'] .= Html::closeElement( 'div' );
384
385 foreach ( $module->getFinalDescription() as $msg ) {
386 $msg->setContext( $context );
387 $help['description'] .= $msg->parseAsBlock();
388 }
389
390 $urls = $module->getHelpUrls();
391 if ( $urls ) {
392 $help['help-urls'] .= Html::openElement( 'div',
393 [ 'class' => 'apihelp-block apihelp-help-urls' ]
394 );
395 $msg = $context->msg( 'api-help-help-urls' );
396 if ( !$msg->isDisabled() ) {
397 $help['help-urls'] .= self::wrap(
398 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
399 );
400 }
401 if ( !is_array( $urls ) ) {
402 $urls = [ $urls ];
403 }
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 )
408 );
409 }
410 $help['help-urls'] .= Html::closeElement( 'ul' );
411 $help['help-urls'] .= Html::closeElement( 'div' );
412 }
413
414 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
415 $dynamicParams = $module->dynamicParameterDocumentation();
416 $groups = [];
417 if ( $params || $dynamicParams !== null ) {
418 $help['parameters'] .= Html::openElement( 'div',
419 [ 'class' => 'apihelp-block apihelp-parameters' ]
420 );
421 $msg = $context->msg( 'api-help-parameters' );
422 if ( !$msg->isDisabled() ) {
423 $help['parameters'] .= self::wrap(
424 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
425 );
426 }
427 $help['parameters'] .= Html::openElement( 'dl' );
428
429 $descriptions = $module->getFinalParamDescription();
430
431 foreach ( $params as $name => $settings ) {
432 if ( !is_array( $settings ) ) {
433 $settings = [ ApiBase::PARAM_DFLT => $settings ];
434 }
435
436 $help['parameters'] .= Html::rawElement( 'dt', null,
437 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
438 );
439
440 // Add description
441 $description = [];
442 if ( isset( $descriptions[$name] ) ) {
443 foreach ( $descriptions[$name] as $msg ) {
444 $msg->setContext( $context );
445 $description[] = $msg->parseAsBlock();
446 }
447 }
448
449 // Add usage info
450 $info = [];
451
452 // Required?
453 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
454 $info[] = $context->msg( 'api-help-param-required' )->parse();
455 }
456
457 // Custom info?
458 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
459 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
460 $tag = array_shift( $i );
461 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
462 ->numParams( count( $i ) )
463 ->params( $context->getLanguage()->commaList( $i ) )
464 ->params( $module->getModulePrefix() )
465 ->parse();
466 }
467 }
468
469 // Templated?
470 if ( !empty( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
471 $vars = [];
472 $msg = 'api-help-param-templated-var-first';
473 foreach ( $settings[ApiBase::PARAM_TEMPLATE_VARS] as $k => $v ) {
474 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
475 $msg = 'api-help-param-templated-var';
476 }
477 $info[] = $context->msg( 'api-help-param-templated' )
478 ->numParams( count( $vars ) )
479 ->params( Message::listParam( $vars ) )
480 ->parse();
481 }
482
483 // Type documentation
484 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
485 $dflt = $settings[ApiBase::PARAM_DFLT] ?? null;
486 if ( is_bool( $dflt ) ) {
487 $settings[ApiBase::PARAM_TYPE] = 'boolean';
488 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
489 $settings[ApiBase::PARAM_TYPE] = 'string';
490 } elseif ( is_int( $dflt ) ) {
491 $settings[ApiBase::PARAM_TYPE] = 'integer';
492 }
493 }
494 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
495 $type = $settings[ApiBase::PARAM_TYPE];
496 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
497 $hintPipeSeparated = true;
498 $count = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
499 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2] + 1
500 : ApiBase::LIMIT_SML2 + 1;
501
502 if ( is_array( $type ) ) {
503 $count = count( $type );
504 $deprecatedValues = $settings[ApiBase::PARAM_DEPRECATED_VALUES] ?? [];
505 $links = $settings[ApiBase::PARAM_VALUE_LINKS] ?? [];
506 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
507 $attr = [];
508 if ( $v !== '' ) {
509 // We can't know whether this contains LTR or RTL text.
510 $attr['dir'] = 'auto';
511 }
512 if ( isset( $deprecatedValues[$v] ) ) {
513 $attr['class'] = 'apihelp-deprecated-value';
514 }
515 $ret = $attr ? Html::element( 'span', $attr, $v ) : $v;
516 if ( isset( $links[$v] ) ) {
517 $ret = "[[{$links[$v]}|$ret]]";
518 }
519 return $ret;
520 }, $type );
521 $i = array_search( '', $type, true );
522 if ( $i === false ) {
523 $values = $context->getLanguage()->commaList( $values );
524 } else {
525 unset( $values[$i] );
526 $values = $context->msg( 'api-help-param-list-can-be-empty' )
527 ->numParams( count( $values ) )
528 ->params( $context->getLanguage()->commaList( $values ) )
529 ->parse();
530 }
531 $info[] = $context->msg( 'api-help-param-list' )
532 ->params( $multi ? 2 : 1 )
533 ->params( $values )
534 ->parse();
535 $hintPipeSeparated = false;
536 } else {
537 switch ( $type ) {
538 case 'submodule':
539 $groups[] = $name;
540
541 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
542 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
543 $defaultAttrs = [];
544 } else {
545 $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
546 $map = [];
547 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
548 $map[$submoduleName] = $prefix . $submoduleName;
549 }
550 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
551 }
552 ksort( $map );
553
554 $submodules = [];
555 $deprecatedSubmodules = [];
556 foreach ( $map as $v => $m ) {
557 $attrs = $defaultAttrs;
558 $arr = &$submodules;
559 try {
560 $submod = $module->getModuleFromPath( $m );
561 if ( $submod ) {
562 if ( $submod->isDeprecated() ) {
563 $arr = &$deprecatedSubmodules;
564 $attrs['class'] = 'apihelp-deprecated-value';
565 }
566 }
567 } catch ( ApiUsageException $ex ) {
568 // Ignore
569 }
570 if ( $attrs ) {
571 $v = Html::element( 'span', $attrs, $v );
572 }
573 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
574 }
575 $submodules = array_merge( $submodules, $deprecatedSubmodules );
576 $count = count( $submodules );
577 $info[] = $context->msg( 'api-help-param-list' )
578 ->params( $multi ? 2 : 1 )
579 ->params( $context->getLanguage()->commaList( $submodules ) )
580 ->parse();
581 $hintPipeSeparated = false;
582 // No type message necessary, we have a list of values.
583 $type = null;
584 break;
585
586 case 'namespace':
587 $namespaces = MWNamespace::getValidNamespaces();
588 if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
589 is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
590 ) {
591 $namespaces = array_merge( $namespaces, $settings[ApiBase::PARAM_EXTRA_NAMESPACES] );
592 }
593 sort( $namespaces );
594 $count = count( $namespaces );
595 $info[] = $context->msg( 'api-help-param-list' )
596 ->params( $multi ? 2 : 1 )
597 ->params( $context->getLanguage()->commaList( $namespaces ) )
598 ->parse();
599 $hintPipeSeparated = false;
600 // No type message necessary, we have a list of values.
601 $type = null;
602 break;
603
604 case 'tags':
605 $tags = ChangeTags::listExplicitlyDefinedTags();
606 $count = count( $tags );
607 $info[] = $context->msg( 'api-help-param-list' )
608 ->params( $multi ? 2 : 1 )
609 ->params( $context->getLanguage()->commaList( $tags ) )
610 ->parse();
611 $hintPipeSeparated = false;
612 $type = null;
613 break;
614
615 case 'limit':
616 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
617 $info[] = $context->msg( 'api-help-param-limit2' )
618 ->numParams( $settings[ApiBase::PARAM_MAX] )
619 ->numParams( $settings[ApiBase::PARAM_MAX2] )
620 ->parse();
621 } else {
622 $info[] = $context->msg( 'api-help-param-limit' )
623 ->numParams( $settings[ApiBase::PARAM_MAX] )
624 ->parse();
625 }
626 break;
627
628 case 'integer':
629 // Possible messages:
630 // api-help-param-integer-min,
631 // api-help-param-integer-max,
632 // api-help-param-integer-minmax
633 $suffix = '';
634 $min = $max = 0;
635 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
636 $suffix .= 'min';
637 $min = $settings[ApiBase::PARAM_MIN];
638 }
639 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
640 $suffix .= 'max';
641 $max = $settings[ApiBase::PARAM_MAX];
642 }
643 if ( $suffix !== '' ) {
644 $info[] =
645 $context->msg( "api-help-param-integer-$suffix" )
646 ->params( $multi ? 2 : 1 )
647 ->numParams( $min, $max )
648 ->parse();
649 }
650 break;
651
652 case 'upload':
653 $info[] = $context->msg( 'api-help-param-upload' )
654 ->parse();
655 // No type message necessary, api-help-param-upload should handle it.
656 $type = null;
657 break;
658
659 case 'string':
660 case 'text':
661 // Displaying a type message here would be useless.
662 $type = null;
663 break;
664 }
665 }
666
667 // Add type. Messages for grep: api-help-param-type-limit
668 // api-help-param-type-integer api-help-param-type-boolean
669 // api-help-param-type-timestamp api-help-param-type-user
670 // api-help-param-type-password
671 if ( is_string( $type ) ) {
672 $msg = $context->msg( "api-help-param-type-$type" );
673 if ( !$msg->isDisabled() ) {
674 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
675 }
676 }
677
678 if ( $multi ) {
679 $extra = [];
680 $lowcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT1] )
681 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT1]
682 : ApiBase::LIMIT_SML1;
683 $highcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
684 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2]
685 : ApiBase::LIMIT_SML2;
686
687 if ( $hintPipeSeparated ) {
688 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
689 }
690 if ( $count > $lowcount ) {
691 if ( $lowcount === $highcount ) {
692 $msg = $context->msg( 'api-help-param-multi-max-simple' )
693 ->numParams( $lowcount );
694 } else {
695 $msg = $context->msg( 'api-help-param-multi-max' )
696 ->numParams( $lowcount, $highcount );
697 }
698 $extra[] = $msg->parse();
699 }
700 if ( $extra ) {
701 $info[] = implode( ' ', $extra );
702 }
703
704 $allowAll = $settings[ApiBase::PARAM_ALL] ?? false;
705 if ( $allowAll || $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
706 if ( $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
707 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
708 } else {
709 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
710 }
711 $info[] = $context->msg( 'api-help-param-multi-all' )
712 ->params( $allSpecifier )
713 ->parse();
714 }
715 }
716 }
717
718 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
719 $info[] = $context->msg( 'api-help-param-maxbytes' )
720 ->numParams( $settings[self::PARAM_MAX_BYTES] );
721 }
722 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
723 $info[] = $context->msg( 'api-help-param-maxchars' )
724 ->numParams( $settings[self::PARAM_MAX_CHARS] );
725 }
726
727 // Add default
728 $default = $settings[ApiBase::PARAM_DFLT] ?? null;
729 if ( $default === '' ) {
730 $info[] = $context->msg( 'api-help-param-default-empty' )
731 ->parse();
732 } elseif ( $default !== null && $default !== false ) {
733 // We can't know whether this contains LTR or RTL text.
734 $info[] = $context->msg( 'api-help-param-default' )
735 ->params( Html::element( 'span', [ 'dir' => 'auto' ], $default ) )
736 ->parse();
737 }
738
739 if ( !array_filter( $description ) ) {
740 $description = [ self::wrap(
741 $context->msg( 'api-help-param-no-description' ),
742 'apihelp-empty'
743 ) ];
744 }
745
746 // Add "deprecated" flag
747 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
748 $help['parameters'] .= Html::openElement( 'dd',
749 [ 'class' => 'info' ] );
750 $help['parameters'] .= self::wrap(
751 $context->msg( 'api-help-param-deprecated' ),
752 'apihelp-deprecated', 'strong'
753 );
754 $help['parameters'] .= Html::closeElement( 'dd' );
755 }
756
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 );
762 }
763
764 foreach ( $info as $i ) {
765 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
766 }
767 }
768
769 if ( $dynamicParams !== null ) {
770 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
771 $module->getModulePrefix(),
772 $module->getModuleName(),
773 $module->getModulePath()
774 ] );
775 $help['parameters'] .= Html::element( 'dt', null, '*' );
776 $help['parameters'] .= Html::rawElement( 'dd',
777 [ 'class' => 'description' ], $dynamicParams->parse() );
778 }
779
780 $help['parameters'] .= Html::closeElement( 'dl' );
781 $help['parameters'] .= Html::closeElement( 'div' );
782 }
783
784 $examples = $module->getExamplesMessages();
785 if ( $examples ) {
786 $help['examples'] .= Html::openElement( 'div',
787 [ 'class' => 'apihelp-block apihelp-examples' ] );
788 $msg = $context->msg( 'api-help-examples' );
789 if ( !$msg->isDisabled() ) {
790 $help['examples'] .= self::wrap(
791 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
792 );
793 }
794
795 $help['examples'] .= Html::openElement( 'dl' );
796 foreach ( $examples as $qs => $msg ) {
797 $msg = ApiBase::makeMessage( $msg, $context, [
798 $module->getModulePrefix(),
799 $module->getModuleName(),
800 $module->getModulePath()
801 ] );
802
803 $link = wfAppendQuery( wfScript( 'api' ), $qs );
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() )
810 );
811 }
812 $help['examples'] .= Html::closeElement( 'dl' );
813 $help['examples'] .= Html::closeElement( 'div' );
814 }
815
816 $subtocnumber = $tocnumber;
817 $subtocnumber[$level + 1] = 0;
818 $suboptions = [
819 'submodules' => $options['recursivesubmodules'],
820 'headerlevel' => $level + 1,
821 'tocnumber' => &$subtocnumber,
822 'noheader' => false,
823 ] + $options;
824
825 if ( $options['submodules'] && $module->getModuleManager() ) {
826 $manager = $module->getModuleManager();
827 $submodules = [];
828 foreach ( $groups as $group ) {
829 $names = $manager->getNames( $group );
830 sort( $names );
831 foreach ( $names as $name ) {
832 $submodules[] = $manager->getModule( $name );
833 }
834 }
835 $help['submodules'] .= self::getHelpInternal(
836 $context,
837 $submodules,
838 $suboptions,
839 $haveModules
840 );
841 }
842
843 $module->modifyHelp( $help, $suboptions, $haveModules );
844
845 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
846
847 $out .= implode( "\n", $help );
848 }
849
850 return $out;
851 }
852
853 public function shouldCheckMaxlag() {
854 return false;
855 }
856
857 public function isReadMode() {
858 return false;
859 }
860
861 public function getCustomPrinter() {
862 $params = $this->extractRequestParams();
863 if ( $params['wrap'] ) {
864 return null;
865 }
866
867 $main = $this->getMain();
868 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
869 return new ApiFormatRaw( $main, $errorPrinter );
870 }
871
872 public function getAllowedParams() {
873 return [
874 'modules' => [
875 ApiBase::PARAM_DFLT => 'main',
876 ApiBase::PARAM_ISMULTI => true,
877 ],
878 'submodules' => false,
879 'recursivesubmodules' => false,
880 'wrap' => false,
881 'toc' => false,
882 ];
883 }
884
885 protected function getExamplesMessages() {
886 return [
887 'action=help'
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',
897 ];
898 }
899
900 public function getHelpUrls() {
901 return [
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',
905 ];
906 }
907 }