Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / api / ApiTag.php
blob4bf799ec90bc8b55451acd57bd2c62880b821ffd
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 protected function getAvailableTags() {
29 return ChangeTags::listExplicitlyDefinedTags();
32 public function execute() {
33 $params = $this->extractRequestParams();
34 $user = $this->getUser();
36 // make sure the user is allowed
37 if ( !$user->isAllowed( 'changetags' ) ) {
38 $this->dieUsage( "You don't have permission to add or remove change tags from individual edits",
39 'permissiondenied' );
42 if ( $user->isBlocked() ) {
43 $this->dieBlocked( $user->getBlock() );
46 // validate and process each revid, rcid and logid
47 $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
48 $ret = array();
49 if ( $params['revid'] ) {
50 foreach ( $params['revid'] as $id ) {
51 $ret[] = $this->processIndividual( 'revid', $params, $id );
54 if ( $params['rcid'] ) {
55 foreach ( $params['rcid'] as $id ) {
56 $ret[] = $this->processIndividual( 'rcid', $params, $id );
59 if ( $params['logid'] ) {
60 foreach ( $params['logid'] as $id ) {
61 $ret[] = $this->processIndividual( 'logid', $params, $id );
65 ApiResult::setIndexedTagName( $ret, 'result' );
66 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
69 protected static function validateLogId( $logid ) {
70 $dbr = wfGetDB( DB_SLAVE );
71 $result = $dbr->selectField( 'logging', 'log_id', array( 'log_id' => $logid ),
72 __METHOD__ );
73 return (bool)$result;
76 protected function processIndividual( $type, $params, $id ) {
77 $idResult = array( $type => $id );
79 // validate the ID
80 $valid = false;
81 switch ( $type ) {
82 case 'rcid':
83 $valid = RecentChange::newFromId( $id );
84 break;
85 case 'revid':
86 $valid = Revision::newFromId( $id );
87 break;
88 case 'logid':
89 $valid = self::validateLogId( $id );
90 break;
93 if ( !$valid ) {
94 $idResult['status'] = 'error';
95 $idResult += $this->parseMsg( array( "nosuch$type", $id ) );
96 return $idResult;
99 $status = ChangeTags::updateTagsWithChecks( $params['add'],
100 $params['remove'],
101 ( $type === 'rcid' ? $id : null ),
102 ( $type === 'revid' ? $id : null ),
103 ( $type === 'logid' ? $id : null ),
104 null,
105 $params['reason'],
106 $this->getUser() );
108 if ( !$status->isOK() ) {
109 if ( $status->hasMessage( 'actionthrottledtext' ) ) {
110 $idResult['status'] = 'skipped';
111 } else {
112 $idResult['status'] = 'failure';
113 $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
115 } else {
116 $idResult['status'] = 'success';
117 if ( is_null( $status->value->logId ) ) {
118 $idResult['noop'] = '';
119 } else {
120 $idResult['actionlogid'] = $status->value->logId;
121 $idResult['added'] = $status->value->addedTags;
122 ApiResult::setIndexedTagName( $idResult['added'], 't' );
123 $idResult['removed'] = $status->value->removedTags;
124 ApiResult::setIndexedTagName( $idResult['removed'], 't' );
127 return $idResult;
130 public function mustBePosted() {
131 return true;
134 public function isWriteMode() {
135 return true;
138 public function getAllowedParams() {
139 return array(
140 'rcid' => array(
141 ApiBase::PARAM_TYPE => 'integer',
142 ApiBase::PARAM_ISMULTI => true,
144 'revid' => array(
145 ApiBase::PARAM_TYPE => 'integer',
146 ApiBase::PARAM_ISMULTI => true,
148 'logid' => array(
149 ApiBase::PARAM_TYPE => 'integer',
150 ApiBase::PARAM_ISMULTI => true,
152 'add' => array(
153 ApiBase::PARAM_TYPE => $this->getAvailableTags(),
154 ApiBase::PARAM_ISMULTI => true,
156 'remove' => array(
157 ApiBase::PARAM_TYPE => 'string',
158 ApiBase::PARAM_ISMULTI => true,
160 'reason' => array(
161 ApiBase::PARAM_DFLT => '',
166 public function needsToken() {
167 return 'csrf';
170 protected function getExamplesMessages() {
171 return array(
172 'action=tag&revid=123&add=vandalism&token=123ABC'
173 => 'apihelp-tag-example-rev',
174 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
175 => 'apihelp-tag-example-log',
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/API:Tag';