forked from torontomulibrary/islandora_usage_stats
-
Notifications
You must be signed in to change notification settings - Fork 2
/
islandora_usage_stats.install
675 lines (656 loc) · 19.4 KB
/
islandora_usage_stats.install
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
<?php
/**
* @file
* Install/uninstall hooks.
*/
/**
* Implements hook_uninstall().
*/
function islandora_usage_stats_uninstall() {
$variables = array(
'islandora_usage_stats_ip_list',
'islandora_usage_stats_cooldown_seconds',
'islandora_usage_stats_exclude_bots',
'islandora_usage_stats_exclude_bots_regex',
'islandora_usage_stats_top_downloads_block_display_rows',
'islandora_usage_stats_top_search_block_display_rows',
);
array_walk($variables, 'variable_del');
}
/**
* Implements hook_schema().
*/
function islandora_usage_stats_schema() {
$schema['islandora_usage_stats_datastreams'] = array(
'description' => 'Contains datastream information.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'dsid' => array(
'description' => 'The datastream identifier.',
'type' => 'varchar',
'length' => 255,
),
'pid_id' => array(
'description' => 'The pid id foreign relations.',
'type' => 'int',
'not NULL' => TRUE,
),
'ds_label' => array(
'description' => 'The datastream label.',
'type' => 'varchar',
'length' => 255,
),
),
'foreign keys' => array(
'pid_id' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('pid_id' => 'id'),
),
),
'primary key' => array('id'),
'unique keys' => array(
'pid_dsid' => array('pid_id', 'dsid'),
),
);
$schema['islandora_usage_stats_top_searches'] = array(
'description' => 'Used to log top searches.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'term' => array(
'description' => 'Search term.',
'type' => 'text',
'size' => 'medium',
),
'search_total' => array(
'description' => 'The total times this term has been searched.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'indexes' => array(
// 767 is maximum index size in worst case.
// But if UTF8 is used then it is 3x. so 255
'term_index' => array(array('term', 255)),
),
'primary key' => array('id'),
);
$schema['islandora_usage_stats_object_access_log'] = array(
'description' => 'This table is used to store the usage information for Fedora objects.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'pid_id' => array(
'description' => 'PID ID of the object.',
'type' => 'int',
'not null' => TRUE,
),
'time' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'IP of user that accessed the object.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'uid' => array(
'description' => 'Drupal user that accessed the object.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'indexes' => array(
'pid_id_index' => array('pid_id'),
),
);
$schema['islandora_usage_stats_object_ds_access_log'] = array(
'description' => 'This table is used to store the usage information for Fedora datastreams.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'ds_id' => array(
'description' => 'Database datastream ID.',
'type' => 'int',
'not null' => TRUE,
),
'time' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'IP of user that accessed the object.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'uid' => array(
'description' => 'Drupal user that accessed the object.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'ds_id' => array(
'table' => 'islandora_usage_stats_datastreams',
'columns' => array('ds_id' => 'id'),
),
),
'indexes' => array(
'ds_id' => array('ds_id'),
),
);
$schema['islandora_usage_stats_objects'] = array(
'description' => 'Used to specify object information in the DB; primarily for views.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'label' => array(
'description' => 'Object label in Fedora.',
'type' => 'varchar',
'length' => 255,
),
'pid' => array(
'description' => 'The PID of the object.',
'type' => 'varchar',
'length' => 64,
'not NULL' => TRUE,
),
),
'primary key' => array('id'),
'unique keys' => array(
'pid' => array('pid'),
),
);
$schema['islandora_usage_stats_collection_access_log'] = array(
'description' => 'Used to log the parent collection(s) of an object being visited.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'object_access_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'collection' => array(
'description' => 'The PID ID of the collection.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'foreign keys' => array(
'object_acces_id' => array(
'table' => 'islandora_usage_stats_object_access_log',
'columns' => array('object_access_id' => 'id'),
),
'collection' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('collection' => 'id'),
),
),
'primary key' => array('id'),
'indexes' => array(
'object_access_id_index' => array('object_access_id'),
'collection_index' => array('collection'),
),
);
$schema['islandora_usage_stats_content_model_access_log'] = array(
'description' => 'Used to log the content model(s) when an object is visited.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'object_access_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'content_model' => array(
'description' => 'The PID ID of the content model.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'foreign keys' => array(
'object_access_id' => array(
'table' => 'islandora_usage_stats_object_access_log',
'columns' => array('object_access_id' => 'id'),
),
'content_model' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('content_model' => 'id'),
),
),
'primary key' => array('id'),
);
return $schema;
}
/**
* Updates schema to support collections and content models.
*/
function islandora_usage_stats_update_7001(&$sandbox) {
$schema = array();
$sandbox['#finished'] = 0;
$pid_tables = array(
'islandora_usage_stats_object_ds_downloads',
'islandora_usage_stats_object_access_log',
'islandora_usage_stats_object_ds_access_log',
'islandora_usage_stats_object_views',
);
// Add new tables.
if (!isset($sandbox['done_first_pass'])) {
$sandbox['done_first_pass'] = TRUE;
$schema['islandora_usage_stats_objects'] = array(
'description' => 'Used to specify object information in the DB; primarily for views.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'label' => array(
'description' => 'Object label in Fedora.',
'type' => 'varchar',
'length' => 255,
),
'pid' => array(
'description' => 'The PID of the object.',
'type' => 'varchar',
'length' => 64,
'not NULL' => TRUE,
),
),
'primary key' => array('id'),
'unique keys' => array(
'pid' => array('pid'),
),
);
$schema['islandora_usage_stats_collection_access_log'] = array(
'description' => 'Used to log the parent collection(s) of an object being visited.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'object_access_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'collection' => array(
'description' => 'The PID ID of the collection.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'foreign keys' => array(
'object_acces_id' => array(
'table' => 'islandora_usage_stats_object_access_log',
'columns' => array('object_access_id' => 'id'),
),
'collection' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('collection' => 'id'),
),
),
'primary key' => array('id'),
);
$schema['islandora_usage_stats_content_model_access_log'] = array(
'description' => 'Used to log the content model(s) when an object is visited.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'object_access_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'content_model' => array(
'description' => 'The PID ID of the content model.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'foreign keys' => array(
'object_access_id' => array(
'table' => 'islandora_usage_stats_object_access_log',
'columns' => array('object_access_id' => 'id'),
),
'content_model' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('content_model' => 'id'),
),
),
'primary key' => array('id'),
);
foreach ($schema as $name => $schema_info) {
db_create_table($name, $schema_info);
}
$pid_queries = array();
foreach ($pid_tables as $table) {
db_drop_index($table, 'pid');
db_add_field(
$table,
'pid_id',
array(
'description' => 'PID ID of the object.',
'type' => 'int',
'not null' => TRUE,
'initial' => 1,
)
);
$pid_queries[] = db_select($table)
->fields($table, array('pid'))
->groupBy('pid');
}
// Populate PID table with existing PIDS.
foreach ($pid_queries as $query) {
if (!isset($pid_query)) {
$pid_query = $query;
}
else {
$pid_query->union($query);
}
}
db_insert('islandora_usage_stats_objects')
->from($pid_query)
->execute();
// Populate PID ID columns, and drop the PID column.
foreach ($pid_tables as $table) {
db_update($table)
->expression(
"$table.pid_id",
"(SELECT islandora_usage_stats_objects.id FROM
islandora_usage_stats_objects WHERE $table.pid = islandora_usage_stats_objects.pid)"
)
->execute();
db_drop_field($table, 'pid');
}
$sandbox['total'] = db_select('islandora_usage_stats_objects')
->fields('islandora_usage_stats_objects', array('pid'))
->countQuery()
->execute()
->fetchField();
}
// Populate label info in object table (batch).
$step = 10;
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
}
$query = db_select('islandora_usage_stats_objects')
->fields('islandora_usage_stats_objects', array('pid'))
->orderBy('islandora_usage_stats_objects.id')
->range($sandbox['progress'], $step)
->execute();
$results = $query->fetchAllAssoc('pid');
$pids = array_keys($results);
foreach ($pids as $pid) {
$object = islandora_object_load($pid);
db_update('islandora_usage_stats_objects')
->fields(array('label' => $object->label))
->condition('pid', $pid)
->execute();
}
$sandbox['progress'] = $sandbox['progress'] + $step;
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
if ($sandbox['#finished'] >= 1) {
return t('Updated database to support logging collection and content model events.');
}
}
/**
* Adds support for top search/top downloads monitoring in islandora.
*/
function islandora_usage_stats_update_7002() {
$schema['islandora_usage_stats_top_searches'] = array(
'description' => 'Used to log top searches',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'term' => array(
'description' => 'Search term',
'type' => 'varchar',
'length' => 255,
),
'search_total' => array(
'description' => 'The total times this term has been searched.',
'type' => 'int',
'not NULL' => TRUE,
),
),
'primary key' => array('id'),
);
$schema['islandora_usage_stats_top_downloads'] = array(
'description' => 'This table is used to store datastream download count for all fedora objects',
'fields' => array(
'id' => array(
'description' => 'Unique id for each row',
'type' => 'serial',
'not null' => TRUE,
),
'pid_id' => array(
'description' => 'PID ID of the object.',
'type' => 'int',
'not null' => TRUE,
),
'ds_id' => array(
'description' => 'The ds relation to the datastreams table',
'type' => 'int',
'not null' => TRUE,
),
'downloads' => array(
'description' => 'The number of views an object has received',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'time' => array(
'description' => 'The time in number of seconds of the last download of the object',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'ds_id' => array(
'table' => 'islandora_usage_stats_datastreams',
'columns' => array('ds_id' => 'id'),
),
'pid_id' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('pid_id' => 'id'),
),
),
'indexes' => array(
'ds_id' => array(
'ds_id',
),
),
);
$schema['islandora_usage_stats_datastreams'] = array(
'description' => 'Used to log top searches',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'dsid' => array(
'description' => 'The datastream identifier',
'type' => 'varchar',
'length' => 255,
),
'pid_id' => array(
'description' => 'The pid id forign relations',
'type' => 'int',
'not NULL' => TRUE,
),
'ds_label' => array(
'description' => 'The datastream label',
'type' => 'varchar',
'length' => 255,
),
),
'foreign keys' => array(
'pid_id' => array(
'table' => 'islandora_usage_stats_objects',
'columns' => array('pid_id' => 'id'),
),
),
'primary key' => array('id'),
);
db_create_table('islandora_usage_stats_top_downloads', $schema['islandora_usage_stats_top_downloads']);
db_create_table('islandora_usage_stats_top_searches', $schema['islandora_usage_stats_top_searches']);
db_create_table('islandora_usage_stats_datastreams', $schema['islandora_usage_stats_datastreams']);
return t('Updated database to support top search logging, and top downloads.');
}
/**
* Large rework of module. All object and datastream records will be lost.
*/
function islandora_usage_stats_update_7003() {
db_drop_table('islandora_usage_stats_object_views');
db_drop_table('islandora_usage_stats_object_ds_downloads');
db_drop_table('islandora_usage_stats_top_downloads');
db_add_unique_key('islandora_usage_stats_datastreams', 'pid_dsid', array('pid_id', 'dsid'));
// Just re-create the tables.
db_drop_table('islandora_usage_stats_object_ds_access_log');
db_drop_table('islandora_usage_stats_object_access_log');
$schema = array();
$schema['islandora_usage_stats_object_access_log'] = array(
'description' => 'This table is used to store the usage information for Fedora objects.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'pid_id' => array(
'description' => 'PID ID of the object.',
'type' => 'int',
'not null' => TRUE,
),
'time' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'IP of user that accessed the object.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'uid' => array(
'description' => 'Drupal user that accessed the object.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
);
$schema['islandora_usage_stats_object_ds_access_log'] = array(
'description' => 'This table is used to store the usage information for Fedora datastreams.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'ds_id' => array(
'description' => 'Database datastream ID.',
'type' => 'int',
'not null' => TRUE,
),
'time' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'IP of user that accessed the object.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'uid' => array(
'description' => 'Drupal user that accessed the object.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'ds_id' => array(
'table' => 'islandora_usage_stats_datastreams',
'columns' => array('ds_id' => 'id'),
),
),
'indexes' => array(
'ds_id' => array('ds_id'),
),
);
db_create_table('islandora_usage_stats_object_ds_access_log', $schema['islandora_usage_stats_object_ds_access_log']);
db_create_table('islandora_usage_stats_object_access_log', $schema['islandora_usage_stats_object_access_log']);
// Now we always log.
variable_del('islandora_usage_stats_use_access_log');
variable_del('islandora_usage_stats_alter_pdf_display');
}
/**
* Adding indexes to speed views.
*/
function islandora_usage_stats_update_7004() {
db_add_index('islandora_usage_stats_object_access_log', 'pid_id_index', array('pid_id'));
db_add_index('islandora_usage_stats_collection_access_log', 'object_access_id_index', array('object_access_id'));
db_add_index('islandora_usage_stats_collection_access_log', 'collection_index', array('collection'));
}
/**
* Fix to search stats table, increasing the size of search strings allowed.
*/
function islandora_usage_stats_update_7005() {
db_change_field(
'islandora_usage_stats_top_searches',
'term',
'term',
array(
'description' => 'Search term.',
'type' => 'text',
'size' => 'medium',
),
array(
'indexes' => array(
// 767 is maximum index size in worst case.
// But if UTF8 is used it is 3x. So 255
'term_index' => array(array('term', 255)),
),
)
);
}