Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiManageTags
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
110
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @license GPL-2.0-or-later
5 * @file
6 */
7
8namespace MediaWiki\Api;
9
10use MediaWiki\ChangeTags\ChangeTags;
11use UnexpectedValueException;
12use Wikimedia\ParamValidator\ParamValidator;
13
14/**
15 * @ingroup API
16 * @since 1.25
17 */
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
74    /** @inheritDoc */
75    public function mustBePosted() {
76        return true;
77    }
78
79    /** @inheritDoc */
80    public function isWriteMode() {
81        return true;
82    }
83
84    /** @inheritDoc */
85    public function getAllowedParams() {
86        return [
87            'operation' => [
88                ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
89                ParamValidator::PARAM_REQUIRED => true,
90                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
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
111    /** @inheritDoc */
112    public function needsToken() {
113        return 'csrf';
114    }
115
116    /** @inheritDoc */
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
130    /** @inheritDoc */
131    public function getHelpUrls() {
132        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
133    }
134}
135
136/** @deprecated class alias since 1.43 */
137class_alias( ApiManageTags::class, 'ApiManageTags' );