MediaWiki  master
ApiPatrol.php
Go to the documentation of this file.
1 <?php
26 
31 class ApiPatrol extends ApiBase {
33  private $revisionStore;
34 
40  public function __construct(
41  ApiMain $main,
42  $action,
44  ) {
45  parent::__construct( $main, $action );
46  $this->revisionStore = $revisionStore;
47  }
48 
52  public function execute() {
53  $params = $this->extractRequestParams();
54  $this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
55 
56  if ( isset( $params['rcid'] ) ) {
57  $rc = RecentChange::newFromId( $params['rcid'] );
58  if ( !$rc ) {
59  $this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
60  }
61  } else {
62  $rev = $this->revisionStore->getRevisionById( $params['revid'] );
63  if ( !$rev ) {
64  $this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
65  }
66  $rc = $this->revisionStore->getRecentChange( $rev );
67  if ( !$rc ) {
68  $this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
69  }
70  }
71 
72  $user = $this->getUser();
73  $tags = $params['tags'];
74 
75  // Check if user can add tags
76  if ( $tags !== null ) {
77  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
78  if ( !$ableToTag->isOK() ) {
79  $this->dieStatus( $ableToTag );
80  }
81  }
82 
83  $retval = $rc->doMarkPatrolled( $user, false, $tags );
84 
85  if ( $retval ) {
86  $this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
87  }
88 
89  $result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
90  ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
91  $this->getResult()->addValue( null, $this->getModuleName(), $result );
92  }
93 
94  public function mustBePosted() {
95  return true;
96  }
97 
98  public function isWriteMode() {
99  return true;
100  }
101 
102  public function getAllowedParams() {
103  return [
104  'rcid' => [
105  ApiBase::PARAM_TYPE => 'integer'
106  ],
107  'revid' => [
108  ApiBase::PARAM_TYPE => 'integer'
109  ],
110  'tags' => [
111  ApiBase::PARAM_TYPE => 'tags',
112  ApiBase::PARAM_ISMULTI => true,
113  ],
114  ];
115  }
116 
117  public function needsToken() {
118  return 'patrol';
119  }
120 
121  protected function getExamplesMessages() {
122  return [
123  'action=patrol&token=123ABC&rcid=230672766'
124  => 'apihelp-patrol-example-rcid',
125  'action=patrol&token=123ABC&revid=230672766'
126  => 'apihelp-patrol-example-revid',
127  ];
128  }
129 
130  public function getHelpUrls() {
131  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
132  }
133 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ApiPatrol\$revisionStore
RevisionStore $revisionStore
Definition: ApiPatrol.php:33
MediaWiki\Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:88
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiPatrol\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiPatrol.php:130
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiPatrol\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiPatrol.php:121
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiPatrol\__construct
__construct(ApiMain $main, $action, RevisionStore $revisionStore)
Definition: ApiPatrol.php:40
ApiPatrol\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiPatrol.php:98
ApiPatrol\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiPatrol.php:94
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiPatrol\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiPatrol.php:117
RecentChange\newFromId
static newFromId( $rcid)
Obtain the recent change with a given rc_id value.
Definition: RecentChange.php:213
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, Authority $performer=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:625
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:844
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1442
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
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:102
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1184
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466
ApiPatrol\execute
execute()
Patrols the article or provides the reason the patrol failed.
Definition: ApiPatrol.php:52