MediaWiki master
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
12use MediaWiki\Languages\LanguageNameUtils;
18
27
28 private IConnectionProvider $dbProvider;
29 private LanguageNameUtils $languageNameUtils;
30
31 public function __construct(
32 ApiMain $mainModule,
33 string $moduleName,
34 IConnectionProvider $dbProvider,
35 LanguageNameUtils $languageNameUtils
36 ) {
37 parent::__construct( $mainModule, $moduleName );
38 $this->dbProvider = $dbProvider;
39 $this->languageNameUtils = $languageNameUtils;
40 }
41
43 protected function getExtendedDescription() {
44 // Check if change language feature is enabled
45 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
46 return 'apihelp-setpagelanguage-extended-description-disabled';
47 }
48 return parent::getExtendedDescription();
49 }
50
58 public function execute() {
59 // Check if change language feature is enabled
60 if ( !$this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) {
61 $this->dieWithError( 'apierror-pagelang-disabled' );
62 }
63
64 // Check if the user has permissions
65 $this->checkUserRightsAny( 'pagelang' );
66
68
69 $params = $this->extractRequestParams();
70
71 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
72 $titleObj = $pageObj->getTitle();
73 $this->getErrorFormatter()->setContextTitle( $titleObj );
74 if ( !$pageObj->exists() ) {
75 $this->dieWithError( 'apierror-missingtitle' );
76 }
77
78 // Check that the user is allowed to edit the page
79 $this->checkTitleUserPermissions( $titleObj, 'edit' );
80
81 // If change tagging was requested, check that the user is allowed to tag,
82 // and the tags are valid
83 if ( $params['tags'] ) {
84 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
85 if ( !$tagStatus->isOK() ) {
86 $this->dieStatus( $tagStatus );
87 }
88 }
89
91 $this,
92 $titleObj,
93 $params['lang'],
94 $params['reason'] ?? '',
95 $params['tags'] ?: [],
96 $this->dbProvider->getPrimaryDatabase()
97 );
98
99 if ( !$status->isOK() ) {
100 $this->dieStatus( $status );
101 }
102
103 $r = [
104 'title' => $titleObj->getPrefixedText(),
105 'oldlanguage' => $status->value->oldLanguage,
106 'newlanguage' => $status->value->newLanguage,
107 'logid' => $status->value->logId
108 ];
109 $this->getResult()->addValue( null, $this->getModuleName(), $r );
110 }
111
113 public function mustBePosted() {
114 return true;
115 }
116
118 public function isWriteMode() {
119 return true;
120 }
121
123 public function getAllowedParams() {
124 return [
125 'title' => null,
126 'pageid' => [
127 ParamValidator::PARAM_TYPE => 'integer'
128 ],
129 'lang' => [
130 ParamValidator::PARAM_TYPE => [
131 'default',
132 ...array_keys( $this->languageNameUtils->getLanguageNames(
133 LanguageNameUtils::AUTONYMS,
134 LanguageNameUtils::SUPPORTED
135 ) )
136 ],
137 ParamValidator::PARAM_REQUIRED => true,
138 ],
139 'reason' => null,
140 'tags' => [
141 ParamValidator::PARAM_TYPE => 'tags',
142 ParamValidator::PARAM_ISMULTI => true,
143 ],
144 ];
145 }
146
148 public function needsToken() {
149 return 'csrf';
150 }
151
153 protected function getExamplesMessages() {
154 $title = Title::newMainPage()->getPrefixedText();
155 $mp = rawurlencode( $title );
156
157 return [
158 "action=setpagelanguage&title={$mp}&lang=eu&token=123ABC"
159 => 'apihelp-setpagelanguage-example-language',
160 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
161 => 'apihelp-setpagelanguage-example-default',
162 ];
163 }
164
166 public function getHelpUrls() {
167 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
168 }
169}
170
172class_alias( ApiSetPageLanguage::class, 'ApiSetPageLanguage' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1620
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1643
getTitleOrPageId( $params, $load=false)
Attempts to load a WikiPage object from a title or pageid parameter, if possible.
Definition ApiBase.php:1147
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
API module that facilitates changing the language of a page.
getExtendedDescription()
Return the extended help text message.This is additional text to display at the top of the help secti...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
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.API modules must override this method...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
__construct(ApiMain $mainModule, string $moduleName, IConnectionProvider $dbProvider, LanguageNameUtils $languageNameUtils)
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
Recent changes tagging.
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:69
Service for formatting and validating API parameters.
Provide primary and replica IDatabase connections.