40826ae34107b4adf99f548db09b37fbf68994fd
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
24 * API module that facilitates changing the language of a page.
25 * The API equivalent of SpecialPageLanguage.
26 * Requires API write mode to be enabled.
30 class ApiSetPageLanguage
extends ApiBase
{
31 // Check if change language feature is enabled
32 protected function getExtendedDescription() {
33 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
34 return 'apihelp-setpagelanguage-extended-description-disabled';
36 return parent
::getExtendedDescription();
40 * Extracts the title and language from the request parameters and invokes
41 * the static SpecialPageLanguage::changePageLanguage() function with these as arguments.
42 * If the language change succeeds, the title, old language, and new language
43 * of the article changed, as well as the performer of the language change
44 * are added to the result object.
46 public function execute() {
47 // Check if change language feature is enabled
48 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
49 $this->dieWithError( 'apierror-pagelang-disabled' );
52 // Check if the user has permissions
53 $this->checkUserRightsAny( 'pagelang' );
55 $this->useTransactionalTimeLimit();
57 $params = $this->extractRequestParams();
59 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
60 if ( !$pageObj->exists() ) {
61 $this->dieWithError( 'apierror-missingtitle' );
64 $titleObj = $pageObj->getTitle();
65 $user = $this->getUser();
67 // Check that the user is allowed to edit the page
68 $this->checkTitleUserPermissions( $titleObj, 'edit' );
70 // If change tagging was requested, check that the user is allowed to tag,
71 // and the tags are valid
72 if ( $params['tags'] ) {
73 $tagStatus = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
74 if ( !$tagStatus->isOK() ) {
75 $this->dieStatus( $tagStatus );
79 $status = SpecialPageLanguage
::changePageLanguage(
83 $params['reason'] === null ?
'' : $params['reason'],
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
92 'title' => $titleObj->getPrefixedText(),
93 'oldlanguage' => $status->value
->oldLanguage
,
94 'newlanguage' => $status->value
->newLanguage
,
95 'logid' => $status->value
->logId
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
100 public function mustBePosted() {
104 public function isWriteMode() {
108 public function getAllowedParams() {
112 ApiBase
::PARAM_TYPE
=> 'integer'
115 ApiBase
::PARAM_TYPE
=> array_merge(
117 array_keys( Language
::fetchLanguageNames( null, 'mwfile' ) )
119 ApiBase
::PARAM_REQUIRED
=> true,
123 ApiBase
::PARAM_TYPE
=> 'tags',
124 ApiBase
::PARAM_ISMULTI
=> true,
129 public function needsToken() {
133 protected function getExamplesMessages() {
135 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
136 => 'apihelp-setpagelanguage-example-language',
137 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
138 => 'apihelp-setpagelanguage-example-default',
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';