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 public function execute() {
29 $params = $this->extractRequestParams();
30 $user = $this->getUser();
32 // make sure the user is allowed
33 $this->checkUserRightsAny( 'changetags' );
35 if ( $user->isBlocked() ) {
36 $this->dieBlocked( $user->getBlock() );
39 // validate and process each revid, rcid and logid
40 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
42 if ( $params['revid'] ) {
43 foreach ( $params['revid'] as $id ) {
44 $ret[] = $this->processIndividual( 'revid', $params, $id );
47 if ( $params['rcid'] ) {
48 foreach ( $params['rcid'] as $id ) {
49 $ret[] = $this->processIndividual( 'rcid', $params, $id );
52 if ( $params['logid'] ) {
53 foreach ( $params['logid'] as $id ) {
54 $ret[] = $this->processIndividual( 'logid', $params, $id );
58 ApiResult
::setIndexedTagName( $ret, 'result' );
59 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
62 protected static function validateLogId( $logid ) {
63 $dbr = wfGetDB( DB_REPLICA
);
64 $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
69 protected function processIndividual( $type, $params, $id ) {
70 $idResult = [ $type => $id ];
76 $valid = RecentChange
::newFromId( $id );
79 $valid = Revision
::newFromId( $id );
82 $valid = self
::validateLogId( $id );
87 $idResult['status'] = 'error';
88 // Messages: apierror-nosuchrcid apierror-nosuchrevid apierror-nosuchlogid
89 $idResult +
= $this->getErrorFormatter()->formatMessage( [ "apierror-nosuch$type", $id ] );
93 $status = ChangeTags
::updateTagsWithChecks( $params['add'],
95 ( $type === 'rcid' ?
$id : null ),
96 ( $type === 'revid' ?
$id : null ),
97 ( $type === 'logid' ?
$id : null ),
102 if ( !$status->isOK() ) {
103 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
104 $idResult['status'] = 'skipped';
106 $idResult['status'] = 'failure';
107 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
110 $idResult['status'] = 'success';
111 if ( is_null( $status->value
->logId
) ) {
112 $idResult['noop'] = true;
114 $idResult['actionlogid'] = $status->value
->logId
;
115 $idResult['added'] = $status->value
->addedTags
;
116 ApiResult
::setIndexedTagName( $idResult['added'], 't' );
117 $idResult['removed'] = $status->value
->removedTags
;
118 ApiResult
::setIndexedTagName( $idResult['removed'], 't' );
124 public function mustBePosted() {
128 public function isWriteMode() {
132 public function getAllowedParams() {
135 ApiBase
::PARAM_TYPE
=> 'integer',
136 ApiBase
::PARAM_ISMULTI
=> true,
139 ApiBase
::PARAM_TYPE
=> 'integer',
140 ApiBase
::PARAM_ISMULTI
=> true,
143 ApiBase
::PARAM_TYPE
=> 'integer',
144 ApiBase
::PARAM_ISMULTI
=> true,
147 ApiBase
::PARAM_TYPE
=> 'tags',
148 ApiBase
::PARAM_ISMULTI
=> true,
151 ApiBase
::PARAM_TYPE
=> 'string',
152 ApiBase
::PARAM_ISMULTI
=> true,
155 ApiBase
::PARAM_DFLT
=> '',
160 public function needsToken() {
164 protected function getExamplesMessages() {
166 'action=tag&revid=123&add=vandalism&token=123ABC'
167 => 'apihelp-tag-example-rev',
168 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
169 => 'apihelp-tag-example-log',
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/API:Tag';