Completed
Push — master ( a33a91...cfad5d )
by adam
04:31
created

src/Service/RevisionRollbacker.php (2 issues)

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
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\Revision;
8
use Mediawiki\DataModel\Title;
9
10
/**
11
 * @access private
12
 *
13
 * @author Addshore
14
 */
15
class RevisionRollbacker {
16
17
	/**
18
	 * @var MediawikiApi
19
	 */
20
	private $api;
21
22
	/**
23
	 * @param MediawikiApi $api
24
	 */
25
	public function __construct( MediawikiApi $api ) {
26
		$this->api = $api;
27
	}
28
29
	/**
30
	 * @since 0.3
31
	 *
32
	 * @param Revision $revision
33
	 * @param Title $title if using MW 1.24 of lower (https://gerrit.wikimedia.org/r/#/c/133063/)
34
	 *
35
	 * @return bool
36
	 */
37
	public function rollback( Revision $revision, Title $title = null ) {
38
		$this->api->postRequest(
39
			new SimpleRequest( 'rollback', $this->getRollbackParams( $revision, $title ) )
40
		);
41
42
		return true;
43
	}
44
45
	/**
46
	 * @param Revision $revision
47
	 * @param Title|null $title
48
	 *
49
	 * @return array
50
	 */
51
	private function getRollbackParams( Revision $revision, $title ) {
52
		$params = [];
53
		if ( !is_null( $title ) ) {
54
			// This is needed prior to https://gerrit.wikimedia.org/r/#/c/133063/
55
			$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...
56
		} else {
57
			// This will work after https://gerrit.wikimedia.org/r/#/c/133063/
58
			$params['pageid'] = $revision->getPageId();
0 ignored issues
show
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...
59
		}
60
		$params['user'] = $revision->getUser();
61
		$params['token'] = $this->getTokenForRevision( $revision );
62
63
		return $params;
64
	}
65
66
	/**
67
	 * @param Revision $revision
68
	 *
69
	 * @returns string
70
	 */
71
	private function getTokenForRevision( Revision $revision ) {
72
		$result = $this->api->postRequest(
73
			new SimpleRequest(
74
				'query', [
75
				'prop' => 'revisions',
76
				'revids' => $revision->getId(),
77
				'rvtoken' => 'rollback',
78
			]
79
			)
80
		);
81
		$result = array_shift( $result['query']['pages'] );
82
83
		return $result['revisions'][0]['rollbacktoken'];
84
	}
85
86
}
87