55ae69262156720e5a560f97dfe88df26617f81f
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 public function doesWrites() {
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
;
56 'label-message' => 'pagelang-name',
57 'default' => $defaultName,
58 'autofocus' => $defaultName === null,
62 // Options for whether to use the default language or select language
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',
70 'options' => $selectoptions,
74 // Building a language selector
75 $userLang = $this->getLanguage()->getCode();
76 $languages = Language
::fetchLanguageNames( $userLang, 'mwfile' );
79 foreach ( $languages as $code => $name ) {
80 $options["$code - $name"] = $code;
84 'id' => 'mw-pl-languageselector',
85 'cssclass' => 'mw-languageselector',
87 'options' => $options,
88 'label-message' => 'pagelang-language',
89 'default' => $this->getConfig()->get( 'LanguageCode' ),
95 protected function postText() {
97 return $this->showLogFragment( $this->par
);
102 protected function getDisplayFormat() {
106 public function alterForm( HTMLForm
$form ) {
107 Hooks
::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
108 $form->setSubmitTextMsg( 'pagelang-submit' );
116 public function onSubmit( array $data ) {
117 $pageName = $data['pagename'];
119 // Check if user wants to use default language
120 if ( $data['selectoptions'] == 1 ) {
121 $newLanguage = 'default';
123 $newLanguage = $data['language'];
127 $title = Title
::newFromTextThrow( $pageName );
128 } catch ( MalformedTitleException
$ex ) {
129 return Status
::newFatal( $ex->getMessageObject() );
132 // Url to redirect to after the operation
133 $this->goToUrl
= $title->getFullURL();
135 return self
::changePageLanguage( $this->getContext(), $title, $newLanguage );
139 * @param IContextSource $context
140 * @param Title $title
141 * @param string $newLanguage Language code
142 * @param array $tags Change tags to apply to the log entry
145 public static function changePageLanguage( IContextSource
$context, Title
$title,
146 $newLanguage, array $tags = [] ) {
147 // Get the default language for the wiki
148 $defLang = $context->getConfig()->get( 'LanguageCode' );
150 $pageId = $title->getArticleID();
152 // Check if article exists
154 return Status
::newFatal(
155 'pagelang-nonexistent-page',
156 wfEscapeWikiText( $title->getPrefixedText() )
160 // Load the page language from DB
161 $dbw = wfGetDB( DB_MASTER
);
162 $oldLanguage = $dbw->selectField(
165 [ 'page_id' => $pageId ],
169 // Check if user wants to use the default language
170 if ( $newLanguage === 'default' ) {
174 // No change in language
175 if ( $newLanguage === $oldLanguage ) {
176 // Check if old language does not exist
177 if ( !$oldLanguage ) {
178 return Status
::newFatal(
179 'pagelang-unchanged-language-default',
180 wfEscapeWikiText( $title->getPrefixedText() )
183 return Status
::newFatal(
184 'pagelang-unchanged-language',
185 wfEscapeWikiText( $title->getPrefixedText() ),
190 // Hardcoded [def] if the language is set to null
191 $logOld = $oldLanguage ?
$oldLanguage : $defLang . '[def]';
192 $logNew = $newLanguage ?
$newLanguage : $defLang . '[def]';
194 // Writing new page language to database
197 [ 'page_lang' => $newLanguage ],
199 'page_id' => $pageId,
200 'page_lang' => $oldLanguage
205 if ( !$dbw->affectedRows() ) {
206 return Status
::newFatal( 'pagelang-db-failed' );
209 // Logging change of language
211 '4::oldlanguage' => $logOld,
212 '5::newlanguage' => $logNew
214 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
215 $entry->setPerformer( $context->getUser() );
216 $entry->setTarget( $title );
217 $entry->setParameters( $logParams );
218 $entry->setTags( $tags );
220 $logid = $entry->insert();
221 $entry->publish( $logid );
223 // Force re-render so that language-based content (parser functions etc.) gets updated
224 $title->invalidateCache();
226 return Status
::newGood( (object)[
227 'oldLanguage' => $logOld,
228 'newLanguage' => $logNew,
233 public function onSuccess() {
234 // Success causes a redirect
235 $this->getOutput()->redirect( $this->goToUrl
);
238 function showLogFragment( $title ) {
239 $moveLogPage = new LogPage( 'pagelang' );
240 $out1 = Xml
::element( 'h2', null, $moveLogPage->getName()->text() );
242 LogEventsList
::showLogExtract( $out2, 'pagelang', $title );
243 return $out1 . $out2;
247 * Return an array of subpages beginning with $search that this special page will accept.
249 * @param string $search Prefix to search for
250 * @param int $limit Maximum number of results to return (usually 10)
251 * @param int $offset Number of results to skip (usually 0)
252 * @return string[] Matching subpages
254 public function prefixSearchSubpages( $search, $limit, $offset ) {
255 return $this->prefixSearchString( $search, $limit, $offset );
258 protected function getGroupName() {