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