MediaWiki REL1_33
ApiHelp.php
Go to the documentation of this file.
1<?php
25
32class ApiHelp extends ApiBase {
33 public function execute() {
35 $modules = [];
36
37 foreach ( $params['modules'] as $path ) {
38 $modules[] = $this->getModuleFromPath( $path );
39 }
40
41 // Get the help
42 $context = new DerivativeContext( $this->getMain()->getContext() );
43 $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
44 $context->setLanguage( $this->getMain()->getLanguage() );
45 $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
46 $out = new OutputPage( $context );
47 $out->setRobotPolicy( 'noindex,nofollow' );
48 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
49 $context->setOutput( $out );
50
52
53 // Grab the output from the skin
54 ob_start();
55 $context->getOutput()->output();
57
58 $result = $this->getResult();
59 if ( $params['wrap'] ) {
60 $data = [
61 'mime' => 'text/html',
62 'filename' => 'api-help.html',
63 'help' => $html,
64 ];
66 $result->addValue( null, $this->getModuleName(), $data );
67 } else {
68 $result->reset();
69 $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
70 $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
71 $result->addValue( null, 'filename', 'api-help.html', ApiResult::NO_SIZE_CHECK );
72 }
73 }
74
94 public static function getHelp( IContextSource $context, $modules, array $options ) {
95 if ( !is_array( $modules ) ) {
96 $modules = [ $modules ];
97 }
98
100 $out->addModuleStyles( [
101 'mediawiki.hlist',
102 'mediawiki.apihelp',
103 ] );
104 if ( !empty( $options['toc'] ) ) {
105 $out->addModuleStyles( 'mediawiki.toc.styles' );
106 }
107 $out->setPageTitle( $context->msg( 'api-help-title' ) );
108
109 $services = MediaWikiServices::getInstance();
110 $cache = $services->getMainWANObjectCache();
111 $cacheKey = null;
112 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain &&
113 $options['recursivesubmodules'] &&
114 $context->getLanguage()->equals( $services->getContentLanguage() )
115 ) {
116 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
117 if ( $cacheHelpTimeout > 0 ) {
118 // Get help text from cache if present
119 $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
120 (int)!empty( $options['toc'] ),
121 str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) );
122 $cached = $cache->get( $cacheKey );
123 if ( $cached ) {
124 $out->addHTML( $cached );
125 return;
126 }
127 }
128 }
129 if ( $out->getHTML() !== '' ) {
130 // Don't save to cache, there's someone else's content in the page
131 // already
132 $cacheKey = null;
133 }
134
135 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
136 $options['submodules'] = $options['recursivesubmodules'] || !empty( $options['submodules'] );
137
138 // Prepend lead
139 if ( empty( $options['nolead'] ) ) {
140 $msg = $context->msg( 'api-help-lead' );
141 if ( !$msg->isDisabled() ) {
142 $out->addHTML( $msg->parseAsBlock() );
143 }
144 }
145
146 $haveModules = [];
148 if ( !empty( $options['toc'] ) && $haveModules ) {
149 $out->addHTML( Linker::generateTOC( $haveModules, $context->getLanguage() ) );
150 }
151 $out->addHTML( $html );
152
153 $helptitle = $options['helptitle'] ?? null;
154 $html = self::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
155 $out->clearHTML();
156 $out->addHTML( $html );
157
158 if ( $cacheKey !== null ) {
159 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
160 }
161 }
162
171 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
172 $formatter = new HtmlFormatter( $html );
173 $doc = $formatter->getDoc();
174 $xpath = new DOMXPath( $doc );
175 $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
176 foreach ( $nodes as $node ) {
177 $href = $node->getAttribute( 'href' );
178 do {
179 $old = $href;
180 $href = rawurldecode( $href );
181 } while ( $old !== $href );
182 if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
183 if ( isset( $localModules[$m[1]] ) ) {
184 $href = $m[2] === '' ? '#' . $m[1] : $m[2];
185 } elseif ( $helptitle !== null ) {
186 $href = Title::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
187 ->getFullURL();
188 } else {
189 $href = wfAppendQuery( wfScript( 'api' ), [
190 'action' => 'help',
191 'modules' => $m[1],
192 ] ) . $m[2];
193 }
194 $node->setAttribute( 'href', $href );
195 $node->removeAttribute( 'title' );
196 }
197 }
198
199 return $formatter->getText();
200 }
201
210 private static function wrap( Message $msg, $class, $tag = 'span' ) {
211 return Html::rawElement( $tag, [ 'class' => $class ],
212 $msg->parse()
213 );
214 }
215
226 array $options, &$haveModules
227 ) {
228 $out = '';
229
230 $level = empty( $options['headerlevel'] ) ? 2 : $options['headerlevel'];
231 if ( empty( $options['tocnumber'] ) ) {
232 $tocnumber = [ 2 => 0 ];
233 } else {
234 $tocnumber = &$options['tocnumber'];
235 }
236
237 foreach ( $modules as $module ) {
238 $tocnumber[$level]++;
239 $path = $module->getModulePath();
240 $module->setContext( $context );
241 $help = [
242 'header' => '',
243 'flags' => '',
244 'description' => '',
245 'help-urls' => '',
246 'parameters' => '',
247 'examples' => '',
248 'submodules' => '',
249 ];
250
251 if ( empty( $options['noheader'] ) || !empty( $options['toc'] ) ) {
252 $anchor = $path;
253 $i = 1;
254 while ( isset( $haveModules[$anchor] ) ) {
255 $anchor = $path . '|' . ++$i;
256 }
257
258 if ( $module->isMain() ) {
259 $headerContent = $context->msg( 'api-help-main-header' )->parse();
260 $headerAttr = [
261 'class' => 'apihelp-header',
262 ];
263 } else {
264 $name = $module->getModuleName();
265 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
266 "=$name";
267 if ( $module->getModulePrefix() !== '' ) {
268 $headerContent .= ' ' .
269 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
270 }
271 // Module names are always in English and not localized,
272 // so English language and direction must be set explicitly,
273 // otherwise parentheses will get broken in RTL wikis
274 $headerAttr = [
275 'class' => 'apihelp-header apihelp-module-name',
276 'dir' => 'ltr',
277 'lang' => 'en',
278 ];
279 }
280
281 $headerAttr['id'] = $anchor;
282
283 $haveModules[$anchor] = [
284 'toclevel' => count( $tocnumber ),
285 'level' => $level,
286 'anchor' => $anchor,
287 'line' => $headerContent,
288 'number' => implode( '.', $tocnumber ),
289 'index' => false,
290 ];
291 if ( empty( $options['noheader'] ) ) {
292 $help['header'] .= Html::element(
293 'h' . min( 6, $level ),
294 $headerAttr,
295 $headerContent
296 );
297 }
298 } else {
299 $haveModules[$path] = true;
300 }
301
302 $links = [];
303 $any = false;
304 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
305 $name = $m->getModuleName();
306 if ( $name === 'main_int' ) {
307 $name = 'main';
308 }
309
310 if ( count( $modules ) === 1 && $m === $modules[0] &&
311 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
312 ) {
313 $link = Html::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
314 } else {
315 $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
316 $link = Html::element( 'a',
317 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
318 $name
319 );
320 $any = true;
321 }
322 array_unshift( $links, $link );
323 }
324 if ( $any ) {
325 $help['header'] .= self::wrap(
326 $context->msg( 'parentheses' )
327 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
328 'apihelp-linktrail', 'div'
329 );
330 }
331
332 $flags = $module->getHelpFlags();
333 $help['flags'] .= Html::openElement( 'div',
334 [ 'class' => 'apihelp-block apihelp-flags' ] );
335 $msg = $context->msg( 'api-help-flags' );
336 if ( !$msg->isDisabled() ) {
337 $help['flags'] .= self::wrap(
338 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
339 );
340 }
341 $help['flags'] .= Html::openElement( 'ul' );
342 foreach ( $flags as $flag ) {
343 $help['flags'] .= Html::rawElement( 'li', null,
344 self::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
345 );
346 }
347 $sourceInfo = $module->getModuleSourceInfo();
348 if ( $sourceInfo ) {
349 if ( isset( $sourceInfo['namemsg'] ) ) {
350 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
351 } else {
352 // Probably English, so wrap it.
353 $extname = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
354 }
355 $help['flags'] .= Html::rawElement( 'li', null,
356 self::wrap(
357 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
358 'apihelp-source'
359 )
360 );
361
362 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
363 if ( isset( $sourceInfo['license-name'] ) ) {
364 $msg = $context->msg( 'api-help-license', $link,
365 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
366 );
367 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
368 $msg = $context->msg( 'api-help-license-noname', $link );
369 } else {
370 $msg = $context->msg( 'api-help-license-unknown' );
371 }
372 $help['flags'] .= Html::rawElement( 'li', null,
373 self::wrap( $msg, 'apihelp-license' )
374 );
375 } else {
376 $help['flags'] .= Html::rawElement( 'li', null,
377 self::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
378 );
379 $help['flags'] .= Html::rawElement( 'li', null,
380 self::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
381 );
382 }
383 $help['flags'] .= Html::closeElement( 'ul' );
384 $help['flags'] .= Html::closeElement( 'div' );
385
386 foreach ( $module->getFinalDescription() as $msg ) {
387 $msg->setContext( $context );
388 $help['description'] .= $msg->parseAsBlock();
389 }
390
391 $urls = $module->getHelpUrls();
392 if ( $urls ) {
393 $help['help-urls'] .= Html::openElement( 'div',
394 [ 'class' => 'apihelp-block apihelp-help-urls' ]
395 );
396 $msg = $context->msg( 'api-help-help-urls' );
397 if ( !$msg->isDisabled() ) {
398 $help['help-urls'] .= self::wrap(
399 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
400 );
401 }
402 if ( !is_array( $urls ) ) {
403 $urls = [ $urls ];
404 }
405 $help['help-urls'] .= Html::openElement( 'ul' );
406 foreach ( $urls as $url ) {
407 $help['help-urls'] .= Html::rawElement( 'li', null,
408 Html::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
409 );
410 }
411 $help['help-urls'] .= Html::closeElement( 'ul' );
412 $help['help-urls'] .= Html::closeElement( 'div' );
413 }
414
415 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
416 $dynamicParams = $module->dynamicParameterDocumentation();
417 $groups = [];
418 if ( $params || $dynamicParams !== null ) {
419 $help['parameters'] .= Html::openElement( 'div',
420 [ 'class' => 'apihelp-block apihelp-parameters' ]
421 );
422 $msg = $context->msg( 'api-help-parameters' );
423 if ( !$msg->isDisabled() ) {
424 $help['parameters'] .= self::wrap(
425 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
426 );
427 }
428 $help['parameters'] .= Html::openElement( 'dl' );
429
430 $descriptions = $module->getFinalParamDescription();
431
432 foreach ( $params as $name => $settings ) {
433 if ( !is_array( $settings ) ) {
434 $settings = [ ApiBase::PARAM_DFLT => $settings ];
435 }
436
437 $help['parameters'] .= Html::rawElement( 'dt', null,
438 Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
439 );
440
441 // Add description
442 $description = [];
443 if ( isset( $descriptions[$name] ) ) {
444 foreach ( $descriptions[$name] as $msg ) {
445 $msg->setContext( $context );
446 $description[] = $msg->parseAsBlock();
447 }
448 }
449
450 // Add usage info
451 $info = [];
452
453 // Required?
454 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
455 $info[] = $context->msg( 'api-help-param-required' )->parse();
456 }
457
458 // Custom info?
459 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
460 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
461 $tag = array_shift( $i );
462 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
463 ->numParams( count( $i ) )
464 ->params( $context->getLanguage()->commaList( $i ) )
465 ->params( $module->getModulePrefix() )
466 ->parse();
467 }
468 }
469
470 // Templated?
471 if ( !empty( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
472 $vars = [];
473 $msg = 'api-help-param-templated-var-first';
474 foreach ( $settings[ApiBase::PARAM_TEMPLATE_VARS] as $k => $v ) {
475 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
476 $msg = 'api-help-param-templated-var';
477 }
478 $info[] = $context->msg( 'api-help-param-templated' )
479 ->numParams( count( $vars ) )
480 ->params( Message::listParam( $vars ) )
481 ->parse();
482 }
483
484 // Type documentation
485 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
486 $dflt = $settings[ApiBase::PARAM_DFLT] ?? null;
487 if ( is_bool( $dflt ) ) {
488 $settings[ApiBase::PARAM_TYPE] = 'boolean';
489 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
490 $settings[ApiBase::PARAM_TYPE] = 'string';
491 } elseif ( is_int( $dflt ) ) {
492 $settings[ApiBase::PARAM_TYPE] = 'integer';
493 }
494 }
495 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
496 $type = $settings[ApiBase::PARAM_TYPE];
497 $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
498 $hintPipeSeparated = true;
499 $count = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
500 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2] + 1
502
503 if ( is_array( $type ) ) {
504 $count = count( $type );
505 $deprecatedValues = $settings[ApiBase::PARAM_DEPRECATED_VALUES] ?? [];
506 $links = $settings[ApiBase::PARAM_VALUE_LINKS] ?? [];
507 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
508 $attr = [];
509 if ( $v !== '' ) {
510 // We can't know whether this contains LTR or RTL text.
511 $attr['dir'] = 'auto';
512 }
513 if ( isset( $deprecatedValues[$v] ) ) {
514 $attr['class'] = 'apihelp-deprecated-value';
515 }
516 $ret = $attr ? Html::element( 'span', $attr, $v ) : $v;
517 if ( isset( $links[$v] ) ) {
518 $ret = "[[{$links[$v]}|$ret]]";
519 }
520 return $ret;
521 }, $type );
522 $i = array_search( '', $type, true );
523 if ( $i === false ) {
524 $values = $context->getLanguage()->commaList( $values );
525 } else {
526 unset( $values[$i] );
527 $values = $context->msg( 'api-help-param-list-can-be-empty' )
528 ->numParams( count( $values ) )
529 ->params( $context->getLanguage()->commaList( $values ) )
530 ->parse();
531 }
532 $info[] = $context->msg( 'api-help-param-list' )
533 ->params( $multi ? 2 : 1 )
534 ->params( $values )
535 ->parse();
536 $hintPipeSeparated = false;
537 } else {
538 switch ( $type ) {
539 case 'submodule':
540 $groups[] = $name;
541
542 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
543 $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
544 $defaultAttrs = [];
545 } else {
546 $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
547 $map = [];
548 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
549 $map[$submoduleName] = $prefix . $submoduleName;
550 }
551 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
552 }
553 ksort( $map );
554
555 $submodules = [];
556 $deprecatedSubmodules = [];
557 foreach ( $map as $v => $m ) {
558 $attrs = $defaultAttrs;
559 $arr = &$submodules;
560 try {
561 $submod = $module->getModuleFromPath( $m );
562 if ( $submod && $submod->isDeprecated() ) {
563 $arr = &$deprecatedSubmodules;
564 $attrs['class'] = 'apihelp-deprecated-value';
565 }
566 } catch ( ApiUsageException $ex ) {
567 // Ignore
568 }
569 if ( $attrs ) {
570 $v = Html::element( 'span', $attrs, $v );
571 }
572 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
573 }
574 $submodules = array_merge( $submodules, $deprecatedSubmodules );
575 $count = count( $submodules );
576 $info[] = $context->msg( 'api-help-param-list' )
577 ->params( $multi ? 2 : 1 )
578 ->params( $context->getLanguage()->commaList( $submodules ) )
579 ->parse();
580 $hintPipeSeparated = false;
581 // No type message necessary, we have a list of values.
582 $type = null;
583 break;
584
585 case 'namespace':
586 $namespaces = MWNamespace::getValidNamespaces();
587 if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
589 ) {
591 }
592 sort( $namespaces );
593 $count = count( $namespaces );
594 $info[] = $context->msg( 'api-help-param-list' )
595 ->params( $multi ? 2 : 1 )
596 ->params( $context->getLanguage()->commaList( $namespaces ) )
597 ->parse();
598 $hintPipeSeparated = false;
599 // No type message necessary, we have a list of values.
600 $type = null;
601 break;
602
603 case 'tags':
605 $count = count( $tags );
606 $info[] = $context->msg( 'api-help-param-list' )
607 ->params( $multi ? 2 : 1 )
608 ->params( $context->getLanguage()->commaList( $tags ) )
609 ->parse();
610 $hintPipeSeparated = false;
611 $type = null;
612 break;
613
614 case 'limit':
615 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
616 $info[] = $context->msg( 'api-help-param-limit2' )
617 ->numParams( $settings[ApiBase::PARAM_MAX] )
618 ->numParams( $settings[ApiBase::PARAM_MAX2] )
619 ->parse();
620 } else {
621 $info[] = $context->msg( 'api-help-param-limit' )
622 ->numParams( $settings[ApiBase::PARAM_MAX] )
623 ->parse();
624 }
625 break;
626
627 case 'integer':
628 // Possible messages:
629 // api-help-param-integer-min,
630 // api-help-param-integer-max,
631 // api-help-param-integer-minmax
632 $suffix = '';
633 $min = $max = 0;
634 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
635 $suffix .= 'min';
636 $min = $settings[ApiBase::PARAM_MIN];
637 }
638 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
639 $suffix .= 'max';
640 $max = $settings[ApiBase::PARAM_MAX];
641 }
642 if ( $suffix !== '' ) {
643 $info[] =
644 $context->msg( "api-help-param-integer-$suffix" )
645 ->params( $multi ? 2 : 1 )
646 ->numParams( $min, $max )
647 ->parse();
648 }
649 break;
650
651 case 'upload':
652 $info[] = $context->msg( 'api-help-param-upload' )
653 ->parse();
654 // No type message necessary, api-help-param-upload should handle it.
655 $type = null;
656 break;
657
658 case 'string':
659 case 'text':
660 // Displaying a type message here would be useless.
661 $type = null;
662 break;
663 }
664 }
665
666 // Add type. Messages for grep: api-help-param-type-limit
667 // api-help-param-type-integer api-help-param-type-boolean
668 // api-help-param-type-timestamp api-help-param-type-user
669 // api-help-param-type-password
670 if ( is_string( $type ) ) {
671 $msg = $context->msg( "api-help-param-type-$type" );
672 if ( !$msg->isDisabled() ) {
673 $info[] = $msg->params( $multi ? 2 : 1 )->parse();
674 }
675 }
676
677 if ( $multi ) {
678 $extra = [];
679 $lowcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT1] )
682 $highcount = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
685
686 if ( $hintPipeSeparated ) {
687 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
688 }
689 if ( $count > $lowcount ) {
690 if ( $lowcount === $highcount ) {
691 $msg = $context->msg( 'api-help-param-multi-max-simple' )
692 ->numParams( $lowcount );
693 } else {
694 $msg = $context->msg( 'api-help-param-multi-max' )
695 ->numParams( $lowcount, $highcount );
696 }
697 $extra[] = $msg->parse();
698 }
699 if ( $extra ) {
700 $info[] = implode( ' ', $extra );
701 }
702
703 $allowAll = $settings[ApiBase::PARAM_ALL] ?? false;
704 if ( $allowAll || $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
705 if ( $settings[ApiBase::PARAM_TYPE] === 'namespace' ) {
706 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
707 } else {
708 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
709 }
710 $info[] = $context->msg( 'api-help-param-multi-all' )
711 ->params( $allSpecifier )
712 ->parse();
713 }
714 }
715 }
716
717 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
718 $info[] = $context->msg( 'api-help-param-maxbytes' )
719 ->numParams( $settings[self::PARAM_MAX_BYTES] );
720 }
721 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
722 $info[] = $context->msg( 'api-help-param-maxchars' )
723 ->numParams( $settings[self::PARAM_MAX_CHARS] );
724 }
725
726 // Add default
727 $default = $settings[ApiBase::PARAM_DFLT] ?? null;
728 if ( $default === '' ) {
729 $info[] = $context->msg( 'api-help-param-default-empty' )
730 ->parse();
731 } elseif ( $default !== null && $default !== false ) {
732 // We can't know whether this contains LTR or RTL text.
733 $info[] = $context->msg( 'api-help-param-default' )
734 ->params( Html::element( 'span', [ 'dir' => 'auto' ], $default ) )
735 ->parse();
736 }
737
738 if ( !array_filter( $description ) ) {
739 $description = [ self::wrap(
740 $context->msg( 'api-help-param-no-description' ),
741 'apihelp-empty'
742 ) ];
743 }
744
745 // Add "deprecated" flag
746 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
747 $help['parameters'] .= Html::openElement( 'dd',
748 [ 'class' => 'info' ] );
749 $help['parameters'] .= self::wrap(
750 $context->msg( 'api-help-param-deprecated' ),
751 'apihelp-deprecated', 'strong'
752 );
753 $help['parameters'] .= Html::closeElement( 'dd' );
754 }
755
756 if ( $description ) {
757 $description = implode( '', $description );
758 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
759 $help['parameters'] .= Html::rawElement( 'dd',
760 [ 'class' => 'description' ], $description );
761 }
762
763 foreach ( $info as $i ) {
764 $help['parameters'] .= Html::rawElement( 'dd', [ 'class' => 'info' ], $i );
765 }
766 }
767
768 if ( $dynamicParams !== null ) {
769 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $context, [
770 $module->getModulePrefix(),
771 $module->getModuleName(),
772 $module->getModulePath()
773 ] );
774 $help['parameters'] .= Html::element( 'dt', null, '*' );
775 $help['parameters'] .= Html::rawElement( 'dd',
776 [ 'class' => 'description' ], $dynamicParams->parse() );
777 }
778
779 $help['parameters'] .= Html::closeElement( 'dl' );
780 $help['parameters'] .= Html::closeElement( 'div' );
781 }
782
783 $examples = $module->getExamplesMessages();
784 if ( $examples ) {
785 $help['examples'] .= Html::openElement( 'div',
786 [ 'class' => 'apihelp-block apihelp-examples' ] );
787 $msg = $context->msg( 'api-help-examples' );
788 if ( !$msg->isDisabled() ) {
789 $help['examples'] .= self::wrap(
790 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
791 );
792 }
793
794 $help['examples'] .= Html::openElement( 'dl' );
795 foreach ( $examples as $qs => $msg ) {
796 $msg = ApiBase::makeMessage( $msg, $context, [
797 $module->getModulePrefix(),
798 $module->getModuleName(),
799 $module->getModulePath()
800 ] );
801
802 $link = wfAppendQuery( wfScript( 'api' ), $qs );
803 $sandbox = SpecialPage::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
804 $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
805 $help['examples'] .= Html::rawElement( 'dd', null,
806 Html::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
807 Html::rawElement( 'a', [ 'href' => $sandbox ],
808 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
809 );
810 }
811 $help['examples'] .= Html::closeElement( 'dl' );
812 $help['examples'] .= Html::closeElement( 'div' );
813 }
814
815 $subtocnumber = $tocnumber;
816 $subtocnumber[$level + 1] = 0;
817 $suboptions = [
818 'submodules' => $options['recursivesubmodules'],
819 'headerlevel' => $level + 1,
820 'tocnumber' => &$subtocnumber,
821 'noheader' => false,
822 ] + $options;
823
824 if ( $options['submodules'] && $module->getModuleManager() ) {
825 $manager = $module->getModuleManager();
826 $submodules = [];
827 foreach ( $groups as $group ) {
828 $names = $manager->getNames( $group );
829 sort( $names );
830 foreach ( $names as $name ) {
831 $submodules[] = $manager->getModule( $name );
832 }
833 }
834 $help['submodules'] .= self::getHelpInternal(
835 $context,
836 $submodules,
837 $suboptions,
838 $haveModules
839 );
840 }
841
842 $module->modifyHelp( $help, $suboptions, $haveModules );
843
844 Hooks::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
845
846 $out .= implode( "\n", $help );
847 }
848
849 return $out;
850 }
851
852 public function shouldCheckMaxlag() {
853 return false;
854 }
855
856 public function isReadMode() {
857 return false;
858 }
859
860 public function getCustomPrinter() {
861 $params = $this->extractRequestParams();
862 if ( $params['wrap'] ) {
863 return null;
864 }
865
866 $main = $this->getMain();
867 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
868 return new ApiFormatRaw( $main, $errorPrinter );
869 }
870
871 public function getAllowedParams() {
872 return [
873 'modules' => [
874 ApiBase::PARAM_DFLT => 'main',
876 ],
877 'submodules' => false,
878 'recursivesubmodules' => false,
879 'wrap' => false,
880 'toc' => false,
881 ];
882 }
883
884 protected function getExamplesMessages() {
885 return [
886 'action=help'
887 => 'apihelp-help-example-main',
888 'action=help&modules=query&submodules=1'
889 => 'apihelp-help-example-submodules',
890 'action=help&recursivesubmodules=1'
891 => 'apihelp-help-example-recursive',
892 'action=help&modules=help'
893 => 'apihelp-help-example-help',
894 'action=help&modules=query+info|query+categorymembers'
895 => 'apihelp-help-example-query',
896 ];
897 }
898
899 public function getHelpUrls() {
900 return [
901 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
902 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
903 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',
904 ];
905 }
906}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:37
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:111
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:96
const PARAM_SUBMODULE_MAP
(string[]) When PARAM_TYPE is 'submodule', map parameter values to submodule paths.
Definition ApiBase.php:165
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition ApiBase.php:105
getModuleFromPath( $path)
Get a module from its module path.
Definition ApiBase.php:594
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition ApiBase.php:1776
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:90
const PARAM_DEPRECATED_VALUES
(array) When PARAM_TYPE is an array, this indicates which of the values are deprecated.
Definition ApiBase.php:202
const PARAM_ISMULTI_LIMIT1
(integer) Maximum number of values, for normal users.
Definition ApiBase.php:208
getMain()
Get the main module.
Definition ApiBase.php:528
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_HELP_MSG_INFO
(array) Specify additional information tags for the parameter.
Definition ApiBase.php:141
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
const PARAM_VALUE_LINKS
(string[]) When PARAM_TYPE is an array, this may be an array mapping those values to page titles whic...
Definition ApiBase.php:148
const PARAM_ISMULTI_LIMIT2
(integer) Maximum number of values, for users with the apihighimits right.
Definition ApiBase.php:215
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:99
const LIMIT_SML2
Slow query, apihighlimits limit.
Definition ApiBase.php:258
const PARAM_TEMPLATE_VARS
(array) Indicate that this is a templated parameter, and specify replacements.
Definition ApiBase.php:245
getResult()
Get the result object.
Definition ApiBase.php:632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:743
getModulePath()
Get the path to this module.
Definition ApiBase.php:576
const PARAM_EXTRA_NAMESPACES
(int[]) When PARAM_TYPE is 'namespace', include these as additional possible values.
Definition ApiBase.php:186
const LIMIT_SML1
Slow query, standard limit.
Definition ApiBase.php:256
const PARAM_ALL
(boolean|string) When PARAM_TYPE has a defined set of values and PARAM_ISMULTI is true,...
Definition ApiBase.php:180
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition ApiBase.php:265
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:512
const ALL_DEFAULT_STRING
Definition ApiBase.php:249
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:51
Formatter that spits out anything you like with any desired MIME type.
Class to output help for an API module.
Definition ApiHelp.php:32
static wrap(Message $msg, $class, $tag='span')
Wrap a message in HTML with a class.
Definition ApiHelp.php:210
isReadMode()
Indicates whether this module requires read rights.
Definition ApiHelp.php:856
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiHelp.php:33
getExamplesMessages()
Returns usage examples for this module.
Definition ApiHelp.php:884
static fixHelpLinks( $html, $helptitle=null, $localModules=[])
Replace Special:ApiHelp links with links to api.php.
Definition ApiHelp.php:171
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition ApiHelp.php:871
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiHelp.php:899
static getHelpInternal(IContextSource $context, array $modules, array $options, &$haveModules)
Recursively-called function to actually construct the help.
Definition ApiHelp.php:225
static getHelp(IContextSource $context, $modules, array $options)
Generate help for the specified modules.
Definition ApiHelp.php:94
shouldCheckMaxlag()
Indicates if this module needs maxlag to be checked.
Definition ApiHelp.php:852
getCustomPrinter()
If the module may only be used with a certain format module, it should override this method to return...
Definition ApiHelp.php:860
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:41
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
Definition ApiResult.php:58
static setSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as subelements rather than attributes.
Exception used to abort API execution with an error.
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
IContextSource $context
getContext()
Get the base IContextSource object.
An IContextSource implementation which will inherit context from another source but allow individual ...
static generateTOC( $tree, $lang=null)
Generate a table of contents from a section tree.
Definition Linker.php:1655
MediaWikiServices is the service locator for the application scope of MediaWiki.
The Message class provides methods which fulfil two basic services:
Definition Message.php:160
parse()
Fully parse the text from wikitext to HTML.
Definition Message.php:941
This class should be covered by a general architecture document which does not exist as of January 20...
static getExtLicenseFileName( $extDir)
Obtains the full path of an extensions copying or license file if one exists.
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition hooks.txt:2228
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:925
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2290
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3069
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
msg( $key)
This is the method for getting translated interface messages.
$cache
Definition mcc.php:33
$help
Definition mcc.php:32
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params