Passed
Push — master ( 6ce606...5a8341 )
by Daimona
01:34
created

Page::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki\Page;
4
5
use BotRiconferme\Logger;
6
use BotRiconferme\Request\RequestBase;
7
use BotRiconferme\Wiki\Controller;
8
use BotRiconferme\Wiki\Element;
9
10
/**
11
 * Represents a single on-wiki page
12
 */
13
class Page extends Element {
14
	/** @var string */
15
	protected $title;
16
	/** @var Controller */
17
	protected $controller;
18
	/** @var string|null */
19
	protected $content;
20
21
	/**
22
	 * @param string $title
23
	 * @param string $domain The site where the page lives, if different from default
24
	 */
25
	public function __construct( string $title, string $domain = DEFAULT_URL ) {
26
		$this->title = $title;
27
		$this->controller = new Controller( $domain );
28
	}
29
30
	/**
31
	 * @return string
32
	 */
33
	public function getTitle() : string {
34
		return $this->title;
35
	}
36
37
	/**
38
	 * Get the content of this page
39
	 *
40
	 * @param int|null $section A section number to retrieve the content of that section
41
	 * @return string
42
	 */
43
	public function getContent( int $section = null ) : string {
44
		if ( $this->content === null ) {
45
			$this->content = $this->controller->getPageContent( $this->title, $section );
46
		}
47
		return $this->content;
48
	}
49
50
	/**
51
	 * Edit this page and update content
52
	 *
53
	 * @param array $params
54
	 */
55
	public function edit( array $params ) {
56
		$params = [
57
			'title' => $this->getTitle()
58
		] + $params;
59
60
		$this->controller->editPage( $params );
61
		if ( isset( $params['text'] ) ) {
62
			$this->content = $params['text'];
63
		} elseif ( isset( $params['appendtext'] ) ) {
64
			$this->content .= $params['appendtext'];
65
		} else {
66
			// Clear the cache anyway
67
			( new Logger )->warning( 'Resetting content cache. Params: ' . var_export( $params, true ) );
68
			$this->content = null;
69
		}
70
	}
71
72
	/**
73
	 * Whether this page exists
74
	 *
75
	 * @return bool
76
	 */
77
	public function exists() : bool {
78
		$res = RequestBase::newFromParams( [
79
			'action' => 'query',
80
			'titles' => $this->getTitle()
81
		] )->execute();
82
		$pages = $res->query->pages;
83
		return !isset( reset( $pages )->missing );
84
	}
85
86
	/**
87
	 * Check whether the page content is matched by the given regex
88
	 *
89
	 * @param string $regex
90
	 * @return bool
91
	 */
92
	public function matches( string $regex ) : bool {
93
		return (bool)preg_match( $regex, $this->getContent() );
94
	}
95
96
	/**
97
	 * Get the matches from a preg_match on the page content, and throws if the
98
	 * regex doesn't match.
99
	 *
100
	 * @param string $regex
101
	 * @return string[]
102
	 * @throws \Exception
103
	 */
104
	public function getMatch( string $regex ) : array {
105
		$ret = [];
106
		if ( preg_match( $regex, $this->getContent(), $ret ) === 0 ) {
107
			throw new \Exception( 'The content does not match the given regex' );
108
		}
109
		return $ret;
110
	}
111
112
	/**
113
	 * Returns a regex for matching the title of this page
114
	 *
115
	 * @inheritDoc
116
	 */
117
	public function getRegex() : string {
118
		return str_replace( ' ', '[ _]', preg_quote( $this->title ) );
119
	}
120
121
	/**
122
	 * For easier logging etc.
123
	 *
124
	 * @return string
125
	 */
126
	public function __toString() {
127
		return $this->getTitle();
128
	}
129
}
130