MediaWiki REL1_28
SpecialPageLanguage.php
Go to the documentation of this file.
1<?php
35 private $goToUrl;
36
37 public function __construct() {
38 parent::__construct( 'PageLanguage', 'pagelang' );
39 }
40
41 public function doesWrites() {
42 return true;
43 }
44
45 protected function preText() {
46 $this->getOutput()->addModules( 'mediawiki.special.pageLanguage' );
47 }
48
49 protected function getFormFields() {
50 // Get default from the subpage of Special page
51 $defaultName = $this->par;
52
53 $page = [];
54 $page['pagename'] = [
55 'type' => 'title',
56 'label-message' => 'pagelang-name',
57 'default' => $defaultName,
58 'autofocus' => $defaultName === null,
59 'exists' => true,
60 ];
61
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,
66 ];
67 $page['selectoptions'] = [
68 'id' => 'mw-pl-options',
69 'type' => 'radio',
70 'options' => $selectoptions,
71 'default' => 1
72 ];
73
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;
81 }
82
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' ),
90 ];
91
92 return $page;
93 }
94
95 protected function postText() {
96 if ( $this->par ) {
97 return $this->showLogFragment( $this->par );
98 }
99 return '';
100 }
101
102 protected function getDisplayFormat() {
103 return 'ooui';
104 }
105
106 public function alterForm( HTMLForm $form ) {
107 Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
108 $form->setSubmitTextMsg( 'pagelang-submit' );
109 }
110
116 public function onSubmit( array $data ) {
117 $title = Title::newFromText( $data['pagename'] );
118
119 // Check if title is valid
120 if ( !$title ) {
121 return false;
122 }
123
124 // Get the default language for the wiki
125 $defLang = $this->getConfig()->get( 'LanguageCode' );
126
127 $pageId = $title->getArticleID();
128
129 // Check if article exists
130 if ( !$pageId ) {
131 return false;
132 }
133
134 // Load the page language from DB
135 $dbw = wfGetDB( DB_MASTER );
136 $langOld = $dbw->selectField(
137 'page',
138 'page_lang',
139 [ 'page_id' => $pageId ],
140 __METHOD__
141 );
142
143 // Url to redirect to after the operation
144 $this->goToUrl = $title->getFullUrlForRedirect();
145
146 // Check if user wants to use default language
147 if ( $data['selectoptions'] == 1 ) {
148 $langNew = null;
149 } else {
150 $langNew = $data['language'];
151 }
152
153 // No change in language
154 if ( $langNew === $langOld ) {
155 return false;
156 }
157
158 // Hardcoded [def] if the language is set to null
159 $logOld = $langOld ? $langOld : $defLang . '[def]';
160 $logNew = $langNew ? $langNew : $defLang . '[def]';
161
162 // Writing new page language to database
163 $dbw = wfGetDB( DB_MASTER );
164 $dbw->update(
165 'page',
166 [ 'page_lang' => $langNew ],
167 [
168 'page_id' => $pageId,
169 'page_lang' => $langOld
170 ],
171 __METHOD__
172 );
173
174 if ( !$dbw->affectedRows() ) {
175 return false;
176 }
177
178 // Logging change of language
179 $logParams = [
180 '4::oldlanguage' => $logOld,
181 '5::newlanguage' => $logNew
182 ];
183 $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
184 $entry->setPerformer( $this->getUser() );
185 $entry->setTarget( $title );
186 $entry->setParameters( $logParams );
187
188 $logid = $entry->insert();
189 $entry->publish( $logid );
190
191 // Force re-render so that language-based content (parser functions etc.) gets updated
192 $title->invalidateCache();
193
194 return true;
195 }
196
197 public function onSuccess() {
198 // Success causes a redirect
199 $this->getOutput()->redirect( $this->goToUrl );
200 }
201
202 function showLogFragment( $title ) {
203 $moveLogPage = new LogPage( 'pagelang' );
204 $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
205 $out2 = '';
206 LogEventsList::showLogExtract( $out2, 'pagelang', $title );
207 return $out1 . $out2;
208 }
209
218 public function prefixSearchSubpages( $search, $limit, $offset ) {
219 return $this->prefixSearchString( $search, $limit, $offset );
220 }
221
222 protected function getGroupName() {
223 return 'pagetools';
224 }
225}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Special page which uses an HTMLForm to handle processing.
string $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:128
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Class to simplify the use of log pages.
Definition LogPage.php:32
Class for creating log entries manually, to inject them into the database.
Definition LogEntry.php:394
Special page for changing the content language of a page.
getFormFields()
Get an HTMLForm descriptor array.
postText()
Add post-text to the form.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
doesWrites()
Indicates whether this special page may perform database writes.
getDisplayFormat()
Get display format for the form.
string $goToUrl
URL to go to if language change successful.
preText()
Add pre-text to the form.
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user's language.
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
msg()
Wrapper around wfMessage that sets the current context.
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition Xml.php:39
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:183
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:986
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1096
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition hooks.txt:1135
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2534
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:887
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const DB_MASTER
Definition defines.php:23
switch( $options['output']) $languages
Definition transstat.php:76