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

namespace Mediawiki\Api\Service;

use InvalidArgumentException;
use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\Page;

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

	/**
	 * @since 0.3
	 *
	 * @param Page $page
	 * @param string[] $protections where the 'key' is the action and the 'value' is the group
	 * @param array $extraParams
	 *
	 * @return bool
	 * @throws InvalidArgumentException
	 */
	public function protect( Page $page, $protections, array $extraParams = [] ) {
		if ( !is_array( $protections ) || empty( $protections ) ) {
			throw new InvalidArgumentException(
				'$protections must be an array with keys and values'
			);
		}

		$params = [
			'pageid' => $page->getId(),
			'token' => $this->api->getToken( 'protect' ),
		];
		$protectionsString = '';
		foreach ( $protections as $action => $value ) {
			if ( !is_string( $action ) || !is_string( $value ) ) {
				throw new InvalidArgumentException(
					'All keys and elements of $protections must be strings'
				);
			}
			$protectionsString = $action . '=' . $value . '|';
		}
		$params['protections'] = rtrim( $protectionsString, '|' );

		$this->api->postRequest(
			new SimpleRequest( 'protect', array_merge( $extraParams, $params ) )
		);

		return true;
	}

}