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