MediaWiki master
ApiManageTags.php
Go to the documentation of this file.
1<?php
2
22namespace MediaWiki\Api;
23
24use ChangeTags;
25use UnexpectedValueException;
27
32class ApiManageTags extends ApiBase {
33
34 public function execute() {
36 $authority = $this->getAuthority();
37
38 // make sure the user is allowed
39 if ( $params['operation'] !== 'delete'
40 && !$authority->isAllowed( 'managechangetags' )
41 ) {
42 $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
43 } elseif ( !$authority->isAllowed( 'deletechangetags' ) ) {
44 $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' );
45 }
46
47 // Check if user can add the log entry tags which were requested
48 if ( $params['tags'] ) {
49 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $authority );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
52 }
53 }
54
55 $result = $this->getResult();
56 $tag = $params['tag'];
57 $reason = $params['reason'];
58 $ignoreWarnings = $params['ignorewarnings'];
59 $tags = $params['tags'] ?: [];
60 switch ( $params['operation'] ) {
61 case 'create':
62 $status = ChangeTags::createTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
63 break;
64 case 'delete':
65 $status = ChangeTags::deleteTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
66 break;
67 case 'activate':
68 $status = ChangeTags::activateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
69 break;
70 case 'deactivate':
71 $status = ChangeTags::deactivateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
72 break;
73 default:
74 // unreachable
75 throw new UnexpectedValueException( 'invalid operation' );
76 }
77 if ( !$status->isOK() ) {
78 $this->dieStatus( $status );
79 }
80
81 $ret = [
82 'operation' => $params['operation'],
83 'tag' => $params['tag'],
84 ];
85 if ( !$status->isGood() ) {
86 $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
87 }
88 $ret['success'] = $status->value !== null;
89 if ( $ret['success'] ) {
90 $ret['logid'] = $status->value;
91 }
92
93 $result->addValue( null, $this->getModuleName(), $ret );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return [
106 'operation' => [
107 ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
108 ParamValidator::PARAM_REQUIRED => true,
110 ],
111 'tag' => [
112 ParamValidator::PARAM_TYPE => 'string',
113 ParamValidator::PARAM_REQUIRED => true,
114 ],
115 'reason' => [
116 ParamValidator::PARAM_TYPE => 'string',
117 ParamValidator::PARAM_DEFAULT => '',
118 ],
119 'ignorewarnings' => [
120 ParamValidator::PARAM_TYPE => 'boolean',
121 ParamValidator::PARAM_DEFAULT => false,
122 ],
123 'tags' => [
124 ParamValidator::PARAM_TYPE => 'tags',
125 ParamValidator::PARAM_ISMULTI => true,
126 ],
127 ];
128 }
129
130 public function needsToken() {
131 return 'csrf';
132 }
133
134 protected function getExamplesMessages() {
135 return [
136 'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
137 => 'apihelp-managetags-example-create',
138 'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
139 => 'apihelp-managetags-example-delete',
140 'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
141 => 'apihelp-managetags-example-activate',
142 'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
143 => 'apihelp-managetags-example-deactivate',
144 ];
145 }
146
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
149 }
150}
151
153class_alias( ApiManageTags::class, 'ApiManageTags' );
array $params
The job parameters.
Recent changes tagging.
static deactivateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deactivates a tag, checking whether it is allowed first, and adding a log entry afterwards.
static deleteTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deletes a tag, checking whether it is allowed first, and adding a log entry afterwards.
static createTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
static canAddTagsAccompanyingChange(array $tags, ?Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
static activateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Activates a tag, checking whether it is allowed first, and adding a log entry afterwards.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:75
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:221
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
mustBePosted()
Indicates whether this module must be called with a POST request.
needsToken()
Returns the token type this module requires in order to execute.
getExamplesMessages()
Returns usage examples for this module.
isWriteMode()
Indicates whether this module requires write access to the wiki.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
Service for formatting and validating API parameters.