API: Introduce "templated parameters"
[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 = isset( $options['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 = isset( $settings[ApiBase::PARAM_DFLT] )
486 ? $settings[ApiBase::PARAM_DFLT]
487 : null;
488 if ( is_bool( $dflt ) ) {
489 $settings[ApiBase::PARAM_TYPE] = 'boolean';
490 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
491 $settings[ApiBase::PARAM_TYPE] = 'string';
492 } elseif ( is_int( $dflt ) ) {
493 $settings[ApiBase::PARAM_TYPE] = 'integer';
494 }
495 }
496 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
497 $type = $settings[ApiBase::PARAM_TYPE];
498 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
499 $hintPipeSeparated = true;
500 $count = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
501 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2] + 1
502 : ApiBase::LIMIT_SML2 + 1;
503
504 if ( is_array( $type ) ) {
505 $count = count( $type );
506 $deprecatedValues = isset( $settings[ApiBase::PARAM_DEPRECATED_VALUES] )
507 ? $settings[ApiBase::PARAM_DEPRECATED_VALUES]
508 : [];
509 $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
510 ? $settings[ApiBase::PARAM_VALUE_LINKS]
511 : [];
512 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
513 $attr = [];
514 if ( $v !== '' ) {
515 // We can't know whether this contains LTR or RTL text.
516 $attr['dir'] = 'auto';
517 }
518 if ( isset( $deprecatedValues[$v] ) ) {
519 $attr['class'] = 'apihelp-deprecated-value';
520 }
521 $ret = $attr ? Html::element( 'span', $attr, $v ) : $v;
522 if ( isset( $links[$v] ) ) {
523 $ret = "[[{$links[$v]}|$ret]]";
524 }
525 return $ret;
526 }, $type );
527 $i = array_search( '', $type, true );
528 if ( $i === false ) {
529 $values = $context->getLanguage()->commaList( $values );
530 } else {
531 unset( $values[$i] );
532 $values = $context->msg( 'api-help-param-list-can-be-empty' )
533 ->numParams( count( $values ) )
534 ->params( $context->getLanguage()->commaList( $values ) )
535 ->parse();
536 }
537 $info[] = $context->msg( 'api-help-param-list' )
538 ->params( $multi ? 2 : 1 )
539 ->params( $values )
540 ->parse();
541 $hintPipeSeparated = false;
542 } else {
543 switch ( $type ) {
544 case 'submodule':
545 $groups[] = $name;
546
547 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
548 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
549 $defaultAttrs = [];
550 } else {
551 $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
552 $map = [];
553 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
554 $map[$submoduleName] = $prefix . $submoduleName;
555 }
556 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
557 }
558 ksort( $map );
559
560 $submodules = [];
561 $deprecatedSubmodules = [];
562 foreach ( $map as $v => $m ) {
563 $attrs = $defaultAttrs;
564 $arr = &$submodules;
565 try {
566 $submod = $module->getModuleFromPath( $m );
567 if ( $submod ) {
568 if ( $submod->isDeprecated() ) {
569 $arr = &$deprecatedSubmodules;
570 $attrs['class'] = 'apihelp-deprecated-value';
571 }
572 }
573 } catch ( ApiUsageException $ex ) {
574 // Ignore
575 }
576 if ( $attrs ) {
577 $v = Html::element( 'span', $attrs, $v );
578 }
579 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
580 }
581 $submodules = array_merge( $submodules, $deprecatedSubmodules );
582 $count = count( $submodules );
583 $info[] = $context->msg( 'api-help-param-list' )
584 ->params( $multi ? 2 : 1 )
585 ->params( $context->getLanguage()->commaList( $submodules ) )
586 ->parse();
587 $hintPipeSeparated = false;
588 // No type message necessary, we have a list of values.
589 $type = null;
590 break;
591
592 case 'namespace':
593 $namespaces = MWNamespace::getValidNamespaces();
594 if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
595 is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
596 ) {
597 $namespaces = array_merge( $namespaces, $settings[ApiBase::PARAM_EXTRA_NAMESPACES] );
598 }
599 sort( $namespaces );
600 $count = count( $namespaces );
601 $info[] = $context->msg( 'api-help-param-list' )
602 ->params( $multi ? 2 : 1 )
603 ->params( $context->getLanguage()->commaList( $namespaces ) )
604 ->parse();
605 $hintPipeSeparated = false;
606 // No type message necessary, we have a list of values.
607 $type = null;
608 break;
609
610 case 'tags':
611 $tags = ChangeTags::listExplicitlyDefinedTags();
612 $count = count( $tags );
613 $info[] = $context->msg( 'api-help-param-list' )
614 ->params( $multi ? 2 : 1 )
615 ->params( $context->getLanguage()->commaList( $tags ) )
616 ->parse();
617 $hintPipeSeparated = false;
618 $type = null;
619 break;
620
621 case 'limit':
622 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
623 $info[] = $context->msg( 'api-help-param-limit2' )
624 ->numParams( $settings[ApiBase::PARAM_MAX] )
625 ->numParams( $settings[ApiBase::PARAM_MAX2] )
626 ->parse();
627 } else {
628 $info[] = $context->msg( 'api-help-param-limit' )
629 ->numParams( $settings[ApiBase::PARAM_MAX] )
630 ->parse();
631 }
632 break;
633
634 case 'integer':
635 // Possible messages:
636 // api-help-param-integer-min,
637 // api-help-param-integer-max,
638 // api-help-param-integer-minmax
639 $suffix = '';
640 $min = $max = 0;
641 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
642 $suffix .= 'min';
643 $min = $settings[ApiBase::PARAM_MIN];
644 }
645 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
646 $suffix .= 'max';
647 $max = $settings[ApiBase::PARAM_MAX];
648 }
649 if ( $suffix !== '' ) {
650 $info[] =
651 $context->msg( "api-help-param-integer-$suffix" )
652 ->params( $multi ? 2 : 1 )
653 ->numParams( $min, $max )
654 ->parse();
655 }
656 break;
657
658 case 'upload':
659 $info[] = $context->msg( 'api-help-param-upload' )
660 ->parse();
661 // No type message necessary, api-help-param-upload should handle it.
662 $type = null;
663 break;
664
665 case 'string':
666 case 'text':
667 // Displaying a type message here would be useless.
668 $type = null;
669 break;
670 }
671 }
672
673 // Add type. Messages for grep: api-help-param-type-limit
674 // api-help-param-type-integer api-help-param-type-boolean
675 // api-help-param-type-timestamp api-help-param-type-user
676 // api-help-param-type-password
677 if ( is_string( $type ) ) {
678 $msg = $context->msg( "api-help-param-type-$type" );
679 if ( !$msg->isDisabled() ) {
680 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
681 }
682 }
683
684 if ( $multi ) {
685 $extra = [];
686 $lowcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT1] )
687 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT1]
688 : ApiBase::LIMIT_SML1;
689 $highcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
690 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2]
691 : ApiBase::LIMIT_SML2;
692
693 if ( $hintPipeSeparated ) {
694 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
695 }
696 if ( $count > $lowcount ) {
697 if ( $lowcount === $highcount ) {
698 $msg = $context->msg( 'api-help-param-multi-max-simple' )
699 ->numParams( $lowcount );
700 } else {
701 $msg = $context->msg( 'api-help-param-multi-max' )
702 ->numParams( $lowcount, $highcount );
703 }
704 $extra[] = $msg->parse();
705 }
706 if ( $extra ) {
707 $info[] = implode( ' ', $extra );
708 }
709
710 $allowAll = isset( $settings[ApiBase::PARAM_ALL] )
711 ? $settings[ApiBase::PARAM_ALL]
712 : false;
713 if ( $allowAll || $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
714 if ( $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
715 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
716 } else {
717 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
718 }
719 $info[] = $context->msg( 'api-help-param-multi-all' )
720 ->params( $allSpecifier )
721 ->parse();
722 }
723 }
724 }
725
726 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
727 $info[] = $context->msg( 'api-help-param-maxbytes' )
728 ->numParams( $settings[self::PARAM_MAX_BYTES] );
729 }
730 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
731 $info[] = $context->msg( 'api-help-param-maxchars' )
732 ->numParams( $settings[self::PARAM_MAX_CHARS] );
733 }
734
735 // Add default
736 $default = isset( $settings[ApiBase::PARAM_DFLT] )
737 ? $settings[ApiBase::PARAM_DFLT]
738 : null;
739 if ( $default === '' ) {
740 $info[] = $context->msg( 'api-help-param-default-empty' )
741 ->parse();
742 } elseif ( $default !== null && $default !== false ) {
743 // We can't know whether this contains LTR or RTL text.
744 $info[] = $context->msg( 'api-help-param-default' )
745 ->params( Html::element( 'span', [ 'dir' => 'auto' ], $default ) )
746 ->parse();
747 }
748
749 if ( !array_filter( $description ) ) {
750 $description = [ self::wrap(
751 $context->msg( 'api-help-param-no-description' ),
752 'apihelp-empty'
753 ) ];
754 }
755
756 // Add "deprecated" flag
757 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
758 $help['parameters'] .= Html::openElement( 'dd',
759 [ 'class' => 'info' ] );
760 $help['parameters'] .= self::wrap(
761 $context->msg( 'api-help-param-deprecated' ),
762 'apihelp-deprecated', 'strong'
763 );
764 $help['parameters'] .= Html::closeElement( 'dd' );
765 }
766
767 if ( $description ) {
768 $description = implode( '', $description );
769 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
770 $help['parameters'] .= Html::rawElement( 'dd',
771 [ 'class' => 'description' ], $description );
772 }
773
774 foreach ( $info as $i ) {
775 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
776 }
777 }
778
779 if ( $dynamicParams !== null ) {
780 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
781 $module->getModulePrefix(),
782 $module->getModuleName(),
783 $module->getModulePath()
784 ] );
785 $help['parameters'] .= Html::element( 'dt', null, '*' );
786 $help['parameters'] .= Html::rawElement( 'dd',
787 [ 'class' => 'description' ], $dynamicParams->parse() );
788 }
789
790 $help['parameters'] .= Html::closeElement( 'dl' );
791 $help['parameters'] .= Html::closeElement( 'div' );
792 }
793
794 $examples = $module->getExamplesMessages();
795 if ( $examples ) {
796 $help['examples'] .= Html::openElement( 'div',
797 [ 'class' => 'apihelp-block apihelp-examples' ] );
798 $msg = $context->msg( 'api-help-examples' );
799 if ( !$msg->isDisabled() ) {
800 $help['examples'] .= self::wrap(
801 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
802 );
803 }
804
805 $help['examples'] .= Html::openElement( 'dl' );
806 foreach ( $examples as $qs => $msg ) {
807 $msg = ApiBase::makeMessage( $msg, $context, [
808 $module->getModulePrefix(),
809 $module->getModuleName(),
810 $module->getModulePath()
811 ] );
812
813 $link = wfAppendQuery( wfScript( 'api' ), $qs );
814 $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
815 $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
816 $help['examples'] .= Html::rawElement( 'dd', null,
817 Html::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
818 Html::rawElement( 'a', [ 'href' => $sandbox ],
819 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
820 );
821 }
822 $help['examples'] .= Html::closeElement( 'dl' );
823 $help['examples'] .= Html::closeElement( 'div' );
824 }
825
826 $subtocnumber = $tocnumber;
827 $subtocnumber[$level + 1] = 0;
828 $suboptions = [
829 'submodules' => $options['recursivesubmodules'],
830 'headerlevel' => $level + 1,
831 'tocnumber' => &$subtocnumber,
832 'noheader' => false,
833 ] + $options;
834
835 if ( $options['submodules'] && $module->getModuleManager() ) {
836 $manager = $module->getModuleManager();
837 $submodules = [];
838 foreach ( $groups as $group ) {
839 $names = $manager->getNames( $group );
840 sort( $names );
841 foreach ( $names as $name ) {
842 $submodules[] = $manager->getModule( $name );
843 }
844 }
845 $help['submodules'] .= self::getHelpInternal(
846 $context,
847 $submodules,
848 $suboptions,
849 $haveModules
850 );
851 }
852
853 $module->modifyHelp( $help, $suboptions, $haveModules );
854
855 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
856
857 $out .= implode( "\n", $help );
858 }
859
860 return $out;
861 }
862
863 public function shouldCheckMaxlag() {
864 return false;
865 }
866
867 public function isReadMode() {
868 return false;
869 }
870
871 public function getCustomPrinter() {
872 $params = $this->extractRequestParams();
873 if ( $params['wrap'] ) {
874 return null;
875 }
876
877 $main = $this->getMain();
878 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
879 return new ApiFormatRaw( $main, $errorPrinter );
880 }
881
882 public function getAllowedParams() {
883 return [
884 'modules' => [
885 ApiBase::PARAM_DFLT => 'main',
886 ApiBase::PARAM_ISMULTI => true,
887 ],
888 'submodules' => false,
889 'recursivesubmodules' => false,
890 'wrap' => false,
891 'toc' => false,
892 ];
893 }
894
895 protected function getExamplesMessages() {
896 return [
897 'action=help'
898 => 'apihelp-help-example-main',
899 'action=help&modules=query&submodules=1'
900 => 'apihelp-help-example-submodules',
901 'action=help&recursivesubmodules=1'
902 => 'apihelp-help-example-recursive',
903 'action=help&modules=help'
904 => 'apihelp-help-example-help',
905 'action=help&modules=query+info|query+categorymembers'
906 => 'apihelp-help-example-query',
907 ];
908 }
909
910 public function getHelpUrls() {
911 return [
912 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
913 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
914 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',
915 ];
916 }
917 }