15use MediaWiki\Languages\LanguageNameUtils;
44private LanguageNameUtils $languageNameUtils;
50 LanguageNameUtils $languageNameUtils,
54 parent::__construct(
'PageLanguage',
'pagelang' );
55 $this->contentHandlerFactory = $contentHandlerFactory;
56 $this->languageNameUtils = $languageNameUtils;
57 $this->dbProvider = $dbProvider;
58 $this->searchEngineFactory = $searchEngineFactory;
68 $this->
getOutput()->addModules(
'mediawiki.misc-authed-ooui' );
69return parent::preHtml();
74// Get default from the subpage of Special page 76 $title = $defaultName ? Title::newFromText( $defaultName ) :
null;
78 $defaultPageLanguage = $this->contentHandlerFactory->getContentHandler( $title->getContentModel() )
79 ->getPageLanguage( $title );
81 $hasCustomLanguageSet = !$defaultPageLanguage->equals( $title->getPageLanguage() );
83 $hasCustomLanguageSet =
false;
89'label-message' =>
'pagelang-name',
90'default' => $title ? $title->getPrefixedText() : $defaultName,
91'autofocus' => $defaultName ===
null,
95// Options for whether to use the default language or select language 97 (string)$this->
msg(
'pagelang-use-default' )->escaped() => 1,
98 (string)$this->
msg(
'pagelang-select-lang' )->escaped() => 2,
100 $page[
'selectoptions'] = [
101'id' =>
'mw-pl-options',
103'options' => $selectoptions,
104'default' => $hasCustomLanguageSet ? 2 : 1
107// Building a language selector 109 $languages = $this->languageNameUtils->getLanguageNames( $userLang, LanguageNameUtils::SUPPORTED );
111foreach ( $languages as $code => $name ) {
112 $options[
"$code - $name"] = $code;
115 $page[
'language'] = [
116'id' =>
'mw-pl-languageselector',
117'cssclass' =>
'mw-languageselector',
119'options' => $options,
120'label-message' =>
'pagelang-language',
122 $title->getPageLanguage()->getCode() :
126// Allow user to enter a comment explaining the change 129'label-message' =>
'pagelang-reason' 138return $this->showLogFragment( $this->par );
158 $pageName = $data[
'pagename'];
160// Check if user wants to use default language 161if ( $data[
'selectoptions'] == 1 ) {
162 $newLanguage =
'default';
164 $newLanguage = $data[
'language'];
168 $title = Title::newFromTextThrow( $pageName );
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 ] ) );
181// Url to redirect to after the operation 182 $this->goToUrl = $title->getFullUrlForRedirect(
183 $title->isRedirect() ? [
'redirect' =>
'no' ] : []
190 $data[
'reason'] ??
'',
192 $this->dbProvider->getPrimaryDatabase()
208 $newLanguage, $reason =
"", array $tags = [], ?
IDatabase $dbw =
null ) {
209// Get the default language for the wiki 214// Check if article exists 216return Status::newFatal(
217'pagelang-nonexistent-page',
222// Load the page language from DB 224 $oldLanguage = $dbw->newSelectQueryBuilder()
225 ->select(
'page_lang' )
227 ->where( [
'page_id' => $pageId ] )
228 ->caller( __METHOD__ )->fetchField();
230// Check if user wants to use the default language 231if ( $newLanguage ===
'default' ) {
235// No change in language 236if ( $newLanguage === $oldLanguage ) {
237// Check if old language does not exist 238if ( !$oldLanguage ) {
239return Status::newFatal( ApiMessage::create(
241'pagelang-unchanged-language-default',
244'pagelang-unchanged-language' 247return Status::newFatal(
248'pagelang-unchanged-language',
254// Hardcoded [def] if the language is set to null 255 $logOld = $oldLanguage ?: $defLang .
'[def]';
256 $logNew = $newLanguage ?: $defLang .
'[def]';
258// Writing new page language to database 259 $dbw->newUpdateQueryBuilder()
261 ->set( [
'page_lang' => $newLanguage ] )
264'page_lang' => $oldLanguage,
266 ->caller( __METHOD__ )->execute();
268if ( !$dbw->affectedRows() ) {
269return Status::newFatal(
'pagelang-db-failed' );
272// Logging change of language 274'4::oldlanguage' => $logOld,
275'5::newlanguage' => $logNew
278 $entry->setPerformer( $context->
getUser() );
279 $entry->setTarget( $title );
280 $entry->setParameters( $logParams );
281 $entry->setComment( is_string( $reason ) ? $reason :
"" );
282 $entry->addTags( $tags );
284 $logid = $entry->insert();
285 $entry->publish( $logid );
287// Force re-render so that language-based content (parser functions etc.) gets updated 290return Status::newGood( (
object)[
291'oldLanguage' => $logOld,
292'newLanguage' => $logNew,
298// Success causes a redirect 299 $this->
getOutput()->redirect( $this->goToUrl );
302privatefunction showLogFragment(
string $title ): string {
303 $moveLogPage = new
LogPage(
'pagelang' );
304 $out1 =
Html::element(
'h2', [], $moveLogPage->getName()->text() );
306 LogEventsList::showLogExtract( $out2,
'pagelang', $title );
319return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
332class_alias( SpecialPageLanguage::class,
'SpecialPageLanguage' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Extension of Message implementing IApiMessage.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
This class is a collection of static functions that serve two purposes:
Variant of the Message class.
Class to simplify the use of log pages.
Class for creating new log entries and inserting them into the database.
A class containing constants representing the names of configuration variables.
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
A StatusValue for permission errors.
Special page which uses an HTMLForm to handle processing.
string null $par
The subpage of the special page.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
getLanguage()
Shortcut to get user's language.
Special page for changing the content language of a page.
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
__construct(IContentHandlerFactory $contentHandlerFactory, LanguageNameUtils $languageNameUtils, IConnectionProvider $dbProvider, SearchEngineFactory $searchEngineFactory)
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason="", array $tags=[], ?IDatabase $dbw=null)
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...
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki....
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
preHtml()
Add pre-HTML to the form.string HTML which will be sent to $form->addPreHtml() 1.38
postHtml()
Add post-HTML to the form.string HTML which will be sent to $form->addPostHtml() 1....
getFormFields()
Get an HTMLForm descriptor array.array
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Represents a title within MediaWiki.
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
invalidateCache( $purgeTime=null)
Updates page_touched for this page; called from LinksUpdate.php.
getPrefixedText()
Get the prefixed title with spaces.
Factory class for SearchEngine.
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
Provide primary and replica IDatabase connections.
Interface to a relational database.
element(SerializerNode $parent, SerializerNode $node, $contents)