Special:Preferences: Split up "Advanced options" on watchlist and RC tabs
[lhc/web/wiklou.git] / includes / api / ApiTag.php
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 MediaWiki\MediaWikiServices;
23 use MediaWiki\Revision\RevisionStore;
24
25 /**
26 * @ingroup API
27 * @since 1.25
28 */
29 class ApiTag extends ApiBase {
30
31 /** @var RevisionStore */
32 private $revisionStore;
33
34 public function execute() {
35 $this->revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
36
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
39
40 // make sure the user is allowed
41 $this->checkUserRightsAny( 'changetags' );
42
43 if ( $user->isBlocked() ) {
44 $this->dieBlocked( $user->getBlock() );
45 }
46
47 // Check if user can add tags
48 if ( $params['tags'] ) {
49 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
52 }
53 }
54
55 // validate and process each revid, rcid and logid
56 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
57 $ret = [];
58 if ( $params['revid'] ) {
59 foreach ( $params['revid'] as $id ) {
60 $ret[] = $this->processIndividual( 'revid', $params, $id );
61 }
62 }
63 if ( $params['rcid'] ) {
64 foreach ( $params['rcid'] as $id ) {
65 $ret[] = $this->processIndividual( 'rcid', $params, $id );
66 }
67 }
68 if ( $params['logid'] ) {
69 foreach ( $params['logid'] as $id ) {
70 $ret[] = $this->processIndividual( 'logid', $params, $id );
71 }
72 }
73
74 ApiResult::setIndexedTagName( $ret, 'result' );
75 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
76 }
77
78 protected static function validateLogId( $logid ) {
79 $dbr = wfGetDB( DB_REPLICA );
80 $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
81 __METHOD__ );
82 return (bool)$result;
83 }
84
85 protected function processIndividual( $type, $params, $id ) {
86 $idResult = [ $type => $id ];
87
88 // validate the ID
89 $valid = false;
90 switch ( $type ) {
91 case 'rcid':
92 $valid = RecentChange::newFromId( $id );
93 break;
94 case 'revid':
95 $valid = $this->revisionStore->getRevisionById( $id );
96 break;
97 case 'logid':
98 $valid = self::validateLogId( $id );
99 break;
100 }
101
102 if ( !$valid ) {
103 $idResult['status'] = 'error';
104 // Messages: apierror-nosuchrcid apierror-nosuchrevid apierror-nosuchlogid
105 $idResult += $this->getErrorFormatter()->formatMessage( [ "apierror-nosuch$type", $id ] );
106 return $idResult;
107 }
108
109 $status = ChangeTags::updateTagsWithChecks( $params['add'],
110 $params['remove'],
111 ( $type === 'rcid' ? $id : null ),
112 ( $type === 'revid' ? $id : null ),
113 ( $type === 'logid' ? $id : null ),
114 null,
115 $params['reason'],
116 $this->getUser() );
117
118 if ( !$status->isOK() ) {
119 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
120 $idResult['status'] = 'skipped';
121 } else {
122 $idResult['status'] = 'failure';
123 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
124 }
125 } else {
126 $idResult['status'] = 'success';
127 if ( is_null( $status->value->logId ) ) {
128 $idResult['noop'] = true;
129 } else {
130 $idResult['actionlogid'] = $status->value->logId;
131 $idResult['added'] = $status->value->addedTags;
132 ApiResult::setIndexedTagName( $idResult['added'], 't' );
133 $idResult['removed'] = $status->value->removedTags;
134 ApiResult::setIndexedTagName( $idResult['removed'], 't' );
135
136 if ( $params['tags'] ) {
137 ChangeTags::addTags( $params['tags'], null, null, $status->value->logId );
138 }
139 }
140 }
141 return $idResult;
142 }
143
144 public function mustBePosted() {
145 return true;
146 }
147
148 public function isWriteMode() {
149 return true;
150 }
151
152 public function getAllowedParams() {
153 return [
154 'rcid' => [
155 ApiBase::PARAM_TYPE => 'integer',
156 ApiBase::PARAM_ISMULTI => true,
157 ],
158 'revid' => [
159 ApiBase::PARAM_TYPE => 'integer',
160 ApiBase::PARAM_ISMULTI => true,
161 ],
162 'logid' => [
163 ApiBase::PARAM_TYPE => 'integer',
164 ApiBase::PARAM_ISMULTI => true,
165 ],
166 'add' => [
167 ApiBase::PARAM_TYPE => 'tags',
168 ApiBase::PARAM_ISMULTI => true,
169 ],
170 'remove' => [
171 ApiBase::PARAM_TYPE => 'string',
172 ApiBase::PARAM_ISMULTI => true,
173 ],
174 'reason' => [
175 ApiBase::PARAM_DFLT => '',
176 ],
177 'tags' => [
178 ApiBase::PARAM_TYPE => 'tags',
179 ApiBase::PARAM_ISMULTI => true,
180 ],
181 ];
182 }
183
184 public function needsToken() {
185 return 'csrf';
186 }
187
188 protected function getExamplesMessages() {
189 return [
190 'action=tag&revid=123&add=vandalism&token=123ABC'
191 => 'apihelp-tag-example-rev',
192 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
193 => 'apihelp-tag-example-log',
194 ];
195 }
196
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag';
199 }
200 }