Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

RevisionSaver::getEditParams()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 36

Duplication

Lines 5
Ratio 13.89 %

Importance

Changes 0
Metric Value
dl 5
loc 36
rs 8.7217
c 0
b 0
f 0
cc 6
nc 10
nop 2
1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\DataModel\EditInfo;
7
use Addwiki\Mediawiki\DataModel\Revision;
8
use RuntimeException;
9
10
/**
11
 * @access private
12
 *
13
 * @author Addshore
14
 * @author DFelten (EditInfo fix)
15
 */
16
class RevisionSaver extends Service {
17
18
	/**
19
	 * @since 0.2
20
	 *
21
	 * @param Revision $revision
22
	 * @param EditInfo|null $editInfo
23
	 *
24
	 * @return bool success
25
	 */
26
	public function save( Revision $revision, EditInfo $editInfo = null ): bool {
27
		$editInfo ??= $revision->getEditInfo();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '='
Loading history...
28
29
		$result = $this->api->postRequest(
30
			new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
31
		);
32
		return ( $result['edit']['result'] == 'Success' );
33
	}
34
35
	/**
36
	 * @param Revision $revision
37
	 * @param EditInfo|null $editInfo
38
	 *
39
	 * @throws RuntimeException
40
	 * @return mixed[]
41
	 */
42
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ): array {
43
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
44
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
45
		}
46
47
		$params = [];
48
49
		$content = $revision->getContent();
50
		$data = $content->getData();
51
		if ( !is_string( $data ) ) {
52
			throw new RuntimeException( 'Dont know how to save content of this model.' );
53
		}
54
		$params['text'] = $content->getData();
55
		$params['md5'] = md5( $content->getData() );
56
57
		$timestamp = $revision->getTimestamp();
58
		if ( $timestamp !== null ) {
59
			$params['basetimestamp'] = $timestamp;
60
		}
61
62
		if ( $revision->getPageIdentifier()->getId() !== null ) {
63
			$params['pageid'] = $revision->getPageIdentifier()->getId();
64
		} else {
65
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
66
		}
67
68
		$params['token'] = $this->api->getToken();
69
70
		if ( $this->api->isLoggedin() ) {
71
			$params['assert'] = 'user';
72
		}
73
74
		$this->addEditInfoParams( $editInfo, $params );
75
76
		return $params;
77
	}
78
79
	/**
80
	 * @param array $params
81
	 */
82
	private function addEditInfoParams( ?EditInfo $editInfo, &$params ): void {
83
		if ( $editInfo !== null ) {
84
			$params['summary'] = $editInfo->getSummary();
85
			if ( $editInfo->getMinor() ) {
86
				$params['minor'] = true;
87
			}
88
			if ( $editInfo->getBot() ) {
89
				$params['bot'] = true;
90
				$params['assert'] = 'bot';
91
			}
92
			if ( $editInfo->getMaxlag() ) {
93
				$params['maxlag'] = $editInfo->getMaxlag();
94
			}
95
		}
96
	}
97
98
}
99