Merge "jquery.accessKeyLabel: make modifier info public"
[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 protected function preText() {
42 $this->getOutput()->addModules( 'mediawiki.special.pageLanguage' );
43 }
44
45 protected function getFormFields() {
46 // Get default from the subpage of Special page
47 $defaultName = $this->par;
48
49 $page = array();
50 $page['pagename'] = array(
51 'type' => 'title',
52 'label-message' => 'pagelang-name',
53 'default' => $defaultName,
54 'autofocus' => $defaultName === null,
55 'exists' => true,
56 );
57
58 // Options for whether to use the default language or select language
59 $selectoptions = array(
60 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
61 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
62 );
63 $page['selectoptions'] = array(
64 'id' => 'mw-pl-options',
65 'type' => 'radio',
66 'options' => $selectoptions,
67 'default' => 1
68 );
69
70 // Building a language selector
71 $userLang = $this->getLanguage()->getCode();
72 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
73 ksort( $languages );
74 $options = array();
75 foreach ( $languages as $code => $name ) {
76 $options["$code - $name"] = $code;
77 }
78
79 $page['language'] = array(
80 'id' => 'mw-pl-languageselector',
81 'cssclass' => 'mw-languageselector',
82 'type' => 'select',
83 'options' => $options,
84 'label-message' => 'pagelang-language',
85 'default' => $this->getConfig()->get( 'LanguageCode' ),
86 );
87
88 return $page;
89 }
90
91 protected function postText() {
92 if ( $this->par ) {
93 return $this->showLogFragment( $this->par );
94 }
95 return '';
96 }
97
98 protected function getDisplayFormat() {
99 return 'ooui';
100 }
101
102 public function alterForm( HTMLForm $form ) {
103 Hooks::run( 'LanguageSelector', array( $this->getOutput(), 'mw-languageselector' ) );
104 $form->setSubmitTextMsg( 'pagelang-submit' );
105 }
106
107 /**
108 *
109 * @param array $data
110 * @return bool
111 */
112 public function onSubmit( array $data ) {
113 $title = Title::newFromText( $data['pagename'] );
114
115 // Check if title is valid
116 if ( !$title ) {
117 return false;
118 }
119
120 // Get the default language for the wiki
121 // Returns the default since the page is not loaded from DB
122 $defLang = $title->getPageLanguage()->getCode();
123
124 $pageId = $title->getArticleID();
125
126 // Check if article exists
127 if ( !$pageId ) {
128 return false;
129 }
130
131 // Load the page language from DB
132 $dbw = wfGetDB( DB_MASTER );
133 $langOld = $dbw->selectField(
134 'page',
135 'page_lang',
136 array( 'page_id' => $pageId ),
137 __METHOD__
138 );
139
140 // Url to redirect to after the operation
141 $this->goToUrl = $title->getFullURL();
142
143 // Check if user wants to use default language
144 if ( $data['selectoptions'] == 1 ) {
145 $langNew = null;
146 } else {
147 $langNew = $data['language'];
148 }
149
150 // No change in language
151 if ( $langNew === $langOld ) {
152 return false;
153 }
154
155 // Hardcoded [def] if the language is set to null
156 $logOld = $langOld ? $langOld : $defLang . '[def]';
157 $logNew = $langNew ? $langNew : $defLang . '[def]';
158
159 // Writing new page language to database
160 $dbw = wfGetDB( DB_MASTER );
161 $dbw->update(
162 'page',
163 array( 'page_lang' => $langNew ),
164 array(
165 'page_id' => $pageId,
166 'page_lang' => $langOld
167 ),
168 __METHOD__
169 );
170
171 if ( !$dbw->affectedRows() ) {
172 return false;
173 }
174
175 // Logging change of language
176 $logParams = array(
177 '4::oldlanguage' => $logOld,
178 '5::newlanguage' => $logNew
179 );
180 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
181 $entry->setPerformer( $this->getUser() );
182 $entry->setTarget( $title );
183 $entry->setParameters( $logParams );
184
185 $logid = $entry->insert();
186 $entry->publish( $logid );
187
188 return true;
189 }
190
191 public function onSuccess() {
192 // Success causes a redirect
193 $this->getOutput()->redirect( $this->goToUrl );
194 }
195
196 function showLogFragment( $title ) {
197 $moveLogPage = new LogPage( 'pagelang' );
198 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
199 $out2 = '';
200 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
201 return $out1 . $out2;
202 }
203
204 /**
205 * Return an array of subpages beginning with $search that this special page will accept.
206 *
207 * @param string $search Prefix to search for
208 * @param int $limit Maximum number of results to return (usually 10)
209 * @param int $offset Number of results to skip (usually 0)
210 * @return string[] Matching subpages
211 */
212 public function prefixSearchSubpages( $search, $limit, $offset ) {
213 $title = Title::newFromText( $search );
214 if ( !$title || !$title->canExist() ) {
215 // No prefix suggestion in special and media namespace
216 return array();
217 }
218 // Autocomplete subpage the same as a normal search
219 $prefixSearcher = new StringPrefixSearch;
220 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
221 return $result;
222 }
223
224 protected function getGroupName() {
225 return 'pagetools';
226 }
227 }