Merge "mediawiki.toc: Remove class="internal" from tocToggleLink"
[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' => 'text',
52 'label-message' => 'pagelang-name',
53 'default' => $defaultName,
54 );
55
56 // Options for whether to use the default language or select language
57 $selectoptions = array(
58 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
59 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
60 );
61 $page['selectoptions'] = array(
62 'id' => 'mw-pl-options',
63 'type' => 'radio',
64 'options' => $selectoptions,
65 'default' => 1
66 );
67
68 // Building a language selector
69 $userLang = $this->getLanguage()->getCode();
70 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
71 ksort( $languages );
72 $options = array();
73 foreach ( $languages as $code => $name ) {
74 $options["$code - $name"] = $code;
75 }
76
77 $page['language'] = array(
78 'id' => 'mw-pl-languageselector',
79 'type' => 'select',
80 'options' => $options,
81 'label-message' => 'pagelang-language',
82 'default' => $this->getConfig()->get( 'LanguageCode' ),
83 );
84
85 return $page;
86 }
87
88 protected function postText() {
89 return $this->showLogFragment( $this->par );
90 }
91
92 public function alterForm( HTMLForm $form ) {
93 $form->setDisplayFormat( 'vform' );
94 $form->setWrapperLegend( false );
95 }
96
97 /**
98 *
99 * @param array $data
100 */
101 public function onSubmit( array $data ) {
102 $title = Title::newFromText( $data['pagename'] );
103
104 // Check if title is valid
105 if ( !$title ) {
106 return false;
107 }
108
109 // Get the default language for the wiki
110 // Returns the default since the page is not loaded from DB
111 $defLang = $title->getPageLanguage()->getCode();
112
113 $pageId = $title->getArticleID();
114
115 // Check if article exists
116 if ( !$pageId ) {
117 return false;
118 }
119
120 // Load the page language from DB
121 $dbw = wfGetDB( DB_MASTER );
122 $langOld = $dbw->selectField(
123 'page',
124 'page_lang',
125 array( 'page_id' => $pageId ),
126 __METHOD__
127 );
128
129 // Url to redirect to after the operation
130 $this->goToUrl = $title->getFullURL();
131
132 // Check if user wants to use default language
133 if ( $data['selectoptions'] == 1 ) {
134 $langNew = null;
135 } else {
136 $langNew = $data['language'];
137 }
138
139 // No change in language
140 if ( $langNew === $langOld ) {
141 return false;
142 }
143
144 // Hardcoded [def] if the language is set to null
145 $logOld = $langOld ? $langOld : $defLang . '[def]';
146 $logNew = $langNew ? $langNew : $defLang . '[def]';
147
148 // Writing new page language to database
149 $dbw = wfGetDB( DB_MASTER );
150 $dbw->update(
151 'page',
152 array( 'page_lang' => $langNew ),
153 array(
154 'page_id' => $pageId,
155 'page_lang' => $langOld
156 ),
157 __METHOD__
158 );
159
160 if ( !$dbw->affectedRows() ) {
161 return false;
162 }
163
164 // Logging change of language
165 $logParams = array(
166 '4::oldlanguage' => $logOld,
167 '5::newlanguage' => $logNew
168 );
169 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
170 $entry->setPerformer( $this->getUser() );
171 $entry->setTarget( $title );
172 $entry->setParameters( $logParams );
173
174 $logid = $entry->insert();
175 $entry->publish( $logid );
176
177 return true;
178 }
179
180 public function onSuccess() {
181 // Success causes a redirect
182 $this->getOutput()->redirect( $this->goToUrl );
183 }
184
185 function showLogFragment( $title ) {
186 $moveLogPage = new LogPage( 'pagelang' );
187 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
188 $out2 = '';
189 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
190 return $out1 . $out2;
191 }
192 }