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
21 * @ingroup SpecialPage
22 * @author Kunal Grover
27 * Special page for changing the content language of a page
29 * @ingroup SpecialPage
31 class SpecialPageLanguage
extends FormSpecialPage
{
33 * @var string URL to go to if language change successful
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
;
50 $page['pagename'] = array(
52 'label-message' => 'pagelang-name',
53 'default' => $defaultName,
54 'autofocus' => $defaultName === null,
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',
66 'options' => $selectoptions,
70 // Building a language selector
71 $userLang = $this->getLanguage()->getCode();
72 $languages = Language
::fetchLanguageNames( $userLang, 'mwfile' );
75 foreach ( $languages as $code => $name ) {
76 $options["$code - $name"] = $code;
79 $page['language'] = array(
80 'id' => 'mw-pl-languageselector',
81 'cssclass' => 'mw-languageselector',
83 'options' => $options,
84 'label-message' => 'pagelang-language',
85 'default' => $this->getConfig()->get( 'LanguageCode' ),
91 protected function postText() {
93 return $this->showLogFragment( $this->par
);
98 protected function getDisplayFormat() {
102 public function alterForm( HTMLForm
$form ) {
103 Hooks
::run( 'LanguageSelector', array( $this->getOutput(), 'mw-languageselector' ) );
104 $form->setSubmitTextMsg( 'pagelang-submit' );
112 public function onSubmit( array $data ) {
113 $title = Title
::newFromText( $data['pagename'] );
115 // Check if title is valid
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
131 // Load the page language from DB
132 $dbw = wfGetDB( DB_MASTER
);
133 $langOld = $dbw->selectField(
136 array( 'page_id' => $pageId ),
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 ) {
147 $langNew = $data['language'];
150 // No change in language
151 if ( $langNew === $langOld ) {
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
);
163 array( 'page_lang' => $langNew ),
165 'page_id' => $pageId,
166 'page_lang' => $langOld
171 if ( !$dbw->affectedRows() ) {
175 // Logging change of language
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 );
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() );
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
218 // Autocomplete subpage the same as a normal search
219 $prefixSearcher = new StringPrefixSearch
;
220 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
224 protected function getGroupName() {