Completed
Push — master ( ddde46...ec2f81 )
by adam
12:43 queued 09:38
created

RevisionSaver::addEditInfoParams()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 7
cts 12
cp 0.5833
rs 9.2
c 1
b 0
f 0
cc 4
eloc 8
nc 5
nop 2
crap 5.1576
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 10
		$result = $this->api->postRequest(
43 10
			new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
44 10
		);
45 10
		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 10
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ) {
56 10
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
57
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
58
		}
59
60 10
		$params = [];
61
62 10
		$content = $revision->getContent();
63 10
		$data = $content->getData();
64 10
		if ( !is_string( $data ) ) {
65
			throw new RuntimeException( 'Dont know how to save content of this model.' );
66
		}
67 10
		$params['text'] = $content->getData();
68 10
		$params['md5'] = md5( $content->getData() );
69
70 10
		$timestamp = $revision->getTimestamp();
71 10
		if ( !is_null( $timestamp ) ) {
72
			$params['basetimestamp'] = $timestamp;
73
		}
74
75 10
		if ( !is_null( $revision->getPageIdentifier()->getId() ) ) {
76
			$params['pageid'] = $revision->getPageIdentifier()->getId();
77
		} else {
78 10
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
79
		}
80
81 10
		$params['token'] = $this->api->getToken();
82
83 10
		if ( $this->api->isLoggedin() ) {
84
			$params['assert'] = 'user';
85
		}
86
87 10
		$this->addEditInfoParams( $editInfo, $params );
88
89 10
		return $params;
90
	}
91
92
	/**
93
	 * @param null|EditInfo $editInfo
94
	 * @param array &$params
95
	 */
96 10
	private function addEditInfoParams( $editInfo, &$params ) {
97 10
		if ( !is_null( $editInfo ) ) {
98 10
			$params['summary'] = $editInfo->getSummary();
99 10
			if ( $editInfo->getMinor() ) {
100
				$params['minor'] = true;
101
			}
102 10
			if ( $editInfo->getBot() ) {
103
				$params['bot'] = true;
104
				$params['assert'] = 'bot';
105
			}
106 10
		}
107 10
	}
108
109
}
110