MediaWiki REL1_35
ApiSetPageLanguage.php
Go to the documentation of this file.
1<?php
24
33 // Check if change language feature is enabled
34 protected function getExtendedDescription() {
35 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
36 return 'apihelp-setpagelanguage-extended-description-disabled';
37 }
38 return parent::getExtendedDescription();
39 }
40
48 public function execute() {
49 // Check if change language feature is enabled
50 if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
51 $this->dieWithError( 'apierror-pagelang-disabled' );
52 }
53
54 // Check if the user has permissions
55 $this->checkUserRightsAny( 'pagelang' );
56
58
59 $params = $this->extractRequestParams();
60
61 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
62 if ( !$pageObj->exists() ) {
63 $this->dieWithError( 'apierror-missingtitle' );
64 }
65
66 $titleObj = $pageObj->getTitle();
67 $user = $this->getUser();
68
69 // Check that the user is allowed to edit the page
70 $this->checkTitleUserPermissions( $titleObj, 'edit' );
71
72 // If change tagging was requested, check that the user is allowed to tag,
73 // and the tags are valid
74 if ( $params['tags'] ) {
75 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
76 if ( !$tagStatus->isOK() ) {
77 $this->dieStatus( $tagStatus );
78 }
79 }
80
82 $this,
83 $titleObj,
84 $params['lang'],
85 $params['reason'] ?? '',
86 $params['tags'] ?: []
87 );
88
89 if ( !$status->isOK() ) {
90 $this->dieStatus( $status );
91 }
92
93 $r = [
94 'title' => $titleObj->getPrefixedText(),
95 'oldlanguage' => $status->value->oldLanguage,
96 'newlanguage' => $status->value->newLanguage,
97 'logid' => $status->value->logId
98 ];
99 $this->getResult()->addValue( null, $this->getModuleName(), $r );
100 }
101
102 public function mustBePosted() {
103 return true;
104 }
105
106 public function isWriteMode() {
107 return true;
108 }
109
110 public function getAllowedParams() {
111 return [
112 'title' => null,
113 'pageid' => [
114 ApiBase::PARAM_TYPE => 'integer'
115 ],
116 'lang' => [
117 ApiBase::PARAM_TYPE => array_merge(
118 [ 'default' ],
119 array_keys( MediaWikiServices::getInstance()
120 ->getLanguageNameUtils()
121 ->getLanguageNames( null, 'mwfile' ) )
122 ),
124 ],
125 'reason' => null,
126 'tags' => [
127 ApiBase::PARAM_TYPE => 'tags',
129 ],
130 ];
131 }
132
133 public function needsToken() {
134 return 'csrf';
135 }
136
137 protected function getExamplesMessages() {
138 return [
139 'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
140 => 'apihelp-setpagelanguage-example-language',
141 'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
142 => 'apihelp-setpagelanguage-example-default',
143 ];
144 }
145
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
148 }
149}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1539
const PARAM_TYPE
Definition ApiBase.php:78
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1564
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1041
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:1294
const PARAM_ISMULTI
Definition ApiBase.php:74
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 Stable to override.
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...
getUser()
Stable to override.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])