MediaWiki REL1_35
ApiManageTags.php
Go to the documentation of this file.
1<?php
2
26class 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' ],
103 ],
104 'tag' => [
105 ApiBase::PARAM_TYPE => 'string',
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',
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}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
const PARAM_TYPE
Definition ApiBase.php:78
getErrorFormatter()
Get the error formatter Stable to override.
Definition ApiBase.php:635
const PARAM_DFLT
Definition ApiBase.php:70
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:692
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
const PARAM_ISMULTI
Definition ApiBase.php:74
needsToken()
Returns the token type this module requires in order to execute.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
static createTagWithChecks( $tag, $reason, User $user, $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
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.
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.
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.
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...
getUser()
Stable to override.