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
26 class ApiTag
extends ApiBase
{
28 protected function getAvailableTags() {
29 return ChangeTags
::listExplicitlyDefinedTags();
32 public function execute() {
33 $params = $this->extractRequestParams();
34 $user = $this->getUser();
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",
42 if ( $user->isBlocked() ) {
43 $this->dieBlocked( $user->getBlock() );
46 // validate and process each revid, rcid and logid
47 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
49 if ( $params['revid'] ) {
50 foreach ( $params['revid'] as $id ) {
51 $ret[] = $this->processIndividual( 'revid', $params, $id );
54 if ( $params['rcid'] ) {
55 foreach ( $params['rcid'] as $id ) {
56 $ret[] = $this->processIndividual( 'rcid', $params, $id );
59 if ( $params['logid'] ) {
60 foreach ( $params['logid'] as $id ) {
61 $ret[] = $this->processIndividual( 'logid', $params, $id );
65 ApiResult
::setIndexedTagName( $ret, 'result' );
66 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
69 protected static function validateLogId( $logid ) {
70 $dbr = wfGetDB( DB_SLAVE
);
71 $result = $dbr->selectField( 'logging', 'log_id', array( 'log_id' => $logid ),
76 protected function processIndividual( $type, $params, $id ) {
77 $idResult = array( $type => $id );
83 $valid = RecentChange
::newFromId( $id );
86 $valid = Revision
::newFromId( $id );
89 $valid = self
::validateLogId( $id );
94 $idResult['status'] = 'error';
95 $idResult +
= $this->parseMsg( array( "nosuch$type", $id ) );
99 $status = ChangeTags
::updateTagsWithChecks( $params['add'],
101 ( $type === 'rcid' ?
$id : null ),
102 ( $type === 'revid' ?
$id : null ),
103 ( $type === 'logid' ?
$id : null ),
108 if ( !$status->isOK() ) {
109 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
110 $idResult['status'] = 'skipped';
112 $idResult['status'] = 'failure';
113 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
116 $idResult['status'] = 'success';
117 if ( is_null( $status->value
->logId
) ) {
118 $idResult['noop'] = '';
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' );
130 public function mustBePosted() {
134 public function isWriteMode() {
138 public function getAllowedParams() {
141 ApiBase
::PARAM_TYPE
=> 'integer',
142 ApiBase
::PARAM_ISMULTI
=> true,
145 ApiBase
::PARAM_TYPE
=> 'integer',
146 ApiBase
::PARAM_ISMULTI
=> true,
149 ApiBase
::PARAM_TYPE
=> 'integer',
150 ApiBase
::PARAM_ISMULTI
=> true,
153 ApiBase
::PARAM_TYPE
=> $this->getAvailableTags(),
154 ApiBase
::PARAM_ISMULTI
=> true,
157 ApiBase
::PARAM_TYPE
=> 'string',
158 ApiBase
::PARAM_ISMULTI
=> true,
161 ApiBase
::PARAM_DFLT
=> '',
166 public function needsToken() {
170 protected function getExamplesMessages() {
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',
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/API:Tag';