Use 'div' instead of 'section' and 'article'
[mediawiki.git] / includes / specials / SpecialPageLanguage.php
blob5c8794ae99a90bcc24511ad54518c66c8da0c288
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 string 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 // Get default from the subpage of Special page
47 $defaultName = $this->par;
49 $page = array();
50 $page['pagename'] = array(
51 'type' => 'text',
52 'label-message' => 'pagelang-name',
53 'default' => $defaultName,
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,
61 $page['selectoptions'] = array(
62 'id' => 'mw-pl-options',
63 'type' => 'radio',
64 'options' => $selectoptions,
65 'default' => 1
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;
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' ),
85 return $page;
88 protected function postText() {
89 return $this->showLogFragment( $this->par );
92 public function alterForm( HTMLForm $form ) {
93 $form->setDisplayFormat( 'vform' );
94 $form->setWrapperLegend( false );
97 /**
99 * @param array $data
100 * @return bool
102 public function onSubmit( array $data ) {
103 $title = Title::newFromText( $data['pagename'] );
105 // Check if title is valid
106 if ( !$title ) {
107 return false;
110 // Get the default language for the wiki
111 // Returns the default since the page is not loaded from DB
112 $defLang = $title->getPageLanguage()->getCode();
114 $pageId = $title->getArticleID();
116 // Check if article exists
117 if ( !$pageId ) {
118 return false;
121 // Load the page language from DB
122 $dbw = wfGetDB( DB_MASTER );
123 $langOld = $dbw->selectField(
124 'page',
125 'page_lang',
126 array( 'page_id' => $pageId ),
127 __METHOD__
130 // Url to redirect to after the operation
131 $this->goToUrl = $title->getFullURL();
133 // Check if user wants to use default language
134 if ( $data['selectoptions'] == 1 ) {
135 $langNew = null;
136 } else {
137 $langNew = $data['language'];
140 // No change in language
141 if ( $langNew === $langOld ) {
142 return false;
145 // Hardcoded [def] if the language is set to null
146 $logOld = $langOld ? $langOld : $defLang . '[def]';
147 $logNew = $langNew ? $langNew : $defLang . '[def]';
149 // Writing new page language to database
150 $dbw = wfGetDB( DB_MASTER );
151 $dbw->update(
152 'page',
153 array( 'page_lang' => $langNew ),
154 array(
155 'page_id' => $pageId,
156 'page_lang' => $langOld
158 __METHOD__
161 if ( !$dbw->affectedRows() ) {
162 return false;
165 // Logging change of language
166 $logParams = array(
167 '4::oldlanguage' => $logOld,
168 '5::newlanguage' => $logNew
170 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
171 $entry->setPerformer( $this->getUser() );
172 $entry->setTarget( $title );
173 $entry->setParameters( $logParams );
175 $logid = $entry->insert();
176 $entry->publish( $logid );
178 return true;
181 public function onSuccess() {
182 // Success causes a redirect
183 $this->getOutput()->redirect( $this->goToUrl );
186 function showLogFragment( $title ) {
187 $moveLogPage = new LogPage( 'pagelang' );
188 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
189 $out2 = '';
190 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
191 return $out1 . $out2;