MediaWiki  master
ApiSetPageLanguage.php
Go to the documentation of this file.
1 <?php
24 
32 class ApiSetPageLanguage extends ApiBase {
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  ),
123  ApiBase::PARAM_REQUIRED => true,
124  ],
125  'reason' => null,
126  'tags' => [
127  ApiBase::PARAM_TYPE => 'tags',
128  ApiBase::PARAM_ISMULTI => true,
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 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:67
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:155
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:986
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiSetPageLanguage\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiSetPageLanguage.php:102
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1484
ApiSetPageLanguage\getExtendedDescription
getExtendedDescription()
Return the extended help text message.
Definition: ApiSetPageLanguage.php:34
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiSetPageLanguage\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiSetPageLanguage.php:106
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
ApiSetPageLanguage
API module that facilitates changing the language of a page.
Definition: ApiSetPageLanguage.php:32
ApiSetPageLanguage\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiSetPageLanguage.php:110
ApiSetPageLanguage\execute
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
Definition: ApiSetPageLanguage.php:48
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1239
ApiSetPageLanguage\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiSetPageLanguage.php:137
ApiSetPageLanguage\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiSetPageLanguage.php:133
SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[])
Definition: SpecialPageLanguage.php:186
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:544
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1440
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1509
ApiSetPageLanguage\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiSetPageLanguage.php:146