MediaWiki  master
ApiPatrol.php
Go to the documentation of this file.
1 <?php
26 
31 class ApiPatrol extends ApiBase {
32 
36  public function execute() {
37  $params = $this->extractRequestParams();
38  $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
39 
40  if ( isset( $params['rcid'] ) ) {
41  $rc = RecentChange::newFromId( $params['rcid'] );
42  if ( !$rc ) {
43  $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
44  }
45  } else {
46  $store = MediaWikiServices::getInstance()->getRevisionStore();
47  $rev = $store->getRevisionById( $params['revid'] );
48  if ( !$rev ) {
49  $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
50  }
51  $rc = $store->getRecentChange( $rev );
52  if ( !$rc ) {
53  $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
54  }
55  }
56 
57  $user = $this->getUser();
58  $tags = $params['tags'];
59 
60  // Check if user can add tags
61  if ( $tags !== null ) {
62  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
63  if ( !$ableToTag->isOK() ) {
64  $this->dieStatus( $ableToTag );
65  }
66  }
67 
68  $retval = $rc->doMarkPatrolled( $user, false, $tags );
69 
70  if ( $retval ) {
71  $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
72  }
73 
74  $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
75  ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
76  $this->getResult()->addValue( null, $this->getModuleName(), $result );
77  }
78 
79  public function mustBePosted() {
80  return true;
81  }
82 
83  public function isWriteMode() {
84  return true;
85  }
86 
87  public function getAllowedParams() {
88  return [
89  'rcid' => [
90  ApiBase::PARAM_TYPE => 'integer'
91  ],
92  'revid' => [
93  ApiBase::PARAM_TYPE => 'integer'
94  ],
95  'tags' => [
96  ApiBase::PARAM_TYPE => 'tags',
97  ApiBase::PARAM_ISMULTI => true,
98  ],
99  ];
100  }
101 
102  public function needsToken() {
103  return 'patrol';
104  }
105 
106  protected function getExamplesMessages() {
107  return [
108  'action=patrol&token=123ABC&rcid=230672766'
109  => 'apihelp-patrol-example-rcid',
110  'action=patrol&token=123ABC&revid=230672766'
111  => 'apihelp-patrol-example-revid',
112  ];
113  }
114 
115  public function getHelpUrls() {
116  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
117  }
118 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:155
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiPatrol\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiPatrol.php:115
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiPatrol\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiPatrol.php:106
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiPatrol\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiPatrol.php:83
ApiPatrol\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiPatrol.php:79
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
ApiPatrol\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiPatrol.php:102
RecentChange\newFromId
static newFromId( $rcid)
Obtain the recent change with a given rc_id value.
Definition: RecentChange.php:207
ApiBase\requireOnlyOneParameter
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:854
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:544
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1440
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiPatrol
Allows user to patrol pages.
Definition: ApiPatrol.php:31
ApiPatrol\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiPatrol.php:87
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1193
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:470
ApiPatrol\execute
execute()
Patrols the article or provides the reason the patrol failed.
Definition: ApiPatrol.php:36