Completed
Push — master ( 11966d...5d848f )
by Sam
13s
created

RevisionRollbacker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Mediawiki\DataModel\Revision;
7
use Mediawiki\DataModel\Title;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class RevisionRollbacker extends Service {
15
16
	/**
17
	 * @since 0.3
18
	 *
19
	 * @param Revision $revision
20
	 * @param Title $title if using MW 1.24 of lower (https://gerrit.wikimedia.org/r/#/c/133063/)
21
	 *
22
	 * @return bool
23
	 */
24
	public function rollback( Revision $revision, Title $title = null ) {
25
		$this->api->postRequest(
26
			new SimpleRequest( 'rollback', $this->getRollbackParams( $revision, $title ) )
27
		);
28
29
		return true;
30
	}
31
32
	/**
33
	 * @param Revision $revision
34
	 * @param Title|null $title
35
	 *
36
	 * @return array
37
	 */
38
	private function getRollbackParams( Revision $revision, $title ) {
39
		$params = [];
40
		if ( !is_null( $title ) ) {
41
			// This is needed prior to https://gerrit.wikimedia.org/r/#/c/133063/
42
			$params['title'] = $title->getTitle();
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Title::getTitle() has been deprecated with message: in 0.6 use getText (makes things look cleaner)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
43
		} else {
44
			// This will work after https://gerrit.wikimedia.org/r/#/c/133063/
45
			$params['pageid'] = $revision->getPageId();
0 ignored issues
show
Bug introduced by
The method getPageId() does not exist on Mediawiki\DataModel\Revision. Did you maybe mean getPageIdentifier()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
		}
47
		$params['user'] = $revision->getUser();
48
		$params['token'] = $this->getTokenForRevision( $revision );
49
50
		return $params;
51
	}
52
53
	/**
54
	 * @param Revision $revision
55
	 *
56
	 * @returns string
57
	 */
58
	private function getTokenForRevision( Revision $revision ) {
59
		$result = $this->api->postRequest(
60
			new SimpleRequest(
61
				'query', [
62
				'prop' => 'revisions',
63
				'revids' => $revision->getId(),
64
				'rvtoken' => 'rollback',
65
			]
66
			)
67
		);
68
		$result = array_shift( $result['query']['pages'] );
69
70
		return $result['revisions'][0]['rollbacktoken'];
71
	}
72
73
}
74