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