-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPageSnapshot.module
More file actions
267 lines (242 loc) · 10.6 KB
/
PageSnapshot.module
File metadata and controls
267 lines (242 loc) · 10.6 KB
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
<?php
namespace ProcessWire;
use VersionControl\DataStore;
/**
* 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-2022 Teppo Koivula & SteveB
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*/
class PageSnapshot extends WireData implements Module {
/**
* Array for storing IDs of hooks altering Pagefile behaviour
*/
protected $pagefile_hooks = [];
/**
* Reference to the data store
*
* @var DataStore
*/
protected $store;
/**
* Initialization function
*/
public function init() {
// Get a reference to the data store.
$this->store = $this->modules->get('VersionControl')->getDataStore();
// 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 = $event->arguments[0] ?? null;
$revision_id = $event->arguments[1] ?? null;
$data = $this->snapshot($page, $time, $revision_id);
if (!$data) return false;
$of = $page->of();
$page->of(false);
$filedata = [];
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] = [];
$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 (!empty($filedata)) {
// filedata comes in chunks and needs to be processed in two steps
if (empty($this->pagefile_hooks)) {
$this->pagefile_hooks = [
$this->addHook('Pagefile::url', $this, 'hookPagefileUrl'),
$this->addHook('Pagefile::filename', $this, 'hookPagefileFilename'),
$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->store->files->getPath() . $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->store->files->getURL() . $filename;
$page->$field->last()->_version_control_filename = $this->store->files->getPath() . $filename;
$item['filename'] = $this->store->files->getPath() . $filename;
$filedata[$field][$key] = $item;
}
}
}
$page->_version_control_filedata = [
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->store->files->getPath()) === 0) {
$event->object->basename = $event->arguments[0];
$event->replace = true;
}
}
/**
* Override Pageimage / Pagefile URL with custom one
*
* @todo add support for variations (width(), height(), etc.)
* @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
$revision = null;
$page->_version_control_revision = null;
if ($revision_id) {
if (!is_integer($revision_id)) {
throw new WireException("Revision ID must be an integer");
}
$revision = $this->store->revisions->getData($revision_id, ['timestamp']);
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();
}
// prepare a list of page IDs, including nested repeater pages
$page_ids = [$page->id];
if ($this->modules->isInstalled('FieldtypeRepeater')) {
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->get($field->name) as $repeater_page) {
$page_ids[] = $repeater_page->id;
}
}
}
}
}
// fetch page data from the database
$page_data = $this->store->data->getForPage($page_ids, $time, $revision_id);
// format data
$data = [];
foreach ($page_data as $data_row) {
$field = $this->fields->get($data_row['fields_id']);
if ($data_row['pages_id'] != $page->id) {
$repeater_page = $this->pages->get($data_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 . '.' . $data_row['property']] = $data_row['data'];
}
}
} else {
$data[$field . '.' . $data_row['property']] = $data_row['data'];
if (!$revision_id && $data_row['revision'] > $page->_version_control_revision) {
$page->_version_control_revision = $data_row['revision'];
}
}
}
return $data;
}
}