Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiPatrol
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
72
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * API for MediaWiki 1.14+
4 *
5 * Copyright © 2008 Soxred93 soxred93@gmail.com,
6 *
7 * @license GPL-2.0-or-later
8 * @file
9 */
10
11namespace MediaWiki\Api;
12
13use MediaWiki\ChangeTags\ChangeTags;
14use MediaWiki\RecentChanges\PatrolManager;
15use MediaWiki\RecentChanges\RecentChangeLookup;
16use MediaWiki\Revision\RevisionStore;
17use Wikimedia\ParamValidator\ParamValidator;
18
19/**
20 * Allows user to patrol pages
21 * @ingroup API
22 */
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
35    /**
36     * Patrols the article or provides the reason the patrol failed.
37     */
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
80    /** @inheritDoc */
81    public function mustBePosted() {
82        return true;
83    }
84
85    /** @inheritDoc */
86    public function isWriteMode() {
87        return true;
88    }
89
90    /** @inheritDoc */
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
106    /** @inheritDoc */
107    public function needsToken() {
108        return 'patrol';
109    }
110
111    /** @inheritDoc */
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
121    /** @inheritDoc */
122    public function getHelpUrls() {
123        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
124    }
125}
126
127/** @deprecated class alias since 1.43 */
128class_alias( ApiPatrol::class, 'ApiPatrol' );