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