Update ApiBase::PARAM_* comments
[mediawiki.git] / includes / api / ApiTag.php
blob527c6cb106b082e69c5ef8dd6801db106fade2a0
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 /**
23 * @ingroup API
24 * @since 1.25
26 class ApiTag extends ApiBase {
28 protected function getAvailableTags() {
29 return ChangeTags::listExplicitlyDefinedTags();
32 public function execute() {
33 $params = $this->extractRequestParams();
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' );
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 );
49 if ( $params['rcid'] ) {
50 foreach ( $params['rcid'] as $id ) {
51 $ret[] = $this->processIndividual( 'rcid', $params, $id );
54 if ( $params['logid'] ) {
55 foreach ( $params['logid'] as $id ) {
56 $ret[] = $this->processIndividual( 'logid', $params, $id );
60 ApiResult::setIndexedTagName( $ret, 'result' );
61 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
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;
71 protected function processIndividual( $type, $params, $id ) {
72 $idResult = array( $type => $id );
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;
88 if ( !$valid ) {
89 $idResult['status'] = 'error';
90 $idResult += $this->parseMsg( array( "nosuch$type", $id ) );
91 return $idResult;
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() );
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' );
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' );
122 return $idResult;
125 public function mustBePosted() {
126 return true;
129 public function isWriteMode() {
130 return true;
133 public function getAllowedParams() {
134 return array(
135 'rcid' => array(
136 ApiBase::PARAM_TYPE => 'integer',
137 ApiBase::PARAM_ISMULTI => true,
139 'revid' => array(
140 ApiBase::PARAM_TYPE => 'integer',
141 ApiBase::PARAM_ISMULTI => true,
143 'logid' => array(
144 ApiBase::PARAM_TYPE => 'integer',
145 ApiBase::PARAM_ISMULTI => true,
147 'add' => array(
148 ApiBase::PARAM_TYPE => $this->getAvailableTags(),
149 ApiBase::PARAM_ISMULTI => true,
151 'remove' => array(
152 ApiBase::PARAM_TYPE => 'string',
153 ApiBase::PARAM_ISMULTI => true,
155 'reason' => array(
156 ApiBase::PARAM_DFLT => '',
161 public function needsToken() {
162 return 'csrf';
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',
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/API:Tag';