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