Merge "Mocha tests: Support language links to en-x-piglatin"
[mediawiki.git] / includes / specials / SpecialApiHelp.php
blob0f5924e7b64df31386486672cb8073242e927c1a
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use LogicException;
24 use MediaWiki\Api\ApiHelp;
25 use MediaWiki\Api\ApiMain;
26 use MediaWiki\Api\ApiUsageException;
27 use MediaWiki\Html\Html;
28 use MediaWiki\SpecialPage\UnlistedSpecialPage;
29 use MediaWiki\Utils\UrlUtils;
31 /**
32 * Redirect to help pages served by api.php.
34 * For situations where linking to full api.php URLs is not wanted
35 * or not possible, e.g. in edit summaries.
37 * @ingroup SpecialPage
39 class SpecialApiHelp extends UnlistedSpecialPage {
41 private UrlUtils $urlUtils;
43 public function __construct(
44 UrlUtils $urlUtils
45 ) {
46 parent::__construct( 'ApiHelp' );
47 $this->urlUtils = $urlUtils;
50 public function execute( $par ) {
51 $this->getOutput()->addModuleStyles( 'mediawiki.codex.messagebox.styles' );
52 if ( !$par ) {
53 $par = 'main';
56 // These come from transclusions
57 $request = $this->getRequest();
58 $options = [
59 'action' => 'help',
60 'nolead' => true,
61 'submodules' => $request->getCheck( 'submodules' ),
62 'recursivesubmodules' => $request->getCheck( 'recursivesubmodules' ),
63 'title' => $request->getVal( 'title', $this->getPageTitle( '$1' )->getPrefixedText() ),
66 // These are for linking from wikitext, since url parameters are a pain
67 // to do.
68 while ( true ) {
69 if ( str_starts_with( $par, 'sub/' ) ) {
70 $par = substr( $par, 4 );
71 $options['submodules'] = 1;
72 continue;
75 if ( str_starts_with( $par, 'rsub/' ) ) {
76 $par = substr( $par, 5 );
77 $options['recursivesubmodules'] = 1;
78 continue;
81 $moduleName = $par;
82 break;
84 if ( !isset( $moduleName ) ) {
85 throw new LogicException( 'Module name should have been found' );
88 if ( !$this->including() ) {
89 unset( $options['nolead'], $options['title'] );
90 $options['modules'] = $moduleName;
91 $link = wfAppendQuery( (string)$this->urlUtils->expand( wfScript( 'api' ), PROTO_CURRENT ), $options );
92 $this->getOutput()->redirect( $link );
93 return;
96 $main = new ApiMain( $this->getContext(), false );
97 try {
98 $module = $main->getModuleFromPath( $moduleName );
99 } catch ( ApiUsageException $ex ) {
100 $this->getOutput()->addHTML( Html::errorBox(
101 $this->msg( 'apihelp-no-such-module', $moduleName )->inContentLanguage()->parse()
102 ) );
103 return;
106 ApiHelp::getHelp( $this->getContext(), $module, $options );
109 public function isIncludable() {
110 return true;
114 /** @deprecated class alias since 1.41 */
115 class_alias( SpecialApiHelp::class, 'SpecialApiHelp' );