forked from teppokoivula/VersionControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageSnapshot.module
299 lines (276 loc) · 12.3 KB
/
PageSnapshot.module
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
291
292
293
294
295
296
297
298
299
<?php
/**
* Return page in the state it was at the given time
*
* Original code for this module was posted by SteveB at the ProcessWire support forum:
* https://processwire.com/talk/topic/2892-module-version-control-for-text-fields/?p=50438
*
* @copyright 2014-2021 Teppo Koivula & SteveB
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*
*/
class PageSnapshot extends WireData implements Module {
/**
* Return information about this module (required)
*
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Page Snapshot',
'summary' => 'Return page in the state it was at the given time.',
'href' => 'http://modules.processwire.com/modules/version-control/',
'author' => 'Teppo Koivula, SteveB',
'version' => '1.1.23',
'singular' => true,
'autoload' => true,
'requires' => array(
'VersionControl',
'ProcessWire>=2.4.1',
),
);
}
/**
* Array for storing IDs of hooks altering Pagefile behaviour
*
*/
protected $pagefile_hooks = array();
/**
* Initialization function
*
* This function attachs required hooks.
*
*/
public function init() {
// add new method snapshot to Page objects
$this->addHook('Page::snapshot', $this, 'hookPageSnapshot');
}
/**
* Return page in the state it was at given time
*
* @param HookEvent $event
* @return null|false
*/
protected function hookPageSnapshot(HookEvent $event) {
$page = $event->object;
$time = isset($event->arguments[0]) ? $event->arguments[0] : null;
$revision_id = isset($event->arguments[1]) ? $event->arguments[1] : null;
$data = $this->snapshot($page, $time, $revision_id);
if (!$data) return false;
$of = $page->of();
$page->of(false);
$filedata = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $subkey => $subvalue) {
list($id, $field, $property) = explode('.', $subkey);
$language = str_replace('data', '', $property);
if (!$language) $page->get($key)->get('id=' . $id)->$field = $subvalue;
else if ($language = $this->languages->get((int) $language)) {
$page->get($key)->get('id=' . $id)->$field->setLanguageValue($language, $subvalue);
}
}
} else {
list($field, $count, $property) = array_pad(explode('.', $key), 3, 0);
if ($property) {
// multipart property (n.data), i.e. file or image field
if (is_null($value)) {
$filedata[$field] = null;
} else if ($value) {
if (!isset($filedata[$field])) $filedata[$field] = array();
$value = json_decode($value, true);
$filedata[$field][$count] = $value;
}
} else {
// "regular" property (data, data1001 etc.)
$property = $count;
$language = str_replace('data', '', $property);
if (!$language) {
// not a language value (data)
if ($page->$field != $value) {
// update field value only if existing value differs
// from new value (note: based on loose comparison)
if ($page->fields->$field->type == 'FieldtypePage') {
// prevent new value from being appended to old
// one by temporarily setting old value to null
$page->$field = null;
}
$page->$field = $value;
}
} else {
$language = $this->languages->get((int) $language);
if ($language->id) {
// language value (data1001 etc.) and language exists
if ($page->$field->getLanguageValue($language) != $value) {
// update field value only if existing value differs
// from new value (note: based on loose comparison)
$page->$field->setLanguageValue($language, $value);
}
}
}
}
}
}
if (count($filedata)) {
// filedata comes in chunks and needs to be processed in two steps
if (!count($this->pagefile_hooks)) {
$this->pagefile_hooks[] = $this->addHook('Pagefile::url', $this, 'hookPagefileUrl');
$this->pagefile_hooks[] = $this->addHook('Pagefile::filename', $this, 'hookPagefileFilename');
$this->pagefile_hooks[] = $this->addHookBefore('Pagefile::install', $this, 'hookPagefileInstall');
}
foreach ($filedata as $field => $items) {
$page->get($field)->deleteAll();
if (is_array($items)) {
ksort($items);
foreach ($items as $key => $item) {
$filename = substr($item['filename'], 0, 2) . '/' . $item['filename'];
$page->$field = $this->modules->VersionControl->path . $filename;
$page->$field->last()->description = $item['description'];
$page->$field->last()->modified = $item['modified'];
$page->$field->last()->created = $item['created'];
if (isset($item['tags'])) $page->$field->last()->tags = $item['tags'];
$page->$field->last()->_version_control_url = $this->modules->VersionControl->url . $filename;
$page->$field->last()->_version_control_filename = $this->modules->VersionControl->path . $filename;
$item['filename'] = $this->modules->VersionControl->path . $filename;
$filedata[$field][$key] = $item;
}
}
}
$page->_version_control_filedata = array(json_encode($filedata));
}
$page->of($of);
}
/**
* Prevent installing Pagefile if path is that of ProcessVersionControl
*
* @param HookEvent $event
*/
protected function hookPagefileInstall(HookEvent $event) {
if ($this->install_pagefiles) return;
if (strpos($event->arguments[0], $this->modules->VersionControl->path) === 0) {
$event->object->basename = $event->arguments[0];
$event->replace = true;
}
}
/**
* Override Pageimage / Pagefile URL with custom one
*
* @param HookEvent $event
*/
protected function hookPagefileUrl(HookEvent $event) {
if ($this->install_pagefiles) return;
if ($event->object->_version_control_url) $event->return = $event->object->_version_control_url;
}
/**
* Override Pageimage / Pagefile filename with custom one
*
* @param HookEvent $event
*/
protected function hookPagefileFilename(HookEvent $event) {
if ($this->install_pagefiles) return;
if ($event->object->_version_control_filename) $event->return = $event->object->_version_control_filename;
}
/**
* Remove hooks we're using to alter Pagefile behaviour
*
*/
public function removePagefileHooks() {
foreach ($this->pagefile_hooks as $key => $hook) {
$this->removeHook($hook);
unset($this->pagefile_hooks[$key]);
}
}
/**
* Return array of contents of given page at specific time or revision
*
* @param Page $page
* @param mixed $time
* @param null|int $revision_id
* @throws WireException if GET param revision_id is set but isn't integer
* @throws WireException if revision defined by GET param revision_id doesn't exist
* @return array
* @todo should null values be set if page, field or value didn't exist at given time?
*/
protected function snapshot(Page $page, $time = null, $revision_id = null) {
if ($time && !is_integer($time)) $time = strtotime($time);
// revision info
$page->_version_control_revision = null;
if ($revision_id) {
if (!is_integer($revision_id)) {
throw new WireException('Revision ID must be an integer');
}
$stmt = $this->database->prepare('SELECT timestamp FROM ' . VersionControl::TABLE_REVISIONS . ' WHERE id = :revision_id');
$stmt->bindValue(':revision_id', $revision_id, PDO::PARAM_INT);
$stmt->execute();
$revision = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$revision) {
throw new WireException('Revision doesn\'t exist: ' . $revision_id);
}
$page->_version_control_revision = $revision_id;
}
// default value for time
if (empty($time)) {
$time = $revision_id ? strtotime($revision['timestamp']) : time();
}
// include repeater pages
$page_ids = array(':p0' => $page->id);
if ($this->modules->isInstalled('FieldtypeRepeater')) {
$p_num = 0;
foreach ($page->fields as $field) {
if ($field->type instanceof FieldtypeRepeater) {
$subfields = $this->templates->get($field->template_id)->versionControlFields;
if ($subfields !== null && count($subfields)) {
foreach ($page->$field as $repeater_page) {
++$p_num;
$page_ids[':p' . $p_num] = $repeater_page->id;
}
}
}
}
}
// find values
$where = $revision_id ? 't1.id <= :revision_id AND ' : '';
$stmt = $this->database->prepare('
SELECT t1.pages_id, t1.id AS revision, t2.fields_id, t2.property, t2.data
FROM (
SELECT MAX(t1.id) id, t1.pages_id, t2.fields_id
FROM ' . VersionControl::TABLE_REVISIONS . ' AS t1, ' . VersionControl::TABLE_DATA . ' AS t2
WHERE ' . $where . 't1.pages_id IN (' . implode(',', array_keys($page_ids)) . ') AND t1.timestamp <= :time AND t2.revisions_id = t1.id
GROUP BY t1.pages_id, t2.fields_id, t2.property
) AS t1
INNER JOIN ' . VersionControl::TABLE_DATA . ' AS t2
ON t2.revisions_id = t1.id AND t2.fields_id = t1.fields_id
GROUP BY revision, t1.pages_id, t2.fields_id, t2.property, t2.data
ORDER BY revision ASC
');
if ($where) {
$stmt->bindValue(':revision_id', $revision_id, \PDO::PARAM_INT);
}
foreach ($page_ids as $p_num => $p_id) {
$stmt->bindValue($p_num, $p_id, \PDO::PARAM_INT);
}
$stmt->bindValue(':time', date('Y-m-d H:i:s', $time), \PDO::PARAM_STR);
$stmt->execute();
// generate data (associative array)
$data = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$field = $this->fields->get($row['fields_id']);
if ($row['pages_id'] != $page->id) {
$repeater_page = $this->pages->get($row['pages_id']);
if ($repeater_page->id) {
$grandparent = $repeater_page->parent()->parent()->name;
if (strpos($grandparent, 'for-field-') === 0) {
$repeater_field = $this->fields->get((int) substr($grandparent, 10))->name;
$data[$repeater_field][$repeater_page . '.' . $field . '.' . $row['property']] = $row['data'];
}
}
} else {
$data[$field . '.' . $row['property']] = $row['data'];
if (!$revision_id && $row['revision'] > $page->_version_control_revision) {
$page->_version_control_revision = $row['revision'];
}
}
}
return $data;
}
}