Completed
Push — master ( ec2f81...11966d )
by adam
03:28
created

RevisionSaver::__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 3
Bugs 0 Features 0
Metric Value
c 3
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\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\EditInfo;
8
use Mediawiki\DataModel\Revision;
9
use RuntimeException;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 * @author DFelten (EditInfo fix)
16
 */
17
class RevisionSaver {
18
19
	/**
20
	 * @var MediawikiApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @param MediawikiApi $api
26
	 */
27
	public function __construct( MediawikiApi $api ) {
28
		$this->api = $api;
29
	}
30
31
	/**
32
	 * @since 0.2
33
	 *
34
	 * @param Revision $revision
35
	 * @param EditInfo $editInfo
36
	 *
37
	 * @return bool success
38
	 */
39
	public function save( Revision $revision, EditInfo $editInfo = null ) {
40
		$editInfo = $editInfo ? $editInfo : $revision->getEditInfo();
41
42
		$result = $this->api->postRequest(
43
			new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
44
		);
45
		return ( $result['edit']['result'] == 'Success' );
46
	}
47
48
	/**
49
	 * @param Revision $revision
50
	 * @param EditInfo $editInfo
51
	 *
52
	 * @throws RuntimeException
53
	 * @returns array
54
	 */
55
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ) {
56
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
57
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
58
		}
59
60
		$params = [];
61
62
		$content = $revision->getContent();
63
		$data = $content->getData();
64
		if ( !is_string( $data ) ) {
65
			throw new RuntimeException( 'Dont know how to save content of this model.' );
66
		}
67
		$params['text'] = $content->getData();
68
		$params['md5'] = md5( $content->getData() );
69
70
		$timestamp = $revision->getTimestamp();
71
		if ( !is_null( $timestamp ) ) {
72
			$params['basetimestamp'] = $timestamp;
73
		}
74
75
		if ( !is_null( $revision->getPageIdentifier()->getId() ) ) {
76
			$params['pageid'] = $revision->getPageIdentifier()->getId();
77
		} else {
78
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
79
		}
80
81
		$params['token'] = $this->api->getToken();
82
83
		if ( $this->api->isLoggedin() ) {
84
			$params['assert'] = 'user';
85
		}
86
87
		$this->addEditInfoParams( $editInfo, $params );
88
89
		return $params;
90
	}
91
92
	/**
93
	 * @param null|EditInfo $editInfo
94
	 * @param array &$params
95
	 */
96
	private function addEditInfoParams( $editInfo, &$params ) {
97
		if ( !is_null( $editInfo ) ) {
98
			$params['summary'] = $editInfo->getSummary();
99
			if ( $editInfo->getMinor() ) {
100
				$params['minor'] = true;
101
			}
102
			if ( $editInfo->getBot() ) {
103
				$params['bot'] = true;
104
				$params['assert'] = 'bot';
105
			}
106
		}
107
	}
108
109
}
110