Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiSetPageLanguage.php
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 /**
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.
27 *
28 * @ingroup API
29 */
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';
35 }
36 return parent::getExtendedDescription();
37 }
38
39 /**
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.
45 */
46 public function execute() {
47 // Check if change language feature is enabled
48 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
49 $this->dieWithError( 'apierror-pagelang-disabled' );
50 }
51
52 // Check if the user has permissions
53 $this->checkUserRightsAny( 'pagelang' );
54
55 $this->useTransactionalTimeLimit();
56
57 $params = $this->extractRequestParams();
58
59 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
60 if ( !$pageObj->exists() ) {
61 $this->dieWithError( 'apierror-missingtitle' );
62 }
63
64 $titleObj = $pageObj->getTitle();
65 $user = $this->getUser();
66
67 // Check that the user is allowed to edit the page
68 $this->checkTitleUserPermissions( $titleObj, 'edit' );
69
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 );
76 }
77 }
78
79 $status = SpecialPageLanguage::changePageLanguage(
80 $this,
81 $titleObj,
82 $params['lang'],
83 $params['reason'] === null ? '' : $params['reason'],
84 $params['tags'] ?: []
85 );
86
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
89 }
90
91 $r = [
92 'title' => $titleObj->getPrefixedText(),
93 'oldlanguage' => $status->value->oldLanguage,
94 'newlanguage' => $status->value->newLanguage,
95 'logid' => $status->value->logId
96 ];
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
98 }
99
100 public function mustBePosted() {
101 return true;
102 }
103
104 public function isWriteMode() {
105 return true;
106 }
107
108 public function getAllowedParams() {
109 return [
110 'title' => null,
111 'pageid' => [
112 ApiBase::PARAM_TYPE => 'integer'
113 ],
114 'lang' => [
115 ApiBase::PARAM_TYPE => array_merge(
116 [ 'default' ],
117 array_keys( Language::fetchLanguageNames( null, 'mwfile' ) )
118 ),
119 ApiBase::PARAM_REQUIRED => true,
120 ],
121 'reason' => null,
122 'tags' => [
123 ApiBase::PARAM_TYPE => 'tags',
124 ApiBase::PARAM_ISMULTI => true,
125 ],
126 ];
127 }
128
129 public function needsToken() {
130 return 'csrf';
131 }
132
133 protected function getExamplesMessages() {
134 return [
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',
139 ];
140 }
141
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
144 }
145 }