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
22 use MediaWiki\MediaWikiServices
;
23 use MediaWiki\Revision\RevisionStore
;
29 class ApiTag
extends ApiBase
{
31 /** @var RevisionStore */
32 private $revisionStore;
34 public function execute() {
35 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
37 $params = $this->extractRequestParams();
38 $user = $this->getUser();
40 // make sure the user is allowed
41 $this->checkUserRightsAny( 'changetags' );
43 // @TODO Use PermissionManager::isBlockedFrom() instead.
44 $block = $user->getBlock();
46 $this->dieBlocked( $block );
49 // Check if user can add tags
50 if ( $params['tags'] ) {
51 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
52 if ( !$ableToTag->isOK() ) {
53 $this->dieStatus( $ableToTag );
57 // validate and process each revid, rcid and logid
58 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
60 if ( $params['revid'] ) {
61 foreach ( $params['revid'] as $id ) {
62 $ret[] = $this->processIndividual( 'revid', $params, $id );
65 if ( $params['rcid'] ) {
66 foreach ( $params['rcid'] as $id ) {
67 $ret[] = $this->processIndividual( 'rcid', $params, $id );
70 if ( $params['logid'] ) {
71 foreach ( $params['logid'] as $id ) {
72 $ret[] = $this->processIndividual( 'logid', $params, $id );
76 ApiResult
::setIndexedTagName( $ret, 'result' );
77 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
80 protected static function validateLogId( $logid ) {
81 $dbr = wfGetDB( DB_REPLICA
);
82 $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
87 protected function processIndividual( $type, $params, $id ) {
88 $idResult = [ $type => $id ];
94 $valid = RecentChange
::newFromId( $id );
97 $valid = $this->revisionStore
->getRevisionById( $id );
100 $valid = self
::validateLogId( $id );
105 $idResult['status'] = 'error';
106 // Messages: apierror-nosuchrcid apierror-nosuchrevid apierror-nosuchlogid
107 $idResult +
= $this->getErrorFormatter()->formatMessage( [ "apierror-nosuch$type", $id ] );
111 $status = ChangeTags
::updateTagsWithChecks( $params['add'],
113 ( $type === 'rcid' ?
$id : null ),
114 ( $type === 'revid' ?
$id : null ),
115 ( $type === 'logid' ?
$id : null ),
120 if ( !$status->isOK() ) {
121 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
122 $idResult['status'] = 'skipped';
124 $idResult['status'] = 'failure';
125 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
128 $idResult['status'] = 'success';
129 if ( is_null( $status->value
->logId
) ) {
130 $idResult['noop'] = true;
132 $idResult['actionlogid'] = $status->value
->logId
;
133 $idResult['added'] = $status->value
->addedTags
;
134 ApiResult
::setIndexedTagName( $idResult['added'], 't' );
135 $idResult['removed'] = $status->value
->removedTags
;
136 ApiResult
::setIndexedTagName( $idResult['removed'], 't' );
138 if ( $params['tags'] ) {
139 ChangeTags
::addTags( $params['tags'], null, null, $status->value
->logId
);
146 public function mustBePosted() {
150 public function isWriteMode() {
154 public function getAllowedParams() {
157 ApiBase
::PARAM_TYPE
=> 'integer',
158 ApiBase
::PARAM_ISMULTI
=> true,
161 ApiBase
::PARAM_TYPE
=> 'integer',
162 ApiBase
::PARAM_ISMULTI
=> true,
165 ApiBase
::PARAM_TYPE
=> 'integer',
166 ApiBase
::PARAM_ISMULTI
=> true,
169 ApiBase
::PARAM_TYPE
=> 'tags',
170 ApiBase
::PARAM_ISMULTI
=> true,
173 ApiBase
::PARAM_TYPE
=> 'string',
174 ApiBase
::PARAM_ISMULTI
=> true,
177 ApiBase
::PARAM_DFLT
=> '',
180 ApiBase
::PARAM_TYPE
=> 'tags',
181 ApiBase
::PARAM_ISMULTI
=> true,
186 public function needsToken() {
190 protected function getExamplesMessages() {
192 'action=tag&revid=123&add=vandalism&token=123ABC'
193 => 'apihelp-tag-example-rev',
194 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
195 => 'apihelp-tag-example-log',
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag';