Completed
Pull Request — master (#46)
by Sam
08:10
created

RevisionSaver::getEditParams()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.3329

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 36
ccs 16
cts 24
cp 0.6667
rs 8.439
cc 6
eloc 22
nc 10
nop 2
crap 7.3329
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 10
	public function __construct( MediawikiApi $api ) {
28 10
		$this->api = $api;
29 10
	}
30
31
	/**
32
	 * @since 0.2
33
	 *
34
	 * @param Revision $revision
35
	 * @param EditInfo $editInfo
36
	 *
37
	 * @return bool success
38
	 */
39 10
	public function save( Revision $revision, EditInfo $editInfo = null ) {
40 10
		$editInfo = $editInfo ? $editInfo : $revision->getEditInfo();
41
42
		$result =
43 10
			$this->api->postRequest(
44 10
				new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
45 10
			);
46 10
		if ( $result['edit']['result'] == 'Success' ) {
47 10
			return true;
48
		}
49
50
		return false;
51
	}
52
53
	/**
54
	 * @param Revision $revision
55
	 * @param EditInfo $editInfo
56
	 *
57
	 * @throws RuntimeException
58
	 * @returns array
59
	 */
60 10
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ) {
61 10
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
62
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
63
		}
64
65 10
		$params = [];
66
67 10
		$content = $revision->getContent();
68 10
		$data = $content->getData();
69 10
		if ( !is_string( $data ) ) {
70
			throw new RuntimeException( 'Dont know how to save content of this model.' );
71
		}
72 10
		$params['text'] = $content->getData();
73 10
		$params['md5'] = md5( $content->getData() );
74
75 10
		$timestamp = $revision->getTimestamp();
76 10
		if ( !is_null( $timestamp ) ) {
77
			$params['basetimestamp'] = $timestamp;
78
		}
79
80 10
		if ( !is_null( $revision->getPageIdentifier()->getId() ) ) {
81
			$params['pageid'] = $revision->getPageIdentifier()->getId();
82
		} else {
83 10
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
84
		}
85
86 10
		$params['token'] = $this->api->getToken();
87
88 10
		if ( $this->api->isLoggedin() ) {
89
			$params['assert'] = 'user';
90
		}
91
92 10
		$this->addEditInfoParams( $editInfo, $params );
93
94 10
		return $params;
95
	}
96
97
	/**
98
	 * @param null|EditInfo $editInfo
99
	 * @param array &$params
100
	 */
101 10
	private function addEditInfoParams( $editInfo, &$params ) {
102 10
		if ( !is_null( $editInfo ) ) {
103 10
			$params['summary'] = $editInfo->getSummary();
104 10
			if ( $editInfo->getMinor() ) {
105
				$params['minor'] = true;
106
			}
107 10
			if ( $editInfo->getBot() ) {
108
				$params['bot'] = true;
109
				$params['assert'] = 'bot';
110
			}
111 10
		}
112 10
	}
113
114
}
115