MediaWiki master
ApiManageTags.php
Go to the documentation of this file.
1<?php
2
8namespace MediaWiki\Api;
9
11use UnexpectedValueException;
13
18class ApiManageTags extends ApiBase {
19
20 public function execute() {
21 $params = $this->extractRequestParams();
22 $authority = $this->getAuthority();
23
24 // make sure the user is allowed
25 if ( $params['operation'] !== 'delete'
26 && !$authority->isAllowed( 'managechangetags' )
27 ) {
28 $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
29 } elseif ( !$authority->isAllowed( 'deletechangetags' ) ) {
30 $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' );
31 }
32
33 // Check if user can add the log entry tags which were requested
34 if ( $params['tags'] ) {
35 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $authority );
36 if ( !$ableToTag->isOK() ) {
37 $this->dieStatus( $ableToTag );
38 }
39 }
40
41 $result = $this->getResult();
42 $tag = $params['tag'];
43 $reason = $params['reason'];
44 $ignoreWarnings = $params['ignorewarnings'];
45 $tags = $params['tags'] ?: [];
46 $fn = match ( $params['operation'] ) {
47 'create' => ChangeTags::createTagWithChecks( ... ),
48 'delete' => ChangeTags::deleteTagWithChecks( ... ),
49 'activate' => ChangeTags::activateTagWithChecks( ... ),
50 'deactivate' => ChangeTags::deactivateTagWithChecks( ... ),
51 // unreachable
52 default => throw new UnexpectedValueException( 'invalid operation' )
53 };
54 $status = $fn( $tag, $reason, $authority, $ignoreWarnings, $tags );
55 if ( !$status->isOK() ) {
56 $this->dieStatus( $status );
57 }
58
59 $ret = [
60 'operation' => $params['operation'],
61 'tag' => $params['tag'],
62 ];
63 if ( !$status->isGood() ) {
64 $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
65 }
66 $ret['success'] = $status->value !== null;
67 if ( $ret['success'] ) {
68 $ret['logid'] = $status->value;
69 }
70
71 $result->addValue( null, $this->getModuleName(), $ret );
72 }
73
75 public function mustBePosted() {
76 return true;
77 }
78
80 public function isWriteMode() {
81 return true;
82 }
83
85 public function getAllowedParams() {
86 return [
87 'operation' => [
88 ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
89 ParamValidator::PARAM_REQUIRED => true,
91 ],
92 'tag' => [
93 ParamValidator::PARAM_TYPE => 'string',
94 ParamValidator::PARAM_REQUIRED => true,
95 ],
96 'reason' => [
97 ParamValidator::PARAM_TYPE => 'string',
98 ParamValidator::PARAM_DEFAULT => '',
99 ],
100 'ignorewarnings' => [
101 ParamValidator::PARAM_TYPE => 'boolean',
102 ParamValidator::PARAM_DEFAULT => false,
103 ],
104 'tags' => [
105 ParamValidator::PARAM_TYPE => 'tags',
106 ParamValidator::PARAM_ISMULTI => true,
107 ],
108 ];
109 }
110
112 public function needsToken() {
113 return 'csrf';
114 }
115
117 protected function getExamplesMessages() {
118 return [
119 'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
120 => 'apihelp-managetags-example-create',
121 'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
122 => 'apihelp-managetags-example-delete',
123 'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
124 => 'apihelp-managetags-example-activate',
125 'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
126 => 'apihelp-managetags-example-deactivate',
127 ];
128 }
129
131 public function getHelpUrls() {
132 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
133 }
134}
135
137class_alias( ApiManageTags::class, 'ApiManageTags' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
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.1.25, returning boolean false is deprecated...
Recent changes tagging.
Service for formatting and validating API parameters.