Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiManageTags.php
blob9528b37b550644f8eecb604bffc10a275dd3522f
1 <?php
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.
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.
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
19 * @file
22 namespace MediaWiki\Api;
24 use ChangeTags;
25 use UnexpectedValueException;
26 use Wikimedia\ParamValidator\ParamValidator;
28 /**
29 * @ingroup API
30 * @since 1.25
32 class ApiManageTags extends ApiBase {
34 public function execute() {
35 $params = $this->extractRequestParams();
36 $authority = $this->getAuthority();
38 // make sure the user is allowed
39 if ( $params['operation'] !== 'delete'
40 && !$authority->isAllowed( 'managechangetags' )
41 ) {
42 $this->dieWithError( 'tags-manage-no-permission', 'permissiondenied' );
43 } elseif ( !$authority->isAllowed( 'deletechangetags' ) ) {
44 $this->dieWithError( 'tags-delete-no-permission', 'permissiondenied' );
47 // Check if user can add the log entry tags which were requested
48 if ( $params['tags'] ) {
49 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $authority );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
55 $result = $this->getResult();
56 $tag = $params['tag'];
57 $reason = $params['reason'];
58 $ignoreWarnings = $params['ignorewarnings'];
59 $tags = $params['tags'] ?: [];
60 switch ( $params['operation'] ) {
61 case 'create':
62 $status = ChangeTags::createTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
63 break;
64 case 'delete':
65 $status = ChangeTags::deleteTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
66 break;
67 case 'activate':
68 $status = ChangeTags::activateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
69 break;
70 case 'deactivate':
71 $status = ChangeTags::deactivateTagWithChecks( $tag, $reason, $authority, $ignoreWarnings, $tags );
72 break;
73 default:
74 // unreachable
75 throw new UnexpectedValueException( 'invalid operation' );
77 if ( !$status->isOK() ) {
78 $this->dieStatus( $status );
81 $ret = [
82 'operation' => $params['operation'],
83 'tag' => $params['tag'],
85 if ( !$status->isGood() ) {
86 $ret['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
88 $ret['success'] = $status->value !== null;
89 if ( $ret['success'] ) {
90 $ret['logid'] = $status->value;
93 $result->addValue( null, $this->getModuleName(), $ret );
96 public function mustBePosted() {
97 return true;
100 public function isWriteMode() {
101 return true;
104 public function getAllowedParams() {
105 return [
106 'operation' => [
107 ParamValidator::PARAM_TYPE => [ 'create', 'delete', 'activate', 'deactivate' ],
108 ParamValidator::PARAM_REQUIRED => true,
109 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
111 'tag' => [
112 ParamValidator::PARAM_TYPE => 'string',
113 ParamValidator::PARAM_REQUIRED => true,
115 'reason' => [
116 ParamValidator::PARAM_TYPE => 'string',
117 ParamValidator::PARAM_DEFAULT => '',
119 'ignorewarnings' => [
120 ParamValidator::PARAM_TYPE => 'boolean',
121 ParamValidator::PARAM_DEFAULT => false,
123 'tags' => [
124 ParamValidator::PARAM_TYPE => 'tags',
125 ParamValidator::PARAM_ISMULTI => true,
130 public function needsToken() {
131 return 'csrf';
134 protected function getExamplesMessages() {
135 return [
136 'action=managetags&operation=create&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
137 => 'apihelp-managetags-example-create',
138 'action=managetags&operation=delete&tag=vandlaism&reason=Misspelt&token=123ABC'
139 => 'apihelp-managetags-example-delete',
140 'action=managetags&operation=activate&tag=spam&reason=For+use+in+edit+patrolling&token=123ABC'
141 => 'apihelp-managetags-example-activate',
142 'action=managetags&operation=deactivate&tag=spam&reason=No+longer+required&token=123ABC'
143 => 'apihelp-managetags-example-deactivate',
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag_management';
152 /** @deprecated class alias since 1.43 */
153 class_alias( ApiManageTags::class, 'ApiManageTags' );