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

RevisionSaver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 71.1%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 13
c 8
b 1
f 0
lcom 1
cbo 5
dl 0
loc 93
ccs 32
cts 45
cp 0.711
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A save() 0 8 2
B getEditParams() 0 36 6
A addEditInfoParams() 0 12 4
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