MediaWiki REL1_34
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
31 // Check if change language feature is enabled
32 protected function getExtendedDescription() {
33 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
34 return 'apihelp-setpagelanguage-extended-description-disabled';
35 }
36 return parent::getExtendedDescription();
37 }
38
46 public function execute() {
47 // Check if change language feature is enabled
48 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
49 $this->dieWithError( 'apierror-pagelang-disabled' );
50 }
51
52 // Check if the user has permissions
53 $this->checkUserRightsAny( 'pagelang' );
54
56
57 $params = $this->extractRequestParams();
58
59 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
60 if ( !$pageObj->exists() ) {
61 $this->dieWithError( 'apierror-missingtitle' );
62 }
63
64 $titleObj = $pageObj->getTitle();
65 $user = $this->getUser();
66
67 // Check that the user is allowed to edit the page
68 $this->checkTitleUserPermissions( $titleObj, 'edit' );
69
70 // If change tagging was requested, check that the user is allowed to tag,
71 // and the tags are valid
72 if ( $params['tags'] ) {
73 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
74 if ( !$tagStatus->isOK() ) {
75 $this->dieStatus( $tagStatus );
76 }
77 }
78
80 $this,
81 $titleObj,
82 $params['lang'],
83 $params['reason'] ?? '',
84 $params['tags'] ?: []
85 );
86
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
89 }
90
91 $r = [
92 'title' => $titleObj->getPrefixedText(),
93 'oldlanguage' => $status->value->oldLanguage,
94 'newlanguage' => $status->value->newLanguage,
95 'logid' => $status->value->logId
96 ];
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
98 }
99
100 public function mustBePosted() {
101 return true;
102 }
103
104 public function isWriteMode() {
105 return true;
106 }
107
108 public function getAllowedParams() {
109 return [
110 'title' => null,
111 'pageid' => [
112 ApiBase::PARAM_TYPE => 'integer'
113 ],
114 'lang' => [
115 ApiBase::PARAM_TYPE => array_merge(
116 [ 'default' ],
117 array_keys( Language::fetchLanguageNames( null, 'mwfile' ) )
118 ),
120 ],
121 'reason' => null,
122 'tags' => [
123 ApiBase::PARAM_TYPE => 'tags',
125 ],
126 ];
127 }
128
129 public function needsToken() {
130 return 'csrf';
131 }
132
133 protected function getExamplesMessages() {
134 return [
135 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
136 => 'apihelp-setpagelanguage-example-language',
137 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
138 => 'apihelp-setpagelanguage-example-default',
139 ];
140 }
141
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
144 }
145}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:118
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2130
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:2156
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1034
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:2086
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1871
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.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...
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, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])