Completed
Push — ciFixes ( 191642...412eb6 )
by adam
07:41 queued 06:01
created

RevisionSaver::addEditInfoParams()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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