5 * Created on Aug 29, 2014
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
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.
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.
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
28 * Class to output help for an API module
30 * @since 1.25 completely rewritten
33 class ApiHelp
extends ApiBase
{
34 public function execute() {
35 $params = $this->extractRequestParams();
38 foreach ( $params['modules'] as $path ) {
39 $modules[] = $this->getModuleFromPath( $path );
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 );
50 self
::getHelp( $context, $modules, $params );
52 // Grab the output from the skin
54 $context->getOutput()->output();
55 $html = ob_get_clean();
57 $result = $this->getResult();
58 if ( $params['wrap'] ) {
60 'mime' => 'text/html',
63 ApiResult
::setSubelementsList( $data, 'help' );
64 $result->addValue( null, $this->getModuleName(), $data );
67 $result->addValue( null, 'text', $html, ApiResult
::NO_SIZE_CHECK
);
68 $result->addValue( null, 'mime', 'text/html', ApiResult
::NO_SIZE_CHECK
);
73 * Generate help for the specified modules
75 * Help is placed into the OutputPage object returned by
76 * $context->getOutput().
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
87 * @param IContextSource $context
88 * @param ApiBase[]|ApiBase $modules
89 * @param array $options Formatting options (described above)
92 public static function getHelp( IContextSource
$context, $modules, array $options ) {
93 global $wgMemc, $wgContLang;
95 if ( !is_array( $modules ) ) {
96 $modules = array( $modules );
99 $out = $context->getOutput();
100 $out->addModules( 'mediawiki.apihelp' );
101 if ( !empty( $options['toc'] ) ) {
102 $out->addModules( 'mediawiki.toc' );
104 $out->setPageTitle( $context->msg( 'api-help-title' ) );
107 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain
&&
108 $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
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 );
118 $out->addHTML( $cached );
123 if ( $out->getHTML() !== '' ) {
124 // Don't save to cache, there's someone else's content in the page
129 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
130 $options['submodules'] = $options['recursivesubmodules'] ||
!empty( $options['submodules'] );
133 if ( empty( $options['nolead'] ) ) {
134 $msg = $context->msg( 'api-help-lead' );
135 if ( !$msg->isDisabled() ) {
136 $out->addHTML( $msg->parseAsBlock() );
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() ) );
145 $out->addHTML( $html );
147 $helptitle = isset( $options['helptitle'] ) ?
$options['helptitle'] : null;
148 $html = self
::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
150 $out->addHTML( $html );
152 if ( $cacheKey !== null ) {
153 $wgMemc->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
158 * Replace Special:ApiHelp links with links to api.php
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
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' );
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] )
183 $href = wfAppendQuery( wfScript( 'api' ), array(
188 $node->setAttribute( 'href', $href );
189 $node->removeAttribute( 'title' );
193 return $formatter->getText();
197 * Wrap a message in HTML with a class.
199 * @param Message $msg
200 * @param string $class
204 private static function wrap( Message
$msg, $class, $tag = 'span' ) {
205 return Html
::rawElement( $tag, array( 'class' => $class ),
211 * Recursively-called function to actually construct the help
213 * @param IContextSource $context
214 * @param ApiBase[] $modules
215 * @param array $options
216 * @param array &$haveModules
219 private static function getHelpInternal( IContextSource
$context, array $modules,
220 array $options, &$haveModules
224 $level = empty( $options['headerlevel'] ) ?
2 : $options['headerlevel'];
225 if ( empty( $options['tocnumber'] ) ) {
226 $tocnumber = array( 2 => 0 );
228 $tocnumber = &$options['tocnumber'];
231 foreach ( $modules as $module ) {
232 $tocnumber[$level]++
;
233 $path = $module->getModulePath();
234 $module->setContext( $context );
245 if ( empty( $options['noheader'] ) ||
!empty( $options['toc'] ) ) {
248 while ( isset( $haveModules[$anchor] ) ) {
249 $anchor = $path . '|' . ++
$i;
252 if ( $module->isMain() ) {
253 $header = $context->msg( 'api-help-main-header' )->parse();
255 $name = $module->getModuleName();
256 $header = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
258 if ( $module->getModulePrefix() !== '' ) {
260 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
263 $haveModules[$anchor] = array(
264 'toclevel' => count( $tocnumber ),
268 'number' => join( '.', $tocnumber ),
271 if ( empty( $options['noheader'] ) ) {
272 $help['header'] .= Html
::element( 'h' . min( 6, $level ),
273 array( 'id' => $anchor, 'class' => 'apihelp-header' ),
278 $haveModules[$path] = true;
283 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
284 $name = $m->getModuleName();
285 if ( $name === 'main_int' ) {
289 if ( count( $modules ) === 1 && $m === $modules[0] &&
290 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
292 $link = Html
::element( 'b', null, $name );
294 $link = SpecialPage
::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
295 $link = Html
::element( 'a',
296 array( 'href' => $link, 'class' => 'apihelp-linktrail' ),
301 array_unshift( $links, $link );
304 $help['header'] .= self
::wrap(
305 $context->msg( 'parentheses' )
306 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
307 'apihelp-linktrail', 'div'
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'
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" )
325 $sourceInfo = $module->getModuleSourceInfo();
327 if ( isset( $sourceInfo['namemsg'] ) ) {
328 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
330 $extname = $sourceInfo['name'];
332 $help['flags'] .= Html
::rawElement( 'li', null,
334 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
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 );
345 $msg = $context->msg( 'api-help-license-unknown' );
347 $help['flags'] .= Html
::rawElement( 'li', null,
348 self
::wrap( $msg, 'apihelp-license' )
351 $help['flags'] .= Html
::rawElement( 'li', null,
352 self
::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
354 $help['flags'] .= Html
::rawElement( 'li', null,
355 self
::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
358 $help['flags'] .= Html
::closeElement( 'ul' );
359 $help['flags'] .= Html
::closeElement( 'div' );
361 foreach ( $module->getFinalDescription() as $msg ) {
362 $msg->setContext( $context );
363 $help['description'] .= $msg->parseAsBlock();
366 $urls = $module->getHelpUrls();
368 $help['help-urls'] .= Html
::openElement( 'div',
369 array( 'class' => 'apihelp-block apihelp-help-urls' )
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'
377 if ( !is_array( $urls ) ) {
378 $urls = array( $urls );
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 )
386 $help['help-urls'] .= Html
::closeElement( 'ul' );
387 $help['help-urls'] .= Html
::closeElement( 'div' );
390 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
393 $help['parameters'] .= Html
::openElement( 'div',
394 array( 'class' => 'apihelp-block apihelp-parameters' )
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'
402 $help['parameters'] .= Html
::openElement( 'dl' );
404 $descriptions = $module->getFinalParamDescription();
406 foreach ( $params as $name => $settings ) {
407 if ( !is_array( $settings ) ) {
408 $settings = array( ApiBase
::PARAM_DFLT
=> $settings );
411 $help['parameters'] .= Html
::element( 'dt', null,
412 $module->encodeParamName( $name ) );
415 $description = array();
416 if ( isset( $descriptions[$name] ) ) {
417 foreach ( $descriptions[$name] as $msg ) {
418 $msg->setContext( $context );
419 $description[] = $msg->parseAsBlock();
427 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
428 $info[] = $context->msg( 'api-help-param-required' )->parse();
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() )
443 // Type documentation
444 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
445 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
446 ?
$settings[ApiBase
::PARAM_DFLT
]
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';
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;
462 if ( is_array( $type ) ) {
463 $count = count( $type );
464 $links = isset( $settings[ApiBase
::PARAM_VALUE_LINKS
] )
465 ?
$settings[ApiBase
::PARAM_VALUE_LINKS
]
467 $type = array_map( function ( $v ) use ( $links ) {
468 $ret = wfEscapeWikiText( $v );
469 if ( isset( $links[$v] ) ) {
470 $ret = "[[{$links[$v]}|$ret]]";
474 $i = array_search( '', $type, true );
475 if ( $i === false ) {
476 $type = $context->getLanguage()->commaList( $type );
479 $type = $context->msg( 'api-help-param-list-can-be-empty' )
480 ->numParams( count( $type ) )
481 ->params( $context->getLanguage()->commaList( $type ) )
484 $info[] = $context->msg( 'api-help-param-list' )
485 ->params( $multi ?
2 : 1 )
488 $hintPipeSeparated = false;
493 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
494 $map = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
496 $submodules = array();
497 foreach ( $map as $v => $m ) {
498 $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
501 $submodules = $module->getModuleManager()->getNames( $name );
503 $prefix = $module->isMain()
504 ?
'' : ( $module->getModulePath() . '+' );
505 $submodules = array_map( function ( $name ) use ( $prefix ) {
506 return "[[Special:ApiHelp/{$prefix}{$name}|{$name}]]";
509 $count = count( $submodules );
510 $info[] = $context->msg( 'api-help-param-list' )
511 ->params( $multi ?
2 : 1 )
512 ->params( $context->getLanguage()->commaList( $submodules ) )
514 $hintPipeSeparated = false;
515 // No type message necessary, we have a list of values.
520 $namespaces = MWNamespace
::getValidNamespaces();
521 $count = count( $namespaces );
522 $info[] = $context->msg( 'api-help-param-list' )
523 ->params( $multi ?
2 : 1 )
524 ->params( $context->getLanguage()->commaList( $namespaces ) )
526 $hintPipeSeparated = false;
527 // No type message necessary, we have a list of values.
532 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
533 $info[] = $context->msg( 'api-help-param-limit2' )
534 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
535 ->numParams( $settings[ApiBase
::PARAM_MAX2
] )
538 $info[] = $context->msg( 'api-help-param-limit' )
539 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
545 // Possible messages:
546 // api-help-param-integer-min,
547 // api-help-param-integer-max,
548 // api-help-param-integer-minmax
551 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
553 $min = $settings[ApiBase
::PARAM_MIN
];
555 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
557 $max = $settings[ApiBase
::PARAM_MAX
];
559 if ( $suffix !== '' ) {
561 $context->msg( "api-help-param-integer-$suffix" )
562 ->params( $multi ?
2 : 1 )
563 ->numParams( $min, $max )
569 $info[] = $context->msg( 'api-help-param-upload' )
571 // No type message necessary, api-help-param-upload should handle it.
577 // Displaying a type message here would be useless.
583 // Add type. Messages for grep: api-help-param-type-limit
584 // api-help-param-type-integer api-help-param-type-boolean
585 // api-help-param-type-timestamp api-help-param-type-user
586 // api-help-param-type-password
587 if ( is_string( $type ) ) {
588 $msg = $context->msg( "api-help-param-type-$type" );
589 if ( !$msg->isDisabled() ) {
590 $info[] = $msg->params( $multi ?
2 : 1 )->parse();
596 if ( $hintPipeSeparated ) {
597 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
599 if ( $count > ApiBase
::LIMIT_SML1
) {
600 $extra[] = $context->msg( 'api-help-param-multi-max' )
601 ->numParams( ApiBase
::LIMIT_SML1
, ApiBase
::LIMIT_SML2
)
605 $info[] = join( ' ', $extra );
611 $default = isset( $settings[ApiBase
::PARAM_DFLT
] )
612 ?
$settings[ApiBase
::PARAM_DFLT
]
614 if ( $default === '' ) {
615 $info[] = $context->msg( 'api-help-param-default-empty' )
617 } elseif ( $default !== null && $default !== false ) {
618 $info[] = $context->msg( 'api-help-param-default' )
619 ->params( wfEscapeWikiText( $default ) )
623 if ( !array_filter( $description ) ) {
624 $description = array( self
::wrap(
625 $context->msg( 'api-help-param-no-description' ),
630 // Add "deprecated" flag
631 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
632 $help['parameters'] .= Html
::openElement( 'dd',
633 array( 'class' => 'info' ) );
634 $help['parameters'] .= self
::wrap(
635 $context->msg( 'api-help-param-deprecated' ),
636 'apihelp-deprecated', 'strong'
638 $help['parameters'] .= Html
::closeElement( 'dd' );
641 if ( $description ) {
642 $description = join( '', $description );
643 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
644 $help['parameters'] .= Html
::rawElement( 'dd',
645 array( 'class' => 'description' ), $description );
648 foreach ( $info as $i ) {
649 $help['parameters'] .= Html
::rawElement( 'dd', array( 'class' => 'info' ), $i );
653 $help['parameters'] .= Html
::closeElement( 'dl' );
654 $help['parameters'] .= Html
::closeElement( 'div' );
657 $examples = $module->getExamplesMessages();
659 $help['examples'] .= Html
::openElement( 'div',
660 array( 'class' => 'apihelp-block apihelp-examples' ) );
661 $msg = $context->msg( 'api-help-examples' );
662 if ( !$msg->isDisabled() ) {
663 $help['examples'] .= self
::wrap(
664 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
668 $help['examples'] .= Html
::openElement( 'dl' );
669 foreach ( $examples as $qs => $msg ) {
670 $msg = ApiBase
::makeMessage( $msg, $context, array(
671 $module->getModulePrefix(),
672 $module->getModuleName(),
673 $module->getModulePath()
676 $link = wfAppendQuery( wfScript( 'api' ), $qs );
677 $help['examples'] .= Html
::rawElement( 'dt', null, $msg->parse() );
678 $help['examples'] .= Html
::rawElement( 'dd', null,
679 Html
::element( 'a', array( 'href' => $link ), "api.php?$qs" )
682 $help['examples'] .= Html
::closeElement( 'dl' );
683 $help['examples'] .= Html
::closeElement( 'div' );
686 $subtocnumber = $tocnumber;
687 $subtocnumber[$level +
1] = 0;
689 'submodules' => $options['recursivesubmodules'],
690 'headerlevel' => $level +
1,
691 'tocnumber' => &$subtocnumber,
695 if ( $options['submodules'] && $module->getModuleManager() ) {
696 $manager = $module->getModuleManager();
697 $submodules = array();
698 foreach ( $groups as $group ) {
699 $names = $manager->getNames( $group );
701 foreach ( $names as $name ) {
702 $submodules[] = $manager->getModule( $name );
705 $help['submodules'] .= self
::getHelpInternal( $context, $submodules, $suboptions, $haveModules );
706 $numSubmodules = count( $submodules );
709 $module->modifyHelp( $help, $suboptions, $haveModules );
711 Hooks
::run( 'APIHelpModifyOutput', array( $module, &$help, $suboptions, &$haveModules ) );
713 $out .= join( "\n", $help );
719 public function shouldCheckMaxlag() {
723 public function isReadMode() {
727 public function getCustomPrinter() {
728 $params = $this->extractRequestParams();
729 if ( $params['wrap'] ) {
733 $main = $this->getMain();
734 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
735 return new ApiFormatRaw( $main, $errorPrinter );
738 public function getAllowedParams() {
741 ApiBase
::PARAM_DFLT
=> 'main',
742 ApiBase
::PARAM_ISMULTI
=> true,
744 'submodules' => false,
745 'recursivesubmodules' => false,
751 protected function getExamplesMessages() {
754 => 'apihelp-help-example-main',
755 'action=help&recursivesubmodules=1'
756 => 'apihelp-help-example-recursive',
757 'action=help&modules=help'
758 => 'apihelp-help-example-help',
759 'action=help&modules=query+info|query+categorymembers'
760 => 'apihelp-help-example-query',
764 public function getHelpUrls() {
766 'https://www.mediawiki.org/wiki/API:Main_page',
767 'https://www.mediawiki.org/wiki/API:FAQ',
768 'https://www.mediawiki.org/wiki/API:Quick_start_guide',