Passed
Push — master ( b3e686...761a52 )
by Daimona
01:54
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\Wiki\Controller;
7
use BotRiconferme\Wiki\Element;
8
9
/**
10
 * Represents a single on-wiki page
11
 */
12
class Page extends Element {
13
	/** @var string */
14
	protected $title;
15
	/** @var Controller */
16
	protected $controller;
17
	/** @var string|null */
18
	protected $content;
19
20
	/**
21
	 * @param string $title
22
	 * @param string $domain The site where the page lives, if different from default
23
	 */
24
	public function __construct( string $title, string $domain = DEFAULT_URL ) {
25
		$this->title = $title;
26
		$this->controller = new Controller( $domain );
27
	}
28
29
	/**
30
	 * @return string
31
	 */
32
	public function getTitle() : string {
33
		return $this->title;
34
	}
35
36
	/**
37
	 * Get the content of this page
38
	 *
39
	 * @param int|null $section A section number to retrieve the content of that section
40
	 * @return string
41
	 */
42
	public function getContent( int $section = null ) : string {
43
		if ( $this->content === null ) {
44
			$this->content = $this->controller->getPageContent( $this->title, $section );
45
		}
46
		return $this->content;
47
	}
48
49
	/**
50
	 * Edit this page and update content
51
	 *
52
	 * @param array $params
53
	 */
54
	public function edit( array $params ) {
55
		$params = [
56
			'title' => $this->getTitle()
57
		] + $params;
58
59
		$this->controller->editPage( $params );
60
		if ( isset( $params['text'] ) ) {
61
			$this->content = $params['text'];
62
		} elseif ( isset( $params['appendtext'] ) ) {
63
			$this->content .= $params['appendtext'];
64
		} else {
65
			// Clear the cache anyway
66
			( new Logger )->warning( 'Resetting content cache. Params: ' . var_export( $params, true ) );
67
			$this->content = null;
68
		}
69
	}
70
71
	/**
72
	 * Check whether the page content is matched by the given regex
73
	 *
74
	 * @param string $regex
75
	 * @return bool
76
	 */
77
	public function matches( string $regex ) : bool {
78
		return (bool)preg_match( $regex, $this->getContent() );
79
	}
80
81
	/**
82
	 * Get the matches from a preg_match on the page content, and throws if the
83
	 * regex doesn't match.
84
	 *
85
	 * @param string $regex
86
	 * @return string[]
87
	 * @throws \Exception
88
	 */
89
	public function getMatch( string $regex ) : array {
90
		$ret = [];
91
		if ( preg_match( $regex, $this->getContent(), $ret ) === 0 ) {
92
			throw new \Exception( 'The content does not match the given regex' );
93
		}
94
		return $ret;
95
	}
96
97
	/**
98
	 * Returns a regex for matching the title of this page
99
	 *
100
	 * @inheritDoc
101
	 */
102
	public function getRegex() : string {
103
		return str_replace( ' ', '[ _]', preg_quote( $this->title ) );
104
	}
105
106
	/**
107
	 * For easier logging etc.
108
	 *
109
	 * @return string
110
	 */
111
	public function __toString() {
112
		return $this->getTitle();
113
	}
114
}
115