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