Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialApiHelp
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
56
 isIncludable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Implements Special:ApiHelp
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24namespace MediaWiki\Specials;
25
26use ApiHelp;
27use ApiMain;
28use ApiUsageException;
29use MediaWiki\Html\Html;
30use MediaWiki\SpecialPage\UnlistedSpecialPage;
31use MediaWiki\Utils\UrlUtils;
32
33/**
34 * Special page to redirect to API help pages, for situations where linking to
35 * the api.php endpoint is not wanted.
36 *
37 * @ingroup SpecialPage
38 */
39class SpecialApiHelp extends UnlistedSpecialPage {
40
41    private UrlUtils $urlUtils;
42
43    /**
44     * @param UrlUtils $urlUtils
45     */
46    public function __construct(
47        UrlUtils $urlUtils
48    ) {
49        parent::__construct( 'ApiHelp' );
50        $this->urlUtils = $urlUtils;
51    }
52
53    public function execute( $par ) {
54        if ( !$par ) {
55            $par = 'main';
56        }
57
58        // These come from transclusions
59        $request = $this->getRequest();
60        $options = [
61            'action' => 'help',
62            'nolead' => true,
63            'submodules' => $request->getCheck( 'submodules' ),
64            'recursivesubmodules' => $request->getCheck( 'recursivesubmodules' ),
65            'title' => $request->getVal( 'title', $this->getPageTitle( '$1' )->getPrefixedText() ),
66        ];
67
68        // These are for linking from wikitext, since url parameters are a pain
69        // to do.
70        while ( true ) {
71            if ( str_starts_with( $par, 'sub/' ) ) {
72                $par = substr( $par, 4 );
73                $options['submodules'] = 1;
74                continue;
75            }
76
77            if ( str_starts_with( $par, 'rsub/' ) ) {
78                $par = substr( $par, 5 );
79                $options['recursivesubmodules'] = 1;
80                continue;
81            }
82
83            $moduleName = $par;
84            break;
85        }
86
87        if ( !$this->including() ) {
88            unset( $options['nolead'], $options['title'] );
89            // @phan-suppress-next-line PhanPossiblyUndeclaredVariable False positive
90            $options['modules'] = $moduleName;
91            $link = wfAppendQuery( (string)$this->urlUtils->expand( wfScript( 'api' ), PROTO_CURRENT ), $options );
92            $this->getOutput()->redirect( $link );
93            return;
94        }
95
96        $main = new ApiMain( $this->getContext(), false );
97        try {
98            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable False positive
99            $module = $main->getModuleFromPath( $moduleName );
100        } catch ( ApiUsageException $ex ) {
101            $this->getOutput()->addHTML( Html::rawElement( 'span', [ 'class' => 'error' ],
102                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable False positive
103                $this->msg( 'apihelp-no-such-module', $moduleName )->inContentLanguage()->parse()
104            ) );
105            return;
106        }
107
108        ApiHelp::getHelp( $this->getContext(), $module, $options );
109    }
110
111    public function isIncludable() {
112        return true;
113    }
114}
115
116/** @deprecated class alias since 1.41 */
117class_alias( SpecialApiHelp::class, 'SpecialApiHelp' );