MediaWiki master
ApiPatrol.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Api;
12
18
23class ApiPatrol extends ApiBase {
24
25 public function __construct(
26 ApiMain $main,
27 string $action,
28 private readonly RevisionStore $revisionStore,
29 private readonly PatrolManager $patrolManager,
30 private readonly RecentChangeLookup $recentChangeLookup,
31 ) {
32 parent::__construct( $main, $action );
33 }
34
38 public function execute() {
39 $params = $this->extractRequestParams();
40 $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
41
42 if ( isset( $params['rcid'] ) ) {
43 $rc = $this->recentChangeLookup->getRecentChangeById( $params['rcid'] );
44 if ( !$rc ) {
45 $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
46 }
47 } else {
48 $rev = $this->revisionStore->getRevisionById( $params['revid'] );
49 if ( !$rev ) {
50 $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
51 }
52 $rc = $this->revisionStore->getRecentChange( $rev );
53 if ( !$rc ) {
54 $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
55 }
56 }
57
58 $user = $this->getUser();
59 $tags = $params['tags'];
60
61 // Check if user can add tags
62 if ( $tags !== null ) {
63 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
64 if ( !$ableToTag->isOK() ) {
65 $this->dieStatus( $ableToTag );
66 }
67 }
68
69 $status = $this->patrolManager->markPatrolled( $rc, $user, $tags );
70
71 if ( !$status->isGood() ) {
72 $this->dieStatus( $status );
73 }
74
75 $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
76 ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
77 $this->getResult()->addValue( null, $this->getModuleName(), $result );
78 }
79
81 public function mustBePosted() {
82 return true;
83 }
84
86 public function isWriteMode() {
87 return true;
88 }
89
91 public function getAllowedParams() {
92 return [
93 'rcid' => [
94 ParamValidator::PARAM_TYPE => 'integer'
95 ],
96 'revid' => [
97 ParamValidator::PARAM_TYPE => 'integer'
98 ],
99 'tags' => [
100 ParamValidator::PARAM_TYPE => 'tags',
101 ParamValidator::PARAM_ISMULTI => true,
102 ],
103 ];
104 }
105
107 public function needsToken() {
108 return 'patrol';
109 }
110
112 protected function getExamplesMessages() {
113 return [
114 'action=patrol&token=123ABC&rcid=230672766'
115 => 'apihelp-patrol-example-rcid',
116 'action=patrol&token=123ABC&revid=230672766'
117 => 'apihelp-patrol-example-revid',
118 ];
119 }
120
122 public function getHelpUrls() {
123 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
124 }
125}
126
128class_alias( ApiPatrol::class, 'ApiPatrol' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:975
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...
__construct(ApiMain $main, string $action, private readonly RevisionStore $revisionStore, private readonly PatrolManager $patrolManager, private readonly RecentChangeLookup $recentChangeLookup,)
Definition ApiPatrol.php:25
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:91
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
Definition ApiPatrol.php:86
execute()
Patrols the article or provides the reason the patrol failed.
Definition ApiPatrol.php:38
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:81
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.