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