AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / SpecialPageLanguage.php
blob5322a04fc86cbdbf4fe4d2d704d6db7003e9b261
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 public function doesWrites() {
42 return true;
45 protected function preText() {
46 $this->getOutput()->addModules( 'mediawiki.special.pageLanguage' );
49 protected function getFormFields() {
50 // Get default from the subpage of Special page
51 $defaultName = $this->par;
53 $page = [];
54 $page['pagename'] = [
55 'type' => 'title',
56 'label-message' => 'pagelang-name',
57 'default' => $defaultName,
58 'autofocus' => $defaultName === null,
59 'exists' => true,
62 // Options for whether to use the default language or select language
63 $selectoptions = [
64 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
65 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
67 $page['selectoptions'] = [
68 'id' => 'mw-pl-options',
69 'type' => 'radio',
70 'options' => $selectoptions,
71 'default' => 1
74 // Building a language selector
75 $userLang = $this->getLanguage()->getCode();
76 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
77 ksort( $languages );
78 $options = [];
79 foreach ( $languages as $code => $name ) {
80 $options["$code - $name"] = $code;
83 $page['language'] = [
84 'id' => 'mw-pl-languageselector',
85 'cssclass' => 'mw-languageselector',
86 'type' => 'select',
87 'options' => $options,
88 'label-message' => 'pagelang-language',
89 'default' => $this->getConfig()->get( 'LanguageCode' ),
92 return $page;
95 protected function postText() {
96 if ( $this->par ) {
97 return $this->showLogFragment( $this->par );
99 return '';
102 protected function getDisplayFormat() {
103 return 'ooui';
106 public function alterForm( HTMLForm $form ) {
107 Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
108 $form->setSubmitTextMsg( 'pagelang-submit' );
113 * @param array $data
114 * @return bool
116 public function onSubmit( array $data ) {
117 $title = Title::newFromText( $data['pagename'] );
119 // Check if title is valid
120 if ( !$title ) {
121 return false;
124 // Get the default language for the wiki
125 $defLang = $this->getConfig()->get( 'LanguageCode' );
127 $pageId = $title->getArticleID();
129 // Check if article exists
130 if ( !$pageId ) {
131 return false;
134 // Load the page language from DB
135 $dbw = wfGetDB( DB_MASTER );
136 $langOld = $dbw->selectField(
137 'page',
138 'page_lang',
139 [ 'page_id' => $pageId ],
140 __METHOD__
143 // Url to redirect to after the operation
144 $this->goToUrl = $title->getFullURL();
146 // Check if user wants to use default language
147 if ( $data['selectoptions'] == 1 ) {
148 $langNew = null;
149 } else {
150 $langNew = $data['language'];
153 // No change in language
154 if ( $langNew === $langOld ) {
155 return false;
158 // Hardcoded [def] if the language is set to null
159 $logOld = $langOld ? $langOld : $defLang . '[def]';
160 $logNew = $langNew ? $langNew : $defLang . '[def]';
162 // Writing new page language to database
163 $dbw = wfGetDB( DB_MASTER );
164 $dbw->update(
165 'page',
166 [ 'page_lang' => $langNew ],
168 'page_id' => $pageId,
169 'page_lang' => $langOld
171 __METHOD__
174 if ( !$dbw->affectedRows() ) {
175 return false;
178 // Logging change of language
179 $logParams = [
180 '4::oldlanguage' => $logOld,
181 '5::newlanguage' => $logNew
183 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
184 $entry->setPerformer( $this->getUser() );
185 $entry->setTarget( $title );
186 $entry->setParameters( $logParams );
188 $logid = $entry->insert();
189 $entry->publish( $logid );
191 return true;
194 public function onSuccess() {
195 // Success causes a redirect
196 $this->getOutput()->redirect( $this->goToUrl );
199 function showLogFragment( $title ) {
200 $moveLogPage = new LogPage( 'pagelang' );
201 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
202 $out2 = '';
203 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
204 return $out1 . $out2;
208 * Return an array of subpages beginning with $search that this special page will accept.
210 * @param string $search Prefix to search for
211 * @param int $limit Maximum number of results to return (usually 10)
212 * @param int $offset Number of results to skip (usually 0)
213 * @return string[] Matching subpages
215 public function prefixSearchSubpages( $search, $limit, $offset ) {
216 return $this->prefixSearchString( $search, $limit, $offset );
219 protected function getGroupName() {
220 return 'pagetools';