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