MediaWiki master
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
18
27
28 public function __construct(
29 ApiMain $mainModule,
30 string $moduleName,
31 private readonly IConnectionProvider $dbProvider,
32 private readonly LanguageNameUtils $languageNameUtils,
33 ) {
34 parent::__construct( $mainModule, $moduleName );
35 }
36
38 protected function getExtendedDescription() {
39 // Check if change language feature is enabled
40 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
41 return 'apihelp-setpagelanguage-extended-description-disabled';
42 }
43 return parent::getExtendedDescription();
44 }
45
53 public function execute() {
54 // Check if change language feature is enabled
55 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
56 $this->dieWithError( 'apierror-pagelang-disabled' );
57 }
58
59 // Check if the user has permissions
60 $this->checkUserRightsAny( 'pagelang' );
61
63
64 $params = $this->extractRequestParams();
65
66 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
67 $titleObj = $pageObj->getTitle();
68 $this->getErrorFormatter()->setContextTitle( $titleObj );
69 if ( !$pageObj->exists() ) {
70 $this->dieWithError( 'apierror-missingtitle' );
71 }
72
73 // Check that the user is allowed to edit the page
74 $this->checkTitleUserPermissions( $titleObj, 'edit' );
75
76 // If change tagging was requested, check that the user is allowed to tag,
77 // and the tags are valid
78 if ( $params['tags'] ) {
79 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
80 if ( !$tagStatus->isOK() ) {
81 $this->dieStatus( $tagStatus );
82 }
83 }
84
86 $this,
87 $titleObj,
88 $params['lang'],
89 $params['reason'] ?? '',
90 $params['tags'] ?: [],
91 $this->dbProvider->getPrimaryDatabase()
92 );
93
94 if ( !$status->isOK() ) {
95 $this->dieStatus( $status );
96 }
97
98 $r = [
99 'title' => $titleObj->getPrefixedText(),
100 'oldlanguage' => $status->value->oldLanguage,
101 'newlanguage' => $status->value->newLanguage,
102 'logid' => $status->value->logId
103 ];
104 $this->getResult()->addValue( null, $this->getModuleName(), $r );
105 }
106
108 public function mustBePosted() {
109 return true;
110 }
111
113 public function isWriteMode() {
114 return true;
115 }
116
118 public function getAllowedParams() {
119 return [
120 'title' => null,
121 'pageid' => [
122 ParamValidator::PARAM_TYPE => 'integer'
123 ],
124 'lang' => [
125 ParamValidator::PARAM_TYPE => [
126 'default',
127 ...array_keys( $this->languageNameUtils->getLanguageNames(
128 LanguageNameUtils::AUTONYMS,
129 LanguageNameUtils::SUPPORTED
130 ) )
131 ],
132 ParamValidator::PARAM_REQUIRED => true,
133 ],
134 'reason' => null,
135 'tags' => [
136 ParamValidator::PARAM_TYPE => 'tags',
137 ParamValidator::PARAM_ISMULTI => true,
138 ],
139 ];
140 }
141
143 public function needsToken() {
144 return 'csrf';
145 }
146
148 protected function getExamplesMessages() {
149 $title = Title::newMainPage()->getPrefixedText();
150 $mp = rawurlencode( $title );
151
152 return [
153 "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
154 => 'apihelp-setpagelanguage-example-language',
155 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
156 => 'apihelp-setpagelanguage-example-default',
157 ];
158 }
159
161 public function getHelpUrls() {
162 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
163 }
164}
165
167class_alias( ApiSetPageLanguage::class, 'ApiSetPageLanguage' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1631
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1369
getResult()
Get the result object.
Definition ApiBase.php:696
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1654
getTitleOrPageId( $params, $load=false)
Attempts to load a WikiPage object from a title or pageid parameter, if possible.
Definition ApiBase.php:1161
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
API module that facilitates changing the language of a page.
getExtendedDescription()
Return the extended help text message.This is additional text to display at the top of the help secti...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
__construct(ApiMain $mainModule, string $moduleName, private readonly IConnectionProvider $dbProvider, private readonly LanguageNameUtils $languageNameUtils,)
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
Recent changes tagging.
A service that provides utilities to do with language names and codes.
A class containing constants representing the names of configuration variables.
const PageLanguageUseDB
Name constant for the PageLanguageUseDB setting, for use with Config::get()
Special page for changing the content language of a page.
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason="", array $tags=[], ?IDatabase $dbw=null)
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Provide primary and replica IDatabase connections.