MediaWiki REL1_37
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->getAuthority()->isAllowed( 'managechangetags' )
35 ) {
36 $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
37 } elseif ( !$this->getAuthority()->isAllowed( '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'], $this->getAuthority() );
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:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
const PARAM_DFLT
Definition ApiBase.php:73
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
const PARAM_ISMULTI
Definition ApiBase.php:77
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.
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 deactivateTagWithChecks( $tag, $reason, Authority $performer, $ignoreWarnings=false, array $logEntryTags=[])
Deactivates a tag, checking whether it is allowed first, and adding a log entry afterwards.
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
static deleteTagWithChecks( $tag, $reason, Authority $performer, $ignoreWarnings=false, array $logEntryTags=[])
Deletes a tag, checking whether it is allowed first, and adding a log entry afterwards.
static createTagWithChecks( $tag, $reason, Authority $performer, $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
static activateTagWithChecks( $tag, $reason, Authority $performer, $ignoreWarnings=false, array $logEntryTags=[])
Activates a tag, checking whether it is allowed first, and adding a log entry afterwards.