Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / specials / SpecialPageLanguage.php
blobdb05ebe587de3eac9017256e39cb6a674ef0478a
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 public function doesWrites() {
42 return true;
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;
53 $page = [];
54 $page['pagename'] = [
55 'type' => 'title',
56 'label-message' => 'pagelang-name',
57 'default' => $defaultName,
58 'autofocus' => $defaultName === null,
59 'exists' => true,
62 // Options for whether to use the default language or select language
63 $selectoptions = [
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',
69 'type' => 'radio',
70 'options' => $selectoptions,
71 'default' => 1
74 // Building a language selector
75 $userLang = $this->getLanguage()->getCode();
76 $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
77 ksort( $languages );
78 $options = [];
79 foreach ( $languages as $code => $name ) {
80 $options["$code - $name"] = $code;
83 $page['language'] = [
84 'id' => 'mw-pl-languageselector',
85 'cssclass' => 'mw-languageselector',
86 'type' => 'select',
87 'options' => $options,
88 'label-message' => 'pagelang-language',
89 'default' => $this->getConfig()->get( 'LanguageCode' ),
92 // Allow user to enter a comment explaining the change
93 $page['reason'] = [
94 'type' => 'text',
95 'label-message' => 'pagelang-reason'
98 return $page;
101 protected function postText() {
102 if ( $this->par ) {
103 return $this->showLogFragment( $this->par );
105 return '';
108 protected function getDisplayFormat() {
109 return 'ooui';
112 public function alterForm( HTMLForm $form ) {
113 Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
114 $form->setSubmitTextMsg( 'pagelang-submit' );
119 * @param array $data
120 * @return Status
122 public function onSubmit( array $data ) {
123 $pageName = $data['pagename'];
125 // Check if user wants to use default language
126 if ( $data['selectoptions'] == 1 ) {
127 $newLanguage = 'default';
128 } else {
129 $newLanguage = $data['language'];
132 try {
133 $title = Title::newFromTextThrow( $pageName );
134 } catch ( MalformedTitleException $ex ) {
135 return Status::newFatal( $ex->getMessageObject() );
138 // Url to redirect to after the operation
139 $this->goToUrl = $title->getFullURL(
140 $title->isRedirect() ? [ 'redirect' => 'no' ] : []
143 return self::changePageLanguage(
144 $this->getContext(),
145 $title,
146 $newLanguage,
147 $data['reason'] === null ? '' : $data['reason']
152 * @param IContextSource $context
153 * @param Title $title
154 * @param string $newLanguage Language code
155 * @param string $reason Reason for the change
156 * @param array $tags Change tags to apply to the log entry
157 * @return Status
159 public static function changePageLanguage( IContextSource $context, Title $title,
160 $newLanguage, $reason, array $tags = [] ) {
161 // Get the default language for the wiki
162 $defLang = $context->getConfig()->get( 'LanguageCode' );
164 $pageId = $title->getArticleID();
166 // Check if article exists
167 if ( !$pageId ) {
168 return Status::newFatal(
169 'pagelang-nonexistent-page',
170 wfEscapeWikiText( $title->getPrefixedText() )
174 // Load the page language from DB
175 $dbw = wfGetDB( DB_MASTER );
176 $oldLanguage = $dbw->selectField(
177 'page',
178 'page_lang',
179 [ 'page_id' => $pageId ],
180 __METHOD__
183 // Check if user wants to use the default language
184 if ( $newLanguage === 'default' ) {
185 $newLanguage = null;
188 // No change in language
189 if ( $newLanguage === $oldLanguage ) {
190 // Check if old language does not exist
191 if ( !$oldLanguage ) {
192 return Status::newFatal( ApiMessage::create(
194 'pagelang-unchanged-language-default',
195 wfEscapeWikiText( $title->getPrefixedText() )
197 'pagelang-unchanged-language'
198 ) );
200 return Status::newFatal(
201 'pagelang-unchanged-language',
202 wfEscapeWikiText( $title->getPrefixedText() ),
203 $oldLanguage
207 // Hardcoded [def] if the language is set to null
208 $logOld = $oldLanguage ? $oldLanguage : $defLang . '[def]';
209 $logNew = $newLanguage ? $newLanguage : $defLang . '[def]';
211 // Writing new page language to database
212 $dbw->update(
213 'page',
214 [ 'page_lang' => $newLanguage ],
216 'page_id' => $pageId,
217 'page_lang' => $oldLanguage
219 __METHOD__
222 if ( !$dbw->affectedRows() ) {
223 return Status::newFatal( 'pagelang-db-failed' );
226 // Logging change of language
227 $logParams = [
228 '4::oldlanguage' => $logOld,
229 '5::newlanguage' => $logNew
231 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
232 $entry->setPerformer( $context->getUser() );
233 $entry->setTarget( $title );
234 $entry->setParameters( $logParams );
235 $entry->setComment( $reason );
236 $entry->setTags( $tags );
238 $logid = $entry->insert();
239 $entry->publish( $logid );
241 // Force re-render so that language-based content (parser functions etc.) gets updated
242 $title->invalidateCache();
244 return Status::newGood( (object)[
245 'oldLanguage' => $logOld,
246 'newLanguage' => $logNew,
247 'logId' => $logid,
248 ] );
251 public function onSuccess() {
252 // Success causes a redirect
253 $this->getOutput()->redirect( $this->goToUrl );
256 function showLogFragment( $title ) {
257 $moveLogPage = new LogPage( 'pagelang' );
258 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
259 $out2 = '';
260 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
261 return $out1 . $out2;
265 * Return an array of subpages beginning with $search that this special page will accept.
267 * @param string $search Prefix to search for
268 * @param int $limit Maximum number of results to return (usually 10)
269 * @param int $offset Number of results to skip (usually 0)
270 * @return string[] Matching subpages
272 public function prefixSearchSubpages( $search, $limit, $offset ) {
273 return $this->prefixSearchString( $search, $limit, $offset );
276 protected function getGroupName() {
277 return 'pagetools';