MediaWiki  master
SpecialPageLanguage.php
Go to the documentation of this file.
1 <?php
27 
37  private $goToUrl;
38 
39  public function __construct() {
40  parent::__construct( 'PageLanguage', 'pagelang' );
41  }
42 
43  public function doesWrites() {
44  return true;
45  }
46 
47  protected function preText() {
48  $this->getOutput()->addModules( 'mediawiki.misc-authed-ooui' );
49  return parent::preText();
50  }
51 
52  protected function getFormFields() {
53  // Get default from the subpage of Special page
54  $defaultName = $this->par;
55  $title = $defaultName ? Title::newFromText( $defaultName ) : null;
56  if ( $title ) {
57  $defaultPageLanguage = MediaWikiServices::getInstance()
58  ->getContentHandlerFactory()
59  ->getContentHandler( $title->getContentModel() )
60  ->getPageLanguage( $title );
61 
62  $hasCustomLanguageSet = !$defaultPageLanguage->equals( $title->getPageLanguage() );
63  } else {
64  $hasCustomLanguageSet = false;
65  }
66 
67  $page = [];
68  $page['pagename'] = [
69  'type' => 'title',
70  'label-message' => 'pagelang-name',
71  'default' => $title ? $title->getPrefixedText() : $defaultName,
72  'autofocus' => $defaultName === null,
73  'exists' => true,
74  ];
75 
76  // Options for whether to use the default language or select language
77  $selectoptions = [
78  (string)$this->msg( 'pagelang-use-default' )->escaped() => 1,
79  (string)$this->msg( 'pagelang-select-lang' )->escaped() => 2,
80  ];
81  $page['selectoptions'] = [
82  'id' => 'mw-pl-options',
83  'type' => 'radio',
84  'options' => $selectoptions,
85  'default' => $hasCustomLanguageSet ? 2 : 1
86  ];
87 
88  // Building a language selector
89  $userLang = $this->getLanguage()->getCode();
90  $languages = MediaWikiServices::getInstance()
91  ->getLanguageNameUtils()
92  ->getLanguageNames( $userLang, 'mwfile' );
93  $options = [];
94  foreach ( $languages as $code => $name ) {
95  $options["$code - $name"] = $code;
96  }
97 
98  $page['language'] = [
99  'id' => 'mw-pl-languageselector',
100  'cssclass' => 'mw-languageselector',
101  'type' => 'select',
102  'options' => $options,
103  'label-message' => 'pagelang-language',
104  'default' => $title ?
105  $title->getPageLanguage()->getCode() :
106  $this->getConfig()->get( 'LanguageCode' ),
107  ];
108 
109  // Allow user to enter a comment explaining the change
110  $page['reason'] = [
111  'type' => 'text',
112  'label-message' => 'pagelang-reason'
113  ];
114 
115  return $page;
116  }
117 
118  protected function postText() {
119  if ( $this->par ) {
120  return $this->showLogFragment( $this->par );
121  }
122  return '';
123  }
124 
125  protected function getDisplayFormat() {
126  return 'ooui';
127  }
128 
129  public function alterForm( HTMLForm $form ) {
130  $this->getHookRunner()->onLanguageSelector( $this->getOutput(), 'mw-languageselector' );
131  $form->setSubmitTextMsg( 'pagelang-submit' );
132  }
133 
138  public function onSubmit( array $data ) {
139  $pageName = $data['pagename'];
140 
141  // Check if user wants to use default language
142  if ( $data['selectoptions'] == 1 ) {
143  $newLanguage = 'default';
144  } else {
145  $newLanguage = $data['language'];
146  }
147 
148  try {
149  $title = Title::newFromTextThrow( $pageName );
150  } catch ( MalformedTitleException $ex ) {
151  return Status::newFatal( $ex->getMessageObject() );
152  }
153 
154  // Check permissions and make sure the user has permission to edit the page
155  $errors = MediaWikiServices::getInstance()->getPermissionManager()
156  ->getPermissionErrors( 'edit', $this->getUser(), $title );
157 
158  if ( $errors ) {
159  $out = $this->getOutput();
160  $wikitext = $out->formatPermissionsErrorMessage( $errors );
161  // Hack to get our wikitext parsed
162  return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
163  }
164 
165  // Url to redirect to after the operation
166  $this->goToUrl = $title->getFullUrlForRedirect(
167  $title->isRedirect() ? [ 'redirect' => 'no' ] : []
168  );
169 
171  $this->getContext(),
172  $title,
173  $newLanguage,
174  $data['reason'] ?? ''
175  );
176  }
177 
186  public static function changePageLanguage( IContextSource $context, Title $title,
187  $newLanguage, $reason, array $tags = [] ) {
188  // Get the default language for the wiki
189  $defLang = $context->getConfig()->get( 'LanguageCode' );
190 
191  $pageId = $title->getArticleID();
192 
193  // Check if article exists
194  if ( !$pageId ) {
195  return Status::newFatal(
196  'pagelang-nonexistent-page',
197  wfEscapeWikiText( $title->getPrefixedText() )
198  );
199  }
200 
201  // Load the page language from DB
202  $dbw = wfGetDB( DB_MASTER );
203  $oldLanguage = $dbw->selectField(
204  'page',
205  'page_lang',
206  [ 'page_id' => $pageId ],
207  __METHOD__
208  );
209 
210  // Check if user wants to use the default language
211  if ( $newLanguage === 'default' ) {
212  $newLanguage = null;
213  }
214 
215  // No change in language
216  if ( $newLanguage === $oldLanguage ) {
217  // Check if old language does not exist
218  if ( !$oldLanguage ) {
220  [
221  'pagelang-unchanged-language-default',
222  wfEscapeWikiText( $title->getPrefixedText() )
223  ],
224  'pagelang-unchanged-language'
225  ) );
226  }
227  return Status::newFatal(
228  'pagelang-unchanged-language',
229  wfEscapeWikiText( $title->getPrefixedText() ),
230  $oldLanguage
231  );
232  }
233 
234  // Hardcoded [def] if the language is set to null
235  $logOld = $oldLanguage ?: $defLang . '[def]';
236  $logNew = $newLanguage ?: $defLang . '[def]';
237 
238  // Writing new page language to database
239  $dbw->update(
240  'page',
241  [ 'page_lang' => $newLanguage ],
242  [
243  'page_id' => $pageId,
244  'page_lang' => $oldLanguage
245  ],
246  __METHOD__
247  );
248 
249  if ( !$dbw->affectedRows() ) {
250  return Status::newFatal( 'pagelang-db-failed' );
251  }
252 
253  // Logging change of language
254  $logParams = [
255  '4::oldlanguage' => $logOld,
256  '5::newlanguage' => $logNew
257  ];
258  $entry = new ManualLogEntry( 'pagelang', 'pagelang' );
259  $entry->setPerformer( $context->getUser() );
260  $entry->setTarget( $title );
261  $entry->setParameters( $logParams );
262  $entry->setComment( $reason );
263  $entry->addTags( $tags );
264 
265  $logid = $entry->insert();
266  $entry->publish( $logid );
267 
268  // Force re-render so that language-based content (parser functions etc.) gets updated
269  $title->invalidateCache();
270 
271  return Status::newGood( (object)[
272  'oldLanguage' => $logOld,
273  'newLanguage' => $logNew,
274  'logId' => $logid,
275  ] );
276  }
277 
278  public function onSuccess() {
279  // Success causes a redirect
280  $this->getOutput()->redirect( $this->goToUrl );
281  }
282 
283  private function showLogFragment( $title ) {
284  $moveLogPage = new LogPage( 'pagelang' );
285  $out1 = Xml::element( 'h2', null, $moveLogPage->getName()->text() );
286  $out2 = '';
287  LogEventsList::showLogExtract( $out2, 'pagelang', $title );
288  return $out1 . $out2;
289  }
290 
299  public function prefixSearchSubpages( $search, $limit, $offset ) {
300  return $this->prefixSearchString( $search, $limit, $offset );
301  }
302 
303  protected function getGroupName() {
304  return 'pagetools';
305  }
306 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
SpecialPageLanguage\postText
postText()
Add post-text to the form.
Definition: SpecialPageLanguage.php:118
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:157
SpecialPageLanguage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPageLanguage.php:303
MalformedTitleException\getMessageObject
getMessageObject()
Definition: MalformedTitleException.php:82
SpecialPageLanguage\onSubmit
onSubmit(array $data)
Definition: SpecialPageLanguage.php:138
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:43
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:774
SpecialPageLanguage\preText
preText()
Add pre-text to the form.
Definition: SpecialPageLanguage.php:47
SpecialPageLanguage\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialPageLanguage.php:129
SpecialPage\prefixSearchString
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
Definition: SpecialPage.php:534
SpecialPageLanguage\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialPageLanguage.php:125
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1011
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:794
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2469
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
$title
$title
Definition: testCompression.php:38
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:754
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:614
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:42
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:363
SpecialPageLanguage\$goToUrl
string $goToUrl
URL to go to if language change successful.
Definition: SpecialPageLanguage.php:37
SpecialPageLanguage\__construct
__construct()
Definition: SpecialPageLanguage.php:39
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
SpecialPageLanguage\showLogFragment
showLogFragment( $title)
Definition: SpecialPageLanguage.php:283
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1488
SpecialPageLanguage\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialPageLanguage.php:299
IContextSource\getUser
getUser()
SpecialPageLanguage\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialPageLanguage.php:278
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:1411
SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])
Definition: SpecialPageLanguage.php:186
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:26
SpecialPageLanguage
Special page for changing the content language of a page.
Definition: SpecialPageLanguage.php:33
IContextSource\getConfig
getConfig()
Get the site configuration.
SpecialPageLanguage\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialPageLanguage.php:52
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:42
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135