-
Notifications
You must be signed in to change notification settings - Fork 4
/
HistoryLogPlugin.php
745 lines (679 loc) · 24.6 KB
/
HistoryLogPlugin.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?php
/**
* History Log
*
* This Omeka 2.0+ plugin logs curatorial actions such as adding, deleting, or
* modifying items, collections and files.
*
* @copyright Copyright 2014 UCSC Library Digital Initiatives
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*
* @package HistoryLog
*/
/**
* History Log plugin class
*
* @package HistoryLog
*/
class HistoryLogPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'upgrade',
'uninstall',
'uninstall_message',
'config_form',
'config',
'define_acl',
'define_routes',
'before_save_record',
'after_save_record',
'before_delete_record',
'before_save_element_text',
'before_delete_element_text',
'before_delete_element',
'export',
'admin_items_show',
'admin_collections_show',
'admin_files_show',
'admin_items_browse_detailed_each',
'admin_head',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_navigation_main',
);
/**
* @var array Options and their default values.
*/
protected $_options = array(
'history_log_display' => '[]',
);
/**
* @var array
*
* Array of prepared log entries saved during the "before save" process for
* updated record. It will be saved only if the process succeeds "after
* save". It allows to simplify the check of updates when the normal hooks
* are used.
*/
private $_preparedLogEntries = array();
/**
* When the plugin installs, create the database tables to store the logs.
*
* @return void
*/
public function hookInstall()
{
$db = $this->_db;
// Main table to log event.
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->HistoryLogEntry}` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`record_type` enum('Item', 'Collection', 'File') NOT NULL,
`record_id` int(10) unsigned NOT NULL,
`part_of` int(10) unsigned NOT NULL DEFAULT 0,
`user_id` int(10) unsigned NOT NULL,
`operation` enum('create', 'update', 'delete', 'import', 'export') NOT NULL,
`added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `record_type_record_id` (`record_type`, `record_id`),
INDEX (`added`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
// Associated table to log changes of each element.
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->HistoryLogChange}` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`entry_id` int(10) unsigned NOT NULL,
`element_id` int(10) unsigned NOT NULL,
`type` enum('none', 'create', 'update', 'delete') NOT NULL,
`text` mediumtext COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
INDEX (`entry_id`),
INDEX `entry_id_element_id` (`entry_id`, `element_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
// Add a new table to simplify complex queries with calendar requests.
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}numerals` (
`i` TINYINT unsigned NOT NULL,
PRIMARY KEY (`i`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
$sql = "INSERT INTO `{$db->prefix}numerals` (`i`) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);";
$db->query($sql);
$this->_installOptions();
}
/**
* Upgrade the plugin.
*/
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
$db = $this->_db;
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upgrade.php';
}
/**
* When the plugin uninstalls, delete the database tables which store the
* logs.
*
* @return void
*/
public function hookUninstall()
{
$db = $this->_db;
$sql = "DROP TABLE IF EXISTS `{$db->prefix}item_history_logs`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->HistoryLogEntry}`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->HistoryLogChange}`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->prefix}numerals`";
$db->query($sql);
$this->_uninstallOptions();
}
/**
* Add a message to the confirm form for uninstallation of the plugin.
*/
public function hookUninstallMessage()
{
echo __('%sWarning%s: All the history log entries will be deleted.', '<strong>', '</strong>');
}
/**
* Shows plugin configuration page.
*/
public function hookConfigForm($args)
{
$view = get_view();
echo $view->partial(
'plugins/history-log-config-form.php'
);
}
/**
* Handle a submitted config form.
*
* @param array Options set in the config form.
*/
public function hookConfig($args)
{
$post = $args['post'];
foreach ($this->_options as $optionKey => $optionValue) {
if (in_array($optionKey, array(
'history_log_display',
))) {
$post[$optionKey] = json_encode($post[$optionKey]) ?: json_encode(array());
}
if (isset($post[$optionKey])) {
set_option($optionKey, $post[$optionKey]);
}
}
}
/**
* Define the plugin's access control list.
*
* @param array $args Parameters supplied by the hook
* @return void
*/
public function hookDefineAcl($args)
{
$args['acl']->addResource('HistoryLog_Index');
}
/**
* Define routes.
*
* @param Zend_Controller_Router_Rewrite $router
*/
public function hookDefineRoutes($args)
{
if (!is_admin_theme()) {
return;
}
$args['router']->addRoute(
'history_log_record_log',
new Zend_Controller_Router_Route(
':type/log/:id',
array(
'module' => 'history-log',
'controller' => 'log',
'action' => 'log',
),
array(
'type' =>'items|collections|files',
'id' => '\d+',
)));
$args['router']->addRoute(
'history_log_undelete',
new Zend_Controller_Router_Route(
':type/undelete/:id',
array(
'module' => 'history-log',
'controller' => 'log',
'action' => 'undelete',
),
array(
'type' =>'items|collections|files',
'id' => '\d+',
)));
}
/**
* When a record is saved, determine whether it is a new record or a record
* update. If it is an update, log the event. Otherwise, wait until after
* the save.
*
* @param array $args An array of parameters passed by the hook
* @return void
*/
public function hookBeforeSaveRecord($args)
{
$record = $args['record'];
if (!$this->_isLoggable($record)) {
return;
}
// If it's not a new record, check for changes.
if (empty($args['insert'])) {
// Prepare the log entry if none.
if (!isset($this->_preparedLogEntries[get_class($record)][$record->id])) {
$logEntry = new HistoryLogEntry();
$logEntry->prepareNewEvent($record);
$this->_preparedLogEntries[get_class($record)][$record->id] = $logEntry;
}
// Else, all elements texts are ready!?
}
}
/**
* When an record is saved, determine whether it is a new record or an
* update. If it is a new record, log the event.
*
* @param array $args An array of parameters passed by the hook
* @return void
*/
public function hookAfterSaveRecord($args)
{
$record = $args['record'];
if (!$this->_isLoggable($record)) {
return;
}
// It's an update of a record.
if (empty($args['insert'])) {
$this->_logEvent($record, HistoryLogEntry::OPERATION_UPDATE);
}
// This is a new record, imported or manually created.
else {
$imported = $this->_isImported();
if ($imported) {
$this->_logEvent($record, HistoryLogEntry::OPERATION_IMPORT, $imported);
}
$this->_logEvent($record, HistoryLogEntry::OPERATION_CREATE);
}
}
/**
* When an record is deleted, log the event.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookBeforeDeleteRecord($args)
{
$record = $args['record'];
if (!$this->_isLoggable($record)) {
return;
}
$this->_logEvent($record, HistoryLogEntry::OPERATION_DELETE);
}
/**
* Hook used before save an element text.
*
* The fonction "update_item" uses Builder_Item, that saves element texts
* before the record, so the old element texts are lost when it is used. So
* a check is done to save them in case of an update.
*
* @param array $args An array of parameters passed by the hook
* @return void
*/
public function hookBeforeSaveElementText($args)
{
$elementText = $args['record'];
// Prepare the log entry if none.
if (!isset($this->_preparedLogEntries[$elementText->record_type][$elementText->record_id])) {
$logEntry = new HistoryLogEntry();
$record = get_record_by_id($elementText->record_type, $elementText->record_id);
if (empty($record)) {
return;
}
$logEntry->prepareNewEvent($record, 'element_text');
$this->_preparedLogEntries[$elementText->record_type][$elementText->record_id] = $logEntry;
}
$logEntry = $this->_preparedLogEntries[$elementText->record_type][$elementText->record_id];
// Save old values for an update.
if (empty($args['insert'])) {
// Return the old and unmodified text too to avoid an issue when
// order of texts change, in particular when a text that is not the
// last is removed.
$db = $this->_db;
$sql = "SELECT text FROM {$db->ElementText} WHERE id = " . (integer) $elementText->id;
$oldText = $db->fetchOne($sql);
$logEntry->prepareOneElementText($elementText->element_id, HistoryLogChange::TYPE_UPDATE, array(
'old' => $oldText,
'new' => $elementText->text,
), $elementText->id);
}
// Save a new value.
else {
$logEntry->prepareOneElementText($elementText->element_id, HistoryLogChange::TYPE_CREATE, $elementText->text);
}
}
/**
* When a record is deleted, log the event.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookBeforeDeleteElementText($args)
{
$elementText = $args['record'];
// Prepare the log entry if none.
if (!isset($this->_preparedLogEntries[$elementText->record_type][$elementText->record_id])) {
$logEntry = new HistoryLogEntry();
$record = get_record_by_id($elementText->record_type, $elementText->record_id);
if (empty($record)) {
return;
}
$logEntry->prepareNewEvent($record, 'element_text');
$this->_preparedLogEntries[$elementText->record_type][$elementText->record_id] = $logEntry;
}
$logEntry = $this->_preparedLogEntries[$elementText->record_type][$elementText->record_id];
$logEntry->prepareOneElementText($elementText->element_id, HistoryLogChange::TYPE_DELETE, $elementText->text, $elementText->id);
}
/**
* When an record is deleted, log the event.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookBeforeDeleteElement($args)
{
$record = $args['record'];
// Search all records with content of this element in order to log them.
//A direct sql is used because the process may be heavy
$user = current_user();
if (is_null($user)) {
throw new Exception(__('Could not retrieve user info before removing of element %d.', $record->id));
}
$db = $this->_db;
// To avoid a transaction with Zend, the date of Entries is set in the
// future, then the ids are recalled for Changes, and finally are reset
// to true time.
$added = $db->quote(date('Y-m-d H:i:s', strtotime('+1 year')));
$sql = "
INSERT INTO `{$db->HistoryLogEntry}` (`record_type`, `record_id`, `user_id`, `operation`, `added`)
SELECT 'Collection', `record_id`, $user->id, '" . HistoryLogEntry::OPERATION_UPDATE . "', $added
FROM `{$db->ElementText}`
WHERE `record_type` = 'Collection'
AND `element_id` = {$record->id}
;";
$db->query($sql);
$sql = "
INSERT INTO `{$db->HistoryLogEntry}` (`record_type`, `record_id`, `part_of`, `user_id`, `operation`, `added`)
SELECT 'Item', `record_id`, `collection_id`, $user->id, '" . HistoryLogEntry::OPERATION_UPDATE . "', $added
FROM `{$db->ElementText}` AS `element_texts`
JOIN `{$db->Item}` AS `items`
ON `element_texts`.`record_type` = 'Item'
AND `items`.`id` = `element_texts`.`record_id`
WHERE `element_texts`.`record_type` = 'Item'
AND `element_texts`.`element_id` = {$record->id}
;";
$db->query($sql);
$sql = "
INSERT INTO `{$db->HistoryLogEntry}` (`record_type`, `record_id`, `part_of`, `user_id`, `operation`, `added`)
SELECT 'Item', `record_id`, `item_id`, $user->id, '" . HistoryLogEntry::OPERATION_UPDATE . "', $added
FROM `{$db->ElementText}` AS `element_texts`
JOIN `{$db->File}` AS `files`
ON `element_texts`.`record_type` = 'File'
AND `files`.`id` = `element_texts`.`record_id`
WHERE `element_texts`.`record_type` = 'File'
AND `element_texts`.`element_id` = {$record->id}
;";
$db->query($sql);
$sql = "
INSERT INTO `{$db->HistoryLogChange}` (`entry_id`, `element_id`, `type`, `text`)
SELECT `entries`.`id`, {$record->id}, '" . HistoryLogChange::TYPE_DELETE . "', `element_texts`.`text`
FROM `{$db->HistoryLogEntry}` AS `entries`
JOIN `{$db->ElementText}` AS `element_texts`
ON `element_texts`.`record_type` = `entries`.`record_type`
AND `element_texts`.`record_id` = `entries`.`record_id`
AND `entries`.`added` = $added
AND `element_texts`.`element_id` = {$record->id}
;";
$db->query($sql);
$sql = "
UPDATE `{$db->HistoryLogEntry}`
SET `added` = NOW()
WHERE `added` = $added
;";
$db->query($sql);
// For integrity purposes, the deletion of element texts is done via one
// sql too.
$sql = "
DELETE FROM `{$db->ElementText}`
WHERE `element_id` = {$record->id}
;";
$db->query($sql);
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$msg = __('The element "%s" (%d) in set "%s" has been removed.', $record->name, $record->id, $record->set_name);
_log('[HistoryLog] ' . $msg, Zend_Log::WARN);
$flash->addMessage($msg, 'success');
$msg = __('The Omeka base should be reindexed.');
_log('[HistoryLog] ' . $msg, Zend_Log::WARN);
$flash->addMessage($msg, 'success');
}
public function hookExport($args)
{
$service = $args['service'];
foreach ($args['records'] as $id => $value) {
$this->_logRecord(
array(
// TODO Manage other exports.
'record_type' => 'Item',
'record_id' => $id,
),
HistoryLogEntry::OPERATION_EXPORT,
$service);
}
}
/**
* Hook for items/show page.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookAdminItemsShow($args)
{
$args['record'] = $args['item'];
unset($args['item']);
$this->_adminRecordShow($args, 'items/show');
}
/**
* Hook for collections/show page.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookAdminCollectionsShow($args)
{
$args['record'] = $args['collection'];
unset($args['collection']);
$this->_adminRecordShow($args, 'collections/show');
}
/**
* Hook for collections/show page.
*
* @param array $args An array of parameters passed by the hook.
* @return void
*/
public function hookAdminFilesShow($args)
{
$args['record'] = $args['file'];
unset($args['file']);
$this->_adminRecordShow($args, 'files/show');
}
/**
* Helper to show the 5 most recent events in the record's history on the
* record's admin page.
*
* @param array $args An array of parameters passed by the hook.
* @param string $page The current type of the page
* @return void
*/
protected function _adminRecordShow($args, $page)
{
$record = $args['record'];
$view = $args['view'];
$currentPages = json_decode(get_option('history_log_display'), true) ?: array();
if (!in_array($page, $currentPages)) {
return;
}
try {
echo $view->showlog($record, 5);
} catch(Exception $e) {
throw $e;
}
}
/**
* Show details for each item.
*
* @param array $args An array of parameters passed by the hook
* @return void
*/
public function hookAdminItemsBrowseDetailedEach($args)
{
$record = $args['item'];
$view = $args['view'];
$currentPages = json_decode(get_option('history_log_display'), true) ?: array();
if (!in_array('items/browse', $currentPages)) {
return;
}
$logEntry = $this->_db->getTable('HistoryLogEntry')
->getLastEntryForRecord($record);
if ($logEntry) {
$html = '<div class="history-log">';
switch ($logEntry->operation) {
case HistoryLogEntry::OPERATION_CREATE:
$html .= __('Created on %s by %s.',
$logEntry->displayAdded(), $logEntry->displayUser());
break;
case HistoryLogEntry::OPERATION_UPDATE:
$html .= __('Updated on %s by %s.',
$logEntry->displayAdded(), $logEntry->displayUser());
break;
case HistoryLogEntry::OPERATION_DELETE:
$html .= __('Deleted on %s by %s.',
$logEntry->displayAdded(), $logEntry->displayUser());
break;
case HistoryLogEntry::OPERATION_IMPORT:
$html .= __('Imported on %s by %s.',
$logEntry->displayAdded(), $logEntry->displayUser());
break;
case HistoryLogEntry::OPERATION_EXPORT:
$html .= __('Exported on %s by %s.',
$logEntry->displayAdded(), $logEntry->displayUser());
break;
}
$html .= '</div>';
echo $html;
}
}
/**
* Load the plugin javascript when admin section loads
*
* @return void
*/
public function hookAdminHead()
{
$requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
$module = isset($requestParams['module']) ? $requestParams['module'] : 'default';
$controller = isset($requestParams['controller']) ? $requestParams['controller'] : 'index';
$action = isset($requestParams['action']) ? $requestParams['action'] : 'index';
if ($module == 'history-log' && $controller == 'index' && $action == 'search') {
queue_js_file('history-log');
}
}
/**
* Add the History Log link to the admin main navigation.
*
* @param array $nav Navigation array.
* @return array $filteredNav Filtered navigation array.
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('History Logs'),
'uri' => url('history-log'),
'resource' => 'HistoryLog_Index',
'privilege' => 'index',
);
return $nav;
}
/**
* Quickly check if a record is loggable (item, collection, file).
*
* @param Record $record
* @return boolean
*/
protected function _isLoggable($record)
{
return in_array(get_class($record), array('Item', 'Collection', 'File'));
}
/**
* Check if a record is imported.
*
* @return string Origin of the import, else, if empty, created manually.
*/
protected function _isImported()
{
$imported = '';
$request = Zend_Controller_Front::getInstance()->getRequest();
if ($request) {
$url = current_url();
if ($url) {
if (strpos('nuxeo-link', $url)) {
$imported = 'Nuxeo';
}
elseif (strpos('youtube', $url)) {
$imported = 'YouTube';
}
elseif (strpos('flickr', $url)) {
$imported = 'Flickr';
}
}
// Else manually created.
}
// Else background script.
else {
$imported = __('Background script');
}
return $imported;
}
/**
* Create a new history log entry.
*
* @uses HistoryLogEntry::logEvent()
*
* @param Object|array $record The Omeka record to log. It should be an
* object for a "create" or an "update".
* @param string $operation The type of event to log (e.g. "create"...).
* @param string|array $changes An extra piece of type specific data for the
* log.
* @return void
*/
private function _logEvent($record, $operation, $changes = null)
{
$user = current_user();
if (is_null($user)) {
// Some contribution plugins like Scripto allow anonymous users to
// edit an element of a record.
$user = new User;
// Only the user id is set in HistoryLog entries.
$user->id = 0;
// The username should be unique. It can be the ip, like the wiki
// used by Scripto.
$user->username = 'anonymous';
$user->role = 'anonymous';
$user->name = 'anonymous';
// throw new Exception(__('Could not retrieve user info.'));
}
// If the operation is an update, get the saved data.
if ($operation == HistoryLogEntry::OPERATION_UPDATE
&& isset($this->_preparedLogEntries[get_class($record)][$record->id])
) {
$logEntry = $this->_preparedLogEntries[get_class($record)][$record->id];
}
// Normal save.
else {
$logEntry = new HistoryLogEntry();
}
try {
// Prepare the log entry.
$result = $logEntry->logEvent($record, $operation, $user, $changes);
// May avoid a double update.
unset($this->_preparedLogEntries[get_class($record)][$record->id]);
// Quick check if the record is loggable.
if (!$result) {
throw new Exception(__('This event is not loggable.'));
}
// Only save if this is useful.
$result = $logEntry->saveIfChanged();
if ($result === false) {
throw new Exception(__('Could not log info.'));
}
} catch(Exception $e) {
throw $e;
}
}
}