ApiPatrol.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * API for MediaWiki 1.14+
  4. *
  5. * Copyright © 2008 Soxred93 soxred93@gmail.com,
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Allows user to patrol pages
  27. * @ingroup API
  28. */
  29. class ApiPatrol extends ApiBase {
  30. /**
  31. * Patrols the article or provides the reason the patrol failed.
  32. */
  33. public function execute() {
  34. $params = $this->extractRequestParams();
  35. $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
  36. if ( isset( $params['rcid'] ) ) {
  37. $rc = RecentChange::newFromId( $params['rcid'] );
  38. if ( !$rc ) {
  39. $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
  40. }
  41. } else {
  42. $store = MediaWikiServices::getInstance()->getRevisionStore();
  43. $rev = $store->getRevisionById( $params['revid'] );
  44. if ( !$rev ) {
  45. $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
  46. }
  47. $rc = $store->getRecentChange( $rev );
  48. if ( !$rc ) {
  49. $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
  50. }
  51. }
  52. $user = $this->getUser();
  53. $tags = $params['tags'];
  54. // Check if user can add tags
  55. if ( !is_null( $tags ) ) {
  56. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
  57. if ( !$ableToTag->isOK() ) {
  58. $this->dieStatus( $ableToTag );
  59. }
  60. }
  61. $retval = $rc->doMarkPatrolled( $user, false, $tags );
  62. if ( $retval ) {
  63. $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
  64. }
  65. $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
  66. ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
  67. $this->getResult()->addValue( null, $this->getModuleName(), $result );
  68. }
  69. public function mustBePosted() {
  70. return true;
  71. }
  72. public function isWriteMode() {
  73. return true;
  74. }
  75. public function getAllowedParams() {
  76. return [
  77. 'rcid' => [
  78. ApiBase::PARAM_TYPE => 'integer'
  79. ],
  80. 'revid' => [
  81. ApiBase::PARAM_TYPE => 'integer'
  82. ],
  83. 'tags' => [
  84. ApiBase::PARAM_TYPE => 'tags',
  85. ApiBase::PARAM_ISMULTI => true,
  86. ],
  87. ];
  88. }
  89. public function needsToken() {
  90. return 'patrol';
  91. }
  92. protected function getExamplesMessages() {
  93. return [
  94. 'action=patrol&token=123ABC&rcid=230672766'
  95. => 'apihelp-patrol-example-rcid',
  96. 'action=patrol&token=123ABC&revid=230672766'
  97. => 'apihelp-patrol-example-revid',
  98. ];
  99. }
  100. public function getHelpUrls() {
  101. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
  102. }
  103. }