Merge "Handle missing namespace prefix in XML dumps more gracefully"
[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['reason'] === null ? '' : $params['reason'],
88 $params['tags'] ?: []
89 );
90
91 if ( !$status->isOK() ) {
92 $this->dieStatus( $status );
93 }
94
95 $r = [
96 'title' => $titleObj->getPrefixedText(),
97 'oldlanguage' => $status->value->oldLanguage,
98 'newlanguage' => $status->value->newLanguage,
99 'logid' => $status->value->logId
100 ];
101 $this->getResult()->addValue( null, $this->getModuleName(), $r );
102 }
103
104 public function mustBePosted() {
105 return true;
106 }
107
108 public function isWriteMode() {
109 return true;
110 }
111
112 public function getAllowedParams() {
113 return [
114 'title' => null,
115 'pageid' => [
116 ApiBase::PARAM_TYPE => 'integer'
117 ],
118 'lang' => [
119 ApiBase::PARAM_TYPE => array_merge(
120 [ 'default' ],
121 array_keys( Language::fetchLanguageNames( 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/API:SetPageLanguage';
148 }
149 }