3 * Copyright © 2017 Justin Du "<justin.d128@gmail.com>"
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
23 namespace MediaWiki\Api
;
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
;
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.
40 class ApiSetPageLanguage
extends ApiBase
{
42 private IConnectionProvider
$dbProvider;
43 private LanguageNameUtils
$languageNameUtils;
45 public function __construct(
48 IConnectionProvider
$dbProvider,
49 LanguageNameUtils
$languageNameUtils
51 parent
::__construct( $mainModule, $moduleName );
52 $this->dbProvider
= $dbProvider;
53 $this->languageNameUtils
= $languageNameUtils;
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';
61 return parent
::getExtendedDescription();
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.
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' );
77 // Check if the user has permissions
78 $this->checkUserRightsAny( 'pagelang' );
80 $this->useTransactionalTimeLimit();
82 $params = $this->extractRequestParams();
84 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
85 $titleObj = $pageObj->getTitle();
86 $this->getErrorFormatter()->setContextTitle( $titleObj );
87 if ( !$pageObj->exists() ) {
88 $this->dieWithError( 'apierror-missingtitle' );
91 // Check that the user is allowed to edit the page
92 $this->checkTitleUserPermissions( $titleObj, 'edit' );
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 );
103 $status = SpecialPageLanguage
::changePageLanguage(
107 $params['reason'] ??
'',
108 $params['tags'] ?
: [],
109 $this->dbProvider
->getPrimaryDatabase()
112 if ( !$status->isOK() ) {
113 $this->dieStatus( $status );
117 'title' => $titleObj->getPrefixedText(),
118 'oldlanguage' => $status->value
->oldLanguage
,
119 'newlanguage' => $status->value
->newLanguage
,
120 'logid' => $status->value
->logId
122 $this->getResult()->addValue( null, $this->getModuleName(), $r );
125 public function mustBePosted() {
129 public function isWriteMode() {
133 public function getAllowedParams() {
137 ParamValidator
::PARAM_TYPE
=> 'integer'
140 ParamValidator
::PARAM_TYPE
=> array_merge(
142 array_keys( $this->languageNameUtils
->getLanguageNames(
143 LanguageNameUtils
::AUTONYMS
,
144 LanguageNameUtils
::SUPPORTED
147 ParamValidator
::PARAM_REQUIRED
=> true,
151 ParamValidator
::PARAM_TYPE
=> 'tags',
152 ParamValidator
::PARAM_ISMULTI
=> true,
157 public function needsToken() {
161 protected function getExamplesMessages() {
162 $title = Title
::newMainPage()->getPrefixedText();
163 $mp = rawurlencode( $title );
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',
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
178 /** @deprecated class alias since 1.43 */
179 class_alias( ApiSetPageLanguage
::class, 'ApiSetPageLanguage' );