MediaWiki master
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
25use ChangeTags;
32
41
42 private IConnectionProvider $dbProvider;
43 private LanguageNameUtils $languageNameUtils;
44
45 public function __construct(
46 ApiMain $mainModule,
47 string $moduleName,
48 IConnectionProvider $dbProvider,
49 LanguageNameUtils $languageNameUtils
50 ) {
51 parent::__construct( $mainModule, $moduleName );
52 $this->dbProvider = $dbProvider;
53 $this->languageNameUtils = $languageNameUtils;
54 }
55
56 // Check if change language feature is enabled
57 protected function getExtendedDescription() {
58 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
59 return 'apihelp-setpagelanguage-extended-description-disabled';
60 }
61 return parent::getExtendedDescription();
62 }
63
71 public function execute() {
72 // Check if change language feature is enabled
73 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
74 $this->dieWithError( 'apierror-pagelang-disabled' );
75 }
76
77 // Check if the user has permissions
78 $this->checkUserRightsAny( 'pagelang' );
79
81
83
84 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
85 $titleObj = $pageObj->getTitle();
86 $this->getErrorFormatter()->setContextTitle( $titleObj );
87 if ( !$pageObj->exists() ) {
88 $this->dieWithError( 'apierror-missingtitle' );
89 }
90
91 // Check that the user is allowed to edit the page
92 $this->checkTitleUserPermissions( $titleObj, 'edit' );
93
94 // If change tagging was requested, check that the user is allowed to tag,
95 // and the tags are valid
96 if ( $params['tags'] ) {
97 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
98 if ( !$tagStatus->isOK() ) {
99 $this->dieStatus( $tagStatus );
100 }
101 }
102
104 $this,
105 $titleObj,
106 $params['lang'],
107 $params['reason'] ?? '',
108 $params['tags'] ?: [],
109 $this->dbProvider->getPrimaryDatabase()
110 );
111
112 if ( !$status->isOK() ) {
113 $this->dieStatus( $status );
114 }
115
116 $r = [
117 'title' => $titleObj->getPrefixedText(),
118 'oldlanguage' => $status->value->oldLanguage,
119 'newlanguage' => $status->value->newLanguage,
120 'logid' => $status->value->logId
121 ];
122 $this->getResult()->addValue( null, $this->getModuleName(), $r );
123 }
124
125 public function mustBePosted() {
126 return true;
127 }
128
129 public function isWriteMode() {
130 return true;
131 }
132
133 public function getAllowedParams() {
134 return [
135 'title' => null,
136 'pageid' => [
137 ParamValidator::PARAM_TYPE => 'integer'
138 ],
139 'lang' => [
140 ParamValidator::PARAM_TYPE => array_merge(
141 [ 'default' ],
142 array_keys( $this->languageNameUtils->getLanguageNames(
143 LanguageNameUtils::AUTONYMS,
144 LanguageNameUtils::SUPPORTED
145 ) )
146 ),
147 ParamValidator::PARAM_REQUIRED => true,
148 ],
149 'reason' => null,
150 'tags' => [
151 ParamValidator::PARAM_TYPE => 'tags',
152 ParamValidator::PARAM_ISMULTI => true,
153 ],
154 ];
155 }
156
157 public function needsToken() {
158 return 'csrf';
159 }
160
161 protected function getExamplesMessages() {
162 $title = Title::newMainPage()->getPrefixedText();
163 $mp = rawurlencode( $title );
164
165 return [
166 "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
167 => 'apihelp-setpagelanguage-example-language',
168 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
169 => 'apihelp-setpagelanguage-example-default',
170 ];
171 }
172
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
175 }
176}
177
179class_alias( ApiSetPageLanguage::class, 'ApiSetPageLanguage' );
array $params
The job parameters.
Recent changes tagging.
static canAddTagsAccompanyingChange(array $tags, ?Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1680
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1413
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1703
getTitleOrPageId( $params, $load=false)
Attempts to load a WikiPage object from a title or pageid parameter, if possible.
Definition ApiBase.php:1172
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
API module that facilitates changing the language of a page.
getExtendedDescription()
Return the extended help text message.
needsToken()
Returns the token type this module requires in order to execute.
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.
getExamplesMessages()
Returns usage examples for this module.
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
__construct(ApiMain $mainModule, string $moduleName, IConnectionProvider $dbProvider, LanguageNameUtils $languageNameUtils)
mustBePosted()
Indicates whether this module must be called with a POST request.
getHelpUrls()
Return links to more detailed help pages about the module.
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:78
Service for formatting and validating API parameters.
Provide primary and replica IDatabase connections.