Completed
Branch master (939199)
by
unknown
39:35
created

includes/api/ApiRollback.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 *
5
 * Created on Jun 20, 2007
6
 *
7
 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 */
26
27
/**
28
 * @ingroup API
29
 */
30
class ApiRollback extends ApiBase {
31
32
	/**
33
	 * @var Title
34
	 */
35
	private $mTitleObj = null;
36
37
	/**
38
	 * @var User
39
	 */
40
	private $mUser = null;
41
42
	public function execute() {
43
		$this->useTransactionalTimeLimit();
44
45
		$user = $this->getUser();
46
		$params = $this->extractRequestParams();
47
48
		$titleObj = $this->getRbTitle( $params );
49
		$pageObj = WikiPage::factory( $titleObj );
50
		$summary = $params['summary'];
51
		$details = [];
52
53
		// If change tagging was requested, check that the user is allowed to tag,
54
		// and the tags are valid
55 View Code Duplication
		if ( count( $params['tags'] ) ) {
56
			$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
57
			if ( !$tagStatus->isOK() ) {
58
				$this->dieStatus( $tagStatus );
59
			}
60
		}
61
62
		$retval = $pageObj->doRollback(
63
			$this->getRbUser( $params ),
64
			$summary,
65
			$params['token'],
66
			$params['markbot'],
67
			$details,
68
			$user,
69
			$params['tags']
70
		);
71
72
		// We don't care about multiple errors, just report one of them
73
		if ( $retval ) {
74
			if ( isset( $retval[0][0] ) &&
75
				( $retval[0][0] == 'alreadyrolled' || $retval[0][0] == 'cantrollback' )
76
			) {
77
				$error = $retval[0];
78
				$userMessage = $this->msg( $error[0], array_slice( $error, 1 ) );
79
				// dieUsageMsg() doesn't support $extraData
80
				$errorCode = $error[0];
81
				$errorInfo = isset( ApiBase::$messageMap[$errorCode] ) ?
82
					ApiBase::$messageMap[$errorCode]['info'] :
83
					$errorCode;
84
				$this->dieUsage( $errorInfo, $errorCode, 0, [
85
					'messageHtml' => $userMessage->parseAsBlock()
86
				] );
87
			}
88
89
			$this->dieUsageMsg( reset( $retval ) );
90
		}
91
92
		$watch = 'preferences';
93
		if ( isset( $params['watchlist'] ) ) {
94
			$watch = $params['watchlist'];
95
		}
96
97
		// Watch pages
98
		$this->setWatch( $watch, $titleObj, 'watchrollback' );
0 ignored issues
show
It seems like $titleObj defined by $this->getRbTitle($params) on line 48 can be null; however, ApiBase::setWatch() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
99
100
		$info = [
101
			'title' => $titleObj->getPrefixedText(),
102
			'pageid' => intval( $details['current']->getPage() ),
103
			'summary' => $details['summary'],
104
			'revid' => intval( $details['newid'] ),
105
			// The revision being reverted (previously the current revision of the page)
106
			'old_revid' => intval( $details['current']->getID() ),
107
			// The revision being restored (the last revision before revision(s) by the reverted user)
108
			'last_revid' => intval( $details['target']->getID() )
109
		];
110
111
		$oldUser = $details['current']->getUserText( Revision::FOR_THIS_USER );
112
		$lastUser = $details['target']->getUserText( Revision::FOR_THIS_USER );
113
		$diffUrl = $titleObj->getFullURL( [
114
			'diff' => $info['revid'],
115
			'oldid' => $info['old_revid'],
116
			'diffonly' => '1'
117
		] );
118
		$info['messageHtml'] = $this->msg( 'rollback-success-notify' )
119
			->params( $oldUser, $lastUser, $diffUrl )
120
			->parseAsBlock();
121
122
		$this->getResult()->addValue( null, $this->getModuleName(), $info );
123
	}
124
125
	public function mustBePosted() {
126
		return true;
127
	}
128
129
	public function isWriteMode() {
130
		return true;
131
	}
132
133
	public function getAllowedParams() {
134
		return [
135
			'title' => null,
136
			'pageid' => [
137
				ApiBase::PARAM_TYPE => 'integer'
138
			],
139
			'tags' => [
140
				ApiBase::PARAM_TYPE => 'tags',
141
				ApiBase::PARAM_ISMULTI => true,
142
			],
143
			'user' => [
144
				ApiBase::PARAM_TYPE => 'user',
145
				ApiBase::PARAM_REQUIRED => true
146
			],
147
			'summary' => '',
148
			'markbot' => false,
149
			'watchlist' => [
150
				ApiBase::PARAM_DFLT => 'preferences',
151
				ApiBase::PARAM_TYPE => [
152
					'watch',
153
					'unwatch',
154
					'preferences',
155
					'nochange'
156
				],
157
			],
158
			'token' => [
159
				// Standard definition automatically inserted
160
				ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
161
			],
162
		];
163
	}
164
165
	public function needsToken() {
166
		return 'rollback';
167
	}
168
169
	/**
170
	 * @param array $params
171
	 *
172
	 * @return string
173
	 */
174
	private function getRbUser( array $params ) {
175
		if ( $this->mUser !== null ) {
176
			return $this->mUser;
177
		}
178
179
		// We need to be able to revert IPs, but getCanonicalName rejects them
180
		$this->mUser = User::isIP( $params['user'] )
181
			? $params['user']
182
			: User::getCanonicalName( $params['user'] );
183
		if ( !$this->mUser ) {
184
			$this->dieUsageMsg( [ 'invaliduser', $params['user'] ] );
185
		}
186
187
		return $this->mUser;
188
	}
189
190
	/**
191
	 * @param array $params
192
	 *
193
	 * @return Title
194
	 */
195
	private function getRbTitle( array $params ) {
196
		if ( $this->mTitleObj !== null ) {
197
			return $this->mTitleObj;
198
		}
199
200
		$this->requireOnlyOneParameter( $params, 'title', 'pageid' );
201
202
		if ( isset( $params['title'] ) ) {
203
			$this->mTitleObj = Title::newFromText( $params['title'] );
204
			if ( !$this->mTitleObj || $this->mTitleObj->isExternal() ) {
205
				$this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
206
			}
207
		} elseif ( isset( $params['pageid'] ) ) {
208
			$this->mTitleObj = Title::newFromID( $params['pageid'] );
209
			if ( !$this->mTitleObj ) {
210
				$this->dieUsageMsg( [ 'nosuchpageid', $params['pageid'] ] );
211
			}
212
		}
213
214
		if ( !$this->mTitleObj->exists() ) {
215
			$this->dieUsageMsg( 'notanarticle' );
216
		}
217
218
		return $this->mTitleObj;
219
	}
220
221
	protected function getExamplesMessages() {
222
		return [
223
			'action=rollback&title=Main%20Page&user=Example&token=123ABC' =>
224
				'apihelp-rollback-example-simple',
225
			'action=rollback&title=Main%20Page&user=192.0.2.5&' .
226
				'token=123ABC&summary=Reverting%20vandalism&markbot=1' =>
227
				'apihelp-rollback-example-summary',
228
		];
229
	}
230
231
	public function getHelpUrls() {
232
		return 'https://www.mediawiki.org/wiki/API:Rollback';
233
	}
234
}
235