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,
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',
64 'options' => $selectoptions,
68 // Building a language selector
69 $userLang = $this->getLanguage()->getCode();
70 $languages = Language
::fetchLanguageNames( $userLang, 'mwfile' );
73 foreach ( $languages as $code => $name ) {
74 $options["$code - $name"] = $code;
77 $page['language'] = array(
78 'id' => 'mw-pl-languageselector',
79 'cssclass' => 'mw-languageselector',
81 'options' => $options,
82 'label-message' => 'pagelang-language',
83 'default' => $this->getConfig()->get( 'LanguageCode' ),
89 protected function postText() {
91 return $this->showLogFragment( $this->par
);
96 protected function getDisplayFormat() {
100 public function alterForm( HTMLForm
$form ) {
101 Hooks
::run( 'LanguageSelector', array( $this->getOutput(), 'mw-languageselector' ) );
109 public function onSubmit( array $data ) {
110 $title = Title
::newFromText( $data['pagename'] );
112 // Check if title is valid
117 // Get the default language for the wiki
118 // Returns the default since the page is not loaded from DB
119 $defLang = $title->getPageLanguage()->getCode();
121 $pageId = $title->getArticleID();
123 // Check if article exists
128 // Load the page language from DB
129 $dbw = wfGetDB( DB_MASTER
);
130 $langOld = $dbw->selectField(
133 array( 'page_id' => $pageId ),
137 // Url to redirect to after the operation
138 $this->goToUrl
= $title->getFullURL();
140 // Check if user wants to use default language
141 if ( $data['selectoptions'] == 1 ) {
144 $langNew = $data['language'];
147 // No change in language
148 if ( $langNew === $langOld ) {
152 // Hardcoded [def] if the language is set to null
153 $logOld = $langOld ?
$langOld : $defLang . '[def]';
154 $logNew = $langNew ?
$langNew : $defLang . '[def]';
156 // Writing new page language to database
157 $dbw = wfGetDB( DB_MASTER
);
160 array( 'page_lang' => $langNew ),
162 'page_id' => $pageId,
163 'page_lang' => $langOld
168 if ( !$dbw->affectedRows() ) {
172 // Logging change of language
174 '4::oldlanguage' => $logOld,
175 '5::newlanguage' => $logNew
177 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
178 $entry->setPerformer( $this->getUser() );
179 $entry->setTarget( $title );
180 $entry->setParameters( $logParams );
182 $logid = $entry->insert();
183 $entry->publish( $logid );
188 public function onSuccess() {
189 // Success causes a redirect
190 $this->getOutput()->redirect( $this->goToUrl
);
193 function showLogFragment( $title ) {
194 $moveLogPage = new LogPage( 'pagelang' );
195 $out1 = Xml
::element( 'h2', null, $moveLogPage->getName()->text() );
197 LogEventsList
::showLogExtract( $out2, 'pagelang', $title );
198 return $out1 . $out2;
202 * Return an array of subpages beginning with $search that this special page will accept.
204 * @param string $search Prefix to search for
205 * @param int $limit Maximum number of results to return (usually 10)
206 * @param int $offset Number of results to skip (usually 0)
207 * @return string[] Matching subpages
209 public function prefixSearchSubpages( $search, $limit, $offset ) {
210 if ( $search === '' ) {
213 // Autocomplete subpage the same as a normal search
214 $prefixSearcher = new StringPrefixSearch
;
215 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
219 protected function getGroupName() {