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