MediaWiki master
ApiPatrol.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Api;
12
18
23class ApiPatrol extends ApiBase {
24 private RevisionStore $revisionStore;
25 private PatrolManager $patrolManager;
26 private RecentChangeLookup $recentChangeLookup;
27
28 public function __construct(
29 ApiMain $main,
30 string $action,
31 RevisionStore $revisionStore,
32 PatrolManager $patrolManager,
33 RecentChangeLookup $recentChangeLookup
34 ) {
35 parent::__construct( $main, $action );
36
37 $this->revisionStore = $revisionStore;
38 $this->patrolManager = $patrolManager;
39 $this->recentChangeLookup = $recentChangeLookup;
40 }
41
45 public function execute() {
46 $params = $this->extractRequestParams();
47 $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
48
49 if ( isset( $params['rcid'] ) ) {
50 $rc = $this->recentChangeLookup->getRecentChangeById( $params['rcid'] );
51 if ( !$rc ) {
52 $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
53 }
54 } else {
55 $rev = $this->revisionStore->getRevisionById( $params['revid'] );
56 if ( !$rev ) {
57 $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
58 }
59 $rc = $this->revisionStore->getRecentChange( $rev );
60 if ( !$rc ) {
61 $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
62 }
63 }
64
65 $user = $this->getUser();
66 $tags = $params['tags'];
67
68 // Check if user can add tags
69 if ( $tags !== null ) {
70 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
71 if ( !$ableToTag->isOK() ) {
72 $this->dieStatus( $ableToTag );
73 }
74 }
75
76 $status = $this->patrolManager->markPatrolled( $rc, $user, $tags );
77
78 if ( !$status->isGood() ) {
79 $this->dieStatus( $status );
80 }
81
82 $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
83 ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
84 $this->getResult()->addValue( null, $this->getModuleName(), $result );
85 }
86
88 public function mustBePosted() {
89 return true;
90 }
91
93 public function isWriteMode() {
94 return true;
95 }
96
98 public function getAllowedParams() {
99 return [
100 'rcid' => [
101 ParamValidator::PARAM_TYPE => 'integer'
102 ],
103 'revid' => [
104 ParamValidator::PARAM_TYPE => 'integer'
105 ],
106 'tags' => [
107 ParamValidator::PARAM_TYPE => 'tags',
108 ParamValidator::PARAM_ISMULTI => true,
109 ],
110 ];
111 }
112
114 public function needsToken() {
115 return 'patrol';
116 }
117
119 protected function getExamplesMessages() {
120 return [
121 'action=patrol&token=123ABC&rcid=230672766'
122 => 'apihelp-patrol-example-rcid',
123 'action=patrol&token=123ABC&revid=230672766'
124 => 'apihelp-patrol-example-revid',
125 ];
126 }
127
129 public function getHelpUrls() {
130 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
131 }
132}
133
135class_alias( ApiPatrol::class, 'ApiPatrol' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getResult()
Get the result object.
Definition ApiBase.php:682
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:961
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
Allows user to patrol pages.
Definition ApiPatrol.php:23
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition ApiPatrol.php:98
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
Definition ApiPatrol.php:93
execute()
Patrols the article or provides the reason the patrol failed.
Definition ApiPatrol.php:45
__construct(ApiMain $main, string $action, RevisionStore $revisionStore, PatrolManager $patrolManager, RecentChangeLookup $recentChangeLookup)
Definition ApiPatrol.php:28
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
Definition ApiPatrol.php:88
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Recent changes tagging.
Service for looking up page revisions.
Service for formatting and validating API parameters.