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 23use MediaWiki\MediaWikiServices; 24 25/** 26 * API module that facilitates changing the language of a page. 27 * The API equivalent of SpecialPageLanguage. 28 * Requires API write mode to be enabled. 29 * 30 * @ingroup API 31 */ 32class ApiSetPageLanguage extends ApiBase { 33 // Check if change language feature is enabled 34 protected function getExtendedDescription() { 35 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) { 36 return 'apihelp-setpagelanguage-extended-description-disabled'; 37 } 38 return parent::getExtendedDescription(); 39 } 40 41 /** 42 * Extracts the title and language from the request parameters and invokes 43 * the static SpecialPageLanguage::changePageLanguage() function with these as arguments. 44 * If the language change succeeds, the title, old language, and new language 45 * of the article changed, as well as the performer of the language change 46 * are added to the result object. 47 */ 48 public function execute() { 49 // Check if change language feature is enabled 50 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) { 51 $this->dieWithError( 'apierror-pagelang-disabled' ); 52 } 53 54 // Check if the user has permissions 55 $this->checkUserRightsAny( 'pagelang' ); 56 57 $this->useTransactionalTimeLimit(); 58 59 $params = $this->extractRequestParams(); 60 61 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); 62 if ( !$pageObj->exists() ) { 63 $this->dieWithError( 'apierror-missingtitle' ); 64 } 65 66 $titleObj = $pageObj->getTitle(); 67 $user = $this->getUser(); 68 69 // Check that the user is allowed to edit the page 70 $this->checkTitleUserPermissions( $titleObj, 'edit' ); 71 72 // If change tagging was requested, check that the user is allowed to tag, 73 // and the tags are valid 74 if ( $params['tags'] ) { 75 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); 76 if ( !$tagStatus->isOK() ) { 77 $this->dieStatus( $tagStatus ); 78 } 79 } 80 81 $status = SpecialPageLanguage::changePageLanguage( 82 $this, 83 $titleObj, 84 $params['lang'], 85 $params['reason'] ?? '', 86 $params['tags'] ?: [] 87 ); 88 89 if ( !$status->isOK() ) { 90 $this->dieStatus( $status ); 91 } 92 93 $r = [ 94 'title' => $titleObj->getPrefixedText(), 95 'oldlanguage' => $status->value->oldLanguage, 96 'newlanguage' => $status->value->newLanguage, 97 'logid' => $status->value->logId 98 ]; 99 $this->getResult()->addValue( null, $this->getModuleName(), $r ); 100 } 101 102 public function mustBePosted() { 103 return true; 104 } 105 106 public function isWriteMode() { 107 return true; 108 } 109 110 public function getAllowedParams() { 111 return [ 112 'title' => null, 113 'pageid' => [ 114 ApiBase::PARAM_TYPE => 'integer' 115 ], 116 'lang' => [ 117 ApiBase::PARAM_TYPE => array_merge( 118 [ 'default' ], 119 array_keys( MediaWikiServices::getInstance() 120 ->getLanguageNameUtils() 121 ->getLanguageNames( null, 'mwfile' ) ) 122 ), 123 ApiBase::PARAM_REQUIRED => true, 124 ], 125 'reason' => null, 126 'tags' => [ 127 ApiBase::PARAM_TYPE => 'tags', 128 ApiBase::PARAM_ISMULTI => true, 129 ], 130 ]; 131 } 132 133 public function needsToken() { 134 return 'csrf'; 135 } 136 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', 143 ]; 144 } 145 146 public function getHelpUrls() { 147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage'; 148 } 149} 150