MediaWiki  master
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  $tag = $params['tag'];
51  $reason = $params['reason'];
52  $ignoreWarnings = $params['ignorewarnings'];
53  $tags = $params['tags'] ?: [];
54  switch ( $params['operation'] ) {
55  case 'create':
56  $status = ChangeTags::createTagWithChecks( $tag, $reason, $user, $ignoreWarnings, $tags );
57  break;
58  case 'delete':
59  $status = ChangeTags::deleteTagWithChecks( $tag, $reason, $user, $ignoreWarnings, $tags );
60  break;
61  case 'activate':
62  $status = ChangeTags::activateTagWithChecks( $tag, $reason, $user, $ignoreWarnings, $tags );
63  break;
64  case 'deactivate':
65  $status = ChangeTags::deactivateTagWithChecks( $tag, $reason, $user, $ignoreWarnings, $tags );
66  break;
67  default:
68  // unreachable
69  throw new \UnexpectedValueException( 'invalid operation' );
70  }
71  if ( !$status->isOK() ) {
72  $this->dieStatus( $status );
73  }
74 
75  $ret = [
76  'operation' => $params['operation'],
77  'tag' => $params['tag'],
78  ];
79  if ( !$status->isGood() ) {
80  $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
81  }
82  $ret['success'] = $status->value !== null;
83  if ( $ret['success'] ) {
84  $ret['logid'] = $status->value;
85  }
86 
87  $result->addValue( null, $this->getModuleName(), $ret );
88  }
89 
90  public function mustBePosted() {
91  return true;
92  }
93 
94  public function isWriteMode() {
95  return true;
96  }
97 
98  public function getAllowedParams() {
99  return [
100  'operation' => [
101  ApiBase::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
102  ApiBase::PARAM_REQUIRED => true,
103  ],
104  'tag' => [
105  ApiBase::PARAM_TYPE => 'string',
106  ApiBase::PARAM_REQUIRED => true,
107  ],
108  'reason' => [
109  ApiBase::PARAM_TYPE => 'string',
110  ],
111  'ignorewarnings' => [
112  ApiBase::PARAM_TYPE => 'boolean',
113  ApiBase::PARAM_DFLT => false,
114  ],
115  'tags' => [
116  ApiBase::PARAM_TYPE => 'tags',
117  ApiBase::PARAM_ISMULTI => true,
118  ],
119  ];
120  }
121 
122  public function needsToken() {
123  return 'csrf';
124  }
125 
126  protected function getExamplesMessages() {
127  return [
128  'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
129  => 'apihelp-managetags-example-create',
130  'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
131  => 'apihelp-managetags-example-delete',
132  'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
133  => 'apihelp-managetags-example-activate',
134  'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
135  => 'apihelp-managetags-example-deactivate',
136  ];
137  }
138 
139  public function getHelpUrls() {
140  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
141  }
142 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:76
ApiManageTags\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiManageTags.php:94
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1381
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:564
ApiManageTags\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiManageTags.php:28
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
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:716
ApiManageTags\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiManageTags.php:90
ApiManageTags\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiManageTags.php:122
ChangeTags\deactivateTagWithChecks
static deactivateTagWithChecks( $tag, $reason, User $user, $ignoreWarnings=false, array $logEntryTags=[])
Deactivates a tag, checking whether it is allowed first, and adding a log entry afterwards.
Definition: ChangeTags.php:1177
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:636
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:545
ApiManageTags\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiManageTags.php:126
ApiManageTags\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiManageTags.php:98
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1439
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:443
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiManageTags\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiManageTags.php:139
ChangeTags\createTagWithChecks
static createTagWithChecks( $tag, $reason, User $user, $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
Definition: ChangeTags.php:1287
ApiManageTags
Definition: ApiManageTags.php:26
ChangeTags\deleteTagWithChecks
static deleteTagWithChecks( $tag, $reason, User $user, $ignoreWarnings=false, array $logEntryTags=[])
Deletes a tag, checking whether it is allowed first, and adding a log entry afterwards.
Definition: ChangeTags.php:1416
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter Stable to override.
Definition: ApiBase.php:579
ChangeTags\activateTagWithChecks
static activateTagWithChecks( $tag, $reason, User $user, $ignoreWarnings=false, array $logEntryTags=[])
Activates a tag, checking whether it is allowed first, and adding a log entry afterwards.
Definition: ChangeTags.php:1112