summaryrefslogtreecommitdiff
path: root/bin/reevotech/vendor/addwiki/mediawiki-api/src/Service/RevisionSaver.php
blob: 00b91ee6d796528a495d07a07c73c119c188ceeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

namespace Mediawiki\Api\Service;

use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\EditInfo;
use Mediawiki\DataModel\Revision;
use RuntimeException;

/**
 * @access private
 *
 * @author Addshore
 * @author DFelten (EditInfo fix)
 */
class RevisionSaver extends Service {

	/**
	 * @since 0.2
	 *
	 * @param Revision $revision
	 * @param EditInfo $editInfo
	 *
	 * @return bool success
	 */
	public function save( Revision $revision, EditInfo $editInfo = null ) {
		$editInfo = $editInfo ? $editInfo : $revision->getEditInfo();

		$result = $this->api->postRequest(
			new SimpleRequest( 'edit', $this->getEditParams( $revision, $editInfo ) )
		);
		return ( $result['edit']['result'] == 'Success' );
	}

	/**
	 * @param Revision $revision
	 * @param EditInfo $editInfo
	 *
	 * @throws RuntimeException
	 * @returns array
	 */
	private function getEditParams( Revision $revision, EditInfo $editInfo = null ) {
		if ( !$revision->getPageIdentifier()->identifiesPage() ) {
			throw new RuntimeException( '$revision PageIdentifier does not identify a page' );
		}

		$params = [];

		$content = $revision->getContent();
		$data = $content->getData();
		if ( !is_string( $data ) ) {
			throw new RuntimeException( 'Dont know how to save content of this model.' );
		}
		$params['text'] = $content->getData();
		$params['md5'] = md5( $content->getData() );

		$timestamp = $revision->getTimestamp();
		if ( !is_null( $timestamp ) ) {
			$params['basetimestamp'] = $timestamp;
		}

		if ( !is_null( $revision->getPageIdentifier()->getId() ) ) {
			$params['pageid'] = $revision->getPageIdentifier()->getId();
		} else {
			$params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
		}

		$params['token'] = $this->api->getToken();

		if ( $this->api->isLoggedin() ) {
			$params['assert'] = 'user';
		}

		$this->addEditInfoParams( $editInfo, $params );

		return $params;
	}

	/**
	 * @param null|EditInfo $editInfo
	 * @param array &$params
	 */
	private function addEditInfoParams( $editInfo, &$params ) {
		if ( !is_null( $editInfo ) ) {
			$params['summary'] = $editInfo->getSummary();
			if ( $editInfo->getMinor() ) {
				$params['minor'] = true;
			}
			if ( $editInfo->getBot() ) {
				$params['bot'] = true;
				$params['assert'] = 'bot';
			}
		}
	}

}