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