MediaWiki master
ApiPatrol.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Api;
26
31
36class ApiPatrol extends ApiBase {
37 private RevisionStore $revisionStore;
38
39 public function __construct(
40 ApiMain $main,
41 string $action,
42 RevisionStore $revisionStore
43 ) {
44 parent::__construct( $main, $action );
45 $this->revisionStore = $revisionStore;
46 }
47
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
54
55 if ( isset( $params['rcid'] ) ) {
56 $rc = RecentChange::newFromId( $params['rcid'] );
57 if ( !$rc ) {
58 $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
59 }
60 } else {
61 $rev = $this->revisionStore->getRevisionById( $params['revid'] );
62 if ( !$rev ) {
63 $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
64 }
65 $rc = $this->revisionStore->getRecentChange( $rev );
66 if ( !$rc ) {
67 $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
68 }
69 }
70
71 $user = $this->getUser();
72 $tags = $params['tags'];
73
74 // Check if user can add tags
75 if ( $tags !== null ) {
76 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
77 if ( !$ableToTag->isOK() ) {
78 $this->dieStatus( $ableToTag );
79 }
80 }
81
82 $status = $rc->markPatrolled( $user, $tags );
83
84 if ( !$status->isGood() ) {
85 $this->dieStatus( $status );
86 }
87
88 $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
89 ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
90 $this->getResult()->addValue( null, $this->getModuleName(), $result );
91 }
92
93 public function mustBePosted() {
94 return true;
95 }
96
97 public function isWriteMode() {
98 return true;
99 }
100
101 public function getAllowedParams() {
102 return [
103 'rcid' => [
104 ParamValidator::PARAM_TYPE => 'integer'
105 ],
106 'revid' => [
107 ParamValidator::PARAM_TYPE => 'integer'
108 ],
109 'tags' => [
110 ParamValidator::PARAM_TYPE => 'tags',
111 ParamValidator::PARAM_ISMULTI => true,
112 ],
113 ];
114 }
115
116 public function needsToken() {
117 return 'patrol';
118 }
119
120 protected function getExamplesMessages() {
121 return [
122 'action=patrol&token=123ABC&rcid=230672766'
123 => 'apihelp-patrol-example-rcid',
124 'action=patrol&token=123ABC&revid=230672766'
125 => 'apihelp-patrol-example-revid',
126 ];
127 }
128
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:75
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:976
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
Allows user to patrol pages.
Definition ApiPatrol.php:36
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isWriteMode()
Indicates whether this module requires write access to the wiki.
Definition ApiPatrol.php:97
execute()
Patrols the article or provides the reason the patrol failed.
Definition ApiPatrol.php:51
getExamplesMessages()
Returns usage examples for this module.
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiPatrol.php:93
__construct(ApiMain $main, string $action, RevisionStore $revisionStore)
Definition ApiPatrol.php:39
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Recent changes tagging.
Utility class for creating and reading rows in the recentchanges table.
Service for looking up page revisions.
Service for formatting and validating API parameters.