-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assignments.php
290 lines (248 loc) · 7.92 KB
/
Assignments.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace dokuwiki\plugin\structpublish\meta;
use dokuwiki\plugin\sqlite\SQLiteDB;
/**
* Class Assignments
*
* Manages the assignment of users to pages and namespaces
* This is a singleton. Assignment data is only loaded once per request.
*
* @see \dokuwiki\plugin\struct\meta\Assignments
*/
class Assignments
{
/** @var SQLiteDB */
protected $sqlite;
/** @var array All the assignments patterns */
protected $patterns;
/** @var Assignments */
protected static $instance = null;
/**
* Get the singleton instance of the Assignments
*
* @param bool $forcereload create a new instance to reload the assignment data
* @return Assignments
*/
public static function getInstance($forcereload = false)
{
if (is_null(self::$instance) or $forcereload) {
$class = get_called_class();
self::$instance = new $class();
}
return self::$instance;
}
/**
* Assignments constructor.
*
* Not public. Use Assignments::getInstance() instead
*/
protected function __construct()
{
/** @var \helper_plugin_structpublish_db $helper */
$helper = plugin_load('helper', 'struct_db');
$this->sqlite = $helper->getDB();
$this->loadPatterns();
}
/**
* Load existing assignment patterns
*/
protected function loadPatterns()
{
$sql = 'SELECT * FROM structpublish_assignments_patterns ORDER BY pattern';
$this->patterns = $this->sqlite->queryAll($sql);
}
/**
* Add a new assignment pattern to the pattern table
*
* @param string $pattern
* @param string $user
* @param string $status
* @return bool
*/
public function addPattern($pattern, $user, $status)
{
// add the pattern
$sql = 'REPLACE INTO structpublish_assignments_patterns (pattern, user, status) VALUES (?,?,?)';
$ok = (bool) $this->sqlite->query($sql, [$pattern, $user, $status]);
// reload patterns
$this->loadPatterns();
// update assignments
// fetch known pages
/** @var \helper_plugin_structpublish_db $dbHelper */
$dbHelper = plugin_load('helper', 'structpublish_db');
$pids = $dbHelper->getPages();
// wrap in transaction
$this->sqlite->query('BEGIN TRANSACTION');
foreach ($pids as $pid) {
$this->updatePageAssignments($pid);
}
$ok = $ok && $this->sqlite->query('COMMIT TRANSACTION');
if (!$ok) {
$this->sqlite->query('ROLLBACK TRANSACTION');
}
return $ok;
}
/**
* Remove an existing assignment pattern from the pattern table
*
* @param string $pattern
* @param string $user
* @param string $status
* @return bool
*/
public function removePattern($pattern, $user, $status)
{
// remove the pattern
$sql = 'DELETE FROM structpublish_assignments_patterns WHERE pattern = ? AND user = ? AND status = ?';
$ok = (bool) $this->sqlite->query($sql, [$pattern, $user, $status]);
// reload patterns
$this->loadPatterns();
// fetch possibly affected pages
$sql = 'SELECT pid FROM structpublish_assignments WHERE user = ? AND status = ?';
$pagerows = $this->sqlite->queryAll($sql, [$user, $status]);
// remove page assignments matching the pattern being removed
$ok = true;
foreach ($pagerows as $row) {
$ok = $ok && $this->deassignPage($row['pid'], $user, $status);
}
return $ok;
}
/**
* Updates all assignments of a given page against the current patterns
*
* @param string $pid
*/
public function updatePageAssignments($pid, $reload = false)
{
if ($reload) {
$this->loadPatterns();
}
$rules = $this->getPageAssignments($pid, true);
foreach ($rules as $status => $users) {
foreach ($users as $user) {
$this->assignPage($pid, $user, $status);
}
}
// FIXME reevalute existing assignments for exclusion
}
/**
* Clear all patterns - deassigns all pages
*
* This is mostly useful for testing and not used in the interface currently
*
* @param bool $full fully delete all previous assignments
* @return bool
*/
public function clear($full = false)
{
$sql = 'DELETE FROM structpublish_assignments_patterns';
$ok = (bool) $this->sqlite->query($sql);
if ($full) {
$sql = 'DELETE FROM structpublish_assignments';
} else {
$sql = 'UPDATE structpublish_assignments SET assigned = 0';
}
$ok = $ok && (bool) $this->sqlite->query($sql);
// reload patterns
$this->loadPatterns();
return $ok;
}
/**
* Add page to assignments
*
* @param string $page
* @param string $user
* @param string $status
* @return bool
*/
public function assignPage($page, $user = null, $status = null)
{
$sql = 'REPLACE INTO structpublish_assignments (pid, user, status, assigned) VALUES (?, ?, ?, 1)';
return (bool) $this->sqlite->query($sql, [$page, $user, $status]);
}
/**
* Remove page from assignments
*
* @param string $page
* @param string $user
* @return bool
*/
public function deassignPage($page, $user, $status)
{
$sql = 'UPDATE structpublish_assignments SET assigned = 0 WHERE pid = ? AND user = ? AND status = ?';
return (bool) $this->sqlite->query($sql, [$page, $user, $status]);
}
/**
* Get the whole pattern table
*
* @return array
*/
public function getAllPatterns()
{
return $this->patterns;
}
/**
* Returns a list of user/group string lists per status assigned to the given page
*
* @param string $page
* @param bool $checkpatterns Should the current patterns be re-evaluated?
* @return array users assigned [role => [user, ...], ...]
*/
public function getPageAssignments($page, $checkpatterns = true)
{
$rules = [];
$page = cleanID($page);
if ($checkpatterns) {
$helper = plugin_load('helper', 'structpublish_assignments');
// evaluate patterns
$pns = ':' . getNS($page) . ':';
foreach ($this->patterns as $row) {
if ($helper->matchPagePattern($row['pattern'], $page, $pns)) {
$rules[$row['status']][] = $row['user'];
}
}
} else {
// just select
$sql = 'SELECT user, status FROM structpublish_assignments WHERE pid = ? AND assigned = 1';
$list = $this->sqlite->queryAll($sql, [$page]);
foreach ($list as $row) {
$rules[$row['status']][] = $row['user'];
}
}
return $rules;
}
/**
* Get the pages known to struct and their assignment state
*
* @param bool $assignedonly limit results to currently assigned only
* @return array
*/
public function getPages($assignedOnly = false)
{
$sql = 'SELECT pid, user, status, assigned FROM structpublish_assignments WHERE 1=1';
$opts = array();
if ($assignedOnly) {
$sql .= ' AND assigned = 1';
}
$sql .= ' ORDER BY pid, user, status';
$list = $this->sqlite->queryAll($sql, $opts);
$result = array();
foreach ($list as $row) {
$pid = $row['pid'];
$user = $row['user'];
$status = $row['status'];
if (!isset($result[$pid])) {
$result[$pid] = array();
}
$result[$pid][$user][$status] = (bool) $row['assigned'];
}
return $result;
}
/**
* @return SQLiteDB
*/
public function getSqlite()
{
return $this->sqlite;
}
}