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
;
52 $title = $defaultName ? Title
::newFromText( $defaultName ) : null;
54 $defaultPageLanguage =
55 ContentHandler
::getForTitle( $title )->getPageLanguage( $title );
56 $hasCustomLanguageSet = !$defaultPageLanguage->equals( $title->getPageLanguage() );
58 $hasCustomLanguageSet = false;
64 'label-message' => 'pagelang-name',
65 'default' => $title ?
$title->getPrefixedText() : $defaultName,
66 'autofocus' => $defaultName === null,
70 // Options for whether to use the default language or select language
72 (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
73 (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
75 $page['selectoptions'] = [
76 'id' => 'mw-pl-options',
78 'options' => $selectoptions,
79 'default' => $hasCustomLanguageSet ?
2 : 1
82 // Building a language selector
83 $userLang = $this->getLanguage()->getCode();
84 $languages = Language
::fetchLanguageNames( $userLang, 'mwfile' );
87 foreach ( $languages as $code => $name ) {
88 $options["$code - $name"] = $code;
92 'id' => 'mw-pl-languageselector',
93 'cssclass' => 'mw-languageselector',
95 'options' => $options,
96 'label-message' => 'pagelang-language',
98 $title->getPageLanguage()->getCode() :
99 $this->getConfig()->get( 'LanguageCode' ),
102 // Allow user to enter a comment explaining the change
105 'label-message' => 'pagelang-reason'
111 protected function postText() {
113 return $this->showLogFragment( $this->par
);
118 protected function getDisplayFormat() {
122 public function alterForm( HTMLForm
$form ) {
123 Hooks
::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
124 $form->setSubmitTextMsg( 'pagelang-submit' );
132 public function onSubmit( array $data ) {
133 $pageName = $data['pagename'];
135 // Check if user wants to use default language
136 if ( $data['selectoptions'] == 1 ) {
137 $newLanguage = 'default';
139 $newLanguage = $data['language'];
143 $title = Title
::newFromTextThrow( $pageName );
144 } catch ( MalformedTitleException
$ex ) {
145 return Status
::newFatal( $ex->getMessageObject() );
148 // Check permissions and make sure the user has permission to edit the page
149 $errors = $title->getUserPermissionsErrors( 'edit', $this->getUser() );
152 $out = $this->getOutput();
153 $wikitext = $out->formatPermissionsErrorMessage( $errors );
154 // Hack to get our wikitext parsed
155 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
158 // Url to redirect to after the operation
159 $this->goToUrl
= $title->getFullUrlForRedirect(
160 $title->isRedirect() ?
[ 'redirect' => 'no' ] : []
163 return self
::changePageLanguage(
167 $data['reason'] === null ?
'' : $data['reason']
172 * @param IContextSource $context
173 * @param Title $title
174 * @param string $newLanguage Language code
175 * @param string $reason Reason for the change
176 * @param array $tags Change tags to apply to the log entry
179 public static function changePageLanguage( IContextSource
$context, Title
$title,
180 $newLanguage, $reason, array $tags = [] ) {
181 // Get the default language for the wiki
182 $defLang = $context->getConfig()->get( 'LanguageCode' );
184 $pageId = $title->getArticleID();
186 // Check if article exists
188 return Status
::newFatal(
189 'pagelang-nonexistent-page',
190 wfEscapeWikiText( $title->getPrefixedText() )
194 // Load the page language from DB
195 $dbw = wfGetDB( DB_MASTER
);
196 $oldLanguage = $dbw->selectField(
199 [ 'page_id' => $pageId ],
203 // Check if user wants to use the default language
204 if ( $newLanguage === 'default' ) {
208 // No change in language
209 if ( $newLanguage === $oldLanguage ) {
210 // Check if old language does not exist
211 if ( !$oldLanguage ) {
212 return Status
::newFatal( ApiMessage
::create(
214 'pagelang-unchanged-language-default',
215 wfEscapeWikiText( $title->getPrefixedText() )
217 'pagelang-unchanged-language'
220 return Status
::newFatal(
221 'pagelang-unchanged-language',
222 wfEscapeWikiText( $title->getPrefixedText() ),
227 // Hardcoded [def] if the language is set to null
228 $logOld = $oldLanguage ?
$oldLanguage : $defLang . '[def]';
229 $logNew = $newLanguage ?
$newLanguage : $defLang . '[def]';
231 // Writing new page language to database
234 [ 'page_lang' => $newLanguage ],
236 'page_id' => $pageId,
237 'page_lang' => $oldLanguage
242 if ( !$dbw->affectedRows() ) {
243 return Status
::newFatal( 'pagelang-db-failed' );
246 // Logging change of language
248 '4::oldlanguage' => $logOld,
249 '5::newlanguage' => $logNew
251 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
252 $entry->setPerformer( $context->getUser() );
253 $entry->setTarget( $title );
254 $entry->setParameters( $logParams );
255 $entry->setComment( $reason );
256 $entry->setTags( $tags );
258 $logid = $entry->insert();
259 $entry->publish( $logid );
261 // Force re-render so that language-based content (parser functions etc.) gets updated
262 $title->invalidateCache();
264 return Status
::newGood( (object)[
265 'oldLanguage' => $logOld,
266 'newLanguage' => $logNew,
271 public function onSuccess() {
272 // Success causes a redirect
273 $this->getOutput()->redirect( $this->goToUrl
);
276 function showLogFragment( $title ) {
277 $moveLogPage = new LogPage( 'pagelang' );
278 $out1 = Xml
::element( 'h2', null, $moveLogPage->getName()->text() );
280 LogEventsList
::showLogExtract( $out2, 'pagelang', $title );
281 return $out1 . $out2;
285 * Return an array of subpages beginning with $search that this special page will accept.
287 * @param string $search Prefix to search for
288 * @param int $limit Maximum number of results to return (usually 10)
289 * @param int $offset Number of results to skip (usually 0)
290 * @return string[] Matching subpages
292 public function prefixSearchSubpages( $search, $limit, $offset ) {
293 return $this->prefixSearchString( $search, $limit, $offset );
296 protected function getGroupName() {