MediaWiki REL1_39
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
27
36
38 private $loadBalancer;
39
41 private $languageNameUtils;
42
49 public function __construct(
50 ApiMain $mainModule,
51 $moduleName,
52 ILoadBalancer $loadBalancer,
53 LanguageNameUtils $languageNameUtils
54 ) {
55 parent::__construct( $mainModule, $moduleName );
56 $this->loadBalancer = $loadBalancer;
57 $this->languageNameUtils = $languageNameUtils;
58 }
59
60 // Check if change language feature is enabled
61 protected function getExtendedDescription() {
62 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
63 return 'apihelp-setpagelanguage-extended-description-disabled';
64 }
65 return parent::getExtendedDescription();
66 }
67
75 public function execute() {
76 // Check if change language feature is enabled
77 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
78 $this->dieWithError( 'apierror-pagelang-disabled' );
79 }
80
81 // Check if the user has permissions
82 $this->checkUserRightsAny( 'pagelang' );
83
85
86 $params = $this->extractRequestParams();
87
88 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
89 $titleObj = $pageObj->getTitle();
90 $this->getErrorFormatter()->setContextTitle( $titleObj );
91 if ( !$pageObj->exists() ) {
92 $this->dieWithError( 'apierror-missingtitle' );
93 }
94
95 // Check that the user is allowed to edit the page
96 $this->checkTitleUserPermissions( $titleObj, 'edit' );
97
98 // If change tagging was requested, check that the user is allowed to tag,
99 // and the tags are valid
100 if ( $params['tags'] ) {
101 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
102 if ( !$tagStatus->isOK() ) {
103 $this->dieStatus( $tagStatus );
104 }
105 }
106
108 $this,
109 $titleObj,
110 $params['lang'],
111 $params['reason'] ?? '',
112 $params['tags'] ?: [],
113 $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_PRIMARY )
114 );
115
116 if ( !$status->isOK() ) {
117 $this->dieStatus( $status );
118 }
119
120 $r = [
121 'title' => $titleObj->getPrefixedText(),
122 'oldlanguage' => $status->value->oldLanguage,
123 'newlanguage' => $status->value->newLanguage,
124 'logid' => $status->value->logId
125 ];
126 $this->getResult()->addValue( null, $this->getModuleName(), $r );
127 }
128
129 public function mustBePosted() {
130 return true;
131 }
132
133 public function isWriteMode() {
134 return true;
135 }
136
137 public function getAllowedParams() {
138 return [
139 'title' => null,
140 'pageid' => [
141 ParamValidator::PARAM_TYPE => 'integer'
142 ],
143 'lang' => [
144 ParamValidator::PARAM_TYPE => array_merge(
145 [ 'default' ],
146 array_keys( $this->languageNameUtils->getLanguageNames(
147 LanguageNameUtils::AUTONYMS,
148 LanguageNameUtils::SUPPORTED
149 ) )
150 ),
151 ParamValidator::PARAM_REQUIRED => true,
152 ],
153 'reason' => null,
154 'tags' => [
155 ParamValidator::PARAM_TYPE => 'tags',
156 ParamValidator::PARAM_ISMULTI => true,
157 ],
158 ];
159 }
160
161 public function needsToken() {
162 return 'csrf';
163 }
164
165 protected function getExamplesMessages() {
166 $title = Title::newMainPage()->getPrefixedText();
167 $mp = rawurlencode( $title );
168
169 return [
170 "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
171 => 'apihelp-setpagelanguage-example-language',
172 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
173 => 'apihelp-setpagelanguage-example-default',
174 ];
175 }
176
177 public function getHelpUrls() {
178 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
179 }
180}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1560
getErrorFormatter()
Definition ApiBase.php:640
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1586
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1036
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1515
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1299
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
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, ILoadBalancer $loadBalancer, 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.
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason="", array $tags=[], IDatabase $dbw=null)
Service for formatting and validating API parameters.
Create and track the database connections and transactions for a given database cluster.