MediaWiki  master
ApiSetPageLanguage.php
Go to the documentation of this file.
1 <?php
25 
33 class ApiSetPageLanguage extends ApiBase {
34 
36  private $loadBalancer;
37 
40 
47  public function __construct(
48  ApiMain $mainModule,
49  $moduleName,
52  ) {
53  parent::__construct( $mainModule, $moduleName );
54  $this->loadBalancer = $loadBalancer;
55  $this->languageNameUtils = $languageNameUtils;
56  }
57 
58  // Check if change language feature is enabled
59  protected function getExtendedDescription() {
60  if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
61  return 'apihelp-setpagelanguage-extended-description-disabled';
62  }
63  return parent::getExtendedDescription();
64  }
65 
73  public function execute() {
74  // Check if change language feature is enabled
75  if ( !$this->getConfig()->get( 'PageLanguageUseDB' ) ) {
76  $this->dieWithError( 'apierror-pagelang-disabled' );
77  }
78 
79  // Check if the user has permissions
80  $this->checkUserRightsAny( 'pagelang' );
81 
83 
84  $params = $this->extractRequestParams();
85 
86  $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
87  $titleObj = $pageObj->getTitle();
88  $this->getErrorFormatter()->setContextTitle( $titleObj );
89  if ( !$pageObj->exists() ) {
90  $this->dieWithError( 'apierror-missingtitle' );
91  }
92 
93  $user = $this->getUser();
94 
95  // Check that the user is allowed to edit the page
96  $this->checkTitleUserPermissions( $titleObj, 'edit' );
97 
98  // If change tagging was requested, check that the user is allowed to tag,
99  // and the tags are valid
100  if ( $params['tags'] ) {
101  $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
102  if ( !$tagStatus->isOK() ) {
103  $this->dieStatus( $tagStatus );
104  }
105  }
106 
108  $this,
109  $titleObj,
110  $params['lang'],
111  $params['reason'] ?? '',
112  $params['tags'] ?: [],
113  $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_PRIMARY )
114  );
115 
116  if ( !$status->isOK() ) {
117  $this->dieStatus( $status );
118  }
119 
120  $r = [
121  'title' => $titleObj->getPrefixedText(),
122  'oldlanguage' => $status->value->oldLanguage,
123  'newlanguage' => $status->value->newLanguage,
124  'logid' => $status->value->logId
125  ];
126  $this->getResult()->addValue( null, $this->getModuleName(), $r );
127  }
128 
129  public function mustBePosted() {
130  return true;
131  }
132 
133  public function isWriteMode() {
134  return true;
135  }
136 
137  public function getAllowedParams() {
138  return [
139  'title' => null,
140  'pageid' => [
141  ApiBase::PARAM_TYPE => 'integer'
142  ],
143  'lang' => [
144  ApiBase::PARAM_TYPE => array_merge(
145  [ 'default' ],
146  array_keys( $this->languageNameUtils->getLanguageNames( null, 'mwfile' ) )
147  ),
148  ApiBase::PARAM_REQUIRED => true,
149  ],
150  'reason' => null,
151  'tags' => [
152  ApiBase::PARAM_TYPE => 'tags',
153  ApiBase::PARAM_ISMULTI => true,
154  ],
155  ];
156  }
157 
158  public function needsToken() {
159  return 'csrf';
160  }
161 
162  protected function getExamplesMessages() {
163  return [
164  'action=setpagelanguage&title=Main%20Page&lang=eu&token=123ABC'
165  => 'apihelp-setpagelanguage-example-language',
166  'action=setpagelanguage&pageid=123&lang=default&token=123ABC'
167  => 'apihelp-setpagelanguage-example-default',
168  ];
169  }
170 
171  public function getHelpUrls() {
172  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetPageLanguage';
173  }
174 }
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1502
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:78
ApiSetPageLanguage\$loadBalancer
ILoadBalancer $loadBalancer
Definition: ApiSetPageLanguage.php:36
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1374
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:976
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiSetPageLanguage\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiSetPageLanguage.php:129
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1476
ApiSetPageLanguage\getExtendedDescription
getExtendedDescription()
Return the extended help text message.
Definition: ApiSetPageLanguage.php:59
SpecialPageLanguage\changePageLanguage
static changePageLanguage(IContextSource $context, Title $title, $newLanguage, $reason, array $tags=[], IDatabase $dbw=null)
Definition: SpecialPageLanguage.php:215
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
ApiSetPageLanguage\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiSetPageLanguage.php:133
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiSetPageLanguage
API module that facilitates changing the language of a page.
Definition: ApiSetPageLanguage.php:33
ApiSetPageLanguage\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiSetPageLanguage.php:137
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, Authority $performer=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:625
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiSetPageLanguage\execute
execute()
Extracts the title and language from the request parameters and invokes the static SpecialPageLanguag...
Definition: ApiSetPageLanguage.php:73
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1232
ApiSetPageLanguage\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiSetPageLanguage.php:162
ApiSetPageLanguage\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiSetPageLanguage.php:158
ApiSetPageLanguage\$languageNameUtils
LanguageNameUtils $languageNameUtils
Definition: ApiSetPageLanguage.php:39
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1432
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiSetPageLanguage\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiSetPageLanguage.php:171
ApiBase\getErrorFormatter
getErrorFormatter()
Definition: ApiBase.php:582
ApiSetPageLanguage\__construct
__construct(ApiMain $mainModule, $moduleName, ILoadBalancer $loadBalancer, LanguageNameUtils $languageNameUtils)
Definition: ApiSetPageLanguage.php:47
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81