Merge "Add API action to set the language of a page"
[lhc/web/wiklou.git] / includes / api / ApiSetPageLanguage.php
1 <?php
2 /**
3 *
4 *
5 * Created on January 1, 2017
6 *
7 * Copyright © 2017 Justin Du "<justin.d128@gmail.com>"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
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.
31 *
32 * @ingroup API
33 */
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';
39 }
40 return parent::getDescriptionMessage();
41 }
42
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.
49 */
50 public function execute() {
51 // Check if change language feature is enabled
52 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
53 $this->dieWithError( 'apierror-pagelang-disabled' );
54 }
55
56 // Check if the user has permissions
57 $this->checkUserRightsAny( 'pagelang' );
58
59 $this->useTransactionalTimeLimit();
60
61 $params = $this->extractRequestParams();
62
63 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
64 if ( !$pageObj->exists() ) {
65 $this->dieWithError( 'apierror-missingtitle' );
66 }
67
68 $titleObj = $pageObj->getTitle();
69 $user = $this->getUser();
70
71 // Check that the user is allowed to edit the page
72 $this->checkTitleUserPermissions( $titleObj, 'edit' );
73
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 );
80 }
81 }
82
83 $status = SpecialPageLanguage::changePageLanguage(
84 $this,
85 $titleObj,
86 $params['lang'],
87 $params['tags'] ?: []
88 );
89
90 if ( !$status->isOK() ) {
91 $this->dieStatus( $status );
92 }
93
94 $r = [
95 'title' => $titleObj->getPrefixedText(),
96 'oldlanguage' => $status->value->oldLanguage,
97 'newlanguage' => $status->value->newLanguage,
98 'logid' => $status->value->logId
99 ];
100 $this->getResult()->addValue( null, $this->getModuleName(), $r );
101 }
102
103 public function mustBePosted() {
104 return true;
105 }
106
107 public function isWriteMode() {
108 return true;
109 }
110
111 public function getAllowedParams() {
112 return [
113 'title' => null,
114 'pageid' => [
115 ApiBase::PARAM_TYPE => 'integer'
116 ],
117 'lang' => [
118 ApiBase::PARAM_TYPE => array_merge(
119 [ 'default' ],
120 array_keys( Language::fetchLanguageNames( null, 'mwfile' ) )
121 ),
122 ApiBase::PARAM_REQUIRED => true,
123 ],
124 'tags' => [
125 ApiBase::PARAM_TYPE => 'tags',
126 ApiBase::PARAM_ISMULTI => true,
127 ],
128 ];
129 }
130
131 public function needsToken() {
132 return 'csrf';
133 }
134
135 protected function getExamplesMessages() {
136 return [
137 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
138 => 'apihelp-setpagelanguage-example-language',
139 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
140 => 'apihelp-setpagelanguage-example-default',
141 ];
142 }
143
144 public function getHelpUrls() {
145 return 'https://www.mediawiki.org/wiki/API:SetPageLanguage';
146 }
147 }