Adds annotations to Mirador 4 implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 lines
3.1 KiB

<?php
/**
* @file
* Install hooks for Islandora Mirador Annotations.
*/
/**
* Implements hook_schema().
*/
function islandora_mirador_annotations_schema() {
$schema['islandora_mirador_annotation'] = [
'description' => 'Stores IIIF Web Annotations created in Mirador.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
],
'annotation_key' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
],
'annotation_id' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
],
'canvas_key' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
],
'canvas_id' => [
'type' => 'text',
'size' => 'big',
'not null' => TRUE,
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'annotation_json' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
],
'created' => [
'type' => 'int',
'not null' => TRUE,
],
'changed' => [
'type' => 'int',
'not null' => TRUE,
],
],
'primary key' => ['id'],
'unique keys' => [
'annotation_key' => ['annotation_key'],
],
'indexes' => [
'canvas_key' => ['canvas_key'],
'uid' => ['uid'],
],
];
return $schema;
}
/**
* Repair rows whose stored canvas differs from the Web Annotation target.
*/
function islandora_mirador_annotations_update_9001() {
$database = \Drupal::database();
$result = $database->select('islandora_mirador_annotation', 'a')
->fields('a', ['id', 'canvas_id', 'annotation_json'])
->execute();
$updated = 0;
foreach ($result as $row) {
$annotation = json_decode((string) $row->annotation_json, TRUE);
if (!is_array($annotation)) {
continue;
}
$target = $annotation['target'] ?? NULL;
$target_canvas_id = NULL;
if (is_string($target)) {
$target_canvas_id = explode('#', $target, 2)[0];
}
elseif (is_array($target)) {
$source = $target['source'] ?? NULL;
if (is_string($source)) {
$target_canvas_id = explode('#', $source, 2)[0];
}
elseif (is_array($source)) {
$value = $source['id'] ?? $source['@id'] ?? NULL;
if (is_string($value)) {
$target_canvas_id = explode('#', $value, 2)[0];
}
}
if (!$target_canvas_id) {
$value = $target['id'] ?? $target['@id'] ?? NULL;
if (is_string($value)) {
$target_canvas_id = explode('#', $value, 2)[0];
}
}
}
if (!$target_canvas_id || $target_canvas_id === $row->canvas_id) {
continue;
}
$database->update('islandora_mirador_annotation')
->fields([
'canvas_id' => $target_canvas_id,
'canvas_key' => hash('sha256', $target_canvas_id),
])
->condition('id', $row->id)
->execute();
$updated++;
}
return t('Re-associated @count Mirador annotation(s) with their target canvas.', [
'@count' => $updated,
]);
}