MediaWiki  master
SpecialApiHelp.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Specials;
25 
26 use ApiHelp;
27 use ApiMain;
32 
40 
42  private $urlUtils;
43 
47  public function __construct(
48  UrlUtils $urlUtils
49  ) {
50  parent::__construct( 'ApiHelp' );
51  $this->urlUtils = $urlUtils;
52  }
53 
54  public function execute( $par ) {
55  if ( empty( $par ) ) {
56  $par = 'main';
57  }
58 
59  // These come from transclusions
60  $request = $this->getRequest();
61  $options = [
62  'action' => 'help',
63  'nolead' => true,
64  'submodules' => $request->getCheck( 'submodules' ),
65  'recursivesubmodules' => $request->getCheck( 'recursivesubmodules' ),
66  'title' => $request->getVal( 'title', $this->getPageTitle( '$1' )->getPrefixedText() ),
67  ];
68 
69  // These are for linking from wikitext, since url parameters are a pain
70  // to do.
71  while ( true ) {
72  if ( str_starts_with( $par, 'sub/' ) ) {
73  $par = substr( $par, 4 );
74  $options['submodules'] = 1;
75  continue;
76  }
77 
78  if ( str_starts_with( $par, 'rsub/' ) ) {
79  $par = substr( $par, 5 );
80  $options['recursivesubmodules'] = 1;
81  continue;
82  }
83 
84  $moduleName = $par;
85  break;
86  }
87 
88  if ( !$this->including() ) {
89  unset( $options['nolead'], $options['title'] );
90  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable False positive
91  $options['modules'] = $moduleName;
92  $link = wfAppendQuery( (string)$this->urlUtils->expand( wfScript( 'api' ), PROTO_CURRENT ), $options );
93  $this->getOutput()->redirect( $link );
94  return;
95  }
96 
97  $main = new ApiMain( $this->getContext(), false );
98  try {
99  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable False positive
100  $module = $main->getModuleFromPath( $moduleName );
101  } catch ( ApiUsageException $ex ) {
102  $this->getOutput()->addHTML( Html::rawElement( 'span', [ 'class' => 'error' ],
103  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable False positive
104  $this->msg( 'apihelp-no-such-module', $moduleName )->inContentLanguage()->parse()
105  ) );
106  return;
107  }
108 
109  ApiHelp::getHelp( $this->getContext(), $module, $options );
110  }
111 
112  public function isIncludable() {
113  return true;
114  }
115 }
116 
120 class_alias( SpecialApiHelp::class, 'SpecialApiHelp' );
const PROTO_CURRENT
Definition: Defines.php:198
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...
Class to output help for an API module.
Definition: ApiHelp.php:40
static getHelp(IContextSource $context, $modules, array $options)
Generate help for the specified modules.
Definition: ApiHelp.php:137
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:58
Exception used to abort API execution with an error.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:219
Special page to redirect to API help pages, for situations where linking to the api....
execute( $par)
Default execute method Checks user permissions.
isIncludable()
Whether it's allowed to transclude the special page via {{Special:Foo/params}}.
A service to expand, parse, and otherwise manipulate URLs.
Definition: UrlUtils.php:17
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
including( $x=null)
Whether the special page is being evaluated via transclusion.
Shortcut to construct a special page which is unlisted by default.