Merge "registration: Convert "config" into an object with metadata"
[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( [
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 = ObjectCache::getMainWANInstance();
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 = wfMemcKey( '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', null, $name );
315 } else {
316 $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
317 $link = Html::element( 'a',
318 [ 'href' => $link, 'class' => 'apihelp-linktrail' ],
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 $extname = $sourceInfo['name'];
354 }
355 $help['flags'] .= Html::rawElement( 'li', null,
356 self::wrap(
357 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
358 'apihelp-source'
359 )
360 );
361
362 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
363 if ( isset( $sourceInfo['license-name'] ) ) {
364 $msg = $context->msg( 'api-help-license', $link, $sourceInfo['license-name'] );
365 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
366 $msg = $context->msg( 'api-help-license-noname', $link );
367 } else {
368 $msg = $context->msg( 'api-help-license-unknown' );
369 }
370 $help['flags'] .= Html::rawElement( 'li', null,
371 self::wrap( $msg, 'apihelp-license' )
372 );
373 } else {
374 $help['flags'] .= Html::rawElement( 'li', null,
375 self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
376 );
377 $help['flags'] .= Html::rawElement( 'li', null,
378 self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
379 );
380 }
381 $help['flags'] .= Html::closeElement( 'ul' );
382 $help['flags'] .= Html::closeElement( 'div' );
383
384 foreach ( $module->getFinalDescription() as $msg ) {
385 $msg->setContext( $context );
386 $help['description'] .= $msg->parseAsBlock();
387 }
388
389 $urls = $module->getHelpUrls();
390 if ( $urls ) {
391 $help['help-urls'] .= Html::openElement( 'div',
392 [ 'class' => 'apihelp-block apihelp-help-urls' ]
393 );
394 $msg = $context->msg( 'api-help-help-urls' );
395 if ( !$msg->isDisabled() ) {
396 $help['help-urls'] .= self::wrap(
397 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
398 );
399 }
400 if ( !is_array( $urls ) ) {
401 $urls = [ $urls ];
402 }
403 $help['help-urls'] .= Html::openElement( 'ul' );
404 foreach ( $urls as $url ) {
405 $help['help-urls'] .= Html::rawElement( 'li', null,
406 Html::element( 'a', [ 'href' => $url ], $url )
407 );
408 }
409 $help['help-urls'] .= Html::closeElement( 'ul' );
410 $help['help-urls'] .= Html::closeElement( 'div' );
411 }
412
413 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
414 $dynamicParams = $module->dynamicParameterDocumentation();
415 $groups = [];
416 if ( $params || $dynamicParams !== null ) {
417 $help['parameters'] .= Html::openElement( 'div',
418 [ 'class' => 'apihelp-block apihelp-parameters' ]
419 );
420 $msg = $context->msg( 'api-help-parameters' );
421 if ( !$msg->isDisabled() ) {
422 $help['parameters'] .= self::wrap(
423 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
424 );
425 }
426 $help['parameters'] .= Html::openElement( 'dl' );
427
428 $descriptions = $module->getFinalParamDescription();
429
430 foreach ( $params as $name => $settings ) {
431 if ( !is_array( $settings ) ) {
432 $settings = [ ApiBase::PARAM_DFLT => $settings ];
433 }
434
435 $help['parameters'] .= Html::element( 'dt', null,
436 $module->encodeParamName( $name ) );
437
438 // Add description
439 $description = [];
440 if ( isset( $descriptions[$name] ) ) {
441 foreach ( $descriptions[$name] as $msg ) {
442 $msg->setContext( $context );
443 $description[] = $msg->parseAsBlock();
444 }
445 }
446
447 // Add usage info
448 $info = [];
449
450 // Required?
451 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
452 $info[] = $context->msg( 'api-help-param-required' )->parse();
453 }
454
455 // Custom info?
456 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
457 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
458 $tag = array_shift( $i );
459 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
460 ->numParams( count( $i ) )
461 ->params( $context->getLanguage()->commaList( $i ) )
462 ->params( $module->getModulePrefix() )
463 ->parse();
464 }
465 }
466
467 // Type documentation
468 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
469 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
470 ? $settings[ApiBase::PARAM_DFLT]
471 : null;
472 if ( is_bool( $dflt ) ) {
473 $settings[ApiBase::PARAM_TYPE] = 'boolean';
474 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
475 $settings[ApiBase::PARAM_TYPE] = 'string';
476 } elseif ( is_int( $dflt ) ) {
477 $settings[ApiBase::PARAM_TYPE] = 'integer';
478 }
479 }
480 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
481 $type = $settings[ApiBase::PARAM_TYPE];
482 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
483 $hintPipeSeparated = true;
484 $count = ApiBase::LIMIT_SML2 + 1;
485
486 if ( is_array( $type ) ) {
487 $count = count( $type );
488 $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
489 ? $settings[ApiBase::PARAM_VALUE_LINKS]
490 : [];
491 $type = array_map( function ( $v ) use ( $links ) {
492 $ret = wfEscapeWikiText( $v );
493 if ( isset( $links[$v] ) ) {
494 $ret = "[[{$links[$v]}|$ret]]";
495 }
496 return $ret;
497 }, $type );
498 $i = array_search( '', $type, true );
499 if ( $i === false ) {
500 $type = $context->getLanguage()->commaList( $type );
501 } else {
502 unset( $type[$i] );
503 $type = $context->msg( 'api-help-param-list-can-be-empty' )
504 ->numParams( count( $type ) )
505 ->params( $context->getLanguage()->commaList( $type ) )
506 ->parse();
507 }
508 $info[] = $context->msg( 'api-help-param-list' )
509 ->params( $multi ? 2 : 1 )
510 ->params( $type )
511 ->parse();
512 $hintPipeSeparated = false;
513 } else {
514 switch ( $type ) {
515 case 'submodule':
516 $groups[] = $name;
517 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
518 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
519 ksort( $map );
520 $submodules = [];
521 foreach ( $map as $v => $m ) {
522 $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
523 }
524 } else {
525 $submodules = $module->getModuleManager()->getNames( $name );
526 sort( $submodules );
527 $prefix = $module->isMain()
528 ? '' : ( $module->getModulePath() . '+' );
529 $submodules = array_map( function ( $name ) use ( $prefix ) {
530 return "[[Special:ApiHelp/{$prefix}{$name}|{$name}]]";
531 }, $submodules );
532 }
533 $count = count( $submodules );
534 $info[] = $context->msg( 'api-help-param-list' )
535 ->params( $multi ? 2 : 1 )
536 ->params( $context->getLanguage()->commaList( $submodules ) )
537 ->parse();
538 $hintPipeSeparated = false;
539 // No type message necessary, we have a list of values.
540 $type = null;
541 break;
542
543 case 'namespace':
544 $namespaces = MWNamespace::getValidNamespaces();
545 $count = count( $namespaces );
546 $info[] = $context->msg( 'api-help-param-list' )
547 ->params( $multi ? 2 : 1 )
548 ->params( $context->getLanguage()->commaList( $namespaces ) )
549 ->parse();
550 $hintPipeSeparated = false;
551 // No type message necessary, we have a list of values.
552 $type = null;
553 break;
554
555 case 'tags':
556 $tags = ChangeTags::listExplicitlyDefinedTags();
557 $count = count( $tags );
558 $info[] = $context->msg( 'api-help-param-list' )
559 ->params( $multi ? 2 : 1 )
560 ->params( $context->getLanguage()->commaList( $tags ) )
561 ->parse();
562 $hintPipeSeparated = false;
563 $type = null;
564 break;
565
566 case 'limit':
567 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
568 $info[] = $context->msg( 'api-help-param-limit2' )
569 ->numParams( $settings[ApiBase::PARAM_MAX] )
570 ->numParams( $settings[ApiBase::PARAM_MAX2] )
571 ->parse();
572 } else {
573 $info[] = $context->msg( 'api-help-param-limit' )
574 ->numParams( $settings[ApiBase::PARAM_MAX] )
575 ->parse();
576 }
577 break;
578
579 case 'integer':
580 // Possible messages:
581 // api-help-param-integer-min,
582 // api-help-param-integer-max,
583 // api-help-param-integer-minmax
584 $suffix = '';
585 $min = $max = 0;
586 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
587 $suffix .= 'min';
588 $min = $settings[ApiBase::PARAM_MIN];
589 }
590 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
591 $suffix .= 'max';
592 $max = $settings[ApiBase::PARAM_MAX];
593 }
594 if ( $suffix !== '' ) {
595 $info[] =
596 $context->msg( "api-help-param-integer-$suffix" )
597 ->params( $multi ? 2 : 1 )
598 ->numParams( $min, $max )
599 ->parse();
600 }
601 break;
602
603 case 'upload':
604 $info[] = $context->msg( 'api-help-param-upload' )
605 ->parse();
606 // No type message necessary, api-help-param-upload should handle it.
607 $type = null;
608 break;
609
610 case 'string':
611 case 'text':
612 // Displaying a type message here would be useless.
613 $type = null;
614 break;
615 }
616 }
617
618 // Add type. Messages for grep: api-help-param-type-limit
619 // api-help-param-type-integer api-help-param-type-boolean
620 // api-help-param-type-timestamp api-help-param-type-user
621 // api-help-param-type-password
622 if ( is_string( $type ) ) {
623 $msg = $context->msg( "api-help-param-type-$type" );
624 if ( !$msg->isDisabled() ) {
625 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
626 }
627 }
628
629 if ( $multi ) {
630 $extra = [];
631 if ( $hintPipeSeparated ) {
632 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
633 }
634 if ( $count > ApiBase::LIMIT_SML1 ) {
635 $extra[] = $context->msg( 'api-help-param-multi-max' )
636 ->numParams( ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2 )
637 ->parse();
638 }
639 if ( $extra ) {
640 $info[] = implode( ' ', $extra );
641 }
642 }
643 }
644
645 // Add default
646 $default = isset( $settings[ApiBase::PARAM_DFLT] )
647 ? $settings[ApiBase::PARAM_DFLT]
648 : null;
649 if ( $default === '' ) {
650 $info[] = $context->msg( 'api-help-param-default-empty' )
651 ->parse();
652 } elseif ( $default !== null && $default !== false ) {
653 $info[] = $context->msg( 'api-help-param-default' )
654 ->params( wfEscapeWikiText( $default ) )
655 ->parse();
656 }
657
658 if ( !array_filter( $description ) ) {
659 $description = [ self::wrap(
660 $context->msg( 'api-help-param-no-description' ),
661 'apihelp-empty'
662 ) ];
663 }
664
665 // Add "deprecated" flag
666 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
667 $help['parameters'] .= Html::openElement( 'dd',
668 [ 'class' => 'info' ] );
669 $help['parameters'] .= self::wrap(
670 $context->msg( 'api-help-param-deprecated' ),
671 'apihelp-deprecated', 'strong'
672 );
673 $help['parameters'] .= Html::closeElement( 'dd' );
674 }
675
676 if ( $description ) {
677 $description = implode( '', $description );
678 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
679 $help['parameters'] .= Html::rawElement( 'dd',
680 [ 'class' => 'description' ], $description );
681 }
682
683 foreach ( $info as $i ) {
684 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
685 }
686 }
687
688 if ( $dynamicParams !== null ) {
689 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
690 $module->getModulePrefix(),
691 $module->getModuleName(),
692 $module->getModulePath()
693 ] );
694 $help['parameters'] .= Html::element( 'dt', null, '*' );
695 $help['parameters'] .= Html::rawElement( 'dd',
696 [ 'class' => 'description' ], $dynamicParams->parse() );
697 }
698
699 $help['parameters'] .= Html::closeElement( 'dl' );
700 $help['parameters'] .= Html::closeElement( 'div' );
701 }
702
703 $examples = $module->getExamplesMessages();
704 if ( $examples ) {
705 $help['examples'] .= Html::openElement( 'div',
706 [ 'class' => 'apihelp-block apihelp-examples' ] );
707 $msg = $context->msg( 'api-help-examples' );
708 if ( !$msg->isDisabled() ) {
709 $help['examples'] .= self::wrap(
710 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
711 );
712 }
713
714 $help['examples'] .= Html::openElement( 'dl' );
715 foreach ( $examples as $qs => $msg ) {
716 $msg = ApiBase::makeMessage( $msg, $context, [
717 $module->getModulePrefix(),
718 $module->getModuleName(),
719 $module->getModulePath()
720 ] );
721
722 $link = wfAppendQuery( wfScript( 'api' ), $qs );
723 $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
724 $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
725 $help['examples'] .= Html::rawElement( 'dd', null,
726 Html::element( 'a', [ 'href' => $link ], "api.php?$qs" ) . ' ' .
727 Html::rawElement( 'a', [ 'href' => $sandbox ],
728 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
729 );
730 }
731 $help['examples'] .= Html::closeElement( 'dl' );
732 $help['examples'] .= Html::closeElement( 'div' );
733 }
734
735 $subtocnumber = $tocnumber;
736 $subtocnumber[$level + 1] = 0;
737 $suboptions = [
738 'submodules' => $options['recursivesubmodules'],
739 'headerlevel' => $level + 1,
740 'tocnumber' => &$subtocnumber,
741 'noheader' => false,
742 ] + $options;
743
744 if ( $options['submodules'] && $module->getModuleManager() ) {
745 $manager = $module->getModuleManager();
746 $submodules = [];
747 foreach ( $groups as $group ) {
748 $names = $manager->getNames( $group );
749 sort( $names );
750 foreach ( $names as $name ) {
751 $submodules[] = $manager->getModule( $name );
752 }
753 }
754 $help['submodules'] .= self::getHelpInternal(
755 $context,
756 $submodules,
757 $suboptions,
758 $haveModules
759 );
760 }
761
762 $module->modifyHelp( $help, $suboptions, $haveModules );
763
764 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
765
766 $out .= implode( "\n", $help );
767 }
768
769 return $out;
770 }
771
772 public function shouldCheckMaxlag() {
773 return false;
774 }
775
776 public function isReadMode() {
777 return false;
778 }
779
780 public function getCustomPrinter() {
781 $params = $this->extractRequestParams();
782 if ( $params['wrap'] ) {
783 return null;
784 }
785
786 $main = $this->getMain();
787 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
788 return new ApiFormatRaw( $main, $errorPrinter );
789 }
790
791 public function getAllowedParams() {
792 return [
793 'modules' => [
794 ApiBase::PARAM_DFLT => 'main',
795 ApiBase::PARAM_ISMULTI => true,
796 ],
797 'submodules' => false,
798 'recursivesubmodules' => false,
799 'wrap' => false,
800 'toc' => false,
801 ];
802 }
803
804 protected function getExamplesMessages() {
805 return [
806 'action=help'
807 => 'apihelp-help-example-main',
808 'action=help&modules=query&submodules=1'
809 => 'apihelp-help-example-submodules',
810 'action=help&recursivesubmodules=1'
811 => 'apihelp-help-example-recursive',
812 'action=help&modules=help'
813 => 'apihelp-help-example-help',
814 'action=help&modules=query+info|query+categorymembers'
815 => 'apihelp-help-example-query',
816 ];
817 }
818
819 public function getHelpUrls() {
820 return [
821 'https://www.mediawiki.org/wiki/API:Main_page',
822 'https://www.mediawiki.org/wiki/API:FAQ',
823 'https://www.mediawiki.org/wiki/API:Quick_start_guide',
824 ];
825 }
826 }