Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / specials / SpecialPageLanguage.php
blob7509bbcb02129082445a2c1227471735bcbd870f
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' => 'title',
52 'label-message' => 'pagelang-name',
53 'default' => $defaultName,
54 'autofocus' => $defaultName === null,
55 'exists' => true,
58 // Options for whether to use the default language or select language
59 $selectoptions = array(
60 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
61 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
63 $page['selectoptions'] = array(
64 'id' => 'mw-pl-options',
65 'type' => 'radio',
66 'options' => $selectoptions,
67 'default' => 1
70 // Building a language selector
71 $userLang = $this->getLanguage()->getCode();
72 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
73 ksort( $languages );
74 $options = array();
75 foreach ( $languages as $code => $name ) {
76 $options["$code - $name"] = $code;
79 $page['language'] = array(
80 'id' => 'mw-pl-languageselector',
81 'cssclass' => 'mw-languageselector',
82 'type' => 'select',
83 'options' => $options,
84 'label-message' => 'pagelang-language',
85 'default' => $this->getConfig()->get( 'LanguageCode' ),
88 return $page;
91 protected function postText() {
92 if ( $this->par ) {
93 return $this->showLogFragment( $this->par );
95 return '';
98 protected function getDisplayFormat() {
99 return 'ooui';
102 public function alterForm( HTMLForm $form ) {
103 Hooks::run( 'LanguageSelector', array( $this->getOutput(), 'mw-languageselector' ) );
104 $form->setSubmitTextMsg( 'pagelang-submit' );
109 * @param array $data
110 * @return bool
112 public function onSubmit( array $data ) {
113 $title = Title::newFromText( $data['pagename'] );
115 // Check if title is valid
116 if ( !$title ) {
117 return false;
120 // Get the default language for the wiki
121 // Returns the default since the page is not loaded from DB
122 $defLang = $title->getPageLanguage()->getCode();
124 $pageId = $title->getArticleID();
126 // Check if article exists
127 if ( !$pageId ) {
128 return false;
131 // Load the page language from DB
132 $dbw = wfGetDB( DB_MASTER );
133 $langOld = $dbw->selectField(
134 'page',
135 'page_lang',
136 array( 'page_id' => $pageId ),
137 __METHOD__
140 // Url to redirect to after the operation
141 $this->goToUrl = $title->getFullURL();
143 // Check if user wants to use default language
144 if ( $data['selectoptions'] == 1 ) {
145 $langNew = null;
146 } else {
147 $langNew = $data['language'];
150 // No change in language
151 if ( $langNew === $langOld ) {
152 return false;
155 // Hardcoded [def] if the language is set to null
156 $logOld = $langOld ? $langOld : $defLang . '[def]';
157 $logNew = $langNew ? $langNew : $defLang . '[def]';
159 // Writing new page language to database
160 $dbw = wfGetDB( DB_MASTER );
161 $dbw->update(
162 'page',
163 array( 'page_lang' => $langNew ),
164 array(
165 'page_id' => $pageId,
166 'page_lang' => $langOld
168 __METHOD__
171 if ( !$dbw->affectedRows() ) {
172 return false;
175 // Logging change of language
176 $logParams = array(
177 '4::oldlanguage' => $logOld,
178 '5::newlanguage' => $logNew
180 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
181 $entry->setPerformer( $this->getUser() );
182 $entry->setTarget( $title );
183 $entry->setParameters( $logParams );
185 $logid = $entry->insert();
186 $entry->publish( $logid );
188 return true;
191 public function onSuccess() {
192 // Success causes a redirect
193 $this->getOutput()->redirect( $this->goToUrl );
196 function showLogFragment( $title ) {
197 $moveLogPage = new LogPage( 'pagelang' );
198 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
199 $out2 = '';
200 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
201 return $out1 . $out2;
205 * Return an array of subpages beginning with $search that this special page will accept.
207 * @param string $search Prefix to search for
208 * @param int $limit Maximum number of results to return (usually 10)
209 * @param int $offset Number of results to skip (usually 0)
210 * @return string[] Matching subpages
212 public function prefixSearchSubpages( $search, $limit, $offset ) {
213 $title = Title::newFromText( $search );
214 if ( !$title || !$title->canExist() ) {
215 // No prefix suggestion in special and media namespace
216 return array();
218 // Autocomplete subpage the same as a normal search
219 $prefixSearcher = new StringPrefixSearch;
220 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
221 return $result;
224 protected function getGroupName() {
225 return 'pagetools';