Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiSetPageLanguage | |
0.00% |
0 / 71 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getExtendedDescription | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
execute | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
56 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright © 2017 Justin Du "<justin.d128@gmail.com>" |
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 | */ |
22 | |
23 | use MediaWiki\Languages\LanguageNameUtils; |
24 | use MediaWiki\MainConfigNames; |
25 | use MediaWiki\Specials\SpecialPageLanguage; |
26 | use MediaWiki\Title\Title; |
27 | use Wikimedia\ParamValidator\ParamValidator; |
28 | use Wikimedia\Rdbms\IConnectionProvider; |
29 | |
30 | /** |
31 | * API module that facilitates changing the language of a page. |
32 | * The API equivalent of SpecialPageLanguage. |
33 | * Requires API write mode to be enabled. |
34 | * |
35 | * @ingroup API |
36 | */ |
37 | class ApiSetPageLanguage extends ApiBase { |
38 | |
39 | private IConnectionProvider $dbProvider; |
40 | private LanguageNameUtils $languageNameUtils; |
41 | |
42 | /** |
43 | * @param ApiMain $mainModule |
44 | * @param string $moduleName |
45 | * @param IConnectionProvider $dbProvider |
46 | * @param LanguageNameUtils $languageNameUtils |
47 | */ |
48 | public function __construct( |
49 | ApiMain $mainModule, |
50 | $moduleName, |
51 | IConnectionProvider $dbProvider, |
52 | LanguageNameUtils $languageNameUtils |
53 | ) { |
54 | parent::__construct( $mainModule, $moduleName ); |
55 | $this->dbProvider = $dbProvider; |
56 | $this->languageNameUtils = $languageNameUtils; |
57 | } |
58 | |
59 | // Check if change language feature is enabled |
60 | protected function getExtendedDescription() { |
61 | if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) { |
62 | return 'apihelp-setpagelanguage-extended-description-disabled'; |
63 | } |
64 | return parent::getExtendedDescription(); |
65 | } |
66 | |
67 | /** |
68 | * Extracts the title and language from the request parameters and invokes |
69 | * the static SpecialPageLanguage::changePageLanguage() function with these as arguments. |
70 | * If the language change succeeds, the title, old language, and new language |
71 | * of the article changed, as well as the performer of the language change |
72 | * are added to the result object. |
73 | */ |
74 | public function execute() { |
75 | // Check if change language feature is enabled |
76 | if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) { |
77 | $this->dieWithError( 'apierror-pagelang-disabled' ); |
78 | } |
79 | |
80 | // Check if the user has permissions |
81 | $this->checkUserRightsAny( 'pagelang' ); |
82 | |
83 | $this->useTransactionalTimeLimit(); |
84 | |
85 | $params = $this->extractRequestParams(); |
86 | |
87 | $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); |
88 | $titleObj = $pageObj->getTitle(); |
89 | $this->getErrorFormatter()->setContextTitle( $titleObj ); |
90 | if ( !$pageObj->exists() ) { |
91 | $this->dieWithError( 'apierror-missingtitle' ); |
92 | } |
93 | |
94 | // Check that the user is allowed to edit the page |
95 | $this->checkTitleUserPermissions( $titleObj, 'edit' ); |
96 | |
97 | // If change tagging was requested, check that the user is allowed to tag, |
98 | // and the tags are valid |
99 | if ( $params['tags'] ) { |
100 | $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() ); |
101 | if ( !$tagStatus->isOK() ) { |
102 | $this->dieStatus( $tagStatus ); |
103 | } |
104 | } |
105 | |
106 | $status = SpecialPageLanguage::changePageLanguage( |
107 | $this, |
108 | $titleObj, |
109 | $params['lang'], |
110 | $params['reason'] ?? '', |
111 | $params['tags'] ?: [], |
112 | $this->dbProvider->getPrimaryDatabase() |
113 | ); |
114 | |
115 | if ( !$status->isOK() ) { |
116 | $this->dieStatus( $status ); |
117 | } |
118 | |
119 | $r = [ |
120 | 'title' => $titleObj->getPrefixedText(), |
121 | 'oldlanguage' => $status->value->oldLanguage, |
122 | 'newlanguage' => $status->value->newLanguage, |
123 | 'logid' => $status->value->logId |
124 | ]; |
125 | $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
126 | } |
127 | |
128 | public function mustBePosted() { |
129 | return true; |
130 | } |
131 | |
132 | public function isWriteMode() { |
133 | return true; |
134 | } |
135 | |
136 | public function getAllowedParams() { |
137 | return [ |
138 | 'title' => null, |
139 | 'pageid' => [ |
140 | ParamValidator::PARAM_TYPE => 'integer' |
141 | ], |
142 | 'lang' => [ |
143 | ParamValidator::PARAM_TYPE => array_merge( |
144 | [ 'default' ], |
145 | array_keys( $this->languageNameUtils->getLanguageNames( |
146 | LanguageNameUtils::AUTONYMS, |
147 | LanguageNameUtils::SUPPORTED |
148 | ) ) |
149 | ), |
150 | ParamValidator::PARAM_REQUIRED => true, |
151 | ], |
152 | 'reason' => null, |
153 | 'tags' => [ |
154 | ParamValidator::PARAM_TYPE => 'tags', |
155 | ParamValidator::PARAM_ISMULTI => true, |
156 | ], |
157 | ]; |
158 | } |
159 | |
160 | public function needsToken() { |
161 | return 'csrf'; |
162 | } |
163 | |
164 | protected function getExamplesMessages() { |
165 | $title = Title::newMainPage()->getPrefixedText(); |
166 | $mp = rawurlencode( $title ); |
167 | |
168 | return [ |
169 | "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC" |
170 | => 'apihelp-setpagelanguage-example-language', |
171 | 'action=setpagelanguage&pageid=123&lang=default&token=123ABC' |
172 | => 'apihelp-setpagelanguage-example-default', |
173 | ]; |
174 | } |
175 | |
176 | public function getHelpUrls() { |
177 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage'; |
178 | } |
179 | } |