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
51 public function __construct(
52 ApiMain $mainModule,
53 $moduleName,
54 IConnectionProvider $dbProvider,
55 LanguageNameUtils $languageNameUtils
56 ) {
57 parent::__construct( $mainModule, $moduleName );
58 $this->dbProvider = $dbProvider;
59 $this->languageNameUtils = $languageNameUtils;
60 }
61
62 // Check if change language feature is enabled
63 protected function getExtendedDescription() {
64 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
65 return 'apihelp-setpagelanguage-extended-description-disabled';
66 }
67 return parent::getExtendedDescription();
68 }
69
77 public function execute() {
78 // Check if change language feature is enabled
79 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
80 $this->dieWithError( 'apierror-pagelang-disabled' );
81 }
82
83 // Check if the user has permissions
84 $this->checkUserRightsAny( 'pagelang' );
85
87
89
90 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
91 $titleObj = $pageObj->getTitle();
92 $this->getErrorFormatter()->setContextTitle( $titleObj );
93 if ( !$pageObj->exists() ) {
94 $this->dieWithError( 'apierror-missingtitle' );
95 }
96
97 // Check that the user is allowed to edit the page
98 $this->checkTitleUserPermissions( $titleObj, 'edit' );
99
100 // If change tagging was requested, check that the user is allowed to tag,
101 // and the tags are valid
102 if ( $params['tags'] ) {
103 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
104 if ( !$tagStatus->isOK() ) {
105 $this->dieStatus( $tagStatus );
106 }
107 }
108
110 $this,
111 $titleObj,
112 $params['lang'],
113 $params['reason'] ?? '',
114 $params['tags'] ?: [],
115 $this->dbProvider->getPrimaryDatabase()
116 );
117
118 if ( !$status->isOK() ) {
119 $this->dieStatus( $status );
120 }
121
122 $r = [
123 'title' => $titleObj->getPrefixedText(),
124 'oldlanguage' => $status->value->oldLanguage,
125 'newlanguage' => $status->value->newLanguage,
126 'logid' => $status->value->logId
127 ];
128 $this->getResult()->addValue( null, $this->getModuleName(), $r );
129 }
130
131 public function mustBePosted() {
132 return true;
133 }
134
135 public function isWriteMode() {
136 return true;
137 }
138
139 public function getAllowedParams() {
140 return [
141 'title' => null,
142 'pageid' => [
143 ParamValidator::PARAM_TYPE => 'integer'
144 ],
145 'lang' => [
146 ParamValidator::PARAM_TYPE => array_merge(
147 [ 'default' ],
148 array_keys( $this->languageNameUtils->getLanguageNames(
149 LanguageNameUtils::AUTONYMS,
150 LanguageNameUtils::SUPPORTED
151 ) )
152 ),
153 ParamValidator::PARAM_REQUIRED => true,
154 ],
155 'reason' => null,
156 'tags' => [
157 ParamValidator::PARAM_TYPE => 'tags',
158 ParamValidator::PARAM_ISMULTI => true,
159 ],
160 ];
161 }
162
163 public function needsToken() {
164 return 'csrf';
165 }
166
167 protected function getExamplesMessages() {
168 $title = Title::newMainPage()->getPrefixedText();
169 $mp = rawurlencode( $title );
170
171 return [
172 "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
173 => 'apihelp-setpagelanguage-example-language',
174 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
175 => 'apihelp-setpagelanguage-example-default',
176 ];
177 }
178
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
181 }
182}
183
185class_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:1577
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1692
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:1425
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1632
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:1715
getTitleOrPageId( $params, $load=false)
Attempts to load a WikiPage object from a title or pageid parameter, if possible.
Definition ApiBase.php:1138
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, $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.