Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
SpecialPageLanguage.php
Go to the documentation of this file.
1<?php
7namespaceMediaWiki\Specials;
8
9useMediaWiki\Api\ApiMessage;
10useMediaWiki\Content\IContentHandlerFactory;
11useMediaWiki\Context\IContextSource;
12useMediaWiki\Html\Html;
13useMediaWiki\HTMLForm\HTMLForm;
14useMediaWiki\Language\RawMessage;
15use MediaWiki\Languages\LanguageNameUtils;
16useMediaWiki\Logging\LogEventsList;
17useMediaWiki\Logging\LogPage;
18useMediaWiki\Logging\ManualLogEntry;
19useMediaWiki\MainConfigNames;
20useMediaWiki\MediaWikiServices;
21useMediaWiki\Permissions\PermissionStatus;
22useMediaWiki\SpecialPage\FormSpecialPage;
23useMediaWiki\Status\Status;
24useMediaWiki\Title\MalformedTitleException;
25useMediaWiki\Title\Title;
26useSearchEngineFactory;
27useWikimedia\Rdbms\IConnectionProvider;
28useWikimedia\Rdbms\IDatabase;
29
37classSpecialPageLanguageextendsFormSpecialPage {
41private $goToUrl;
42
43privateIContentHandlerFactory $contentHandlerFactory;
44private LanguageNameUtils $languageNameUtils;
45privateIConnectionProvider $dbProvider;
46privateSearchEngineFactory $searchEngineFactory;
47
48publicfunction__construct(
49IContentHandlerFactory $contentHandlerFactory,
50 LanguageNameUtils $languageNameUtils,
51IConnectionProvider $dbProvider,
52SearchEngineFactory $searchEngineFactory
53 ) {
54 parent::__construct('PageLanguage','pagelang' );
55 $this->contentHandlerFactory = $contentHandlerFactory;
56 $this->languageNameUtils = $languageNameUtils;
57 $this->dbProvider = $dbProvider;
58 $this->searchEngineFactory = $searchEngineFactory;
59 }
60
62publicfunctiondoesWrites() {
63returntrue;
64 }
65
67protectedfunctionpreHtml() {
68 $this->getOutput()->addModules('mediawiki.misc-authed-ooui' );
69return parent::preHtml();
70 }
71
73protectedfunctiongetFormFields() {
74// Get default from the subpage of Special page
75 $defaultName =$this->par;
76 $title = $defaultName ? Title::newFromText( $defaultName ) :null;
77if ( $title ) {
78 $defaultPageLanguage = $this->contentHandlerFactory->getContentHandler( $title->getContentModel() )
79 ->getPageLanguage( $title );
80
81 $hasCustomLanguageSet = !$defaultPageLanguage->equals( $title->getPageLanguage() );
82 }else {
83 $hasCustomLanguageSet =false;
84 }
85
86 $page = [];
87 $page['pagename'] = [
88'type' =>'title',
89'label-message' =>'pagelang-name',
90'default' => $title ? $title->getPrefixedText() : $defaultName,
91'autofocus' => $defaultName ===null,
92'exists' =>true,
93 ];
94
95// Options for whether to use the default language or select language
96 $selectoptions = [
97 (string)$this->msg('pagelang-use-default' )->escaped() => 1,
98 (string)$this->msg('pagelang-select-lang' )->escaped() => 2,
99 ];
100 $page['selectoptions'] = [
101'id' =>'mw-pl-options',
102'type' =>'radio',
103'options' => $selectoptions,
104'default' => $hasCustomLanguageSet ? 2 : 1
105 ];
106
107// Building a language selector
108 $userLang = $this->getLanguage()->getCode();
109 $languages = $this->languageNameUtils->getLanguageNames( $userLang, LanguageNameUtils::SUPPORTED );
110 $options = [];
111foreach ( $languages as $code => $name ) {
112 $options["$code - $name"] = $code;
113 }
114
115 $page['language'] = [
116'id' =>'mw-pl-languageselector',
117'cssclass' =>'mw-languageselector',
118'type' =>'select',
119'options' => $options,
120'label-message' =>'pagelang-language',
121'default' => $title ?
122 $title->getPageLanguage()->getCode() :
124 ];
125
126// Allow user to enter a comment explaining the change
127 $page['reason'] = [
128'type' =>'text',
129'label-message' =>'pagelang-reason'
130 ];
131
132return $page;
133 }
134
136protectedfunctionpostHtml() {
137if ( $this->par ) {
138return $this->showLogFragment( $this->par );
139 }
140return'';
141 }
142
144protectedfunctiongetDisplayFormat() {
145return'ooui';
146 }
147
148publicfunctionalterForm(HTMLForm $form ) {
149 $this->getHookRunner()->onLanguageSelector( $this->getOutput(),'mw-languageselector' );
150 $form->setSubmitTextMsg('pagelang-submit' );
151 }
152
157publicfunctiononSubmit( array $data ) {
158 $pageName = $data['pagename'];
159
160// Check if user wants to use default language
161if ( $data['selectoptions'] == 1 ) {
162 $newLanguage ='default';
163 }else {
164 $newLanguage = $data['language'];
165 }
166
167try {
168 $title = Title::newFromTextThrow( $pageName );
169 }catch (MalformedTitleException $ex ) {
170return Status::newFatal( $ex->getMessageObject() );
171 }
172
173// Check permissions and make sure the user has permission to edit the page
174 $status = PermissionStatus::newEmpty();
175if ( !$this->getAuthority()->authorizeWrite('edit', $title, $status ) ) {
176 $wikitext = $this->getOutput()->formatPermissionStatus( $status );
177// Hack to get our wikitext parsed
178return Status::newFatal(newRawMessage('$1', [ $wikitext ] ) );
179 }
180
181// Url to redirect to after the operation
182 $this->goToUrl = $title->getFullUrlForRedirect(
183 $title->isRedirect() ? ['redirect' =>'no' ] : []
184 );
185
187 $this->getContext(),
188 $title,
189 $newLanguage,
190 $data['reason'] ??'',
191 [],
192 $this->dbProvider->getPrimaryDatabase()
193 );
194 }
195
207publicstaticfunctionchangePageLanguage(IContextSource $context,Title $title,
208 $newLanguage, $reason ="", array $tags = [], ?IDatabase $dbw =null ) {
209// Get the default language for the wiki
210 $defLang = $context->getConfig()->get(MainConfigNames::LanguageCode );
211
212 $pageId = $title->getArticleID();
213
214// Check if article exists
215if ( !$pageId ) {
216return Status::newFatal(
217'pagelang-nonexistent-page',
219 );
220 }
221
222// Load the page language from DB
223 $dbw ??=MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
224 $oldLanguage = $dbw->newSelectQueryBuilder()
225 ->select('page_lang' )
226 ->from('page' )
227 ->where( ['page_id' => $pageId ] )
228 ->caller( __METHOD__ )->fetchField();
229
230// Check if user wants to use the default language
231if ( $newLanguage ==='default' ) {
232 $newLanguage =null;
233 }
234
235// No change in language
236if ( $newLanguage === $oldLanguage ) {
237// Check if old language does not exist
238if ( !$oldLanguage ) {
239return Status::newFatal( ApiMessage::create(
240 [
241'pagelang-unchanged-language-default',
243 ],
244'pagelang-unchanged-language'
245 ) );
246 }
247return Status::newFatal(
248'pagelang-unchanged-language',
250 $oldLanguage
251 );
252 }
253
254// Hardcoded [def] if the language is set to null
255 $logOld = $oldLanguage ?: $defLang .'[def]';
256 $logNew = $newLanguage ?: $defLang .'[def]';
257
258// Writing new page language to database
259 $dbw->newUpdateQueryBuilder()
260 ->update('page' )
261 ->set( ['page_lang' => $newLanguage ] )
262 ->where( [
263'page_id' => $pageId,
264'page_lang' => $oldLanguage,
265 ] )
266 ->caller( __METHOD__ )->execute();
267
268if ( !$dbw->affectedRows() ) {
269return Status::newFatal('pagelang-db-failed' );
270 }
271
272// Logging change of language
273 $logParams = [
274'4::oldlanguage' => $logOld,
275'5::newlanguage' => $logNew
276 ];
277 $entry =newManualLogEntry('pagelang','pagelang' );
278 $entry->setPerformer( $context->getUser() );
279 $entry->setTarget( $title );
280 $entry->setParameters( $logParams );
281 $entry->setComment( is_string( $reason ) ? $reason :"" );
282 $entry->addTags( $tags );
283
284 $logid = $entry->insert();
285 $entry->publish( $logid );
286
287// Force re-render so that language-based content (parser functions etc.) gets updated
288 $title->invalidateCache();
289
290return Status::newGood( (object)[
291'oldLanguage' => $logOld,
292'newLanguage' => $logNew,
293'logId' => $logid,
294 ] );
295 }
296
297publicfunctiononSuccess() {
298// Success causes a redirect
299 $this->getOutput()->redirect( $this->goToUrl );
300 }
301
302privatefunction showLogFragment(string $title ): string {
303 $moveLogPage = newLogPage('pagelang' );
304 $out1 =Html::element('h2', [], $moveLogPage->getName()->text() );
305 $out2 ='';
306 LogEventsList::showLogExtract( $out2,'pagelang', $title );
307return $out1 . $out2;
308 }
309
318publicfunctionprefixSearchSubpages( $search, $limit, $offset ) {
319return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
320 }
321
323protectedfunctiongetGroupName() {
324return'pagetools';
325 }
326}
327
332class_alias( SpecialPageLanguage::class,'SpecialPageLanguage' );
wfEscapeWikiText
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
DefinitionGlobalFunctions.php:1060
MediaWiki\Api\ApiMessage
Extension of Message implementing IApiMessage.
DefinitionApiMessage.php:19
MediaWiki\HTMLForm\HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
DefinitionHTMLForm.php:195
MediaWiki\HTMLForm\HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
DefinitionHTMLForm.php:1540
MediaWiki\Html\Html
This class is a collection of static functions that serve two purposes:
DefinitionHtml.php:43
MediaWiki\Language\RawMessage
Variant of the Message class.
DefinitionRawMessage.php:25
MediaWiki\Logging\LogEventsList
DefinitionLogEventsList.php:52
MediaWiki\Logging\LogPage
Class to simplify the use of log pages.
DefinitionLogPage.php:35
MediaWiki\Logging\ManualLogEntry
Class for creating new log entries and inserting them into the database.
DefinitionManualLogEntry.php:41
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
DefinitionMainConfigNames.php:22
MediaWiki\MainConfigNames\LanguageCode
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
DefinitionMainConfigNames.php:1802
MediaWiki\MediaWikiServices
Service locator for MediaWiki core services.
DefinitionMediaWikiServices.php:256
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
DefinitionMediaWikiServices.php:344
MediaWiki\Permissions\PermissionStatus
A StatusValue for permission errors.
DefinitionPermissionStatus.php:28
MediaWiki\SpecialPage\FormSpecialPage
Special page which uses an HTMLForm to handle processing.
DefinitionFormSpecialPage.php:26
MediaWiki\SpecialPage\FormSpecialPage\$par
string null $par
The subpage of the special page.
DefinitionFormSpecialPage.php:31
MediaWiki\SpecialPage\SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
DefinitionSpecialPage.php:949
MediaWiki\SpecialPage\SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
DefinitionSpecialPage.php:848
MediaWiki\SpecialPage\SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
DefinitionSpecialPage.php:985
MediaWiki\SpecialPage\SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
DefinitionSpecialPage.php:874
MediaWiki\SpecialPage\SpecialPage\getAuthority
getAuthority()
Shortcut to get the Authority executing this instance.
DefinitionSpecialPage.php:894
MediaWiki\SpecialPage\SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
DefinitionSpecialPage.php:914
MediaWiki\SpecialPage\SpecialPage\getHookRunner
getHookRunner()
DefinitionSpecialPage.php:1194
MediaWiki\Specials\SpecialPageLanguage
Special page for changing the content language of a page.
DefinitionSpecialPageLanguage.php:37
MediaWiki\Specials\SpecialPageLanguage\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
DefinitionSpecialPageLanguage.php:144
MediaWiki\Specials\SpecialPageLanguage\__construct
__construct(IContentHandlerFactory $contentHandlerFactory, LanguageNameUtils $languageNameUtils, IConnectionProvider $dbProvider, SearchEngineFactory $searchEngineFactory)
DefinitionSpecialPageLanguage.php:48
MediaWiki\Specials\SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason="", array $tags=[], ?IDatabase $dbw=null)
DefinitionSpecialPageLanguage.php:207
MediaWiki\Specials\SpecialPageLanguage\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
DefinitionSpecialPageLanguage.php:297
MediaWiki\Specials\SpecialPageLanguage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
DefinitionSpecialPageLanguage.php:323
MediaWiki\Specials\SpecialPageLanguage\doesWrites
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki....
DefinitionSpecialPageLanguage.php:62
MediaWiki\Specials\SpecialPageLanguage\onSubmit
onSubmit(array $data)
DefinitionSpecialPageLanguage.php:157
MediaWiki\Specials\SpecialPageLanguage\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
DefinitionSpecialPageLanguage.php:148
MediaWiki\Specials\SpecialPageLanguage\preHtml
preHtml()
Add pre-HTML to the form.string HTML which will be sent to $form->addPreHtml() 1.38
DefinitionSpecialPageLanguage.php:67
MediaWiki\Specials\SpecialPageLanguage\postHtml
postHtml()
Add post-HTML to the form.string HTML which will be sent to $form->addPostHtml() 1....
DefinitionSpecialPageLanguage.php:136
MediaWiki\Specials\SpecialPageLanguage\getFormFields
getFormFields()
Get an HTMLForm descriptor array.array
DefinitionSpecialPageLanguage.php:73
MediaWiki\Specials\SpecialPageLanguage\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
DefinitionSpecialPageLanguage.php:318
MediaWiki\Status\Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
DefinitionStatus.php:44
MediaWiki\Title\MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
DefinitionMalformedTitleException.php:18
MediaWiki\Title\MalformedTitleException\getMessageObject
getMessageObject()
DefinitionMalformedTitleException.php:78
MediaWiki\Title\Title
Represents a title within MediaWiki.
DefinitionTitle.php:69
MediaWiki\Title\Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
DefinitionTitle.php:2558
MediaWiki\Title\Title\invalidateCache
invalidateCache( $purgeTime=null)
Updates page_touched for this page; called from LinksUpdate.php.
DefinitionTitle.php:3298
MediaWiki\Title\Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
DefinitionTitle.php:1857
SearchEngineFactory
Factory class for SearchEngine.
DefinitionSearchEngineFactory.php:11
MediaWiki\Content\IContentHandlerFactory
DefinitionIContentHandlerFactory.php:10
MediaWiki\Context\IContextSource
Interface for objects which can provide a MediaWiki context on request.
DefinitionIContextSource.php:57
MediaWiki\Context\IContextSource\getUser
getUser()
MediaWiki\Context\IContextSource\getConfig
getConfig()
Get the site configuration.
Wikimedia\Rdbms\IConnectionProvider
Provide primary and replica IDatabase connections.
DefinitionIConnectionProvider.php:21
Wikimedia\Rdbms\IDatabase
Interface to a relational database.
DefinitionIDatabase.php:31
MediaWiki\Html\element
element(SerializerNode $parent, SerializerNode $node, $contents)
DefinitionHtmlHelperTrait.php:38
MediaWiki\Specials

[8]ページ先頭

©2009-2025 Movatter.jp