Merge "Correct deprecation warning for $.quoteString"
[mediawiki.git] / includes / specials / SpecialPageLanguage.php
bloba432f104dbba38edb0cbc1ecc7368b5aede5cd3c
1 <?php
2 /**
3 * Implements Special:PageLanguage
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Kunal Grover
23 * @since 1.24
26 /**
27 * Special page for changing the content language of a page
29 * @ingroup SpecialPage
31 class SpecialPageLanguage extends FormSpecialPage {
32 /**
33 * @var $goToUrl URL to go to if language change successful
35 private $goToUrl;
37 public function __construct() {
38 parent::__construct( 'PageLanguage', 'pagelang' );
41 protected function preText() {
42 $this->getOutput()->addModules( 'mediawiki.special.pageLanguage' );
45 protected function getFormFields() {
46 global $wgLanguageCode;
47 // Get default from the subpage of Special page
48 $defaultName = $this->par;
50 $page = array();
51 $page['pagename'] = array(
52 'type' => 'text',
53 'label-message' => 'pagelang-name',
54 'default' => $defaultName,
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,
62 $page['selectoptions'] = array(
63 'id' => 'mw-pl-options',
64 'type' => 'radio',
65 'options' => $selectoptions,
66 'default' => 1
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;
78 $page['language'] = array(
79 'id' => 'mw-pl-languageselector',
80 'type' => 'select',
81 'options' => $options,
82 'label-message' => 'pagelang-language',
83 'default' => $wgLanguageCode
86 return $page;
89 public function alterForm( HTMLForm $form ) {
90 $form->setDisplayFormat( 'vform' );
91 $form->setWrapperLegend( false );
94 /**
96 * @param array $data
98 public function onSubmit( array $data ) {
99 $title = Title::newFromText( $data['pagename'] );
101 // Check if title is valid
102 if ( !$title ) {
103 return false;
106 // Get the default language for the wiki
107 // Returns the default since the page is not loaded from DB
108 $defLang = $title->getPageLanguage()->getCode();
110 $pageId = $title->getArticleID();
112 // Check if article exists
113 if ( !$pageId ) {
114 return false;
117 // Load the page language from DB
118 $dbw = wfGetDB( DB_MASTER );
119 $langOld = $dbw->selectField(
120 'page',
121 'page_lang',
122 array( 'page_id' => $pageId ),
123 __METHOD__
126 // Url to redirect to after the operation
127 $this->goToUrl = $title->getFullURL();
129 // Check if user wants to use default language
130 if ( $data['selectoptions'] == 1 ) {
131 $langNew = null;
132 } else {
133 $langNew = $data['language'];
136 // No change in language
137 if ( $langNew === $langOld ) {
138 return false;
141 // Hardcoded [def] if the language is set to null
142 $logOld = $langOld ? $langOld : $defLang . '[def]';
143 $logNew = $langNew ? $langNew : $defLang . '[def]';
145 // Writing new page language to database
146 $dbw = wfGetDB( DB_MASTER );
147 $dbw->update(
148 'page',
149 array( 'page_lang' => $langNew ),
150 array(
151 'page_id' => $pageId,
152 'page_lang' => $langOld
154 __METHOD__
157 if ( !$dbw->affectedRows() ) {
158 return false;
161 // Logging change of language
162 $logParams = array(
163 '4::oldlanguage' => $logOld,
164 '5::newlanguage' => $logNew
166 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
167 $entry->setPerformer( $this->getUser() );
168 $entry->setTarget( $title );
169 $entry->setParameters( $logParams );
171 $logid = $entry->insert();
172 $entry->publish( $logid );
174 return true;
177 public function onSuccess() {
178 // Success causes a redirect
179 $this->getOutput()->redirect( $this->goToUrl );