Merge "Fix wrong @return type hints in Language::tsTo… methods"
[lhc/web/wiklou.git] / includes / api / ApiHelp.php
1 <?php
2 /**
3 *
4 *
5 * Created on Aug 29, 2014
6 *
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
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
29 /**
30 * Class to output help for an API module
31 *
32 * @since 1.25 completely rewritten
33 * @ingroup API
34 */
35 class ApiHelp extends ApiBase {
36 public function execute() {
37 $params = $this->extractRequestParams();
38 $modules = [];
39
40 foreach ( $params['modules'] as $path ) {
41 $modules[] = $this->getModuleFromPath( $path );
42 }
43
44 // Get the help
45 $context = new DerivativeContext( $this->getMain()->getContext() );
46 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
47 $context->setLanguage( $this->getMain()->getLanguage() );
48 $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
49 $out = new OutputPage( $context );
50 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
51 $context->setOutput( $out );
52
53 self::getHelp( $context, $modules, $params );
54
55 // Grab the output from the skin
56 ob_start();
57 $context->getOutput()->output();
58 $html = ob_get_clean();
59
60 $result = $this->getResult();
61 if ( $params['wrap'] ) {
62 $data = [
63 'mime' => 'text/html',
64 'help' => $html,
65 ];
66 ApiResult::setSubelementsList( $data, 'help' );
67 $result->addValue( null, $this->getModuleName(), $data );
68 } else {
69 $result->reset();
70 $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
71 $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
72 }
73 }
74
75 /**
76 * Generate help for the specified modules
77 *
78 * Help is placed into the OutputPage object returned by
79 * $context->getOutput().
80 *
81 * Recognized options include:
82 * - headerlevel: (int) Header tag level
83 * - nolead: (bool) Skip the inclusion of api-help-lead
84 * - noheader: (bool) Skip the inclusion of the top-level section headers
85 * - submodules: (bool) Include help for submodules of the current module
86 * - recursivesubmodules: (bool) Include help for submodules recursively
87 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
88 * - toc: (bool) Include a table of contents
89 *
90 * @param IContextSource $context
91 * @param ApiBase[]|ApiBase $modules
92 * @param array $options Formatting options (described above)
93 * @return string
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( 'mediawiki.hlist' );
104 $out->addModuleStyles( 'mediawiki.apihelp' );
105 if ( !empty( $options['toc'] ) ) {
106 $out->addModules( 'mediawiki.toc' );
107 }
108 $out->setPageTitle( $context->msg( 'api-help-title' ) );
109
110 $cache = ObjectCache::getMainWANInstance();
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 = wfMemcKey( '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 $header = $context->msg( 'api-help-main-header' )->parse();
259 } else {
260 $name = $module->getModuleName();
261 $header = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
262 "=$name";
263 if ( $module->getModulePrefix() !== '' ) {
264 $header .= ' ' .
265 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
266 }
267 }
268 $haveModules[$anchor] = [
269 'toclevel' => count( $tocnumber ),
270 'level' => $level,
271 'anchor' => $anchor,
272 'line' => $header,
273 'number' => implode( '.', $tocnumber ),
274 'index' => false,
275 ];
276 if ( empty( $options['noheader'] ) ) {
277 $help['header'] .= Html::element( 'h' . min( 6, $level ),
278 [ 'id' => $anchor, 'class' => 'apihelp-header' ],
279 $header
280 );
281 }
282 } else {
283 $haveModules[$path] = true;
284 }
285
286 $links = [];
287 $any = false;
288 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
289 $name = $m->getModuleName();
290 if ( $name === 'main_int' ) {
291 $name = 'main';
292 }
293
294 if ( count( $modules ) === 1 && $m === $modules[0] &&
295 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
296 ) {
297 $link = Html::element( 'b', null, $name );
298 } else {
299 $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
300 $link = Html::element( 'a',
301 [ 'href' => $link, 'class' => 'apihelp-linktrail' ],
302 $name
303 );
304 $any = true;
305 }
306 array_unshift( $links, $link );
307 }
308 if ( $any ) {
309 $help['header'] .= self::wrap(
310 $context->msg( 'parentheses' )
311 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
312 'apihelp-linktrail', 'div'
313 );
314 }
315
316 $flags = $module->getHelpFlags();
317 $help['flags'] .= Html::openElement( 'div',
318 [ 'class' => 'apihelp-block apihelp-flags' ] );
319 $msg = $context->msg( 'api-help-flags' );
320 if ( !$msg->isDisabled() ) {
321 $help['flags'] .= self::wrap(
322 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
323 );
324 }
325 $help['flags'] .= Html::openElement( 'ul' );
326 foreach ( $flags as $flag ) {
327 $help['flags'] .= Html::rawElement( 'li', null,
328 self::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
329 );
330 }
331 $sourceInfo = $module->getModuleSourceInfo();
332 if ( $sourceInfo ) {
333 if ( isset( $sourceInfo['namemsg'] ) ) {
334 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
335 } else {
336 $extname = $sourceInfo['name'];
337 }
338 $help['flags'] .= Html::rawElement( 'li', null,
339 self::wrap(
340 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
341 'apihelp-source'
342 )
343 );
344
345 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
346 if ( isset( $sourceInfo['license-name'] ) ) {
347 $msg = $context->msg( 'api-help-license', $link, $sourceInfo['license-name'] );
348 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
349 $msg = $context->msg( 'api-help-license-noname', $link );
350 } else {
351 $msg = $context->msg( 'api-help-license-unknown' );
352 }
353 $help['flags'] .= Html::rawElement( 'li', null,
354 self::wrap( $msg, 'apihelp-license' )
355 );
356 } else {
357 $help['flags'] .= Html::rawElement( 'li', null,
358 self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
359 );
360 $help['flags'] .= Html::rawElement( 'li', null,
361 self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
362 );
363 }
364 $help['flags'] .= Html::closeElement( 'ul' );
365 $help['flags'] .= Html::closeElement( 'div' );
366
367 foreach ( $module->getFinalDescription() as $msg ) {
368 $msg->setContext( $context );
369 $help['description'] .= $msg->parseAsBlock();
370 }
371
372 $urls = $module->getHelpUrls();
373 if ( $urls ) {
374 $help['help-urls'] .= Html::openElement( 'div',
375 [ 'class' => 'apihelp-block apihelp-help-urls' ]
376 );
377 $msg = $context->msg( 'api-help-help-urls' );
378 if ( !$msg->isDisabled() ) {
379 $help['help-urls'] .= self::wrap(
380 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
381 );
382 }
383 if ( !is_array( $urls ) ) {
384 $urls = [ $urls ];
385 }
386 $help['help-urls'] .= Html::openElement( 'ul' );
387 foreach ( $urls as $url ) {
388 $help['help-urls'] .= Html::rawElement( 'li', null,
389 Html::element( 'a', [ 'href' => $url ], $url )
390 );
391 }
392 $help['help-urls'] .= Html::closeElement( 'ul' );
393 $help['help-urls'] .= Html::closeElement( 'div' );
394 }
395
396 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
397 $dynamicParams = $module->dynamicParameterDocumentation();
398 $groups = [];
399 if ( $params || $dynamicParams !== null ) {
400 $help['parameters'] .= Html::openElement( 'div',
401 [ 'class' => 'apihelp-block apihelp-parameters' ]
402 );
403 $msg = $context->msg( 'api-help-parameters' );
404 if ( !$msg->isDisabled() ) {
405 $help['parameters'] .= self::wrap(
406 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
407 );
408 }
409 $help['parameters'] .= Html::openElement( 'dl' );
410
411 $descriptions = $module->getFinalParamDescription();
412
413 foreach ( $params as $name => $settings ) {
414 if ( !is_array( $settings ) ) {
415 $settings = [ ApiBase::PARAM_DFLT => $settings ];
416 }
417
418 $help['parameters'] .= Html::element( 'dt', null,
419 $module->encodeParamName( $name ) );
420
421 // Add description
422 $description = [];
423 if ( isset( $descriptions[$name] ) ) {
424 foreach ( $descriptions[$name] as $msg ) {
425 $msg->setContext( $context );
426 $description[] = $msg->parseAsBlock();
427 }
428 }
429
430 // Add usage info
431 $info = [];
432
433 // Required?
434 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
435 $info[] = $context->msg( 'api-help-param-required' )->parse();
436 }
437
438 // Custom info?
439 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
440 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
441 $tag = array_shift( $i );
442 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
443 ->numParams( count( $i ) )
444 ->params( $context->getLanguage()->commaList( $i ) )
445 ->params( $module->getModulePrefix() )
446 ->parse();
447 }
448 }
449
450 // Type documentation
451 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
452 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
453 ? $settings[ApiBase::PARAM_DFLT]
454 : null;
455 if ( is_bool( $dflt ) ) {
456 $settings[ApiBase::PARAM_TYPE] = 'boolean';
457 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
458 $settings[ApiBase::PARAM_TYPE] = 'string';
459 } elseif ( is_int( $dflt ) ) {
460 $settings[ApiBase::PARAM_TYPE] = 'integer';
461 }
462 }
463 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
464 $type = $settings[ApiBase::PARAM_TYPE];
465 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
466 $hintPipeSeparated = true;
467 $count = ApiBase::LIMIT_SML2 + 1;
468
469 if ( is_array( $type ) ) {
470 $count = count( $type );
471 $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
472 ? $settings[ApiBase::PARAM_VALUE_LINKS]
473 : [];
474 $type = array_map( function ( $v ) use ( $links ) {
475 $ret = wfEscapeWikiText( $v );
476 if ( isset( $links[$v] ) ) {
477 $ret = "[[{$links[$v]}|$ret]]";
478 }
479 return $ret;
480 }, $type );
481 $i = array_search( '', $type, true );
482 if ( $i === false ) {
483 $type = $context->getLanguage()->commaList( $type );
484 } else {
485 unset( $type[$i] );
486 $type = $context->msg( 'api-help-param-list-can-be-empty' )
487 ->numParams( count( $type ) )
488 ->params( $context->getLanguage()->commaList( $type ) )
489 ->parse();
490 }
491 $info[] = $context->msg( 'api-help-param-list' )
492 ->params( $multi ? 2 : 1 )
493 ->params( $type )
494 ->parse();
495 $hintPipeSeparated = false;
496 } else {
497 switch ( $type ) {
498 case 'submodule':
499 $groups[] = $name;
500 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
501 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
502 ksort( $map );
503 $submodules = [];
504 foreach ( $map as $v => $m ) {
505 $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
506 }
507 } else {
508 $submodules = $module->getModuleManager()->getNames( $name );
509 sort( $submodules );
510 $prefix = $module->isMain()
511 ? '' : ( $module->getModulePath() . '+' );
512 $submodules = array_map( function ( $name ) use ( $prefix ) {
513 return "[[Special:ApiHelp/{$prefix}{$name}|{$name}]]";
514 }, $submodules );
515 }
516 $count = count( $submodules );
517 $info[] = $context->msg( 'api-help-param-list' )
518 ->params( $multi ? 2 : 1 )
519 ->params( $context->getLanguage()->commaList( $submodules ) )
520 ->parse();
521 $hintPipeSeparated = false;
522 // No type message necessary, we have a list of values.
523 $type = null;
524 break;
525
526 case 'namespace':
527 $namespaces = MWNamespace::getValidNamespaces();
528 $count = count( $namespaces );
529 $info[] = $context->msg( 'api-help-param-list' )
530 ->params( $multi ? 2 : 1 )
531 ->params( $context->getLanguage()->commaList( $namespaces ) )
532 ->parse();
533 $hintPipeSeparated = false;
534 // No type message necessary, we have a list of values.
535 $type = null;
536 break;
537
538 case 'tags':
539 $tags = ChangeTags::listExplicitlyDefinedTags();
540 $count = count( $tags );
541 $info[] = $context->msg( 'api-help-param-list' )
542 ->params( $multi ? 2 : 1 )
543 ->params( $context->getLanguage()->commaList( $tags ) )
544 ->parse();
545 $hintPipeSeparated = false;
546 $type = null;
547 break;
548
549 case 'limit':
550 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
551 $info[] = $context->msg( 'api-help-param-limit2' )
552 ->numParams( $settings[ApiBase::PARAM_MAX] )
553 ->numParams( $settings[ApiBase::PARAM_MAX2] )
554 ->parse();
555 } else {
556 $info[] = $context->msg( 'api-help-param-limit' )
557 ->numParams( $settings[ApiBase::PARAM_MAX] )
558 ->parse();
559 }
560 break;
561
562 case 'integer':
563 // Possible messages:
564 // api-help-param-integer-min,
565 // api-help-param-integer-max,
566 // api-help-param-integer-minmax
567 $suffix = '';
568 $min = $max = 0;
569 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
570 $suffix .= 'min';
571 $min = $settings[ApiBase::PARAM_MIN];
572 }
573 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
574 $suffix .= 'max';
575 $max = $settings[ApiBase::PARAM_MAX];
576 }
577 if ( $suffix !== '' ) {
578 $info[] =
579 $context->msg( "api-help-param-integer-$suffix" )
580 ->params( $multi ? 2 : 1 )
581 ->numParams( $min, $max )
582 ->parse();
583 }
584 break;
585
586 case 'upload':
587 $info[] = $context->msg( 'api-help-param-upload' )
588 ->parse();
589 // No type message necessary, api-help-param-upload should handle it.
590 $type = null;
591 break;
592
593 case 'string':
594 case 'text':
595 // Displaying a type message here would be useless.
596 $type = null;
597 break;
598 }
599 }
600
601 // Add type. Messages for grep: api-help-param-type-limit
602 // api-help-param-type-integer api-help-param-type-boolean
603 // api-help-param-type-timestamp api-help-param-type-user
604 // api-help-param-type-password
605 if ( is_string( $type ) ) {
606 $msg = $context->msg( "api-help-param-type-$type" );
607 if ( !$msg->isDisabled() ) {
608 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
609 }
610 }
611
612 if ( $multi ) {
613 $extra = [];
614 if ( $hintPipeSeparated ) {
615 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
616 }
617 if ( $count > ApiBase::LIMIT_SML1 ) {
618 $extra[] = $context->msg( 'api-help-param-multi-max' )
619 ->numParams( ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2 )
620 ->parse();
621 }
622 if ( $extra ) {
623 $info[] = implode( ' ', $extra );
624 }
625 }
626 }
627
628 // Add default
629 $default = isset( $settings[ApiBase::PARAM_DFLT] )
630 ? $settings[ApiBase::PARAM_DFLT]
631 : null;
632 if ( $default === '' ) {
633 $info[] = $context->msg( 'api-help-param-default-empty' )
634 ->parse();
635 } elseif ( $default !== null && $default !== false ) {
636 $info[] = $context->msg( 'api-help-param-default' )
637 ->params( wfEscapeWikiText( $default ) )
638 ->parse();
639 }
640
641 if ( !array_filter( $description ) ) {
642 $description = [ self::wrap(
643 $context->msg( 'api-help-param-no-description' ),
644 'apihelp-empty'
645 ) ];
646 }
647
648 // Add "deprecated" flag
649 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
650 $help['parameters'] .= Html::openElement( 'dd',
651 [ 'class' => 'info' ] );
652 $help['parameters'] .= self::wrap(
653 $context->msg( 'api-help-param-deprecated' ),
654 'apihelp-deprecated', 'strong'
655 );
656 $help['parameters'] .= Html::closeElement( 'dd' );
657 }
658
659 if ( $description ) {
660 $description = implode( '', $description );
661 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
662 $help['parameters'] .= Html::rawElement( 'dd',
663 [ 'class' => 'description' ], $description );
664 }
665
666 foreach ( $info as $i ) {
667 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
668 }
669 }
670
671 if ( $dynamicParams !== null ) {
672 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
673 $module->getModulePrefix(),
674 $module->getModuleName(),
675 $module->getModulePath()
676 ] );
677 $help['parameters'] .= Html::element( 'dt', null, '*' );
678 $help['parameters'] .= Html::rawElement( 'dd',
679 [ 'class' => 'description' ], $dynamicParams->parse() );
680 }
681
682 $help['parameters'] .= Html::closeElement( 'dl' );
683 $help['parameters'] .= Html::closeElement( 'div' );
684 }
685
686 $examples = $module->getExamplesMessages();
687 if ( $examples ) {
688 $help['examples'] .= Html::openElement( 'div',
689 [ 'class' => 'apihelp-block apihelp-examples' ] );
690 $msg = $context->msg( 'api-help-examples' );
691 if ( !$msg->isDisabled() ) {
692 $help['examples'] .= self::wrap(
693 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
694 );
695 }
696
697 $help['examples'] .= Html::openElement( 'dl' );
698 foreach ( $examples as $qs => $msg ) {
699 $msg = ApiBase::makeMessage( $msg, $context, [
700 $module->getModulePrefix(),
701 $module->getModuleName(),
702 $module->getModulePath()
703 ] );
704
705 $link = wfAppendQuery( wfScript( 'api' ), $qs );
706 $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
707 $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
708 $help['examples'] .= Html::rawElement( 'dd', null,
709 Html::element( 'a', [ 'href' => $link ], "api.php?$qs" ) . ' ' .
710 Html::rawElement( 'a', [ 'href' => $sandbox ],
711 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
712 );
713 }
714 $help['examples'] .= Html::closeElement( 'dl' );
715 $help['examples'] .= Html::closeElement( 'div' );
716 }
717
718 $subtocnumber = $tocnumber;
719 $subtocnumber[$level + 1] = 0;
720 $suboptions = [
721 'submodules' => $options['recursivesubmodules'],
722 'headerlevel' => $level + 1,
723 'tocnumber' => &$subtocnumber,
724 'noheader' => false,
725 ] + $options;
726
727 if ( $options['submodules'] && $module->getModuleManager() ) {
728 $manager = $module->getModuleManager();
729 $submodules = [];
730 foreach ( $groups as $group ) {
731 $names = $manager->getNames( $group );
732 sort( $names );
733 foreach ( $names as $name ) {
734 $submodules[] = $manager->getModule( $name );
735 }
736 }
737 $help['submodules'] .= self::getHelpInternal(
738 $context,
739 $submodules,
740 $suboptions,
741 $haveModules
742 );
743 }
744
745 $module->modifyHelp( $help, $suboptions, $haveModules );
746
747 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
748
749 $out .= implode( "\n", $help );
750 }
751
752 return $out;
753 }
754
755 public function shouldCheckMaxlag() {
756 return false;
757 }
758
759 public function isReadMode() {
760 return false;
761 }
762
763 public function getCustomPrinter() {
764 $params = $this->extractRequestParams();
765 if ( $params['wrap'] ) {
766 return null;
767 }
768
769 $main = $this->getMain();
770 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
771 return new ApiFormatRaw( $main, $errorPrinter );
772 }
773
774 public function getAllowedParams() {
775 return [
776 'modules' => [
777 ApiBase::PARAM_DFLT => 'main',
778 ApiBase::PARAM_ISMULTI => true,
779 ],
780 'submodules' => false,
781 'recursivesubmodules' => false,
782 'wrap' => false,
783 'toc' => false,
784 ];
785 }
786
787 protected function getExamplesMessages() {
788 return [
789 'action=help'
790 => 'apihelp-help-example-main',
791 'action=help&modules=query&submodules=1'
792 => 'apihelp-help-example-submodules',
793 'action=help&recursivesubmodules=1'
794 => 'apihelp-help-example-recursive',
795 'action=help&modules=help'
796 => 'apihelp-help-example-help',
797 'action=help&modules=query+info|query+categorymembers'
798 => 'apihelp-help-example-query',
799 ];
800 }
801
802 public function getHelpUrls() {
803 return [
804 'https://www.mediawiki.org/wiki/API:Main_page',
805 'https://www.mediawiki.org/wiki/API:FAQ',
806 'https://www.mediawiki.org/wiki/API:Quick_start_guide',
807 ];
808 }
809 }