Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiSetPageLanguage.php
blob3ff99f11c0c8448b07bd073c2157b18f92204a77
1 <?php
2 /**
5 * Created on January 1, 2017
7 * Copyright © 2017 Justin Du "<justin.d128@gmail.com>"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API module that facilitates changing the language of a page.
29 * The API equivalent of SpecialPageLanguage.
30 * Requires API write mode to be enabled.
32 * @ingroup API
34 class ApiSetPageLanguage extends ApiBase {
35 // Check if change language feature is enabled
36 protected function getDescriptionMessage() {
37 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
38 return 'apihelp-setpagelanguage-description-disabled';
40 return parent::getDescriptionMessage();
43 /**
44 * Extracts the title and language from the request parameters and invokes
45 * the static SpecialPageLanguage::changePageLanguage() function with these as arguments.
46 * If the language change succeeds, the title, old language, and new language
47 * of the article changed, as well as the performer of the language change
48 * are added to the result object.
50 public function execute() {
51 // Check if change language feature is enabled
52 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
53 $this->dieWithError( 'apierror-pagelang-disabled' );
56 // Check if the user has permissions
57 $this->checkUserRightsAny( 'pagelang' );
59 $this->useTransactionalTimeLimit();
61 $params = $this->extractRequestParams();
63 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
64 if ( !$pageObj->exists() ) {
65 $this->dieWithError( 'apierror-missingtitle' );
68 $titleObj = $pageObj->getTitle();
69 $user = $this->getUser();
71 // Check that the user is allowed to edit the page
72 $this->checkTitleUserPermissions( $titleObj, 'edit' );
74 // If change tagging was requested, check that the user is allowed to tag,
75 // and the tags are valid
76 if ( count( $params['tags'] ) ) {
77 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
78 if ( !$tagStatus->isOK() ) {
79 $this->dieStatus( $tagStatus );
83 $status = SpecialPageLanguage::changePageLanguage(
84 $this,
85 $titleObj,
86 $params['lang'],
87 $params['reason'] === null ? '' : $params['reason'],
88 $params['tags'] ?: []
91 if ( !$status->isOK() ) {
92 $this->dieStatus( $status );
95 $r = [
96 'title' => $titleObj->getPrefixedText(),
97 'oldlanguage' => $status->value->oldLanguage,
98 'newlanguage' => $status->value->newLanguage,
99 'logid' => $status->value->logId
101 $this->getResult()->addValue( null, $this->getModuleName(), $r );
104 public function mustBePosted() {
105 return true;
108 public function isWriteMode() {
109 return true;
112 public function getAllowedParams() {
113 return [
114 'title' => null,
115 'pageid' => [
116 ApiBase::PARAM_TYPE => 'integer'
118 'lang' => [
119 ApiBase::PARAM_TYPE => array_merge(
120 [ 'default' ],
121 array_keys( Language::fetchLanguageNames( null, 'mwfile' ) )
123 ApiBase::PARAM_REQUIRED => true,
125 'reason' => null,
126 'tags' => [
127 ApiBase::PARAM_TYPE => 'tags',
128 ApiBase::PARAM_ISMULTI => true,
133 public function needsToken() {
134 return 'csrf';
137 protected function getExamplesMessages() {
138 return [
139 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
140 => 'apihelp-setpagelanguage-example-language',
141 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
142 => 'apihelp-setpagelanguage-example-default',
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/API:SetPageLanguage';