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