Merge "registration: Don't initialize MWServices super early"
[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 // Allow user to enter a comment explaining the change
93 $page['reason'] = [
94 'type' => 'text',
95 'label-message' => 'pagelang-reason'
96 ];
97
98 return $page;
99 }
100
101 protected function postText() {
102 if ( $this->par ) {
103 return $this->showLogFragment( $this->par );
104 }
105 return '';
106 }
107
108 protected function getDisplayFormat() {
109 return 'ooui';
110 }
111
112 public function alterForm( HTMLForm $form ) {
113 Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
114 $form->setSubmitTextMsg( 'pagelang-submit' );
115 }
116
117 /**
118 *
119 * @param array $data
120 * @return Status
121 */
122 public function onSubmit( array $data ) {
123 $pageName = $data['pagename'];
124
125 // Check if user wants to use default language
126 if ( $data['selectoptions'] == 1 ) {
127 $newLanguage = 'default';
128 } else {
129 $newLanguage = $data['language'];
130 }
131
132 try {
133 $title = Title::newFromTextThrow( $pageName );
134 } catch ( MalformedTitleException $ex ) {
135 return Status::newFatal( $ex->getMessageObject() );
136 }
137
138 // Url to redirect to after the operation
139 $this->goToUrl = $title->getFullURL();
140
141 return self::changePageLanguage(
142 $this->getContext(),
143 $title,
144 $newLanguage,
145 $data['reason'] === null ? '' : $data['reason']
146 );
147 }
148
149 /**
150 * @param IContextSource $context
151 * @param Title $title
152 * @param string $newLanguage Language code
153 * @param string $reason Reason for the change
154 * @param array $tags Change tags to apply to the log entry
155 * @return Status
156 */
157 public static function changePageLanguage( IContextSource $context, Title $title,
158 $newLanguage, $reason, array $tags = [] ) {
159 // Get the default language for the wiki
160 $defLang = $context->getConfig()->get( 'LanguageCode' );
161
162 $pageId = $title->getArticleID();
163
164 // Check if article exists
165 if ( !$pageId ) {
166 return Status::newFatal(
167 'pagelang-nonexistent-page',
168 wfEscapeWikiText( $title->getPrefixedText() )
169 );
170 }
171
172 // Load the page language from DB
173 $dbw = wfGetDB( DB_MASTER );
174 $oldLanguage = $dbw->selectField(
175 'page',
176 'page_lang',
177 [ 'page_id' => $pageId ],
178 __METHOD__
179 );
180
181 // Check if user wants to use the default language
182 if ( $newLanguage === 'default' ) {
183 $newLanguage = null;
184 }
185
186 // No change in language
187 if ( $newLanguage === $oldLanguage ) {
188 // Check if old language does not exist
189 if ( !$oldLanguage ) {
190 return Status::newFatal( ApiMessage::create(
191 [
192 'pagelang-unchanged-language-default',
193 wfEscapeWikiText( $title->getPrefixedText() )
194 ],
195 'pagelang-unchanged-language'
196 ) );
197 }
198 return Status::newFatal(
199 'pagelang-unchanged-language',
200 wfEscapeWikiText( $title->getPrefixedText() ),
201 $oldLanguage
202 );
203 }
204
205 // Hardcoded [def] if the language is set to null
206 $logOld = $oldLanguage ? $oldLanguage : $defLang . '[def]';
207 $logNew = $newLanguage ? $newLanguage : $defLang . '[def]';
208
209 // Writing new page language to database
210 $dbw->update(
211 'page',
212 [ 'page_lang' => $newLanguage ],
213 [
214 'page_id' => $pageId,
215 'page_lang' => $oldLanguage
216 ],
217 __METHOD__
218 );
219
220 if ( !$dbw->affectedRows() ) {
221 return Status::newFatal( 'pagelang-db-failed' );
222 }
223
224 // Logging change of language
225 $logParams = [
226 '4::oldlanguage' => $logOld,
227 '5::newlanguage' => $logNew
228 ];
229 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
230 $entry->setPerformer( $context->getUser() );
231 $entry->setTarget( $title );
232 $entry->setParameters( $logParams );
233 $entry->setComment( $reason );
234 $entry->setTags( $tags );
235
236 $logid = $entry->insert();
237 $entry->publish( $logid );
238
239 // Force re-render so that language-based content (parser functions etc.) gets updated
240 $title->invalidateCache();
241
242 return Status::newGood( (object)[
243 'oldLanguage' => $logOld,
244 'newLanguage' => $logNew,
245 'logId' => $logid,
246 ] );
247 }
248
249 public function onSuccess() {
250 // Success causes a redirect
251 $this->getOutput()->redirect( $this->goToUrl );
252 }
253
254 function showLogFragment( $title ) {
255 $moveLogPage = new LogPage( 'pagelang' );
256 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
257 $out2 = '';
258 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
259 return $out1 . $out2;
260 }
261
262 /**
263 * Return an array of subpages beginning with $search that this special page will accept.
264 *
265 * @param string $search Prefix to search for
266 * @param int $limit Maximum number of results to return (usually 10)
267 * @param int $offset Number of results to skip (usually 0)
268 * @return string[] Matching subpages
269 */
270 public function prefixSearchSubpages( $search, $limit, $offset ) {
271 return $this->prefixSearchString( $search, $limit, $offset );
272 }
273
274 protected function getGroupName() {
275 return 'pagetools';
276 }
277 }