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