MediaWiki  1.34.0
ApiManageTags.php
Go to the documentation of this file.
1 <?php
2 
26 class ApiManageTags extends ApiBase {
27 
28  public function execute() {
29  $params = $this->extractRequestParams();
30  $user = $this->getUser();
31 
32  // make sure the user is allowed
33  if ( $params['operation'] !== 'delete'
34  && !$this->getPermissionManager()->userHasRight( $user, 'managechangetags' )
35  ) {
36  $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
37  } elseif ( !$this->getPermissionManager()->userHasRight( $user, 'deletechangetags' ) ) {
38  $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' );
39  }
40 
41  // Check if user can add the log entry tags which were requested
42  if ( $params['tags'] ) {
43  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
44  if ( !$ableToTag->isOK() ) {
45  $this->dieStatus( $ableToTag );
46  }
47  }
48 
49  $result = $this->getResult();
50  $funcName = "{$params['operation']}TagWithChecks";
51  $status = ChangeTags::$funcName(
52  $params['tag'],
53  $params['reason'],
54  $user,
55  $params['ignorewarnings'],
56  $params['tags'] ?: []
57  );
58 
59  if ( !$status->isOK() ) {
60  $this->dieStatus( $status );
61  }
62 
63  $ret = [
64  'operation' => $params['operation'],
65  'tag' => $params['tag'],
66  ];
67  if ( !$status->isGood() ) {
68  $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
69  }
70  $ret['success'] = $status->value !== null;
71  if ( $ret['success'] ) {
72  $ret['logid'] = $status->value;
73  }
74 
75  $result->addValue( null, $this->getModuleName(), $ret );
76  }
77 
78  public function mustBePosted() {
79  return true;
80  }
81 
82  public function isWriteMode() {
83  return true;
84  }
85 
86  public function getAllowedParams() {
87  return [
88  'operation' => [
89  ApiBase::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
91  ],
92  'tag' => [
93  ApiBase::PARAM_TYPE => 'string',
95  ],
96  'reason' => [
97  ApiBase::PARAM_TYPE => 'string',
98  ],
99  'ignorewarnings' => [
100  ApiBase::PARAM_TYPE => 'boolean',
101  ApiBase::PARAM_DFLT => false,
102  ],
103  'tags' => [
104  ApiBase::PARAM_TYPE => 'tags',
105  ApiBase::PARAM_ISMULTI => true,
106  ],
107  ];
108  }
109 
110  public function needsToken() {
111  return 'csrf';
112  }
113 
114  protected function getExamplesMessages() {
115  return [
116  'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
117  => 'apihelp-managetags-example-create',
118  'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
119  => 'apihelp-managetags-example-delete',
120  'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
121  => 'apihelp-managetags-example-activate',
122  'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
123  => 'apihelp-managetags-example-deactivate',
124  ];
125  }
126 
127  public function getHelpUrls() {
128  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
129  }
130 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiManageTags\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiManageTags.php:82
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
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
ApiManageTags\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiManageTags.php:28
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
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiManageTags\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiManageTags.php:78
ApiManageTags\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiManageTags.php:110
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:710
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
ApiManageTags\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiManageTags.php:114
ApiManageTags\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiManageTags.php:86
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
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
ApiManageTags\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiManageTags.php:127
ApiManageTags
Definition: ApiManageTags.php:26
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654