Repository navigation
Expand file tree
/
Copy pathModule.php
More file actions
433 lines (410 loc) · 22.4 KB
/
Copy pathModule.php
File metadata and controls
433 lines (410 loc) · 22.4 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
namespace RevisionHistory;
use Doctrine\ORM\Events;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Module\AbstractModule;
use RevisionHistory\Capture\Recorder;
// Omeka only autoloads a module's classes once it is active; install() runs before that.
spl_autoload_register(function ($class) {
if (strpos($class, __NAMESPACE__ . '\\') === 0) {
$file = __DIR__ . '/src/' . str_replace('\\', '/', substr($class, strlen(__NAMESPACE__) + 1)) . '.php';
if (is_file($file)) {
require $file;
}
}
});
class Module extends AbstractModule
{
/** The roles that may read the history. Copying other modules' logs is global admin only. */
const READERS = ['global_admin', 'site_admin', 'editor', 'reviewer'];
/** Everybody who can save a resource, and so sees what a save would change first. */
const SAVERS = ['global_admin', 'site_admin', 'editor', 'reviewer', 'author'];
/**
* Who may put back a save of THEIR OWN, when nobody has saved the resource since
* (1.0.5). An editor could already cancel a Batch Values run but had to find a global
* administrator to undo one save of their own; the rule in restoreAction() keeps
* everybody else's work out of their reach. 1.2.3: a reviewer too — Omeka lets a
* reviewer change every resource, so undoing their own save is no more than that.
*/
const OWN_RESTORERS = ['site_admin', 'editor', 'reviewer'];
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function install(ServiceLocatorInterface $services)
{
(new Store($services->get('Omeka\Connection')))->install();
$settings = $services->get('Omeka\Settings');
if (!$settings->get('revision_history_from')) {
// The moment this archive started keeping its own history. Before it, only
// what was copied from other modules can be shown.
$settings->set('revision_history_from', gmdate('Y-m-d H:i:s'));
}
$stopped = $settings->get('revision_history_stopped');
if ($stopped) {
// Installed again after being uninstalled: nothing was recorded in between,
// and the page has to say so rather than let the gap look like a quiet week.
$gaps = (array) $settings->get('revision_history_gaps', []);
$gaps[] = [$stopped, gmdate('Y-m-d H:i:s')];
$settings->set('revision_history_gaps', $gaps);
$settings->delete('revision_history_stopped');
}
// The field read as a resource's identifier (an accession or call number), shown
// beside its title and searchable on the history page. Empty: none is shown.
if ($settings->get('revision_history_accession_term') === null) {
$settings->set('revision_history_accession_term', 'dcterms:identifier');
}
// The field read as the title people know a resource by.
if ($settings->get('revision_history_title_term') === null) {
$settings->set('revision_history_title_term', 'dcterms:title');
}
// How old the last check of the data itself may be before another one is started
// while somebody is working in the admin pages. An hour, to begin with.
if ($settings->get('revision_history_watch_hours') === null) {
$settings->set('revision_history_watch_hours', 1);
}
// The admin pages show what a save would change, and ask, before making it.
if ($settings->get('revision_history_confirm') === null) {
$settings->set('revision_history_confirm', true);
}
// And say so when somebody else saved the resource after the form was opened (1.0.4).
if ($settings->get('revision_history_confirm_stale') === null) {
$settings->set('revision_history_confirm_stale', true);
}
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services)
{
$this->install($services);
}
/**
* Uninstall leaves the tables where they are, on purpose.
*
* The three log modules this one sits next to all DROP their tables on
* uninstall, which means one click in the Modules page ends the history for
* good. A history that can be deleted by tidying up the module list is not a
* history. To remove it for real, drop `revision_history_*` by hand.
*/
public function uninstall(ServiceLocatorInterface $services)
{
$services->get('Omeka\Settings')->set('revision_history_stopped', gmdate('Y-m-d H:i:s'));
}
/**
* Whether the admin pages show what a save would change before making it, and which
* two fields name a resource on the history page. Omeka lets only a global
* administrator open a module's settings.
*/
public function getConfigForm(\Laminas\View\Renderer\PhpRenderer $renderer)
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$e = function ($text) use ($renderer) {
return $renderer->escapeHtml($renderer->translate($text));
};
$field = function (string $name, string $label, string $info, string $input) use ($e) {
return '<div class="field"><div class="field-meta">'
. '<label for="' . $name . '">' . $e($label) . '</label>'
. '<div class="field-description">' . $e($info) . '</div></div>'
. '<div class="inputs">' . $input . '</div></div>';
};
$text = function (string $name, string $value) use ($renderer) {
return '<input type="text" id="' . $name . '" name="' . $name . '" value="'
. $renderer->escapeHtml($value) . '" placeholder="dcterms:title">';
};
$on = (bool) $settings->get('revision_history_confirm', true);
$stale = (bool) $settings->get('revision_history_confirm_stale', true);
return $field(
'revision_history_confirm',
'Check before saving', // @translate
'Show what a save would change, and ask, before it is made. The history is kept either way.', // @translate
'<input type="hidden" name="revision_history_confirm" value="0">'
. '<input type="checkbox" id="revision_history_confirm" name="revision_history_confirm" value="1"' . ($on ? ' checked' : '') . '>'
) . $field(
'revision_history_confirm_stale',
'Warn when somebody else saved first', // @translate
'In the check before saving, say so when the resource was saved by somebody else after the form was opened — saving the form as it stands would undo their work.', // @translate
'<input type="hidden" name="revision_history_confirm_stale" value="0">'
. '<input type="checkbox" id="revision_history_confirm_stale" name="revision_history_confirm_stale" value="1"' . ($stale ? ' checked' : '') . '>'
) . $field(
'revision_history_title_term',
'Title field', // @translate
'The property read as the name of a resource on the history page, as prefix:name. Default dcterms:title.', // @translate
$text('revision_history_title_term', (string) $settings->get('revision_history_title_term', 'dcterms:title'))
) . $field(
'revision_history_accession_term',
'Identifier field', // @translate
'The property read as an identifier (accession number, call number) shown beside the title and searchable on the history page, as prefix:name. Leave empty for none. Default dcterms:identifier.', // @translate
$text('revision_history_accession_term', (string) $settings->get('revision_history_accession_term', 'dcterms:identifier'))
);
}
public function handleConfigForm(\Laminas\Mvc\Controller\AbstractController $controller)
{
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$params = $controller->params();
$settings->set('revision_history_confirm', (bool) $params->fromPost('revision_history_confirm'));
$settings->set('revision_history_confirm_stale', (bool) $params->fromPost('revision_history_confirm_stale'));
foreach (['revision_history_title_term', 'revision_history_accession_term'] as $name) {
$term = trim((string) $params->fromPost($name, ''));
if ($term !== '' && !preg_match('/^[A-Za-z][A-Za-z0-9_-]*:[A-Za-z_][A-Za-z0-9_.-]*$/', $term)) {
$controller->messenger()->addError(sprintf(
$controller->translate('"%s" is not a property term like dcterms:title.'), // @translate
$term
));
return false;
}
$settings->set($name, $term);
}
return true;
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$services = $this->getServiceLocator();
$acl = $services->get('Omeka\Acl');
$acl->allow(self::READERS, [Controller\Admin\IndexController::class], ['browse', 'item', 'export']);
$acl->allow(self::SAVERS, [Controller\Admin\IndexController::class], ['preview', 'refused']);
$acl->allow(self::OWN_RESTORERS, [Controller\Admin\IndexController::class], ['restore']);
$acl->allow('global_admin', [Controller\Admin\IndexController::class]);
$recorder = new Recorder($services);
$services->get('Omeka\EntityManager')->getEventManager()->addEventListener(
[Events::onFlush, Events::postFlush],
$recorder
);
// Before any other module's listener on the same event (History Log uses -50).
$shared = $event->getApplication()->getEventManager()->getSharedManager();
foreach (['Omeka\Entity\Item', 'Omeka\Entity\Media', 'Omeka\Entity\ItemSet'] as $entity) {
$shared->attach($entity, 'entity.remove.pre', [$recorder, 'beforeRemove'], 10000);
}
foreach (['api.delete.pre', 'api.batch_delete.pre'] as $name) {
$shared->attach('*', $name, [$recorder, 'beforeApiDelete'], 10000);
}
foreach (['api.delete.post', 'api.batch_delete.post'] as $name) {
$shared->attach('*', $name, [$recorder, 'afterApiDelete'], -10000);
}
// What happens without a resource being saved: signing in and out, a sign-in
// that failed, and settings. See Capture\Moments.
$moments = new Capture\Moments($services, $recorder);
$shared->attach('*', 'user.login', [$moments, 'signedIn']);
$shared->attach('*', 'user.logout', [$moments, 'signedOut']);
foreach (['setting.insert', 'setting.update', 'setting.delete'] as $name) {
$shared->attach('*', $name, [$moments, 'setting']);
}
$event->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_FINISH,
[$moments, 'finished'],
-1000
);
// Every request that comes through the API is asked whether it belongs to a run
// of another module. Nothing is done with the request itself.
$shared->attach('*', 'api.execute.pre', [$recorder, 'noteRun'], 10000);
// Every admin form is looked at as it arrives, with the same question Omeka is
// about to ask of its token, so that a save refused before it reached anything
// is written down with the state of that moment instead of vanishing. Runs
// after the route is known and before the page is dispatched; it reads, and
// whatever it finds, the request goes on exactly as it would have.
$event->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_ROUTE,
function (MvcEvent $e) use ($services) {
Watch\Refused::look($services, $e);
},
-1000
);
}
public function attachListeners(SharedEventManagerInterface $shared)
{
// Every admin page asks, once, whether a check of the data itself is due. That
// question is one small query; the check itself, if it happens at all, is a
// background job and the page does not wait for it.
$shared->attach('*', 'view.layout', [$this, 'checkIfDue']);
// What another module says happened to a record, sent without naming this module
// (1.2.0; see Entries and the README).
$shared->attach('HistoryEntry', 'history.entry', [$this, 'historyEntry']);
foreach (['Omeka\Controller\Admin\Item', 'Omeka\Controller\Admin\Media', 'Omeka\Controller\Admin\ItemSet'] as $controller) {
$shared->attach($controller, 'view.show.sidebar', [$this, 'sidebar']);
$shared->attach($controller, 'view.add.before', [$this, 'confirmBeforeSave']);
$shared->attach($controller, 'view.edit.before', [$this, 'confirmBeforeSave']);
$shared->attach($controller, 'view.batch_edit.before', [$this, 'confirmBeforeSave']);
}
}
/**
* The event history.entry: the same keys as Entries::add(). The id of the entry is set
* as the parameter entry_id and returned (0 when nothing was written).
*/
public function historyEntry(Event $event): int
{
$id = Entries::fromServices($this->getServiceLocator())->add($event->getParams() instanceof \ArrayAccess
? (array) $event->getParams()->getArrayCopy()
: (array) $event->getParams());
$event->setParam('entry_id', $id);
return $id;
}
/**
* The dialog that shows what a save would change, on the pages where a resource is
* written. Nothing here can stop a save: the page only gets a stylesheet, a script
* and what the script needs to know, and the script itself lets every save through
* if anything at all goes wrong.
*/
public function confirmBeforeSave(Event $event): void
{
try {
$services = $this->getServiceLocator();
if (!$services->get('Omeka\Settings')->get('revision_history_confirm', true)) {
return;
}
$route = $services->get('Application')->getMvcEvent()->getRouteMatch();
if (!$route) {
return;
}
$kind = [
'Omeka\Controller\Admin\Item' => 'item',
'Omeka\Controller\Admin\Media' => 'media',
'Omeka\Controller\Admin\ItemSet' => 'item_set',
][(string) $route->getParam('controller')] ?? null;
if (!$kind) {
return;
}
$batch = strpos((string) $route->getParam('action'), 'batch') === 0;
$id = (int) $route->getParam('id');
// WHERE THE RESOURCE STOOD WHEN THIS FORM WAS OPENED: the last line of its
// history, and its own modified time for a resource the history has no line
// for. Sent back with the check before saving, so that a save somebody else
// made in between can be named (1.0.4).
$seen = 0;
$opened = '';
if (!$batch && $id > 0
&& $services->get('Omeka\Settings')->get('revision_history_confirm_stale', true)) {
try {
$conn = $services->get('Omeka\Connection');
$seen = (int) $conn->fetchOne(
// an entry another module wrote changes no value, so it is not a save (1.2.0)
"SELECT COALESCE(MAX(`id`), 0) FROM `revision_history_event` WHERE (`resource_id` = ? OR `item_id` = ?) AND `action` <> 'noted'",
[$id, $id]
);
$opened = (string) ($conn->fetchOne('SELECT `modified` FROM `resource` WHERE `id` = ?', [$id]) ?: '');
} catch (\Throwable $e) {
$seen = 0;
$opened = '';
}
}
$view = $event->getTarget();
$view->headLink()->appendStylesheet($view->assetUrl('css/history.css', 'RevisionHistory'));
$view->headLink()->appendStylesheet($view->assetUrl('css/confirm.css', 'RevisionHistory'));
$view->headScript()->appendScript('window.RevisionHistoryConfirm = ' . json_encode([
'url' => $view->url('admin/revision-history', ['action' => 'preview']),
'kind' => $kind,
'id' => $id,
'mode' => $batch ? 'batch' : 'one',
'seen' => $seen,
'opened' => $opened,
// whether this form can be told that somebody else saved first; where it can,
// a check that does not answer is said, not taken as "nobody did" (1.0.5)
'stale' => !$batch && $id > 0
&& (bool) $services->get('Omeka\Settings')->get('revision_history_confirm_stale', true),
// The form that saves the resource, and nothing else: the Delete panel on
// the same page is a form too, and asking "what would this save change"
// before a deletion is both wrong and in the way. Omeka asks about a
// deletion itself.
'selector' => $batch ? '#content form[id^="batch-edit"]' : '#content form.resource-form',
'one' => '%s ' . ['item' => 'item', 'media' => 'media', 'item_set' => 'item set'][$kind],
'many' => '%s ' . ['item' => 'items', 'media' => 'media', 'item_set' => 'item sets'][$kind],
'nothing' => $view->translate('Nothing has been filled in.'), // @translate
'title' => $view->translate('Before you save'), // @translate
'what' => $view->translate('Check what this save would change.'), // @translate
'leaveTitle' => $view->translate('Before you leave'), // @translate
'leaveWhat' => $view->translate('This has not been saved. Leaving loses it.'), // @translate
'save' => $view->translate('Save'), // @translate
'leave' => $view->translate('Leave without saving'), // @translate
'stay' => $view->translate('Keep editing'), // @translate
'cancel' => $view->translate('Cancel'), // @translate
'waiting' => $view->translate('Loading…'), // @translate
'uncheckedHead' => $view->translate('Could not check whether somebody else saved this.'), // @translate
'uncheckedSay' => $view->translate('The check did not answer. Check again — nothing you typed is lost — or tick the box and save anyway.'), // @translate
'uncheckedTick' => $view->translate('I understand this save may undo a save somebody else made'), // @translate
'saveAnyway' => $view->translate('Save anyway'), // @translate
'checkAgain' => $view->translate('Check again'), // @translate
'wait' => 8000,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . ';');
$view->headScript()->appendFile($view->assetUrl('js/confirm.js', 'RevisionHistory'));
} catch (\Throwable $e) {
// The page works, and saves, whether or not the dialog could be set up.
}
}
/**
* A check of the data itself, when the last one is older than the archive asked for.
* Attached to the pages staff open all day, not only to the history's own, so that
* an archive without cron still gets checked while people work.
*/
public function checkIfDue(): void
{
static $asked = false;
if ($asked) {
return;
}
$asked = true;
try {
$services = $this->getServiceLocator();
// Only the admin pages, and only for somebody who may run a check anyway.
if (!$services->get('Omeka\Status')->isAdminRequest()
|| !$services->get('Omeka\Acl')->userIsAllowed(Controller\Admin\IndexController::class, 'watch')
) {
return;
}
// A background job is started and the page carries on. Where Omeka is set to
// run jobs inside the request instead, a page would have to wait for the
// check to finish, so it is left to the button and to cron.
if (strpos(get_class($services->get('Omeka\Job\DispatchStrategy')), 'Synchronous') !== false) {
return;
}
$watchman = new Watch\Watchman($services->get('Omeka\Connection'), $services->get('Omeka\Settings'));
if (!$watchman->due()) {
return;
}
$watchman->sent();
$services->get('Omeka\Job\Dispatcher')->dispatch(Job\WatchJob::class, []);
} catch (\Throwable $e) {
// The page opens whether or not a check could be started.
}
}
/** The last few things that happened to this resource, on its own page. */
public function sidebar(Event $event)
{
$view = $event->getTarget();
$resource = $view->vars()->offsetGet('resource');
if (!$resource) {
return;
}
$acl = $this->getServiceLocator()->get('Omeka\Acl');
if (!$acl->userIsAllowed(Controller\Admin\IndexController::class, 'item')) {
return;
}
$services = $this->getServiceLocator();
try {
$reader = new Reader($services->get('Omeka\Connection'));
$page = $reader->events(['resource' => $resource->id()], (string) ($services->get('Omeka\Settings')->get('time_zone') ?: 'UTC'), 5);
// A media page also says who added the file: Omeka's "Owner" was the item's
// owner for every file added before 1.0.8 began writing it down, whoever uploaded it.
$added = null;
$since = null;
if ($resource->resourceName() === 'media') {
$conn = $services->get('Omeka\Connection');
$added = $conn->fetchAssociative(
"SELECT happened_at, user_name, via FROM revision_history_event
WHERE resource_kind = 'media' AND resource_id = ? AND action = 'created' ORDER BY id DESC LIMIT 1",
[(int) $resource->id()]
) ?: null;
if (!$added) {
$since = $conn->fetchOne("SELECT MIN(happened_at) FROM revision_history_event WHERE resource_kind = 'media'") ?: null;
}
}
} catch (\Throwable $e) {
return;
}
echo $view->partial('revision-history/admin/index/sidebar', [
'resource' => $resource,
'page' => $page,
'added' => $added,
'since' => $since,
]);
}
}