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 if ( $user->isBlocked() ) {
44 $this->dieBlocked( $user->getBlock() );
47 // Check if user can add tags
48 if ( $params['tags'] ) {
49 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
55 // validate and process each revid, rcid and logid
56 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
58 if ( $params['revid'] ) {
59 foreach ( $params['revid'] as $id ) {
60 $ret[] = $this->processIndividual( 'revid', $params, $id );
63 if ( $params['rcid'] ) {
64 foreach ( $params['rcid'] as $id ) {
65 $ret[] = $this->processIndividual( 'rcid', $params, $id );
68 if ( $params['logid'] ) {
69 foreach ( $params['logid'] as $id ) {
70 $ret[] = $this->processIndividual( 'logid', $params, $id );
74 ApiResult
::setIndexedTagName( $ret, 'result' );
75 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
78 protected static function validateLogId( $logid ) {
79 $dbr = wfGetDB( DB_REPLICA
);
80 $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
85 protected function processIndividual( $type, $params, $id ) {
86 $idResult = [ $type => $id ];
92 $valid = RecentChange
::newFromId( $id );
95 $valid = $this->revisionStore
->getRevisionById( $id );
98 $valid = self
::validateLogId( $id );
103 $idResult['status'] = 'error';
104 // Messages: apierror-nosuchrcid apierror-nosuchrevid apierror-nosuchlogid
105 $idResult +
= $this->getErrorFormatter()->formatMessage( [ "apierror-nosuch$type", $id ] );
109 $status = ChangeTags
::updateTagsWithChecks( $params['add'],
111 ( $type === 'rcid' ?
$id : null ),
112 ( $type === 'revid' ?
$id : null ),
113 ( $type === 'logid' ?
$id : null ),
118 if ( !$status->isOK() ) {
119 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
120 $idResult['status'] = 'skipped';
122 $idResult['status'] = 'failure';
123 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
126 $idResult['status'] = 'success';
127 if ( is_null( $status->value
->logId
) ) {
128 $idResult['noop'] = true;
130 $idResult['actionlogid'] = $status->value
->logId
;
131 $idResult['added'] = $status->value
->addedTags
;
132 ApiResult
::setIndexedTagName( $idResult['added'], 't' );
133 $idResult['removed'] = $status->value
->removedTags
;
134 ApiResult
::setIndexedTagName( $idResult['removed'], 't' );
136 if ( $params['tags'] ) {
137 ChangeTags
::addTags( $params['tags'], null, null, $status->value
->logId
);
144 public function mustBePosted() {
148 public function isWriteMode() {
152 public function getAllowedParams() {
155 ApiBase
::PARAM_TYPE
=> 'integer',
156 ApiBase
::PARAM_ISMULTI
=> true,
159 ApiBase
::PARAM_TYPE
=> 'integer',
160 ApiBase
::PARAM_ISMULTI
=> true,
163 ApiBase
::PARAM_TYPE
=> 'integer',
164 ApiBase
::PARAM_ISMULTI
=> true,
167 ApiBase
::PARAM_TYPE
=> 'tags',
168 ApiBase
::PARAM_ISMULTI
=> true,
171 ApiBase
::PARAM_TYPE
=> 'string',
172 ApiBase
::PARAM_ISMULTI
=> true,
175 ApiBase
::PARAM_DFLT
=> '',
178 ApiBase
::PARAM_TYPE
=> 'tags',
179 ApiBase
::PARAM_ISMULTI
=> true,
184 public function needsToken() {
188 protected function getExamplesMessages() {
190 'action=tag&revid=123&add=vandalism&token=123ABC'
191 => 'apihelp-tag-example-rev',
192 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
193 => 'apihelp-tag-example-log',
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag';