Merge "Use LinkTarget in EmailNotification::updateWatchlistTimestamp"
[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 = array();
54 $page['pagename'] = array(
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 = array(
64 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
65 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
66 );
67 $page['selectoptions'] = array(
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 = array();
79 foreach ( $languages as $code => $name ) {
80 $options["$code - $name"] = $code;
81 }
82
83 $page['language'] = array(
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', array( $this->getOutput(), 'mw-languageselector' ) );
108 $form->setSubmitTextMsg( 'pagelang-submit' );
109 }
110
111 /**
112 *
113 * @param array $data
114 * @return bool
115 */
116 public function onSubmit( array $data ) {
117 $title = Title::newFromText( $data['pagename'] );
118
119 // Check if title is valid
120 if ( !$title ) {
121 return false;
122 }
123
124 // Get the default language for the wiki
125 // Returns the default since the page is not loaded from DB
126 $defLang = $title->getPageLanguage()->getCode();
127
128 $pageId = $title->getArticleID();
129
130 // Check if article exists
131 if ( !$pageId ) {
132 return false;
133 }
134
135 // Load the page language from DB
136 $dbw = wfGetDB( DB_MASTER );
137 $langOld = $dbw->selectField(
138 'page',
139 'page_lang',
140 array( 'page_id' => $pageId ),
141 __METHOD__
142 );
143
144 // Url to redirect to after the operation
145 $this->goToUrl = $title->getFullURL();
146
147 // Check if user wants to use default language
148 if ( $data['selectoptions'] == 1 ) {
149 $langNew = null;
150 } else {
151 $langNew = $data['language'];
152 }
153
154 // No change in language
155 if ( $langNew === $langOld ) {
156 return false;
157 }
158
159 // Hardcoded [def] if the language is set to null
160 $logOld = $langOld ? $langOld : $defLang . '[def]';
161 $logNew = $langNew ? $langNew : $defLang . '[def]';
162
163 // Writing new page language to database
164 $dbw = wfGetDB( DB_MASTER );
165 $dbw->update(
166 'page',
167 array( 'page_lang' => $langNew ),
168 array(
169 'page_id' => $pageId,
170 'page_lang' => $langOld
171 ),
172 __METHOD__
173 );
174
175 if ( !$dbw->affectedRows() ) {
176 return false;
177 }
178
179 // Logging change of language
180 $logParams = array(
181 '4::oldlanguage' => $logOld,
182 '5::newlanguage' => $logNew
183 );
184 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
185 $entry->setPerformer( $this->getUser() );
186 $entry->setTarget( $title );
187 $entry->setParameters( $logParams );
188
189 $logid = $entry->insert();
190 $entry->publish( $logid );
191
192 return true;
193 }
194
195 public function onSuccess() {
196 // Success causes a redirect
197 $this->getOutput()->redirect( $this->goToUrl );
198 }
199
200 function showLogFragment( $title ) {
201 $moveLogPage = new LogPage( 'pagelang' );
202 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
203 $out2 = '';
204 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
205 return $out1 . $out2;
206 }
207
208 /**
209 * Return an array of subpages beginning with $search that this special page will accept.
210 *
211 * @param string $search Prefix to search for
212 * @param int $limit Maximum number of results to return (usually 10)
213 * @param int $offset Number of results to skip (usually 0)
214 * @return string[] Matching subpages
215 */
216 public function prefixSearchSubpages( $search, $limit, $offset ) {
217 return $this->prefixSearchString( $search, $limit, $offset );
218 }
219
220 protected function getGroupName() {
221 return 'pagetools';
222 }
223 }