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 / 48
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 / 2
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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25use MediaWiki\Revision\RevisionStore;
26use Wikimedia\ParamValidator\ParamValidator;
27
28/**
29 * Allows user to patrol pages
30 * @ingroup API
31 */
32class ApiPatrol extends ApiBase {
33    private RevisionStore $revisionStore;
34
35    /**
36     * @param ApiMain $main
37     * @param string $action
38     * @param RevisionStore $revisionStore
39     */
40    public function __construct(
41        ApiMain $main,
42        $action,
43        RevisionStore $revisionStore
44    ) {
45        parent::__construct( $main, $action );
46        $this->revisionStore = $revisionStore;
47    }
48
49    /**
50     * Patrols the article or provides the reason the patrol failed.
51     */
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                ParamValidator::PARAM_TYPE => 'integer'
106            ],
107            'revid' => [
108                ParamValidator::PARAM_TYPE => 'integer'
109            ],
110            'tags' => [
111                ParamValidator::PARAM_TYPE => 'tags',
112                ParamValidator::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}