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\MediaWikiServices;
26
27/**
28 * Allows user to patrol pages
29 * @ingroup API
30 */
31class ApiPatrol extends ApiBase {
32
33	/**
34	 * Patrols the article or provides the reason the patrol failed.
35	 */
36	public function execute() {
37		$params = $this->extractRequestParams();
38		$this->requireOnlyOneParameter( $params, 'rcid', 'revid' );
39
40		if ( isset( $params['rcid'] ) ) {
41			$rc = RecentChange::newFromId( $params['rcid'] );
42			if ( !$rc ) {
43				$this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
44			}
45		} else {
46			$store = MediaWikiServices::getInstance()->getRevisionStore();
47			$rev = $store->getRevisionById( $params['revid'] );
48			if ( !$rev ) {
49				$this->dieWithError( [ 'apierror-nosuchrevid', $params['revid'] ] );
50			}
51			$rc = $store->getRecentChange( $rev );
52			if ( !$rc ) {
53				$this->dieWithError( [ 'apierror-notpatrollable', $params['revid'] ] );
54			}
55		}
56
57		$user = $this->getUser();
58		$tags = $params['tags'];
59
60		// Check if user can add tags
61		if ( $tags !== null ) {
62			$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
63			if ( !$ableToTag->isOK() ) {
64				$this->dieStatus( $ableToTag );
65			}
66		}
67
68		$retval = $rc->doMarkPatrolled( $user, false, $tags );
69
70		if ( $retval ) {
71			$this->dieStatus( $this->errorArrayToStatus( $retval, $user ) );
72		}
73
74		$result = [ 'rcid' => (int)$rc->getAttribute( 'rc_id' ) ];
75		ApiQueryBase::addTitleInfo( $result, $rc->getTitle() );
76		$this->getResult()->addValue( null, $this->getModuleName(), $result );
77	}
78
79	public function mustBePosted() {
80		return true;
81	}
82
83	public function isWriteMode() {
84		return true;
85	}
86
87	public function getAllowedParams() {
88		return [
89			'rcid' => [
90				ApiBase::PARAM_TYPE => 'integer'
91			],
92			'revid' => [
93				ApiBase::PARAM_TYPE => 'integer'
94			],
95			'tags' => [
96				ApiBase::PARAM_TYPE => 'tags',
97				ApiBase::PARAM_ISMULTI => true,
98			],
99		];
100	}
101
102	public function needsToken() {
103		return 'patrol';
104	}
105
106	protected function getExamplesMessages() {
107		return [
108			'action=patrol&token=123ABC&rcid=230672766'
109				=> 'apihelp-patrol-example-rcid',
110			'action=patrol&token=123ABC&revid=230672766'
111				=> 'apihelp-patrol-example-revid',
112		];
113	}
114
115	public function getHelpUrls() {
116		return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Patrol';
117	}
118}
119