MediaWiki  1.29.2
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  // Allow user to enter a comment explaining the change
93  $page['reason'] = [
94  'type' => 'text',
95  'label-message' => 'pagelang-reason'
96  ];
97 
98  return $page;
99  }
100 
101  protected function postText() {
102  if ( $this->par ) {
103  return $this->showLogFragment( $this->par );
104  }
105  return '';
106  }
107 
108  protected function getDisplayFormat() {
109  return 'ooui';
110  }
111 
112  public function alterForm( HTMLForm $form ) {
113  Hooks::run( 'LanguageSelector', [ $this->getOutput(), 'mw-languageselector' ] );
114  $form->setSubmitTextMsg( 'pagelang-submit' );
115  }
116 
122  public function onSubmit( array $data ) {
123  $pageName = $data['pagename'];
124 
125  // Check if user wants to use default language
126  if ( $data['selectoptions'] == 1 ) {
127  $newLanguage = 'default';
128  } else {
129  $newLanguage = $data['language'];
130  }
131 
132  try {
133  $title = Title::newFromTextThrow( $pageName );
134  } catch ( MalformedTitleException $ex ) {
135  return Status::newFatal( $ex->getMessageObject() );
136  }
137 
138  // Url to redirect to after the operation
139  $this->goToUrl = $title->getFullUrlForRedirect(
140  $title->isRedirect() ? [ 'redirect' => 'no' ] : []
141  );
142 
144  $this->getContext(),
145  $title,
146  $newLanguage,
147  $data['reason'] === null ? '' : $data['reason']
148  );
149  }
150 
160  $newLanguage, $reason, array $tags = [] ) {
161  // Get the default language for the wiki
162  $defLang = $context->getConfig()->get( 'LanguageCode' );
163 
164  $pageId = $title->getArticleID();
165 
166  // Check if article exists
167  if ( !$pageId ) {
168  return Status::newFatal(
169  'pagelang-nonexistent-page',
170  wfEscapeWikiText( $title->getPrefixedText() )
171  );
172  }
173 
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__
181  );
182 
183  // Check if user wants to use the default language
184  if ( $newLanguage === 'default' ) {
185  $newLanguage = null;
186  }
187 
188  // No change in language
189  if ( $newLanguage === $oldLanguage ) {
190  // Check if old language does not exist
191  if ( !$oldLanguage ) {
193  [
194  'pagelang-unchanged-language-default',
195  wfEscapeWikiText( $title->getPrefixedText() )
196  ],
197  'pagelang-unchanged-language'
198  ) );
199  }
200  return Status::newFatal(
201  'pagelang-unchanged-language',
202  wfEscapeWikiText( $title->getPrefixedText() ),
203  $oldLanguage
204  );
205  }
206 
207  // Hardcoded [def] if the language is set to null
208  $logOld = $oldLanguage ? $oldLanguage : $defLang . '[def]';
209  $logNew = $newLanguage ? $newLanguage : $defLang . '[def]';
210 
211  // Writing new page language to database
212  $dbw->update(
213  'page',
214  [ 'page_lang' => $newLanguage ],
215  [
216  'page_id' => $pageId,
217  'page_lang' => $oldLanguage
218  ],
219  __METHOD__
220  );
221 
222  if ( !$dbw->affectedRows() ) {
223  return Status::newFatal( 'pagelang-db-failed' );
224  }
225 
226  // Logging change of language
227  $logParams = [
228  '4::oldlanguage' => $logOld,
229  '5::newlanguage' => $logNew
230  ];
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 );
237 
238  $logid = $entry->insert();
239  $entry->publish( $logid );
240 
241  // Force re-render so that language-based content (parser functions etc.) gets updated
242  $title->invalidateCache();
243 
244  return Status::newGood( (object)[
245  'oldLanguage' => $logOld,
246  'newLanguage' => $logNew,
247  'logId' => $logid,
248  ] );
249  }
250 
251  public function onSuccess() {
252  // Success causes a redirect
253  $this->getOutput()->redirect( $this->goToUrl );
254  }
255 
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;
262  }
263 
272  public function prefixSearchSubpages( $search, $limit, $offset ) {
273  return $this->prefixSearchString( $search, $limit, $offset );
274  }
275 
276  protected function getGroupName() {
277  return 'pagetools';
278  }
279 }
ContextSource\getConfig
getConfig()
Get the Config object.
Definition: ContextSource.php:68
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
SpecialPageLanguage\postText
postText()
Add post-text to the form.
Definition: SpecialPageLanguage.php:101
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:675
$languages
switch( $options['output']) $languages
Definition: transstat.php:76
SpecialPageLanguage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPageLanguage.php:276
MalformedTitleException\getMessageObject
getMessageObject()
Definition: MalformedTitleException.php:80
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
SpecialPageLanguage\onSubmit
onSubmit(array $data)
Definition: SpecialPageLanguage.php:122
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialPageLanguage\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialPageLanguage.php:41
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:705
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:133
SpecialPageLanguage\preText
preText()
Add pre-text to the form.
Definition: SpecialPageLanguage.php:45
php
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:35
SpecialPageLanguage\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialPageLanguage.php:112
SpecialPage\prefixSearchString
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
Definition: SpecialPage.php:448
SpecialPageLanguage\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialPageLanguage.php:108
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:714
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
Language\fetchLanguageNames
static fetchLanguageNames( $inLanguage=null, $include='mw')
Get an array of language names, indexed by code.
Definition: Language.php:803
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
$page
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:2536
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:31
$limit
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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 please use GetContentModels hook to make them known to core 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:1049
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:564
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:212
string
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:177
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:648
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:295
SpecialPageLanguage\$goToUrl
string $goToUrl
URL to go to if language change successful.
Definition: SpecialPageLanguage.php:35
SpecialPageLanguage\__construct
__construct()
Definition: SpecialPageLanguage.php:37
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:746
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
FormSpecialPage\$par
string $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
SpecialPageLanguage\showLogFragment
showLogFragment( $title)
Definition: SpecialPageLanguage.php:256
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1657
SpecialPageLanguage\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialPageLanguage.php:272
SpecialPageLanguage\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialPageLanguage.php:251
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1359
SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])
Definition: SpecialPageLanguage.php:159
Title
Represents a title within MediaWiki.
Definition: Title.php:39
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:25
SpecialPageLanguage
Special page for changing the content language of a page.
Definition: SpecialPageLanguage.php:31
$code
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:783
as
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
Definition: distributors.txt:9
SpecialPageLanguage\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialPageLanguage.php:49
ManualLogEntry
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:396
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
array
the array() calling protocol came about after MediaWiki 1.4rc1.
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:128