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