summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api/src/Service/PageRestorer.php
blob: a422bd970a5172a4880f1ad619169766d308ea52 (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
<?php

namespace Mediawiki\Api\Service;

use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\Page;
use Mediawiki\DataModel\Title;
use OutOfBoundsException;

/**
 * @access private
 *
 * @author Addshore
 */
class PageRestorer extends Service {

	/**
	 * @since 0.3
	 *
	 * @param Page $page
	 * @param array $extraParams
	 *
	 * @return bool
	 */
	public function restore( Page $page, array $extraParams = [] ) {
		$this->api->postRequest(
			new SimpleRequest(
				'undelete',
				$this->getUndeleteParams( $page->getTitle(), $extraParams )
			)
		);

		return true;
	}

	/**
	 * @param Title $title
	 * @param array $extraParams
	 *
	 * @return array
	 */
	private function getUndeleteParams( Title $title, $extraParams ) {
		$params = [];

		$params['title'] = $title->getTitle();
		$params['token'] = $this->getUndeleteToken( $title );

		return array_merge( $extraParams, $params );
	}

	/**
	 * @param Title $title
	 *
	 * @throws OutOfBoundsException
	 * @returns string
	 */
	private function getUndeleteToken( Title $title ) {
		$response = $this->api->postRequest(
			new SimpleRequest(
				'query', [
				'list' => 'deletedrevs',
				'titles' => $title->getTitle(),
				'drprop' => 'token',
			]
			)
		);
		if ( array_key_exists( 'token', $response['query']['deletedrevs'][0] ) ) {
			return $response['query']['deletedrevs'][0]['token'];
		} else {
			throw new OutOfBoundsException(
				'Could not get page undelete token from list=deletedrevs query'
			);
		}
	}

}