From 49e032be191b3b59a93d969fc5df2a64cdf34264 Mon Sep 17 00:00:00 2001 From: astanley Date: Fri, 27 Nov 2026 14:30:27 -0400 Subject: [PATCH] Canvas alias resolution, shared annotation retrieval, and contextual target rewriting.a --- src/Controller/AnnotationController.php | 155 ++++++++++++++++++------ 1 file changed, 116 insertions(+), 39 deletions(-) diff --git a/src/Controller/AnnotationController.php b/src/Controller/AnnotationController.php index 87806e2a..a1af44f1 100644 --- a/src/Controller/AnnotationController.php +++ b/src/Controller/AnnotationController.php @@ -84,27 +84,9 @@ final class AnnotationController extends ControllerBase { } $items = []; - foreach (array_chunk($canvas_ids, 500) as $chunk) { - $keys = array_map(static fn ($id) => hash('sha256', $id), $chunk); - $result = $this->database->select('islandora_mirador_annotation', 'a') - ->fields('a', ['canvas_id', 'annotation_json', 'created']) - ->condition('canvas_key', $keys, 'IN') - ->orderBy('created', 'ASC') - ->execute(); - - foreach ($result as $row) { - try { - $annotation = json_decode($row->annotation_json, TRUE, 512, JSON_THROW_ON_ERROR); - if (is_array($annotation)) { - $items[] = [ - 'canvasId' => $row->canvas_id, - 'annotation' => $this->normalizeAnnotation($annotation), - ]; - } - } - catch (\JsonException) { - // Ignore a corrupt row rather than breaking the entire manifest list. - } + foreach ($this->annotationsForCanvases($canvas_ids, $request) as $canvas_id => $annotations) { + foreach ($annotations as $annotation) { + $items[] = ['canvasId' => $canvas_id, 'annotation' => $annotation]; } } @@ -324,24 +306,7 @@ final class AnnotationController extends ControllerBase { * Builds an AnnotationPage from stored rows. */ private function annotationPage(string $canvas_id, Request $request): array { - $items = []; - $result = $this->database->select('islandora_mirador_annotation', 'a') - ->fields('a', ['annotation_json']) - ->condition('canvas_key', hash('sha256', $canvas_id)) - ->orderBy('created', 'ASC') - ->execute(); - - foreach ($result as $row) { - try { - $annotation = json_decode($row->annotation_json, TRUE, 512, JSON_THROW_ON_ERROR); - if (is_array($annotation)) { - $items[] = $this->normalizeAnnotation($annotation); - } - } - catch (\JsonException) { - // Ignore a corrupt row rather than breaking every annotation on canvas. - } - } + $items = $this->annotationsForCanvases([$canvas_id], $request)[$canvas_id] ?? []; $page_id = $request->getSchemeAndHttpHost() . $request->getBaseUrl() . '/islandora-mirador/annotations?canvas=' . rawurlencode($canvas_id); @@ -353,4 +318,116 @@ final class AnnotationController extends ControllerBase { ]; } + + /** + * Resolves local page/book canvas aliases in batches, preserving exact matches. + * Only node IDs linked to the same media through media_of/member_of qualify. + */ + private function canvasAliases(array $canvas_ids, Request $request): array { + $base = $request->getSchemeAndHttpHost() . $request->getBaseUrl(); + $pattern = '~^' . preg_quote($base, '~') . '/node/([0-9]+)/canvas/([0-9]+)$~D'; + $aliases = []; + $parsed = []; + foreach ($canvas_ids as $id) { + $aliases[$id] = [$id]; + if (preg_match($pattern, $id, $matches)) { + $parsed[$id] = [(int) $matches[1], (int) $matches[2]]; + } + } + $contexts = []; + $mids = array_values(array_unique(array_column($parsed, 1))); + foreach (array_chunk($mids, 500) as $chunk) { + $query = $this->database->select('media__field_media_of', 'm'); + $query->leftJoin('node__field_member_of', 'p', 'p.entity_id = m.field_media_of_target_id AND p.deleted = 0'); + $query->fields('m', ['entity_id', 'field_media_of_target_id']); + $query->addField('p', 'field_member_of_target_id', 'parent_nid'); + $query->condition('m.entity_id', $chunk, 'IN'); + $query->condition('m.deleted', 0); + foreach ($query->execute() as $row) { + $mid = (int) $row->entity_id; + $page = (int) $row->field_media_of_target_id; + $contexts[$mid][$page] = $page; + if ($row->parent_nid !== NULL) { + $parent = (int) $row->parent_nid; + $contexts[$mid][$parent] = $parent; + } + } + } + foreach ($parsed as $id => [$nid, $mid]) { + if (!isset($contexts[$mid][$nid])) { + continue; + } + foreach ($contexts[$mid] as $context) { + $aliases[$id][] = $base . '/node/' . $context . '/canvas/' . $mid; + } + $aliases[$id] = array_values(array_unique($aliases[$id])); + } + return $aliases; + } + + /** + * Rewrites exact canvas references or canvas fragments, not annotation IDs. + */ + private function rewriteTarget(mixed $value, string $from, string $to): mixed { + if (is_array($value)) { + foreach ($value as $key => $child) { + $value[$key] = $this->rewriteTarget($child, $from, $to); + } + } + elseif (is_string($value)) { + if ($value === $from || str_starts_with($value, $from . '#')) { + return $to . substr($value, strlen($from)); + } + } + return $value; + } + + /** + * Retrieves shared records and presents targets in the requested context. + */ + private function annotationsForCanvases(array $canvas_ids, Request $request): array { + $aliases = $this->canvasAliases($canvas_ids, $request); + $by_key = []; + $result = []; + foreach ($aliases as $requested => $ids) { + $result[$requested] = []; + foreach ($ids as $id) { + $by_key[hash('sha256', $id)][$requested] = $id; + } + } + $rows = []; + foreach (array_chunk(array_keys($by_key), 500) as $keys) { + $query = $this->database->select('islandora_mirador_annotation', 'a') + ->fields('a', ['id', 'canvas_key', 'canvas_id', 'annotation_json', 'created']) + ->condition('canvas_key', $keys, 'IN'); + foreach ($query->execute() as $row) { + $rows[] = $row; + } + } + usort($rows, static fn ($a, $b) => [(int) $a->created, (int) $a->id] <=> [(int) $b->created, (int) $b->id]); + foreach ($rows as $row) { + try { + $annotation = json_decode($row->annotation_json, TRUE, 512, JSON_THROW_ON_ERROR); + } + catch (\JsonException) { + continue; + } + if (!is_array($annotation)) { + continue; + } + $annotation = $this->normalizeAnnotation($annotation); + foreach ($by_key[$row->canvas_key] ?? [] as $requested => $matched) { + if ($matched !== $row->canvas_id) { + continue; + } + $copy = $annotation; + if (isset($copy['target'])) { + $copy['target'] = $this->rewriteTarget($copy['target'], $row->canvas_id, $requested); + } + $result[$requested][] = $copy; + } + } + return $result; + } + }