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();
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",
41 // validate and process each revid, rcid and logid
42 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
43 $result = $this->getResult();
45 if ( $params['revid'] ) {
46 foreach ( $params['revid'] as $id ) {
47 $ret[] = $this->processIndividual( 'revid', $params, $id, $result );
50 if ( $params['rcid'] ) {
51 foreach ( $params['rcid'] as $id ) {
52 $ret[] = $this->processIndividual( 'rcid', $params, $id, $result );
55 if ( $params['logid'] ) {
56 foreach ( $params['logid'] as $id ) {
57 $ret[] = $this->processIndividual( 'logid', $params, $id, $result );
61 $result->setIndexedTagName( $ret, 'result' );
62 $result->addValue( null, $this->getModuleName(), $ret );
65 protected static function validateLogId( $logid ) {
66 $dbr = wfGetDB( DB_SLAVE
);
67 $result = $dbr->selectField( 'logging', 'log_id', array( 'log_id' => $logid ),
72 protected function processIndividual( $type, $params, $id, &$result ) {
73 $idResult = array( $type => $id );
79 $valid = RecentChange
::newFromId( $id );
82 $valid = Revision
::newFromId( $id );
85 $valid = self
::validateLogId( $id );
90 $idResult['status'] = 'error';
91 $idResult +
= $this->parseMsg( array( "nosuch$type", $id ) );
95 $status = ChangeTags
::updateTagsWithChecks( $params['add'],
97 ( $type === 'rcid' ?
$id : null ),
98 ( $type === 'revid' ?
$id : null ),
99 ( $type === 'logid' ?
$id : null ),
104 if ( !$status->isOK() ) {
105 if ( $status->hasWarning( 'actionthrottledtext' ) ) {
106 $idResult['status'] = 'skipped';
108 $idResult['status'] = 'failure';
109 $ret['errors'] = $result->convertStatusToArray( $status, 'error' );
112 $idResult['status'] = 'success';
113 if ( is_null( $status->value
->logId
) ) {
114 $idResult['noop'] = '';
116 $idResult['actionlogid'] = $status->value
->logId
;
117 $idResult['added'] = $status->value
->addedTags
;
118 $result->setIndexedTagName( $idResult['added'], 't' );
119 $idResult['removed'] = $status->value
->removedTags
;
120 $result->setIndexedTagName( $idResult['removed'], 't' );
126 public function mustBePosted() {
130 public function isWriteMode() {
134 public function getAllowedParams() {
137 ApiBase
::PARAM_TYPE
=> 'integer',
138 ApiBase
::PARAM_ISMULTI
=> true,
141 ApiBase
::PARAM_TYPE
=> 'integer',
142 ApiBase
::PARAM_ISMULTI
=> true,
145 ApiBase
::PARAM_TYPE
=> 'integer',
146 ApiBase
::PARAM_ISMULTI
=> true,
149 ApiBase
::PARAM_TYPE
=> $this->getAvailableTags(),
150 ApiBase
::PARAM_ISMULTI
=> true,
153 ApiBase
::PARAM_TYPE
=> 'string',
154 ApiBase
::PARAM_ISMULTI
=> true,
157 ApiBase
::PARAM_DFLT
=> '',
162 public function needsToken() {
166 protected function getExamplesMessages() {
168 'action=tag&revid=123&add=vandalism&token=123ABC'
169 => 'apihelp-tag-example-rev',
170 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
171 => 'apihelp-tag-example-log',
175 public function getHelpUrls() {
176 return 'https://www.mediawiki.org/wiki/API:Tag';