Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 79 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiManageTags | |
0.00% |
0 / 79 |
|
0.00% |
0 / 7 |
462 | |
0.00% |
0 / 1 |
execute | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
240 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | use Wikimedia\ParamValidator\ParamValidator; |
23 | |
24 | /** |
25 | * @ingroup API |
26 | * @since 1.25 |
27 | */ |
28 | class ApiManageTags extends ApiBase { |
29 | |
30 | public function execute() { |
31 | $params = $this->extractRequestParams(); |
32 | $authority = $this->getAuthority(); |
33 | |
34 | // make sure the user is allowed |
35 | if ( $params['operation'] !== 'delete' |
36 | && !$authority->isAllowed( 'managechangetags' ) |
37 | ) { |
38 | $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' ); |
39 | } elseif ( !$authority->isAllowed( 'deletechangetags' ) ) { |
40 | $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' ); |
41 | } |
42 | |
43 | // Check if user can add the log entry tags which were requested |
44 | if ( $params['tags'] ) { |
45 | $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $authority ); |
46 | if ( !$ableToTag->isOK() ) { |
47 | $this->dieStatus( $ableToTag ); |
48 | } |
49 | } |
50 | |
51 | $result = $this->getResult(); |
52 | $tag = $params['tag']; |
53 | $reason = $params['reason']; |
54 | $ignoreWarnings = $params['ignorewarnings']; |
55 | $tags = $params['tags'] ?: []; |
56 | switch ( $params['operation'] ) { |
57 | case 'create': |
58 | $status = ChangeTags::createTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
59 | break; |
60 | case 'delete': |
61 | $status = ChangeTags::deleteTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
62 | break; |
63 | case 'activate': |
64 | $status = ChangeTags::activateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
65 | break; |
66 | case 'deactivate': |
67 | $status = ChangeTags::deactivateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags ); |
68 | break; |
69 | default: |
70 | // unreachable |
71 | throw new UnexpectedValueException( 'invalid operation' ); |
72 | } |
73 | if ( !$status->isOK() ) { |
74 | $this->dieStatus( $status ); |
75 | } |
76 | |
77 | $ret = [ |
78 | 'operation' => $params['operation'], |
79 | 'tag' => $params['tag'], |
80 | ]; |
81 | if ( !$status->isGood() ) { |
82 | $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
83 | } |
84 | $ret['success'] = $status->value !== null; |
85 | if ( $ret['success'] ) { |
86 | $ret['logid'] = $status->value; |
87 | } |
88 | |
89 | $result->addValue( null, $this->getModuleName(), $ret ); |
90 | } |
91 | |
92 | public function mustBePosted() { |
93 | return true; |
94 | } |
95 | |
96 | public function isWriteMode() { |
97 | return true; |
98 | } |
99 | |
100 | public function getAllowedParams() { |
101 | return [ |
102 | 'operation' => [ |
103 | ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ], |
104 | ParamValidator::PARAM_REQUIRED => true, |
105 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
106 | ], |
107 | 'tag' => [ |
108 | ParamValidator::PARAM_TYPE => 'string', |
109 | ParamValidator::PARAM_REQUIRED => true, |
110 | ], |
111 | 'reason' => [ |
112 | ParamValidator::PARAM_TYPE => 'string', |
113 | ParamValidator::PARAM_DEFAULT => '', |
114 | ], |
115 | 'ignorewarnings' => [ |
116 | ParamValidator::PARAM_TYPE => 'boolean', |
117 | ParamValidator::PARAM_DEFAULT => false, |
118 | ], |
119 | 'tags' => [ |
120 | ParamValidator::PARAM_TYPE => 'tags', |
121 | ParamValidator::PARAM_ISMULTI => true, |
122 | ], |
123 | ]; |
124 | } |
125 | |
126 | public function needsToken() { |
127 | return 'csrf'; |
128 | } |
129 | |
130 | protected function getExamplesMessages() { |
131 | return [ |
132 | 'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC' |
133 | => 'apihelp-managetags-example-create', |
134 | 'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC' |
135 | => 'apihelp-managetags-example-delete', |
136 | 'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC' |
137 | => 'apihelp-managetags-example-activate', |
138 | 'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC' |
139 | => 'apihelp-managetags-example-deactivate', |
140 | ]; |
141 | } |
142 | |
143 | public function getHelpUrls() { |
144 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management'; |
145 | } |
146 | } |