Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / specials / SpecialApiHelp.php
blob54480132e4cafbe68830566867afabf8dcadc86e
1 <?php
2 /**
3 * Implements Special:ApiHelp
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page to redirect to API help pages, for situations where linking to
26 * the api.php endpoint is not wanted.
28 * @ingroup SpecialPage
30 class SpecialApiHelp extends UnlistedSpecialPage {
31 public function __construct() {
32 parent::__construct( 'ApiHelp' );
35 public function execute( $par ) {
36 if ( empty( $par ) ) {
37 $par = 'main';
40 // These come from transclusions
41 $request = $this->getRequest();
42 $options = [
43 'action' => 'help',
44 'nolead' => true,
45 'submodules' => $request->getCheck( 'submodules' ),
46 'recursivesubmodules' => $request->getCheck( 'recursivesubmodules' ),
47 'title' => $request->getVal( 'title', $this->getPageTitle( '$1' )->getPrefixedText() ),
50 // These are for linking from wikitext, since url parameters are a pain
51 // to do.
52 while ( true ) {
53 if ( substr( $par, 0, 4 ) === 'sub/' ) {
54 $par = substr( $par, 4 );
55 $options['submodules'] = 1;
56 continue;
59 if ( substr( $par, 0, 5 ) === 'rsub/' ) {
60 $par = substr( $par, 5 );
61 $options['recursivesubmodules'] = 1;
62 continue;
65 $moduleName = $par;
66 break;
69 if ( !$this->including() ) {
70 unset( $options['nolead'], $options['title'] );
71 $options['modules'] = $moduleName;
72 $link = wfAppendQuery( wfExpandUrl( wfScript( 'api' ), PROTO_CURRENT ), $options );
73 $this->getOutput()->redirect( $link );
74 return;
77 $main = new ApiMain( $this->getContext(), false );
78 try {
79 $module = $main->getModuleFromPath( $moduleName );
80 } catch ( ApiUsageException $ex ) {
81 $this->getOutput()->addHTML( Html::rawElement( 'span', [ 'class' => 'error' ],
82 $this->msg( 'apihelp-no-such-module', $moduleName )->inContentLanguage()->parse()
83 ) );
84 return;
85 } catch ( UsageException $ex ) {
86 $this->getOutput()->addHTML( Html::rawElement( 'span', [ 'class' => 'error' ],
87 $this->msg( 'apihelp-no-such-module', $moduleName )->inContentLanguage()->parse()
88 ) );
89 return;
92 ApiHelp::getHelp( $this->getContext(), $module, $options );
95 public function isIncludable() {
96 return true;