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