|
1
|
|
|
<?php declare( strict_types=1 ); |
|
2
|
|
|
|
|
3
|
|
|
namespace BotRiconferme\Wiki\Page; |
|
4
|
|
|
|
|
5
|
|
|
use BotRiconferme\Exception\MissingMatchException; |
|
6
|
|
|
use BotRiconferme\Utils\IRegexable; |
|
7
|
|
|
use BotRiconferme\Wiki\Wiki; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a single on-wiki page |
|
12
|
|
|
*/ |
|
13
|
|
|
class Page implements IRegexable { |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $title; |
|
16
|
|
|
/** @var string|null */ |
|
17
|
|
|
protected $content; |
|
18
|
|
|
/** @var Wiki */ |
|
19
|
|
|
protected $wiki; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $title |
|
23
|
|
|
* @param Wiki $wiki For the site where the page lives |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( string $title, Wiki $wiki ) { |
|
26
|
|
|
$this->wiki = $wiki; |
|
27
|
|
|
$this->title = $title; |
|
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->wiki->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
|
|
|
* @phan-param array<int|string|bool> $params |
|
55
|
|
|
* @throws LogicException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function edit( array $params ): void { |
|
58
|
|
|
$params = [ |
|
59
|
|
|
'title' => $this->getTitle() |
|
60
|
|
|
] + $params; |
|
61
|
|
|
|
|
62
|
|
|
$this->wiki->editPage( $params ); |
|
63
|
|
|
if ( isset( $params['text'] ) ) { |
|
64
|
|
|
$this->content = $params['text']; |
|
65
|
|
|
} elseif ( isset( $params['appendtext'] ) ) { |
|
66
|
|
|
$this->content .= $params['appendtext']; |
|
67
|
|
|
} elseif ( isset( $params['prependtext'] ) ) { |
|
68
|
|
|
$this->content = $params['prependtext'] . $this->content; |
|
69
|
|
|
} else { |
|
70
|
|
|
throw new LogicException( |
|
71
|
|
|
'Unrecognized text param for edit. Params: ' . var_export( $params, true ) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Whether this page exists |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function exists(): bool { |
|
82
|
|
|
$pages = $this->wiki->getRequestFactory()->createStandaloneRequest( [ |
|
83
|
|
|
'action' => 'query', |
|
84
|
|
|
'titles' => $this->getTitle() |
|
85
|
|
|
] )->executeAsQuery(); |
|
86
|
|
|
return !isset( $pages->current()->missing ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Check whether the page content is matched by the given regex |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $regex |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function matches( string $regex ): bool { |
|
96
|
|
|
return (bool)preg_match( $regex, $this->getContent() ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the matches from a preg_match on the page content, and throws if the |
|
101
|
|
|
* regex doesn't match. Check $this->matches() first. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $regex |
|
104
|
|
|
* @return string[] |
|
105
|
|
|
* @throws MissingMatchException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getMatch( string $regex ): array { |
|
108
|
|
|
$ret = []; |
|
109
|
|
|
if ( preg_match( $regex, $this->getContent(), $ret ) === 0 ) { |
|
110
|
|
|
throw new MissingMatchException( "The content of $this does not match the given regex $regex" ); |
|
111
|
|
|
} |
|
112
|
|
|
return $ret; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns a regex for matching the title of this page |
|
117
|
|
|
* |
|
118
|
|
|
* @inheritDoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getRegex( string $delimiter = '/' ): string { |
|
121
|
|
|
return str_replace( ' ', '[ _]', preg_quote( $this->title, $delimiter ) ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* For easier logging etc. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function __toString(): string { |
|
130
|
|
|
return $this->getTitle(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|