MediaWiki REL1_37
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
25
34
37
40
47 public function __construct(
48 ApiMain $mainModule,
49 $moduleName,
52 ) {
53 parent::__construct( $mainModule, $moduleName );
54 $this->loadBalancer = $loadBalancer;
55 $this->languageNameUtils = $languageNameUtils;
56 }
57
58 // Check if change language feature is enabled
59 protected function getExtendedDescription() {
60 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
61 return 'apihelp-setpagelanguage-extended-description-disabled';
62 }
63 return parent::getExtendedDescription();
64 }
65
73 public function execute() {
74 // Check if change language feature is enabled
75 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
76 $this->dieWithError( 'apierror-pagelang-disabled' );
77 }
78
79 // Check if the user has permissions
80 $this->checkUserRightsAny( 'pagelang' );
81
83
84 $params = $this->extractRequestParams();
85
86 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
87 $titleObj = $pageObj->getTitle();
88 $this->getErrorFormatter()->setContextTitle( $titleObj );
89 if ( !$pageObj->exists() ) {
90 $this->dieWithError( 'apierror-missingtitle' );
91 }
92
93 $user = $this->getUser();
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 ApiBase::PARAM_TYPE => 'integer'
142 ],
143 'lang' => [
144 ApiBase::PARAM_TYPE => array_merge(
145 [ 'default' ],
146 array_keys( $this->languageNameUtils->getLanguageNames( null, 'mwfile' ) )
147 ),
149 ],
150 'reason' => null,
151 'tags' => [
152 ApiBase::PARAM_TYPE => 'tags',
154 ],
155 ];
156 }
157
158 public function needsToken() {
159 return 'csrf';
160 }
161
162 protected function getExamplesMessages() {
163 return [
164 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
165 => 'apihelp-setpagelanguage-example-language',
166 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
167 => 'apihelp-setpagelanguage-example-default',
168 ];
169 }
170
171 public function getHelpUrls() {
172 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
173 }
174}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1539
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1565
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1033
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1293
const PARAM_ISMULTI
Definition ApiBase.php:77
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
API module that facilitates changing the language of a page.
needsToken()
Returns the token type this module requires in order to execute.
ILoadBalancer $loadBalancer
getExtendedDescription()
Return the extended help text message.
isWriteMode()
Indicates whether this module requires write mode.
LanguageNameUtils $languageNameUtils
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)
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.
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[], IDatabase $dbw=null)
Database cluster connection, tracking, load balancing, and transaction manager interface.