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