ApiTag.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\MediaWikiServices;
  21. /**
  22. * @ingroup API
  23. * @since 1.25
  24. */
  25. class ApiTag extends ApiBase {
  26. use ApiBlockInfoTrait;
  27. /** @var \MediaWiki\Revision\RevisionStore */
  28. private $revisionStore;
  29. public function execute() {
  30. $this->revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
  31. $params = $this->extractRequestParams();
  32. $user = $this->getUser();
  33. // make sure the user is allowed
  34. $this->checkUserRightsAny( 'changetags' );
  35. // Fail early if the user is sitewide blocked.
  36. $block = $user->getBlock();
  37. if ( $block && $block->isSitewide() ) {
  38. $this->dieBlocked( $block );
  39. }
  40. // Check if user can add tags
  41. if ( $params['tags'] ) {
  42. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  43. if ( !$ableToTag->isOK() ) {
  44. $this->dieStatus( $ableToTag );
  45. }
  46. }
  47. // validate and process each revid, rcid and logid
  48. $this->requireAtLeastOneParameter( $params, 'revid', 'rcid', 'logid' );
  49. $ret = [];
  50. if ( $params['revid'] ) {
  51. foreach ( $params['revid'] as $id ) {
  52. $ret[] = $this->processIndividual( 'revid', $params, $id );
  53. }
  54. }
  55. if ( $params['rcid'] ) {
  56. foreach ( $params['rcid'] as $id ) {
  57. $ret[] = $this->processIndividual( 'rcid', $params, $id );
  58. }
  59. }
  60. if ( $params['logid'] ) {
  61. foreach ( $params['logid'] as $id ) {
  62. $ret[] = $this->processIndividual( 'logid', $params, $id );
  63. }
  64. }
  65. ApiResult::setIndexedTagName( $ret, 'result' );
  66. $this->getResult()->addValue( null, $this->getModuleName(), $ret );
  67. }
  68. protected static function validateLogId( $logid ) {
  69. $dbr = wfGetDB( DB_REPLICA );
  70. $result = $dbr->selectField( 'logging', 'log_id', [ 'log_id' => $logid ],
  71. __METHOD__ );
  72. return (bool)$result;
  73. }
  74. protected function processIndividual( $type, $params, $id ) {
  75. $user = $this->getUser();
  76. $idResult = [ $type => $id ];
  77. // validate the ID
  78. $valid = false;
  79. switch ( $type ) {
  80. case 'rcid':
  81. $valid = RecentChange::newFromId( $id );
  82. if ( $valid && $this->getPermissionManager()->isBlockedFrom( $user, $valid->getTitle() ) ) {
  83. $idResult['status'] = 'error';
  84. // @phan-suppress-next-line PhanTypeMismatchArgument
  85. $idResult += $this->getErrorFormatter()->formatMessage( ApiMessage::create(
  86. 'apierror-blocked',
  87. 'blocked',
  88. [ 'blockinfo' => $this->getBlockDetails( $user->getBlock() ) ]
  89. ) );
  90. return $idResult;
  91. }
  92. break;
  93. case 'revid':
  94. $valid = $this->revisionStore->getRevisionById( $id );
  95. if (
  96. $valid &&
  97. $this->getPermissionManager()->isBlockedFrom( $user, $valid->getPageAsLinkTarget() )
  98. ) {
  99. $idResult['status'] = 'error';
  100. // @phan-suppress-next-line PhanTypeMismatchArgument
  101. $idResult += $this->getErrorFormatter()->formatMessage( ApiMessage::create(
  102. 'apierror-blocked',
  103. 'blocked',
  104. [ 'blockinfo' => $this->getBlockDetails( $user->getBlock() ) ]
  105. ) );
  106. return $idResult;
  107. }
  108. break;
  109. case 'logid':
  110. $valid = self::validateLogId( $id );
  111. break;
  112. }
  113. if ( !$valid ) {
  114. $idResult['status'] = 'error';
  115. // Messages: apierror-nosuchrcid apierror-nosuchrevid apierror-nosuchlogid
  116. $idResult += $this->getErrorFormatter()->formatMessage( [ "apierror-nosuch$type", $id ] );
  117. return $idResult;
  118. }
  119. $status = ChangeTags::updateTagsWithChecks( $params['add'],
  120. $params['remove'],
  121. ( $type === 'rcid' ? $id : null ),
  122. ( $type === 'revid' ? $id : null ),
  123. ( $type === 'logid' ? $id : null ),
  124. null,
  125. $params['reason'],
  126. $this->getUser() );
  127. if ( !$status->isOK() ) {
  128. if ( $status->hasMessage( 'actionthrottledtext' ) ) {
  129. $idResult['status'] = 'skipped';
  130. } else {
  131. $idResult['status'] = 'failure';
  132. $idResult['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
  133. }
  134. } else {
  135. $idResult['status'] = 'success';
  136. if ( is_null( $status->value->logId ) ) {
  137. $idResult['noop'] = true;
  138. } else {
  139. $idResult['actionlogid'] = $status->value->logId;
  140. $idResult['added'] = $status->value->addedTags;
  141. ApiResult::setIndexedTagName( $idResult['added'], 't' );
  142. $idResult['removed'] = $status->value->removedTags;
  143. ApiResult::setIndexedTagName( $idResult['removed'], 't' );
  144. if ( $params['tags'] ) {
  145. ChangeTags::addTags( $params['tags'], null, null, $status->value->logId );
  146. }
  147. }
  148. }
  149. return $idResult;
  150. }
  151. public function mustBePosted() {
  152. return true;
  153. }
  154. public function isWriteMode() {
  155. return true;
  156. }
  157. public function getAllowedParams() {
  158. return [
  159. 'rcid' => [
  160. ApiBase::PARAM_TYPE => 'integer',
  161. ApiBase::PARAM_ISMULTI => true,
  162. ],
  163. 'revid' => [
  164. ApiBase::PARAM_TYPE => 'integer',
  165. ApiBase::PARAM_ISMULTI => true,
  166. ],
  167. 'logid' => [
  168. ApiBase::PARAM_TYPE => 'integer',
  169. ApiBase::PARAM_ISMULTI => true,
  170. ],
  171. 'add' => [
  172. ApiBase::PARAM_TYPE => 'tags',
  173. ApiBase::PARAM_ISMULTI => true,
  174. ],
  175. 'remove' => [
  176. ApiBase::PARAM_TYPE => 'string',
  177. ApiBase::PARAM_ISMULTI => true,
  178. ],
  179. 'reason' => [
  180. ApiBase::PARAM_DFLT => '',
  181. ],
  182. 'tags' => [
  183. ApiBase::PARAM_TYPE => 'tags',
  184. ApiBase::PARAM_ISMULTI => true,
  185. ],
  186. ];
  187. }
  188. public function needsToken() {
  189. return 'csrf';
  190. }
  191. protected function getExamplesMessages() {
  192. return [
  193. 'action=tag&revid=123&add=vandalism&token=123ABC'
  194. => 'apihelp-tag-example-rev',
  195. 'action=tag&logid=123&remove=spam&reason=Wrongly+applied&token=123ABC'
  196. => 'apihelp-tag-example-log',
  197. ];
  198. }
  199. public function getHelpUrls() {
  200. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tag';
  201. }
  202. }