User::isSafeToLoad() should return false if MW_NO_SESSION
[mediawiki.git] / includes / api / ApiTag.php
blobc2d1e0d29d15aaddd88f32114555a02df685f309
1 <?php
3 /**
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
19 * @file
22 /**
23 * @ingroup API
24 * @since 1.25
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 if ( !$user->isAllowed( 'changetags' ) ) {
34 $this->dieUsage( "You don't have permission to add or remove change tags from individual edits",
35 'permissiondenied' );
38 if ( $user->isBlocked() ) {
39 $this->dieBlocked( $user->getBlock() );
42 // validate and process each revid, rcid and logid
43 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
44 $ret = [];
45 if ( $params['revid'] ) {
46 foreach ( $params['revid'] as $id ) {
47 $ret[] = $this->processIndividual( 'revid', $params, $id );
50 if ( $params['rcid'] ) {
51 foreach ( $params['rcid'] as $id ) {
52 $ret[] = $this->processIndividual( 'rcid', $params, $id );
55 if ( $params['logid'] ) {
56 foreach ( $params['logid'] as $id ) {
57 $ret[] = $this->processIndividual( 'logid', $params, $id );
61 ApiResult::setIndexedTagName( $ret, 'result' );
62 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
65 protected static function validateLogId( $logid ) {
66 $dbr = wfGetDB( DB_SLAVE );
67 $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
68 __METHOD__ );
69 return (bool)$result;
72 protected function processIndividual( $type, $params, $id ) {
73 $idResult = [ $type => $id ];
75 // validate the ID
76 $valid = false;
77 switch ( $type ) {
78 case 'rcid':
79 $valid = RecentChange::newFromId( $id );
80 break;
81 case 'revid':
82 $valid = Revision::newFromId( $id );
83 break;
84 case 'logid':
85 $valid = self::validateLogId( $id );
86 break;
89 if ( !$valid ) {
90 $idResult['status'] = 'error';
91 $idResult += $this->parseMsg( [ "nosuch$type", $id ] );
92 return $idResult;
95 $status = ChangeTags::updateTagsWithChecks( $params['add'],
96 $params['remove'],
97 ( $type === 'rcid' ? $id : null ),
98 ( $type === 'revid' ? $id : null ),
99 ( $type === 'logid' ? $id : null ),
100 null,
101 $params['reason'],
102 $this->getUser() );
104 if ( !$status->isOK() ) {
105 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
106 $idResult['status'] = 'skipped';
107 } else {
108 $idResult['status'] = 'failure';
109 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
111 } else {
112 $idResult['status'] = 'success';
113 if ( is_null( $status->value->logId ) ) {
114 $idResult['noop'] = '';
115 } else {
116 $idResult['actionlogid'] = $status->value->logId;
117 $idResult['added'] = $status->value->addedTags;
118 ApiResult::setIndexedTagName( $idResult['added'], 't' );
119 $idResult['removed'] = $status->value->removedTags;
120 ApiResult::setIndexedTagName( $idResult['removed'], 't' );
123 return $idResult;
126 public function mustBePosted() {
127 return true;
130 public function isWriteMode() {
131 return true;
134 public function getAllowedParams() {
135 return [
136 'rcid' => [
137 ApiBase::PARAM_TYPE => 'integer',
138 ApiBase::PARAM_ISMULTI => true,
140 'revid' => [
141 ApiBase::PARAM_TYPE => 'integer',
142 ApiBase::PARAM_ISMULTI => true,
144 'logid' => [
145 ApiBase::PARAM_TYPE => 'integer',
146 ApiBase::PARAM_ISMULTI => true,
148 'add' => [
149 ApiBase::PARAM_TYPE => 'tags',
150 ApiBase::PARAM_ISMULTI => true,
152 'remove' => [
153 ApiBase::PARAM_TYPE => 'string',
154 ApiBase::PARAM_ISMULTI => true,
156 'reason' => [
157 ApiBase::PARAM_DFLT => '',
162 public function needsToken() {
163 return 'csrf';
166 protected function getExamplesMessages() {
167 return [
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';