Merge "Add userExpLevel filter in the RCFilters UI"
[lhc/web/wiklou.git] / includes / specials / SpecialPageLanguage.php
1 <?php
2 /**
3 * Implements Special:PageLanguage
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 * @ingroup SpecialPage
22 * @author Kunal Grover
23 * @since 1.24
24 */
25
26 /**
27 * Special page for changing the content language of a page
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialPageLanguage extends FormSpecialPage {
32 /**
33 * @var string URL to go to if language change successful
34 */
35 private $goToUrl;
36
37 public function __construct() {
38 parent::__construct( 'PageLanguage', 'pagelang' );
39 }
40
41 public function doesWrites() {
42 return true;
43 }
44
45 protected function preText() {
46 $this->getOutput()->addModules( 'mediawiki.special.pageLanguage' );
47 }
48
49 protected function getFormFields() {
50 // Get default from the subpage of Special page
51 $defaultName = $this->par;
52
53 $page = [];
54 $page['pagename'] = [
55 'type' => 'title',
56 'label-message' => 'pagelang-name',
57 'default' => $defaultName,
58 'autofocus' => $defaultName === null,
59 'exists' => true,
60 ];
61
62 // Options for whether to use the default language or select language
63 $selectoptions = [
64 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
65 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
66 ];
67 $page['selectoptions'] = [
68 'id' => 'mw-pl-options',
69 'type' => 'radio',
70 'options' => $selectoptions,
71 'default' => 1
72 ];
73
74 // Building a language selector
75 $userLang = $this->getLanguage()->getCode();
76 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
77 ksort( $languages );
78 $options = [];
79 foreach ( $languages as $code => $name ) {
80 $options["$code - $name"] = $code;
81 }
82
83 $page['language'] = [
84 'id' => 'mw-pl-languageselector',
85 'cssclass' => 'mw-languageselector',
86 'type' => 'select',
87 'options' => $options,
88 'label-message' => 'pagelang-language',
89 'default' => $this->getConfig()->get( 'LanguageCode' ),
90 ];
91
92 return $page;
93 }
94
95 protected function postText() {
96 if ( $this->par ) {
97 return $this->showLogFragment( $this->par );
98 }
99 return '';
100 }
101
102 protected function getDisplayFormat() {
103 return 'ooui';
104 }
105
106 public function alterForm( HTMLForm $form ) {
107 Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
108 $form->setSubmitTextMsg( 'pagelang-submit' );
109 }
110
111 /**
112 *
113 * @param array $data
114 * @return Status
115 */
116 public function onSubmit( array $data ) {
117 $pageName = $data['pagename'];
118
119 // Check if user wants to use default language
120 if ( $data['selectoptions'] == 1 ) {
121 $newLanguage = 'default';
122 } else {
123 $newLanguage = $data['language'];
124 }
125
126 try {
127 $title = Title::newFromTextThrow( $pageName );
128 } catch ( MalformedTitleException $ex ) {
129 return Status::newFatal( $ex->getMessageObject() );
130 }
131
132 // Url to redirect to after the operation
133 $this->goToUrl = $title->getFullURL();
134
135 return self::changePageLanguage( $this->getContext(), $title, $newLanguage );
136 }
137
138 /**
139 * @param IContextSource $context
140 * @param Title $title
141 * @param string $newLanguage Language code
142 * @param array $tags Change tags to apply to the log entry
143 * @return Status
144 */
145 public static function changePageLanguage( IContextSource $context, Title $title,
146 $newLanguage, array $tags = [] ) {
147 // Get the default language for the wiki
148 $defLang = $context->getConfig()->get( 'LanguageCode' );
149
150 $pageId = $title->getArticleID();
151
152 // Check if article exists
153 if ( !$pageId ) {
154 return Status::newFatal(
155 'pagelang-nonexistent-page',
156 wfEscapeWikiText( $title->getPrefixedText() )
157 );
158 }
159
160 // Load the page language from DB
161 $dbw = wfGetDB( DB_MASTER );
162 $oldLanguage = $dbw->selectField(
163 'page',
164 'page_lang',
165 [ 'page_id' => $pageId ],
166 __METHOD__
167 );
168
169 // Check if user wants to use the default language
170 if ( $newLanguage === 'default' ) {
171 $newLanguage = null;
172 }
173
174 // No change in language
175 if ( $newLanguage === $oldLanguage ) {
176 // Check if old language does not exist
177 if ( !$oldLanguage ) {
178 return Status::newFatal( ApiMessage::create(
179 [
180 'pagelang-unchanged-language-default',
181 wfEscapeWikiText( $title->getPrefixedText() )
182 ],
183 'pagelang-unchanged-language'
184 ) );
185 }
186 return Status::newFatal(
187 'pagelang-unchanged-language',
188 wfEscapeWikiText( $title->getPrefixedText() ),
189 $oldLanguage
190 );
191 }
192
193 // Hardcoded [def] if the language is set to null
194 $logOld = $oldLanguage ? $oldLanguage : $defLang . '[def]';
195 $logNew = $newLanguage ? $newLanguage : $defLang . '[def]';
196
197 // Writing new page language to database
198 $dbw->update(
199 'page',
200 [ 'page_lang' => $newLanguage ],
201 [
202 'page_id' => $pageId,
203 'page_lang' => $oldLanguage
204 ],
205 __METHOD__
206 );
207
208 if ( !$dbw->affectedRows() ) {
209 return Status::newFatal( 'pagelang-db-failed' );
210 }
211
212 // Logging change of language
213 $logParams = [
214 '4::oldlanguage' => $logOld,
215 '5::newlanguage' => $logNew
216 ];
217 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
218 $entry->setPerformer( $context->getUser() );
219 $entry->setTarget( $title );
220 $entry->setParameters( $logParams );
221 $entry->setTags( $tags );
222
223 $logid = $entry->insert();
224 $entry->publish( $logid );
225
226 // Force re-render so that language-based content (parser functions etc.) gets updated
227 $title->invalidateCache();
228
229 return Status::newGood( (object)[
230 'oldLanguage' => $logOld,
231 'newLanguage' => $logNew,
232 'logId' => $logid,
233 ] );
234 }
235
236 public function onSuccess() {
237 // Success causes a redirect
238 $this->getOutput()->redirect( $this->goToUrl );
239 }
240
241 function showLogFragment( $title ) {
242 $moveLogPage = new LogPage( 'pagelang' );
243 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
244 $out2 = '';
245 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
246 return $out1 . $out2;
247 }
248
249 /**
250 * Return an array of subpages beginning with $search that this special page will accept.
251 *
252 * @param string $search Prefix to search for
253 * @param int $limit Maximum number of results to return (usually 10)
254 * @param int $offset Number of results to skip (usually 0)
255 * @return string[] Matching subpages
256 */
257 public function prefixSearchSubpages( $search, $limit, $offset ) {
258 return $this->prefixSearchString( $search, $limit, $offset );
259 }
260
261 protected function getGroupName() {
262 return 'pagetools';
263 }
264 }