MediaWiki  1.34.0
ApiSetPageLanguage.php
Go to the documentation of this file.
1 <?php
30 class ApiSetPageLanguage extends ApiBase {
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  ),
119  ApiBase::PARAM_REQUIRED => true,
120  ],
121  'reason' => null,
122  'tags' => [
123  ApiBase::PARAM_TYPE => 'tags',
124  ApiBase::PARAM_ISMULTI => true,
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 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:1034
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiSetPageLanguage\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiSetPageLanguage.php:100
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:2130
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:2156
ApiSetPageLanguage\getExtendedDescription
getExtendedDescription()
Return the extended help text message.
Definition: ApiSetPageLanguage.php:32
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiSetPageLanguage\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiSetPageLanguage.php:104
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiSetPageLanguage
API module that facilitates changing the language of a page.
Definition: ApiSetPageLanguage.php:30
ApiSetPageLanguage\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiSetPageLanguage.php:108
ApiSetPageLanguage\execute
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
Definition: ApiSetPageLanguage.php:46
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
ApiSetPageLanguage\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiSetPageLanguage.php:133
ApiSetPageLanguage\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiSetPageLanguage.php:129
SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])
Definition: SpecialPageLanguage.php:179
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:521
$status
return $status
Definition: SyntaxHighlight.php:347
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
Language\fetchLanguageNames
static fetchLanguageNames( $inLanguage=self::AS_AUTONYMS, $include='mw')
Get an array of language names, indexed by code.
Definition: Language.php:818
ApiSetPageLanguage\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiSetPageLanguage.php:142