From f0912f611c49341505173b3d42e2201139b4eeb2 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 16 Oct 2023 21:46:58 -0300 Subject: [PATCH 01/39] Refactor IIIF, create IIIF Info Service. --- .../islandora_iiif.services.yml | 4 + modules/islandora_iiif/src/IiifInfo.php | 113 ++++++++++++++++++ .../src/Plugin/views/style/IIIFManifest.php | 12 +- 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 modules/islandora_iiif/islandora_iiif.services.yml create mode 100644 modules/islandora_iiif/src/IiifInfo.php diff --git a/modules/islandora_iiif/islandora_iiif.services.yml b/modules/islandora_iiif/islandora_iiif.services.yml new file mode 100644 index 000000000..42b6054ac --- /dev/null +++ b/modules/islandora_iiif/islandora_iiif.services.yml @@ -0,0 +1,4 @@ +services: + islandora_iiif: + class: Drupal\islandora_iiif\IiifInfo + arguments: ['@config.factory', '@http_client', '@logger.channel.islandora'] diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php new file mode 100644 index 000000000..294af19e5 --- /dev/null +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -0,0 +1,113 @@ +configFactory = $config_factory; + + $this->iiifConfig= $this->configFactory->get('islandora_iiif.settings'); + $this->httpClient = $http_client; + $this->logger = $channel; + } + + /** + * The IIIF base URL for an image. + * Visiting this URL will resolve to the info.json for the image. + * + * @return string + * The absolute URL on the IIIF server. + */ + public function baseUrl($image) { + + if ($this->iiifConfig->get('use_relative_paths')) { + $file_url = ltrim($image->createFileUrl(TRUE), '/'); + } + else { + $file_url = $image->createFileUrl(FALSE); + } + + $iiif_address = $this->iiifConfig->get('iiif_server'); + $iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url); + + return $iiif_url; + } + + /** + * Retrieve an image's dimensions via the IIIF server. + * + * @param \Drupal\File\FileInterface $file + * The image file. + * @return array|FALSE + * The image dimensions in an array as [$width, $height] + */ + public function getImageDimensions(FileInterface $file) { + $iiif_url = $this->baseUrl($file); + try { + $info_json = $this->httpClient->get($iiif_url)->getBody(); + $resource = json_decode($info_json, TRUE); + $width = $resource['width']; + $height = $resource['height']; + if (is_numeric($width) && is_numeric($height)) { + return [intval($width), intval($height)]; + } + } + catch (ClientException | ConnectException $e) { + $this->logger->info("Error getting image file dimensions from IIIF server: " . $e->getMessage()); + } + return FALSE; + } + + +} diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index f0aca47da..e93891413 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -146,6 +146,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->messenger = $messenger; $this->utils = $utils; $this->moduleHandler = $moduleHandler; + $this->utils = $utils; } /** @@ -213,6 +214,11 @@ public function render() { $label = $this->t("IIIF Manifest"); } + /** + * @var \Drupal\taxonomy\TermInterface|null + */ + $structured_text_term = $this->utils->getTermForUri($this->options['structured_text_term_uri']); + // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ '@type' => 'sc:Manifest', @@ -232,7 +238,7 @@ public function render() { // For each row in the View result. foreach ($this->view->result as $row) { // Add the IIIF URL to the image to print out as JSON. - $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id); + $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id, $structured_text_term); foreach ($canvases as $tile_source) { $json['sequences'][0]['canvases'][] = $tile_source; } @@ -261,11 +267,13 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". + * @param \Drupal\taxonomy\TermInterface|null $structured_text_term + * The term that structured text media references, if any. * * @return array * List of IIIF URLs to display in the Openseadragon viewer. */ - protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id) { + protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, $structured_text_term) { $canvases = []; foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) { $viewsField = $this->view->field[$iiif_tile_field]; From bc06fca00c5b34df91092109dfb31a91cd5aef55 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 17 Oct 2023 00:02:56 -0300 Subject: [PATCH 02/39] Islandora IIIF: Add action to retrieve image attributes from IIIF server. --- ...tion.media_attributes_from_iiif_action.yml | 10 ++ modules/islandora_iiif/islandora_iiif.install | 16 ++ .../Plugin/Action/MediaAttributesFromIiif.php | 169 ++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml create mode 100644 modules/islandora_iiif/islandora_iiif.install create mode 100644 modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php diff --git a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml new file mode 100644 index 000000000..dc0b18b91 --- /dev/null +++ b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml @@ -0,0 +1,10 @@ +langcode: en +status: true +dependencies: + module: + - islandora_iiif +id: media_attributes_from_iiif_action +label: 'Media attributes from IIIF' +type: media +plugin: islandora_iiif:media_attributes_from_iiif_action:media +configuration: { } diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install new file mode 100644 index 000000000..9b0407351 --- /dev/null +++ b/modules/islandora_iiif/islandora_iiif.install @@ -0,0 +1,16 @@ +getPath('islandora_iiif') . '/config/optional/' . $config_id .'.yml'; + $data = \Symfony\Component\Yaml\Yaml::parseFile($config_path); + \Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE); +} diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php new file mode 100644 index 000000000..9bb951e23 --- /dev/null +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -0,0 +1,169 @@ +httpClient = $http_client; +$this->iiifInfo = $iiif_info; +$this->utils = $islandora_utils; +$this->mediaSource = $media_source; +$this->logger = $channel; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('datetime.time'), + $container->get('http_client'), + $container->get('islandora_iiif'), + $container->get('islandora.utils'), + $container->get('islandora.media_source_service'), + $container->get('logger.channel.islandora') + ); + } + + /** + * {@inheritdoc} + */ + public function execute($entity = NULL ) { + $width = $height = FALSE; + + // Get the original File media use term. + $original_file_term = $this->utils->getTermForUri('http://pcdm.org/use#OriginalFile'); + + /** + * @var \Drupal\media\MediaInterface $original_file_media + */ + $original_file_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $original_file_term); + if (!empty($original_file_mids)) { + + // Ordinarily there shouldn't be more than one Original File media but it's not guaranteed. + foreach($original_file_mids as $original_file_mid) { + + /* + * @var \Drupal\Media\MediaInterface $original_file_media + */ + $original_file_media = $this->entityTypeManager->getStorage('media')->load($original_file_mid); + + // Get the media MIME Type + $original_file = $this->mediaSource->getSourceFile($original_file_media); + $mime_type = $original_file->getMimeType(); + + if (in_array($mime_type, ['image/tiff', 'image/jp2'])) { + [$width, $height] = $this->iiifInfo->getImageDimensions($original_file); + } + + + // @todo Make field configurable. Low priority since this whole thing is a workaround for an Islandora limitation. + if ($original_file_media->hasField('field_width') && $original_file_media->hasField('field_height')) { + $original_file_media->set('field_height', $height); + $original_file_media->set('field_width', $width); + $original_file_media->save(); + } + } + } + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + + /** @var \Drupal\Core\Entity\EntityInterface $object */ + return $object->access('update', $account, $return_as_object); + } + + } From eb235f8e1d176c0d1c7c85a71040f88ea731d64f Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 17 Oct 2023 13:56:09 -0300 Subject: [PATCH 03/39] Islandora IIIF: Add auth headers to IIIF Info request. --- .../islandora_iiif.services.yml | 2 +- modules/islandora_iiif/src/IiifInfo.php | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/islandora_iiif/islandora_iiif.services.yml b/modules/islandora_iiif/islandora_iiif.services.yml index 42b6054ac..fd39211cd 100644 --- a/modules/islandora_iiif/islandora_iiif.services.yml +++ b/modules/islandora_iiif/islandora_iiif.services.yml @@ -1,4 +1,4 @@ services: islandora_iiif: class: Drupal\islandora_iiif\IiifInfo - arguments: ['@config.factory', '@http_client', '@logger.channel.islandora'] + arguments: ['@config.factory', '@http_client', '@logger.channel.islandora', '@jwt.authentication.jwt'] diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 294af19e5..7e1e74600 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -5,6 +5,8 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\file\FileInterface; +use Drupal\jwt\Authentication\Provider\JwtAuth; + use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; @@ -36,6 +38,13 @@ class IiifInfo { */ protected $iiifConfig; +/** + * JWT Auth provider service. + * + * @var \Drupal\jwt\Authentication\Provider\JwtAuth + */ + protected $jwtAuth; + /** * The logger. * @@ -53,13 +62,16 @@ class IiifInfo { * The HTTP Client. * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. + * @param \Drupal\jwt\Authentication\Provider\JwtAuth $jwt_auth + * The JWT auth provider. */ - public function __construct(ConfigFactoryInterface $config_factory, Client $http_client, LoggerChannelInterface $channel) { + public function __construct(ConfigFactoryInterface $config_factory, Client $http_client, LoggerChannelInterface $channel, JwtAuth $jwt_auth) { $this->configFactory = $config_factory; $this->iiifConfig= $this->configFactory->get('islandora_iiif.settings'); $this->httpClient = $http_client; $this->logger = $channel; + $this->jwtAuth = $jwt_auth; } /** @@ -95,7 +107,11 @@ public function baseUrl($image) { public function getImageDimensions(FileInterface $file) { $iiif_url = $this->baseUrl($file); try { - $info_json = $this->httpClient->get($iiif_url)->getBody(); + $info_json = $this->httpClient->request('get', $iiif_url, [ + 'headers' => [ + 'Authorization' => 'bearer ' . $this->jwtAuth->generateToken() + ] + ])->getBody(); $resource = json_decode($info_json, TRUE); $width = $resource['width']; $height = $resource['height']; From c9bd0910bf0bfa211c30d7cd9720f057c9d8b120 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 18 Oct 2023 04:48:59 -0300 Subject: [PATCH 04/39] Islandora IIIF: Change media action to node. --- .../system.action.media_attributes_from_iiif_action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml index dc0b18b91..36e7c8f9b 100644 --- a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml +++ b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml @@ -5,6 +5,6 @@ dependencies: - islandora_iiif id: media_attributes_from_iiif_action label: 'Media attributes from IIIF' -type: media +type: node plugin: islandora_iiif:media_attributes_from_iiif_action:media configuration: { } From 916e343679f1888e08a7a2a5b69d52a420d420e9 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 18 Oct 2023 05:03:30 -0300 Subject: [PATCH 05/39] Islandora IIIF: Update README. --- modules/islandora_iiif/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/islandora_iiif/README.md b/modules/islandora_iiif/README.md index c1f89872c..7cbcc8840 100644 --- a/modules/islandora_iiif/README.md +++ b/modules/islandora_iiif/README.md @@ -38,6 +38,21 @@ This module implements a Views Style plugin. It provides the following settings: 1. Tile Source: A field that was added to the views list of fields with the image to be served. This should be a File or Image type field on a Media. 2. Structured Text field: This lets you specify a file field where OCR text with positional data, e.g., hOCR can be found. + +### Media Attributes from IIIF Action + +The module also provides an action that lets a site owner populate a TIFF or JP2 image's width and +height attributes into fields so the IIIF server is not bogged down trying to generate a manifest if +it doesn't have them. + +To use it, either: + +- Add it as a derivative reaction to a node with an Original FIle as its child, or +- Use it as a batch action, such as on a Paged Content object's list of child pages. + +The action assumes the media type has fields with machine names of field_height and +field_width. Making this configurable would mean they would not appear +on entity list pages. ## Documentation Official documentation is available on the [Islandora 8 documentation site](https://islandora.github.io/documentation/). From 470a078aeac68c0abd619c8a8057120a18f0726a Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sat, 8 Jun 2024 20:16:33 -0300 Subject: [PATCH 06/39] Finish rebase. --- .../src/Plugin/views/style/IIIFManifest.php | 79 ++++++++++++------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index e93891413..c725eec95 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -11,8 +11,11 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\iiif_presentation_api\Encoder\V3\IiifP; use Drupal\islandora\IslandoraUtils; use Drupal\taxonomy\TermInterface; +use Drupal\islandora_iiif\IiiffInfo; +use Drupal\islandora_iiif\IiifInfo; use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\ResultRow; use GuzzleHttp\Client; @@ -68,6 +71,13 @@ class IIIFManifest extends StylePluginBase { */ protected $serializer; + /** + * The IIIF Info service. + * + * @var IiifInfo + */ + protected $iiifInfo; + /** * The request service. * @@ -134,7 +144,7 @@ class IIIFManifest extends StylePluginBase { /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger, ModuleHandlerInterface $moduleHandler, IslandoraUtils $utils) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger, ModuleHandlerInterface $moduleHandler, IslandoraUtils $utils, IiifInfo $iiif_info) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->serializer = $serializer; @@ -147,6 +157,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->utils = $utils; $this->moduleHandler = $moduleHandler; $this->utils = $utils; + $this->iiifInfo = $iiif_info; } /** @@ -165,7 +176,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('http_client'), $container->get('messenger'), $container->get('module_handler'), - $container->get('islandora.utils') + $container->get('islandora.utils'), + $container->get('islandora_iiif') ); } @@ -377,38 +389,49 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { - return [intval($image->width), intval($image->height)]; + return [intval($image->width), + intval($image->height)]; } - try { - $info_json = $this->httpClient->get($iiif_url)->getBody(); - $resource = json_decode($info_json, TRUE); - $width = $resource['width']; - $height = $resource['height']; + if ($properties = $image->getProperties() + && isset($properties['width']) && is_numeric($properties['width']) + && isset($properties['height']) && is_numeric($properties['width'])) { + return [intval($properties['width']), + intval($properties['height'])]; } - catch (ClientException | ServerException | ConnectException $e) { - // If we couldn't get the info.json from IIIF - // try seeing if we can get it from Drupal. - if (empty($width) || empty($height)) { - // Get the image properties so we know the image width/height. - $properties = $image->getProperties(); - $width = isset($properties['width']) ? $properties['width'] : 0; - $height = isset($properties['height']) ? $properties['height'] : 0; - - // If this is a TIFF AND we don't know the width/height - // see if we can get the image size via PHP's core function. - if ($mime_type === 'image/tiff' && (!$width || !$height)) { - $uri = $image->entity->getFileUri(); - $path = $this->fileSystem->realpath($uri); - $image_size = getimagesize($path); - if ($image_size) { - $width = $image_size[0]; - $height = $image_size[1]; - } + + $entity = $image->entity; + if ($entity->hasField('field_height') && !$entity->get('field_height')->isEmpty() + && $entity->get('field_height')->value > 0 + && $entity->hasField('field_width') + && !$entity->get('field_width')->isEmpty() + && $entity->get('field_width')->value > 0) { + return [ $entity->get('field_width')->value, + $entity->get('field_height')->value]; + } + + if ($mime_type === 'image/tiff') { + // If this is a TIFF AND we don't know the width/height + // see if we can get the image size via PHP's core function. + $uri = $image->entity->getFileUri(); + $path = $this->fileSystem->realpath($uri); + if (!empty($path)) { + $image_size = getimagesize($path); + if ($image_size) { + return [intval($image_size[0]), + intval($image_size[1])]; } } } - return [$width, $height]; + + // As a last resort, get it from the IIIF server. + // This can be very slow and will fail if there are too many pages. + $dimensions = $this->iiifInfo->getImageDimensions($image->entity); + if ($dimensions !== FALSE) { + return $dimensions; + } + + return [0, 0]; } /** From 27d77051a1045286044eee0201868db8de2e109c Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 18 Oct 2023 20:35:01 -0300 Subject: [PATCH 07/39] Islandora IIIF: Address PHPCS errors. --- modules/islandora_iiif/islandora_iiif.install | 8 ++- .../Plugin/Action/MediaAttributesFromIiif.php | 62 +++++++++---------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install index 9b0407351..6d79442c2 100644 --- a/modules/islandora_iiif/islandora_iiif.install +++ b/modules/islandora_iiif/islandora_iiif.install @@ -5,12 +5,14 @@ * Install/update hook implementations. */ - /** +use Symfony\Component\Yaml\Yaml; + +/** * Add Media Attributes from IIIF action. */ function islandora_iiif_update_92001(&$sandbox) { $config_id = 'system.action.media_attributes_from_iiif_action'; - $config_path = \Drupal::service('extension.list.module')->getPath('islandora_iiif') . '/config/optional/' . $config_id .'.yml'; - $data = \Symfony\Component\Yaml\Yaml::parseFile($config_path); + $config_path = \Drupal::service('extension.list.module')->getPath('islandora_iiif') . '/config/optional/' . $config_id . '.yml'; + $data = Yaml::parseFile($config_path); \Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE); } diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index 9bb951e23..2cb5c06ea 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -4,20 +4,15 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Action\Plugin\Action\SaveAction; -use Drupal\Core\Config\ImmutableConfig; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\file\FileInterface; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Session\AccountInterface; use Drupal\islandora\IslandoraUtils; use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\islandora_iiif\IiifInfo; -use Drupal\media\MediaInterface; -use Drupal\node\NodeInterface; use GuzzleHttp\Client; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; + /** * Provides an action that can save any entity. * @@ -30,18 +25,18 @@ class MediaAttributesFromIiif extends SaveAction { /** - * The HTTP client + * The HTTP client. * - * @var \GuzzleHttp\Client; + * @var \GuzzleHttp\Client */ protected $httpClient; -/** - * The IIIF Info service. - * - * @var IiifInfo - */ -protected $iiifInfo; + /** + * The IIIF Info service. + * + * @var \Drupal\islandora_iiif\IiifInfo + */ + protected $iiifInfo; /** * The logger. @@ -50,7 +45,7 @@ class MediaAttributesFromIiif extends SaveAction { */ protected $logger; - /** + /** * Islandora utility functions. * * @var \Drupal\islandora\IslandoraUtils @@ -76,14 +71,15 @@ class MediaAttributesFromIiif extends SaveAction { * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Component\Datetime\TimeInterface $time - * @param * The time service. * @param \Guzzle\Http\Client $http_client - * The HTTP Client. - * @param IiifInfo $iiif_info - * The IIIF INfo service. + * The HTTP Client. + * @param \Drupal\islandora_iiif\IiifInfo $iiif_info + * The IIIF INfo service. + * @param \Drupal\islandora\IslandoraUtils $islandora_utils + * Islandora utility functions. * @param \Drupal\islandora\MediaSource\MediaSourceService $media_source - * Media source service. + * Islandora media service. * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. */ @@ -91,10 +87,10 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $time); $this->httpClient = $http_client; -$this->iiifInfo = $iiif_info; -$this->utils = $islandora_utils; -$this->mediaSource = $media_source; -$this->logger = $channel; + $this->iiifInfo = $iiif_info; + $this->utils = $islandora_utils; + $this->mediaSource = $media_source; + $this->logger = $channel; } /** @@ -118,7 +114,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function execute($entity = NULL ) { + public function execute($entity = NULL) { $width = $height = FALSE; // Get the original File media use term. @@ -130,15 +126,16 @@ public function execute($entity = NULL ) { $original_file_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $original_file_term); if (!empty($original_file_mids)) { - // Ordinarily there shouldn't be more than one Original File media but it's not guaranteed. - foreach($original_file_mids as $original_file_mid) { + // Ordinarily there shouldn't be more than one Original File media but + // it's not guaranteed. + foreach ($original_file_mids as $original_file_mid) { - /* - * @var \Drupal\Media\MediaInterface $original_file_media - */ + /** + * @var \Drupal\Media\MediaInterface $original_file_media + */ $original_file_media = $this->entityTypeManager->getStorage('media')->load($original_file_mid); - // Get the media MIME Type + // Get the media MIME Type. $original_file = $this->mediaSource->getSourceFile($original_file_media); $mime_type = $original_file->getMimeType(); @@ -146,7 +143,6 @@ public function execute($entity = NULL ) { [$width, $height] = $this->iiifInfo->getImageDimensions($original_file); } - // @todo Make field configurable. Low priority since this whole thing is a workaround for an Islandora limitation. if ($original_file_media->hasField('field_width') && $original_file_media->hasField('field_height')) { $original_file_media->set('field_height', $height); @@ -166,4 +162,4 @@ public function access($object, AccountInterface $account = NULL, $return_as_obj return $object->access('update', $account, $return_as_object); } - } +} From 6e832b52816b488098e23fce0d770e3abd140422 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 18 Oct 2023 21:06:36 -0300 Subject: [PATCH 08/39] Islandora IIIF: Address PHPCS errors. --- modules/islandora_iiif/src/IiifInfo.php | 36 +++++++++---------- .../src/Plugin/views/style/IIIFManifest.php | 26 +++++++------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 7e1e74600..8cc66d4ef 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -25,24 +25,24 @@ class IiifInfo { /** - * The HTTP client + * The HTTP client. * - * @var \GuzzleHttp\Client; + * @var \GuzzleHttp\Client */ protected $httpClient; -/** + /** * This module's config. * * @var \Drupal\Core\Config\ImmutableConfig */ protected $iiifConfig; -/** - * JWT Auth provider service. - * - * @var \Drupal\jwt\Authentication\Provider\JwtAuth - */ + /** + * JWT Auth provider service. + * + * @var \Drupal\jwt\Authentication\Provider\JwtAuth + */ protected $jwtAuth; /** @@ -52,23 +52,22 @@ class IiifInfo { */ protected $logger; - /** * Constructs an IiifInfo object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. * @param \Guzzle\Http\Client $http_client - * The HTTP Client. + * The HTTP Client. * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. * @param \Drupal\jwt\Authentication\Provider\JwtAuth $jwt_auth - * The JWT auth provider. + * The JWT auth provider. */ public function __construct(ConfigFactoryInterface $config_factory, Client $http_client, LoggerChannelInterface $channel, JwtAuth $jwt_auth) { $this->configFactory = $config_factory; - $this->iiifConfig= $this->configFactory->get('islandora_iiif.settings'); + $this->iiifConfig = $this->configFactory->get('islandora_iiif.settings'); $this->httpClient = $http_client; $this->logger = $channel; $this->jwtAuth = $jwt_auth; @@ -76,10 +75,11 @@ public function __construct(ConfigFactoryInterface $config_factory, Client $http /** * The IIIF base URL for an image. + * * Visiting this URL will resolve to the info.json for the image. * * @return string - * The absolute URL on the IIIF server. + * The absolute URL on the IIIF server. */ public function baseUrl($image) { @@ -101,7 +101,8 @@ public function baseUrl($image) { * * @param \Drupal\File\FileInterface $file * The image file. - * @return array|FALSE + * + * @return array|false * The image dimensions in an array as [$width, $height] */ public function getImageDimensions(FileInterface $file) { @@ -109,9 +110,9 @@ public function getImageDimensions(FileInterface $file) { try { $info_json = $this->httpClient->request('get', $iiif_url, [ 'headers' => [ - 'Authorization' => 'bearer ' . $this->jwtAuth->generateToken() - ] - ])->getBody(); + 'Authorization' => 'bearer ' . $this->jwtAuth->generateToken(), + ], + ])->getBody(); $resource = json_decode($info_json, TRUE); $width = $resource['width']; $height = $resource['height']; @@ -125,5 +126,4 @@ public function getImageDimensions(FileInterface $file) { return FALSE; } - } diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index c725eec95..20ad4178e 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -6,12 +6,11 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; -use Drupal\iiif_presentation_api\Encoder\V3\IiifP; use Drupal\islandora\IslandoraUtils; use Drupal\taxonomy\TermInterface; use Drupal\islandora_iiif\IiiffInfo; @@ -19,12 +18,9 @@ use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\ResultRow; use GuzzleHttp\Client; -use GuzzleHttp\Exception\ClientException; -use GuzzleHttp\Exception\ConnectException; -use GuzzleHttp\Exception\ServerException; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Serializer\SerializerInterface; /** * Provide serializer format for IIIF Manifest. @@ -74,7 +70,7 @@ class IIIFManifest extends StylePluginBase { /** * The IIIF Info service. * - * @var IiifInfo + * @var \Drupal\islandora_iiif\IiifInfo */ protected $iiifInfo; @@ -390,14 +386,16 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { return [intval($image->width), - intval($image->height)]; + intval($image->height), + ]; } if ($properties = $image->getProperties() && isset($properties['width']) && is_numeric($properties['width']) && isset($properties['height']) && is_numeric($properties['width'])) { return [intval($properties['width']), - intval($properties['height'])]; + intval($properties['height']), + ]; } $entity = $image->entity; @@ -406,8 +404,9 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima && $entity->hasField('field_width') && !$entity->get('field_width')->isEmpty() && $entity->get('field_width')->value > 0) { - return [ $entity->get('field_width')->value, - $entity->get('field_height')->value]; + return [$entity->get('field_width')->value, + $entity->get('field_height')->value, + ]; } if ($mime_type === 'image/tiff') { @@ -419,7 +418,8 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima $image_size = getimagesize($path); if ($image_size) { return [intval($image_size[0]), - intval($image_size[1])]; + intval($image_size[1]), + ]; } } } @@ -441,8 +441,6 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * The entity at the current row. * * @return string|false - * The absolute URL of the current row's structured text, - * or FALSE if none. */ protected function getOcrUrl(EntityInterface $entity) { $ocr_url = FALSE; From f68a8e964aec3c6345c5fabb41ad99cb7baab3a0 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 13 Nov 2023 17:21:28 -0400 Subject: [PATCH 09/39] islandora_iiif: Fix Authorization header syntax. --- modules/islandora_iiif/src/IiifInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 8cc66d4ef..32ccedfab 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -110,7 +110,7 @@ public function getImageDimensions(FileInterface $file) { try { $info_json = $this->httpClient->request('get', $iiif_url, [ 'headers' => [ - 'Authorization' => 'bearer ' . $this->jwtAuth->generateToken(), + 'Authorization' => 'Bearer ' . $this->jwtAuth->generateToken(), ], ])->getBody(); $resource = json_decode($info_json, TRUE); From dd7360d8c674701e2cbe6844c0e9fa6b7c6b5f5f Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 20 Nov 2023 01:22:12 -0400 Subject: [PATCH 10/39] 959-iiif-width-height-caching Islandora IIIF: Add search endpoint config to manifest. --- .../config/schema/islandora_iiif.schema.yml | 10 ++++++++++ .../src/Plugin/views/style/IIIFManifest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 11fff4c71..4eea7362e 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -20,12 +20,22 @@ views.style.iiif_manifest: label: "Tile source field(s)" sequence: type: string +<<<<<<< HEAD iiif_ocr_file_field: type: sequence label: "Structured OCR data file field" sequence: type: string structured_text_term_uri: +======= + label: "Tile source field(s)" + iiif_ocr_file_field: + type: sequence + sequence: + type: string: + label: "IIIF hOCR file field" + structured_text_term: +>>>>>>> ec29a45b (959-iiif-width-height-caching Islandora IIIF: Add search endpoint config to manifest.) type: string label: "Structured text term" search_endpoint: diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 20ad4178e..1d2ffd5b5 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -501,6 +501,23 @@ public function getEntityTitle(string $content_path): string { return $entity_title; } + protected function addSearchEndpoint(array &$json, array $url_components) { + $url_base = $this->getRequest()->getSchemeAndHttpHost(); + $hocr_search_path = $this->options['search_endpoint']; + $hocr_search_url = $url_base . '/' . ltrim($hocr_search_path, '/'); + + $hocr_search_url = str_replace('%node', $url_components[1], $hocr_search_url); + + $json['service'][] = [ + "@context" => "http://iiif.io/api/search/0/context.json", + "@id" => $hocr_search_url, + "profile" => "http://iiif.io/api/search/0/search", + "label" => t("Search inside this work"), + ]; + + + } + /** * {@inheritdoc} */ From 5becfa51ebdc7fe3c1aaa6a912882f650291f2ea Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 6 Feb 2024 12:19:48 -0400 Subject: [PATCH 11/39] 959-iiif-width-height-caching Add handler for ServerException in Islandora IIIF manifest generator. --- modules/islandora_iiif/src/IiifInfo.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 32ccedfab..66d83fe35 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -10,6 +10,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\ServerException; /** * Get IIIF related info for a given File or Image entity. @@ -120,7 +121,7 @@ public function getImageDimensions(FileInterface $file) { return [intval($width), intval($height)]; } } - catch (ClientException | ConnectException $e) { + catch (ClientException | ConnectException | ServerException $e) { $this->logger->info("Error getting image file dimensions from IIIF server: " . $e->getMessage()); } return FALSE; From c86a454111d5e0240309b03bdb845f35d2d41b67 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 7 Feb 2024 16:09:19 -0400 Subject: [PATCH 12/39] 959-iiif-width-height-caching Add option to skip retrieveing TIFF and JP2 dimensions from IIIF server. --- .../config/schema/islandora_iiif.schema.yml | 3 +++ .../src/Plugin/views/style/IIIFManifest.php | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 4eea7362e..87f7976c8 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -38,6 +38,9 @@ views.style.iiif_manifest: >>>>>>> ec29a45b (959-iiif-width-height-caching Islandora IIIF: Add search endpoint config to manifest.) type: string label: "Structured text term" + getdimensions_from_sewrver: + type: boolean + label: "Retrieve image dimensions from IIIF server" search_endpoint: type: string label: "Search endpoint path" diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 1d2ffd5b5..8520fcf72 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -313,6 +313,9 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas $annotation_id = $iiif_base_id . '/annotation/' . $entity->id(); [$width, $height] = $this->getCanvasDimensions($iiif_url, $image, $mime_type); + if ($width == 0) { + continue; + } $tmp_canvas = [ // @see https://iiif.io/api/presentation/2.1/#canvas @@ -426,9 +429,11 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima // As a last resort, get it from the IIIF server. // This can be very slow and will fail if there are too many pages. - $dimensions = $this->iiifInfo->getImageDimensions($image->entity); - if ($dimensions !== FALSE) { - return $dimensions; + if ($this->options['get_dimensions_from_server']) { + $dimensions = $this->iiifInfo->getImageDimensions($image->entity); + if ($dimensions !== FALSE) { + return $dimensions; + } } return [0, 0]; @@ -623,6 +628,13 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#description' => $this->t('Term indicating the media that holds structured text, such as hOCR, for the given object. Use this if the text is on a separate media from the tile source.'), ]; + $form['get_dimensions_from_server'] = [ + '#type' => 'checkbox', + '#title' => $this->t("Retrieve image dimensions from IIIF server"), + '#description' => $this->t("For TIFFs and JP2s, if the media doesn't have width and height values populated, as a last resort, retrieve the info from the IIIF server. This can be very slow and is not recommended."), + '#default_value' => $this->options['get_dimensions_from_server'], + ]; + $form['search_endpoint'] = [ '#type' => 'textfield', '#title' => $this->t("Search endpoint path."), From 8dd22c0e31654bdc448c8eda7212f8cbd03bb9f1 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Fri, 9 Feb 2024 11:45:07 -0400 Subject: [PATCH 13/39] Checking for width/height on media first when generating IIIF manifest --- .../src/Plugin/views/style/IIIFManifest.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 8520fcf72..35c379b66 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -11,6 +11,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\media\Entity\Media; use Drupal\islandora\IslandoraUtils; use Drupal\taxonomy\TermInterface; use Drupal\islandora_iiif\IiiffInfo; @@ -312,7 +313,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas $canvas_id = $iiif_base_id . '/canvas/' . $entity->id(); $annotation_id = $iiif_base_id . '/annotation/' . $entity->id(); - [$width, $height] = $this->getCanvasDimensions($iiif_url, $image, $mime_type); + [$width, $height] = $this->getCanvasDimensions($iiif_url, $entity, $image, $mime_type); if ($width == 0) { continue; } @@ -384,8 +385,21 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas * @return [string] * The width and height of the image. */ - protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $image, string $mime_type) { + protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItemInterface $image, string $mime_type) { + + // If the media has field_height and field_width, return those values. + if ($media->hasField('field_height') + && !$media->get('field_height')->isEmpty() + && $media->get('field_height')->value > 0 + && $media->hasField('field_width') + && !$media->get('field_width')->isEmpty() + && $media->get('field_width')->value > 0) { + return [intval($media->get('field_width')->value), + intval($media->get('field_height')->value), + ]; + } + // Otherwise start looking at the field/file level for the numbers. if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { return [intval($image->width), @@ -429,11 +443,9 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima // As a last resort, get it from the IIIF server. // This can be very slow and will fail if there are too many pages. - if ($this->options['get_dimensions_from_server']) { - $dimensions = $this->iiifInfo->getImageDimensions($image->entity); - if ($dimensions !== FALSE) { - return $dimensions; - } + $dimensions = $this->iiifInfo->getImageDimensions($image->entity); + if ($dimensions !== FALSE) { + return $dimensions; } return [0, 0]; @@ -628,13 +640,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#description' => $this->t('Term indicating the media that holds structured text, such as hOCR, for the given object. Use this if the text is on a separate media from the tile source.'), ]; - $form['get_dimensions_from_server'] = [ - '#type' => 'checkbox', - '#title' => $this->t("Retrieve image dimensions from IIIF server"), - '#description' => $this->t("For TIFFs and JP2s, if the media doesn't have width and height values populated, as a last resort, retrieve the info from the IIIF server. This can be very slow and is not recommended."), - '#default_value' => $this->options['get_dimensions_from_server'], - ]; - $form['search_endpoint'] = [ '#type' => 'textfield', '#title' => $this->t("Search endpoint path."), From 6b75a85a00f4853f83f1f1f97e2017aedb99751d Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 15 Feb 2024 09:06:04 -0400 Subject: [PATCH 14/39] 959-iiif-width-height-caching Add IIIF service function to get downlaod link with given dimensions. --- modules/islandora_iiif/src/IiifInfo.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 66d83fe35..c39a3cee5 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -98,7 +98,7 @@ public function baseUrl($image) { } /** - * Retrieve an image's dimensions via the IIIF server. + * Retrieve an image's original dimensions via the IIIF server. * * @param \Drupal\File\FileInterface $file * The image file. @@ -127,4 +127,27 @@ public function getImageDimensions(FileInterface $file) { return FALSE; } +/** + * The IIIF base URL for an image. + * + * Visiting this URL will resolve to the full image resized to the maximum dimensions given. + * + * @see https://iiif.io/api/image/2.1/ + * + * @param Drupal\file\FileInterface $image + * The image entity. + * @param int width + * The maximum width of the image to be returned. 0 for no constraint. + * @param int $height + * The maxim um height of the image to be returned. 0 for no contraint. + * + * @return string + * The IIIF URl to retrieve the full image with the given max dimensions. + */ + public function getImageWithMaxDimensions($image, $width = 0, $height = 0) { + $base_url = $this->baseUrl($image); + return $base_url . "/full/!$width,$height/0/default.jpg"; + + } + } From 885fc3482ddc3fdffc05c62e1408855b8c3eb841 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sat, 8 Jun 2024 20:27:18 -0300 Subject: [PATCH 15/39] Fix redundant function from rebase. --- .../src/Plugin/views/style/IIIFManifest.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 35c379b66..57426a5de 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -518,23 +518,6 @@ public function getEntityTitle(string $content_path): string { return $entity_title; } - protected function addSearchEndpoint(array &$json, array $url_components) { - $url_base = $this->getRequest()->getSchemeAndHttpHost(); - $hocr_search_path = $this->options['search_endpoint']; - $hocr_search_url = $url_base . '/' . ltrim($hocr_search_path, '/'); - - $hocr_search_url = str_replace('%node', $url_components[1], $hocr_search_url); - - $json['service'][] = [ - "@context" => "http://iiif.io/api/search/0/context.json", - "@id" => $hocr_search_url, - "profile" => "http://iiif.io/api/search/0/search", - "label" => t("Search inside this work"), - ]; - - - } - /** * {@inheritdoc} */ From 16f53dc9ef5a3c735883061f90a7a7dec92456c3 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 9 Jun 2024 21:31:51 -0300 Subject: [PATCH 16/39] Rebase resolve conflict. --- .../config/schema/islandora_iiif.schema.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 87f7976c8..54f4ccf25 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -20,22 +20,12 @@ views.style.iiif_manifest: label: "Tile source field(s)" sequence: type: string -<<<<<<< HEAD iiif_ocr_file_field: type: sequence label: "Structured OCR data file field" sequence: type: string structured_text_term_uri: -======= - label: "Tile source field(s)" - iiif_ocr_file_field: - type: sequence - sequence: - type: string: - label: "IIIF hOCR file field" - structured_text_term: ->>>>>>> ec29a45b (959-iiif-width-height-caching Islandora IIIF: Add search endpoint config to manifest.) type: string label: "Structured text term" getdimensions_from_sewrver: From 5ed299fbfbb2cc87cd6a416927a66be82ad6297a Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 10 Jun 2024 03:58:52 -0300 Subject: [PATCH 17/39] WIP: Make Get dimensions from File image action target fields configurable. --- ...tion.media_attributes_from_iiif_action.yml | 2 +- .../Plugin/Action/MediaAttributesFromIiif.php | 90 +++++++++++++++++-- 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml index 36e7c8f9b..cdc00de9f 100644 --- a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml +++ b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml @@ -6,5 +6,5 @@ dependencies: id: media_attributes_from_iiif_action label: 'Media attributes from IIIF' type: node -plugin: islandora_iiif:media_attributes_from_iiif_action:media +plugin: islandora_iiif:media_attributes_from_iiif_action:node configuration: { } diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index 2cb5c06ea..a371f8bd1 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -3,9 +3,14 @@ namespace Drupal\islandora_iiif\Plugin\Action; use Drupal\Component\Datetime\TimeInterface; -use Drupal\Core\Action\Plugin\Action\SaveAction; +use Drupal\Core\Action\ConfigurableActionBase; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Field\FieldConfigInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\islandora\IslandoraUtils; use Drupal\islandora\MediaSource\MediaSourceService; @@ -17,12 +22,26 @@ * Provides an action that can save any entity. * * @Action( - * id = "islandora_iiif:media_attributes_from_iiif_action", - * action_label = @Translation("Add image dimensions retrieved from the IIIF server"), - * deriver = "Drupal\Core\Action\Plugin\Action\Derivative\EntityChangedActionDeriver", + * id = "media_attributes_from_iiif_action", + * label = @Translation("Add image dimensions retrieved from the IIIF server"), + * type = "node" * ) */ -class MediaAttributesFromIiif extends SaveAction { +class MediaAttributesFromIiif extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + + /** + * Entity Field Manager + * + * @var Drupal\Core\Entity\EntityFieldManagerInterface + */ +protected $entityFieldManager; + +/** + * Entity type Manager. + * + * @var Drupal\Core\Entity\EntityTypeManagerInterface + */ +protected $entityTypeManager; /** * The HTTP client. @@ -83,14 +102,16 @@ class MediaAttributesFromIiif extends SaveAction { * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, Client $http_client, IiifInfo $iiif_info, IslandoraUtils $islandora_utils, MediaSourceService $media_source, LoggerChannelInterface $channel) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, Client $http_client, IiifInfo $iiif_info, IslandoraUtils $islandora_utils, MediaSourceService $media_source, LoggerChannelInterface $channel, EntityFieldManagerInterface $entity_field_manager ) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $time); + $this->entityTypeManager = $entity_type_manager; $this->httpClient = $http_client; $this->iiifInfo = $iiif_info; $this->utils = $islandora_utils; $this->mediaSource = $media_source; $this->logger = $channel; + $this->entityFieldManager =$entity_field_manager; } /** @@ -107,7 +128,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('islandora_iiif'), $container->get('islandora.utils'), $container->get('islandora.media_source_service'), - $container->get('logger.channel.islandora') + $container->get('logger.channel.islandora'), + $container->get('entity_field.manager') ); } @@ -162,4 +184,58 @@ public function access($object, AccountInterface $account = NULL, $return_as_obj return $object->access('update', $account, $return_as_object); } + /** + * (@InheritDoc) + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { +$integer_fields = $this->getIntegerFields(); + + $form['width_field'] = [ + '#type' => 'select', + '#title' => $this->t('Width Field'), + '#description' => $this->t("Field to populate with an image's width."), + '#options' => $integer_fields, + ]; + return $form; + + $form['height_field'] = [ + '#type' => 'select', + '#title' => $this->t('Height Field'), + '#description' => $this->t("Field to populate with an image's height."), + '#options' => $integer_fields, + ]; + + + } + + /** + * (@InheritDoc) + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + + } + + protected function getIntegerFields() { + // get media types + $media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple(); + $all_integer_fields = []; + foreach(array_keys($media_types) as $key => $value) { + $fields = $this->entityFieldManager->getFieldDefinitions("media", $value); + + $integer_fields = array_filter( + $fields, + function ($field_value, $field_key) { + // only keep fields of type 'integer' + return (strpos($field_value->getType(), 'integer') > -1) + && is_a($field_value, '\Drupal\Core\Field\FieldConfigInterface') ; + }, ARRAY_FILTER_USE_BOTH + ); + foreach($integer_fields as $integer_field) { + $all_integer_fields[$integer_field->id()] = $integer_field->getTargetEntityTypeId() + . ' -- ' . $integer_field->getTargetBundle() . ' -- ' . $integer_field->getLabel(); + } + + } + return $all_integer_fields; + } } From f0d37c89ec86addebd5cdce02a79d045b77399b9 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 11 Jun 2024 08:51:10 -0300 Subject: [PATCH 18/39] WIP --- .../Plugin/Action/MediaAttributesFromIiif.php | 78 ++++++++++++++----- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index a371f8bd1..4ba939c40 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -4,6 +4,7 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Action\ConfigurableActionBase; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -29,6 +30,13 @@ */ class MediaAttributesFromIiif extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + /** + * Config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * Entity Field Manager * @@ -101,10 +109,13 @@ class MediaAttributesFromIiif extends ConfigurableActionBase implements Containe * Islandora media service. * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. + * @param Drupal\Core\Config\ConfigFactoryInterface $config_factory + * Config factory. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, Client $http_client, IiifInfo $iiif_info, IslandoraUtils $islandora_utils, MediaSourceService $media_source, LoggerChannelInterface $channel, EntityFieldManagerInterface $entity_field_manager ) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, Client $http_client, IiifInfo $iiif_info, IslandoraUtils $islandora_utils, MediaSourceService $media_source, LoggerChannelInterface $channel, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $time); + $this->configFactory = $config_factory; $this->entityTypeManager = $entity_type_manager; $this->httpClient = $http_client; $this->iiifInfo = $iiif_info; @@ -129,7 +140,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('islandora.utils'), $container->get('islandora.media_source_service'), $container->get('logger.channel.islandora'), - $container->get('entity_field.manager') + $container->get('entity_field.manager'), + $container->get('config.factory') ); } @@ -140,29 +152,26 @@ public function execute($entity = NULL) { $width = $height = FALSE; // Get the original File media use term. - $original_file_term = $this->utils->getTermForUri('http://pcdm.org/use#OriginalFile'); - /** - * @var \Drupal\media\MediaInterface $original_file_media - */ - $original_file_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $original_file_term); - if (!empty($original_file_mids)) { + $source_term = $this->utils->getTermForUri($this->configuration['source_term_uri']); + + $source_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $source_term); + if (!empty($source_mids)) { + - // Ordinarily there shouldn't be more than one Original File media but - // it's not guaranteed. - foreach ($original_file_mids as $original_file_mid) { + foreach ($source_mids as $source_mid) { /** - * @var \Drupal\Media\MediaInterface $original_file_media + * @var \Drupal\Media\MediaInterface */ - $original_file_media = $this->entityTypeManager->getStorage('media')->load($original_file_mid); + $source_media = $this->entityTypeManager->getStorage('media')->load($source_mid); // Get the media MIME Type. - $original_file = $this->mediaSource->getSourceFile($original_file_media); - $mime_type = $original_file->getMimeType(); + $source_file = $this->mediaSource->getSourceFile($source_media); + $mime_type = $source_file->getMimeType(); if (in_array($mime_type, ['image/tiff', 'image/jp2'])) { - [$width, $height] = $this->iiifInfo->getImageDimensions($original_file); + [$width, $height] = $this->iiifInfo->getImageDimensions($source_file); } // @todo Make field configurable. Low priority since this whole thing is a workaround for an Islandora limitation. @@ -175,6 +184,7 @@ public function execute($entity = NULL) { } } + /** * {@inheritdoc} */ @@ -188,30 +198,60 @@ public function access($object, AccountInterface $account = NULL, $return_as_obj * (@InheritDoc) */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { -$integer_fields = $this->getIntegerFields(); + + $integer_fields = $this->getIntegerFields(); + + $form['source_term'] = [ + '#type' => 'entity_autocomplete', + '#target_type' => 'taxonomy_term', + '#title' => $this->t('Source media use term'), + '#default_value' => $this->utils->getTermForUri($this->configuration['source_term_uri']), + '#required' => TRUE, + '#description' => $this->t('Term indicating the source media'), + ]; $form['width_field'] = [ '#type' => 'select', '#title' => $this->t('Width Field'), '#description' => $this->t("Field to populate with an image's width."), + '#default_value' => $this->configuration['width_field'], '#options' => $integer_fields, ]; - return $form; $form['height_field'] = [ '#type' => 'select', '#title' => $this->t('Height Field'), '#description' => $this->t("Field to populate with an image's height."), + '#default_value' => $this->configuration['height_field'], '#options' => $integer_fields, ]; + return $form; + } + +/** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $config = parent::defaultConfiguration(); + + $config['media_use_term'] = ''; + $config['width_field'] = ''; + $config['height_field'] = ''; + return $config; } /** - * (@InheritDoc) + * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $tid = $form_state->getValue('source_term'); + $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); + $this->configuration['source_term_uri'] = $this->utils->getUriForTerm($term); + + $this->configuration['width_field'] = $form_state->getValue('width_field'); + $this->configuration['height_field'] = $form_state->getValue('height_field'); } From c99c77dc48360826cadaced286612be136efe2c0 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 12 Jun 2024 07:03:39 -0300 Subject: [PATCH 19/39] WIP use configured height and width fields. --- .../Plugin/Action/MediaAttributesFromIiif.php | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index 4ba939c40..49235841f 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -174,11 +174,12 @@ public function execute($entity = NULL) { [$width, $height] = $this->iiifInfo->getImageDimensions($source_file); } - // @todo Make field configurable. Low priority since this whole thing is a workaround for an Islandora limitation. - if ($original_file_media->hasField('field_width') && $original_file_media->hasField('field_height')) { - $original_file_media->set('field_height', $height); - $original_file_media->set('field_width', $width); - $original_file_media->save(); + $width_field = $this->getShortFieldName($this->configuration['width_field']); + $height_field = $this->getShortFieldName($this->configuration['height_field']); + if ($source_media->hasField($width_field) && $source_media->hasField($height_field)) { + $source_media->set('field_height', $height); + $source_media->set($width_field, $width); + $source_media->save(); } } } @@ -278,4 +279,17 @@ function ($field_value, $field_key) { } return $all_integer_fields; } + + /** + * Retrusn teh last part of a qualified field anme. + * + * @param string $field_id + * The full field id, e.g., 'media.file.field_height'. + * @return string + * The short field name, e.g., 'field_height'. + */ + protected function getShortFieldName(string $field_id): string { + [$entity_type, $bundle, $field_name] = explode('.', $field_id); + return $field_name; + } From e465a752e99bf5b5bc016292f53f60b226b572b1 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 12 Jun 2024 20:25:45 -0300 Subject: [PATCH 20/39] Extract TIFF and JP2 dimensions action: Make width and height field configurable. --- .../src/Plugin/Action/MediaAttributesFromIiif.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index 49235841f..613447d78 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -177,8 +177,8 @@ public function execute($entity = NULL) { $width_field = $this->getShortFieldName($this->configuration['width_field']); $height_field = $this->getShortFieldName($this->configuration['height_field']); if ($source_media->hasField($width_field) && $source_media->hasField($height_field)) { - $source_media->set('field_height', $height); $source_media->set($width_field, $width); + $source_media->set($height_field, $height); $source_media->save(); } } @@ -291,5 +291,6 @@ function ($field_value, $field_key) { protected function getShortFieldName(string $field_id): string { [$entity_type, $bundle, $field_name] = explode('.', $field_id); return $field_name; + } } From 2a8976f30e2e603c1a26a85a2249f0b99432f56a Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 13 Jun 2024 06:37:06 -0300 Subject: [PATCH 21/39] Islandora IIIF: Make media height and width fields configurable. --- modules/islandora_iiif/src/IiifInfo.php | 5 +- .../src/Plugin/views/style/IIIFManifest.php | 70 ++++++++++++++----- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index c39a3cee5..a11a5e5a9 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -6,11 +6,12 @@ use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\file\FileInterface; use Drupal\jwt\Authentication\Provider\JwtAuth; - use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\ServerException; +use Symfony\Component\HttpKernel\Event\RequestEvent; /** * Get IIIF related info for a given File or Image entity. @@ -121,7 +122,7 @@ public function getImageDimensions(FileInterface $file) { return [intval($width), intval($height)]; } } - catch (ClientException | ConnectException | ServerException $e) { + catch (ClientException | ConnectException | RequestException | ServerException $e) { $this->logger->info("Error getting image file dimensions from IIIF server: " . $e->getMessage()); } return FALSE; diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 57426a5de..a63f64ae2 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -226,7 +226,7 @@ public function render() { /** * @var \Drupal\taxonomy\TermInterface|null */ - $structured_text_term = $this->utils->getTermForUri($this->options['structured_text_term_uri']); + $structured_text_term = !empty($this->options['structured_text_term_uri']) ? $this->utils->getTermForUri($this->options['structured_text_term_uri']) : FALSE; // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ @@ -387,19 +387,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas */ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItemInterface $image, string $mime_type) { - // If the media has field_height and field_width, return those values. - if ($media->hasField('field_height') - && !$media->get('field_height')->isEmpty() - && $media->get('field_height')->value > 0 - && $media->hasField('field_width') - && !$media->get('field_width')->isEmpty() - && $media->get('field_width')->value > 0) { - return [intval($media->get('field_width')->value), - intval($media->get('field_height')->value), - ]; - } - // Otherwise start looking at the field/file level for the numbers. if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { return [intval($image->width), @@ -426,6 +414,21 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem ]; } + // If the media has width and height fields, return those values. + $width_field = !empty($this->options['advanced']['custom_width_height']['width_field']) ? $this->options['advanced']['custom_width_height']['height_field'] : 'field_width'; + $height_field = !empty($this->options['advanced']['custom_width_height']['height_field']) ? $this->options['advanced']['custom_width_height']['height_field'] : 'field_height'; + if ($media->hasField($height_field) + && !$media->get($height_field)->isEmpty() + && $media->get($height_field)->value > 0 + && $media->hasField($width_field) + && !$media->get($width_field)->isEmpty() + && $media->get($width_field)->value > 0) { + return [intval($media->get($width_field)->value), + intval($media->get($height_field)->value), + ]; + } + + if ($mime_type === 'image/tiff') { // If this is a TIFF AND we don't know the width/height // see if we can get the image size via PHP's core function. @@ -461,7 +464,7 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem */ protected function getOcrUrl(EntityInterface $entity) { $ocr_url = FALSE; - $iiif_ocr_file_field = !empty($this->options['iiif_ocr_file_field']) ? array_filter(array_values($this->options['iiif_ocr_file_field'])) : []; + $iiif_ocr_file_field = !empty($this->options['advanced']['iiif_ocr_file_field']) ? array_filter(array_values($this->options['advanced']['iiif_ocr_file_field'])) : []; $ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL; if ($ocrField) { $ocr_entity = $entity; @@ -593,6 +596,8 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { You will need to add a field to this View'), 'error'); } + $dimensions_field_options = array_merge(['' => $this->t(' - None -- ')],array_combine(array_keys($fields), array_keys($fields))); + $form['iiif_tile_field'] = [ '#title' => $this->t('Tile source field(s)'), '#type' => 'checkboxes', @@ -605,10 +610,37 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#required' => count($field_options) > 0, ]; - $form['iiif_ocr_file_field'] = [ + $form['advanced'] = [ + '#type' => 'details', + '#title' => $this->t('Advanced'), + '#open' => FALSE, + ]; + + $form['advanced']['custom_width_height'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Custom width and height fields.'), + '#description' => $this->t('Use these if the media type of the image does not have built in Width and height fields, e.g., File. As a fallback, if the media has fields with the name "field_width" and "field_height" this formatter will try and get the width from that.'), + ]; + + $form['advanced']['custom_width_height']['height_field'] = [ + '#type' => 'select', + '#title' => $this->t('Custom Height field'), + '#default_value' => $this->options['advanced']['custom_width_height']['height_field'], + '#options' => $dimensions_field_options, + ]; + +$form['advanced']['custom_width_height']['width_field'] = [ + '#type' => 'select', + '#title' => $this->t('Custom width field'), + '#default_value' => $this->options['advanced']['custom_width_height']['width_field'], + '#options' => $dimensions_field_options, +]; + + + $form['advanced']['iiif_ocr_file_field'] = [ '#title' => $this->t('Structured OCR data file field'), '#type' => 'checkboxes', - '#default_value' => $this->options['iiif_ocr_file_field'], + '#default_value' => $this->options['advanced']['iiif_ocr_file_field'], '#description' => $this->t("If the hOCR is a field on the same entity as the image source field above, select it here. If it's found in a related entity via the term below, leave this blank."), '#options' => $field_options, '#required' => FALSE, @@ -627,7 +659,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#type' => 'textfield', '#title' => $this->t("Search endpoint path."), '#description' => $this->t("If there is a search endpoint to search within the book that returns IIIF annotations, put it here. Use %node substitution where needed.
E.g., paged-content-search/%node"), - '#default_value' => $this->options['search_endpoint'], + '#default_value' => !empty($this->options['search_endpoint']) ?$this->options['search_endpoint'] : '', '#required' => FALSE, ]; } @@ -659,7 +691,9 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) { $tid = $style_options['structured_text_term']; unset($style_options['structured_text_term']); $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); - $style_options['structured_text_term_uri'] = $this->utils->getUriForTerm($term); + if ($term) { + $style_options['structured_text_term_uri'] = $this->utils->getUriForTerm($term); + } $form_state->setValue('style_options', $style_options); parent::submitOptionsForm($form, $form_state); } From e5b2a1281735922333fc08ef036d77b898d4c2e6 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sat, 15 Jun 2024 22:24:39 -0300 Subject: [PATCH 22/39] Islandora IIIF: Make media width and height fields configurable in manifest plugin. --- .../src/Plugin/views/style/IIIFManifest.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index a63f64ae2..a1e31c068 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -314,6 +314,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas $annotation_id = $iiif_base_id . '/annotation/' . $entity->id(); [$width, $height] = $this->getCanvasDimensions($iiif_url, $entity, $image, $mime_type); + if ($width == 0) { continue; } @@ -387,7 +388,6 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas */ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItemInterface $image, string $mime_type) { - if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { return [intval($image->width), @@ -404,6 +404,7 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem } $entity = $image->entity; + if ($entity->hasField('field_height') && !$entity->get('field_height')->isEmpty() && $entity->get('field_height')->value > 0 && $entity->hasField('field_width') @@ -448,6 +449,7 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem // This can be very slow and will fail if there are too many pages. $dimensions = $this->iiifInfo->getImageDimensions($image->entity); if ($dimensions !== FALSE) { + $this->storeImageDimensions($media, $dimensions[0], $dimensions[1]); return $dimensions; } @@ -588,6 +590,10 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { (!empty($field->options['type']) && in_array($field->options['type'], $file_views_field_formatters))) { $field_options[$field_name] = $field->adminLabel(); } + else { + // Put it in the list of fields that could contain the custom width or height value. + $dimensions_field_options[$field_name] = $field->adminLabel(); + } } // If no fields to choose from, add an error message indicating such. @@ -596,7 +602,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { You will need to add a field to this View'), 'error'); } - $dimensions_field_options = array_merge(['' => $this->t(' - None -- ')],array_combine(array_keys($fields), array_keys($fields))); + $dimensions_field_options = array_merge(['' => $this->t(' - None -- ')],$dimensions_field_options); $form['iiif_tile_field'] = [ '#title' => $this->t('Tile source field(s)'), @@ -713,4 +719,23 @@ protected function getStructuredTextTerm() : ?TermInterface { return $this->structuredTextTerm; } + protected function storeImageDimensions(EntityInterface $entity, $width, $height) { + $height_field = !empty($this->options['advanced']['custom_width_height']['height_field']) ? $this->view->field[$this->options['advanced']['custom_width_height']['height_field']]->definition['field_name'] : 'field_height'; + $width_field = !empty($this->options['advanced']['custom_width_height']['width_field']) ? $this->view->field[$this->options['advanced']['custom_width_height']['width_field']]->definition['field_name'] : 'field_width'; + + $needs_save = FALSE; + if ($entity->hasField($height_field) && $entity->get($height_field)->getString() !== $height) { + $entity->set($height_field, $height); + $needs_save = TRUE; + } + + if ($entity->hasField($width_field) && $entity->get($width_field)->getString() !== $width) { + $entity->set($width_field, $width); + $needs_save = TRUE; + } + + if ($needs_save) { + $entity->save(); + } + } } From c3166090d38cbed5caafdb0f838894561d138b14 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 17 Jun 2024 04:48:23 -0300 Subject: [PATCH 23/39] Islandora IIIF: Add config schema update hook. --- modules/islandora_iiif/islandora_iiif.install | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install index 6d79442c2..0e3f68136 100644 --- a/modules/islandora_iiif/islandora_iiif.install +++ b/modules/islandora_iiif/islandora_iiif.install @@ -8,11 +8,37 @@ use Symfony\Component\Yaml\Yaml; /** - * Add Media Attributes from IIIF action. + * Update config schema. */ -function islandora_iiif_update_92001(&$sandbox) { - $config_id = 'system.action.media_attributes_from_iiif_action'; - $config_path = \Drupal::service('extension.list.module')->getPath('islandora_iiif') . '/config/optional/' . $config_id . '.yml'; - $data = Yaml::parseFile($config_path); - \Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE); -} +function islandora_iiif_update_92002(&$sandbox) { + + /** + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + $config_factory = \Drupal::service('config.factory'); + $all_configs = $config_factory->listAll(); + $views_configs = array_values(array_filter($all_configs, function($config) { + return str_starts_with($config, 'views.view.'); + })); + + foreach ($views_configs as $views_config_name) { + $needs_save = FALSE; + $view_config = $config_factory->getEditable($views_config_name); + $displays = $view_config->get('display'); + foreach ($displays as $display_name => $display) { + if ($display['display_plugin'] == 'rest_export' + && $display['display_options']['style']['type'] == 'iiif_manifest') { + + if (!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { + $display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; + unset($display['display_options']['style']['options']['iiif_ocr_file_field']); + $displays->set($display_name . '.display_options.style.options', $display['display_options']['style']['options']); + $needs_save = TRUE; + } + } + } + if ($needs_save) { + $view_config->save(); + } + } + } From 86586c6f14a9dbb4515ee2cd309828cfcc0c2691 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 18 Jun 2024 01:56:28 -0300 Subject: [PATCH 24/39] Islandora IIIF: Fix warning on Views display plugin page. --- modules/islandora_iiif/islandora_iiif.install | 13 ++++++------- .../src/Plugin/views/style/IIIFManifest.php | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install index 0e3f68136..f8e545d85 100644 --- a/modules/islandora_iiif/islandora_iiif.install +++ b/modules/islandora_iiif/islandora_iiif.install @@ -27,14 +27,13 @@ function islandora_iiif_update_92002(&$sandbox) { $displays = $view_config->get('display'); foreach ($displays as $display_name => $display) { if ($display['display_plugin'] == 'rest_export' - && $display['display_options']['style']['type'] == 'iiif_manifest') { + && $display['display_options']['style']['type'] == 'iiif_manifest' + &&!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { - if (!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { - $display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; - unset($display['display_options']['style']['options']['iiif_ocr_file_field']); - $displays->set($display_name . '.display_options.style.options', $display['display_options']['style']['options']); - $needs_save = TRUE; - } + $display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; + unset($display['display_options']['style']['options']['iiif_ocr_file_field']); + $view_config->set('display.' . $display_name . '.display_options.style.options', $display['display_options']['style']['options']); + $needs_save = TRUE; } } if ($needs_save) { diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index a1e31c068..a63533c1a 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -577,6 +577,8 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { // File formatters. 'file_default', 'file_url_plain', ]; + $dimensions_field_options = []; + /** @var \Drupal\views\Plugin\views\field\FieldPluginBase[] $fields */ foreach ($fields as $field_name => $field) { // If this is a known Islandora file/image field From 816b6b5d0f12b145f8156ebde75346ecb65ec5b4 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 24 Jun 2024 20:45:19 -0300 Subject: [PATCH 25/39] Remove outdated action. --- ...system.action.media_attributes_from_iiif_action.yml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml diff --git a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml deleted file mode 100644 index cdc00de9f..000000000 --- a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml +++ /dev/null @@ -1,10 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - islandora_iiif -id: media_attributes_from_iiif_action -label: 'Media attributes from IIIF' -type: node -plugin: islandora_iiif:media_attributes_from_iiif_action:node -configuration: { } From 01988df3737b3327ee355b7d0a7700426a439808 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 24 Jun 2024 20:56:20 -0300 Subject: [PATCH 26/39] Islandora IIIF: Update the README. --- modules/islandora_iiif/README.md | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/modules/islandora_iiif/README.md b/modules/islandora_iiif/README.md index 7cbcc8840..4b0262a2c 100644 --- a/modules/islandora_iiif/README.md +++ b/modules/islandora_iiif/README.md @@ -6,7 +6,7 @@ ## Introduction -Provides IIIF manifests using views. +Provides [IIIF manifests](https://iiif.io) using views. ## Requirements @@ -20,7 +20,7 @@ For a full digital repository solution, see our [installation documentation](htt To download/enable just this module, use the following from the command line: ```bash -$ composer require islandora/islandora +$ composer require drupal/islandora $ drush en islandora_core_feature $ drush mim islandora_tags $ drush en islandora_iiif @@ -38,6 +38,8 @@ This module implements a Views Style plugin. It provides the following settings: 1. Tile Source: A field that was added to the views list of fields with the image to be served. This should be a File or Image type field on a Media. 2. Structured Text field: This lets you specify a file field where OCR text with positional data, e.g., hOCR can be found. +3. Structured Text Term: If your Islandora Object has a separate media with hOCR, point to it with this setting. +4. File Width and Height fields: If you want to use TIFF or JP2 files directly, set the fields where the files' width and height can be found with these settings. ### Media Attributes from IIIF Action @@ -45,17 +47,20 @@ The module also provides an action that lets a site owner populate a TIFF or JP2 height attributes into fields so the IIIF server is not bogged down trying to generate a manifest if it doesn't have them. -To use it, either: +It is a complex action that must be configured. Go to +Admin -> Actions UI -> Actions, choose +"Add image dimensions retrieved from the IIIF server" from the Create Action drop-down +And on the next screen, choose the width +and height fields that the action should +populate. +To use it, either: - Add it as a derivative reaction to a node with an Original FIle as its child, or - Use it as a batch action, such as on a Paged Content object's list of child pages. -The action assumes the media type has fields with machine names of field_height and -field_width. Making this configurable would mean they would not appear -on entity list pages. ## Documentation -Official documentation is available on the [Islandora 8 documentation site](https://islandora.github.io/documentation/). +Official documentation is available on the [Islandora 2 documentation site](https://islandora.github.io/documentation/). ## Development @@ -63,8 +68,6 @@ If you would like to contribute, please get involved by attending our weekly [Te If you would like to contribute code to the project, you need to be covered by an Islandora Foundation [Contributor License Agreement](http://islandora.ca/sites/default/files/islandora_cla.pdf) or [Corporate Contributor License Agreement](http://islandora.ca/sites/default/files/islandora_ccla.pdf). Please see the [Contributors](http://islandora.ca/resources/contributors) pages on Islandora.ca for more information. -We recommend using the [islandora-playbook](https://github.com/Islandora-Devops/islandora-playbook) to get started. - ## License [GPLv2](http://www.gnu.org/licenses/gpl-2.0.txt) From 3f26b3ed361277eec5ab78d639f4b2a5bed5b7d9 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 24 Jun 2024 21:11:32 -0300 Subject: [PATCH 27/39] Islandora IIIF: Update PHPDoc description of IIIF Dimensions complex action. --- .../src/Plugin/Action/MediaAttributesFromIiif.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index 613447d78..ef42e90b3 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -20,7 +20,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides an action that can save any entity. + * Provides an action that can retrieve a large image file's + * dimensions from an IIIF server and save them to a media's fields. * * @Action( * id = "media_attributes_from_iiif_action", From ce763f38d81d0e71766a6b50f2b9e8c178df1eb0 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 24 Jun 2024 22:26:12 -0300 Subject: [PATCH 28/39] Islandora IIIF: Add instructions for setting up a context reaction for the IIIF Dimensions actions. --- modules/islandora_iiif/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/islandora_iiif/README.md b/modules/islandora_iiif/README.md index 4b0262a2c..676723e9e 100644 --- a/modules/islandora_iiif/README.md +++ b/modules/islandora_iiif/README.md @@ -58,6 +58,23 @@ To use it, either: - Add it as a derivative reaction to a node with an Original FIle as its child, or - Use it as a batch action, such as on a Paged Content object's list of child pages. +### Setting up the action as a Context Reaction + +These instructions assume a standard Islandora Starter Site, so if you have different field names or Contexts, adjust accordingly. + +1. Go to admin/config/system/actions +2. Choose "Add image dimensions retrieved from the IIIF server" from the Create Complex Action drop-down and then click Create. +3. Enter Original File for the Source Media Use term. +4. Choose media -- file -- Width and Media -- File -- Height for the corresponding configuration values and click Save. +5. Go to admin/structure/context, and click Duplicate oon the Page Derivatives row. +6. Name the new context something like "Retrieve Page Dimensionss". and edit it. +7. This is the tricky bit, delete 'Original File' from the 'Media has term with URI' field and replace it with Service File. The explanation for this is that to retrieve a file from the IIIF server, it must be part of an Islandora Media that has been fully created and saved and given a URL. This hasn't happened yet when Original File derivatives are being created, so we need to hang our action onto a derivative that is created after the original one. +8. Under Reactions, deselect the existing actions and select "Add i mage dimensions from IIIF sserver" and click Save and continue. +9. Go back to your Paged Content object and add another child with a File media, to which you should upload another TIFF or JP2 file. +10. Without going to the Original File Book Manifestt, make sure that a service file has been generated, then click Edit on the Original File media. +11. Ensure that the Width and Height fields are populated with thee correct values from the file. + + ## Documentation Official documentation is available on the [Islandora 2 documentation site](https://islandora.github.io/documentation/). From 923157befe3114c2f639b821a4d410480d038f12 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 26 Jun 2024 13:50:57 -0300 Subject: [PATCH 29/39] Update modules/islandora_iiif/config/schema/islandora_iiif.schema.yml Co-authored-by: Adam <607975+adam-vessey@users.noreply.github.com> --- modules/islandora_iiif/config/schema/islandora_iiif.schema.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 54f4ccf25..e9bf48ca8 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -28,7 +28,7 @@ views.style.iiif_manifest: structured_text_term_uri: type: string label: "Structured text term" - getdimensions_from_sewrver: + getdimensions_from_server: type: boolean label: "Retrieve image dimensions from IIIF server" search_endpoint: From 773eb742d2b900c27e3b7ff229300ace4f801944 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 20:33:37 -0300 Subject: [PATCH 30/39] Islandora IIIF: Address PHPCS errors. --- .../src/Plugin/views/style/IIIFManifest.php | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index a63533c1a..108d211f4 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -11,11 +11,10 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; -use Drupal\media\Entity\Media; use Drupal\islandora\IslandoraUtils; -use Drupal\taxonomy\TermInterface; -use Drupal\islandora_iiif\IiiffInfo; use Drupal\islandora_iiif\IiifInfo; +use Drupal\media\MediaInterface; +use Drupal\taxonomy\TermInterface; use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\ResultRow; use GuzzleHttp\Client; @@ -378,6 +377,8 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas * * @param string $iiif_url * Base URL of the canvas. + * @param \Drupal\media\MediaInterface $media + * The Media entity. * @param \Drupal\Core\Field\FieldItemInterface $image * The image field. * @param string $mime_type @@ -386,7 +387,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas * @return [string] * The width and height of the image. */ - protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItemInterface $image, string $mime_type) { + protected function getCanvasDimensions(string $iiif_url, MediaInterface $media, FieldItemInterface $image, string $mime_type) { if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { @@ -429,7 +430,6 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem ]; } - if ($mime_type === 'image/tiff') { // If this is a TIFF AND we don't know the width/height // see if we can get the image size via PHP's core function. @@ -463,6 +463,7 @@ protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItem * The entity at the current row. * * @return string|false + * The URL where the OCR text is found. */ protected function getOcrUrl(EntityInterface $entity) { $ocr_url = FALSE; @@ -604,7 +605,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { You will need to add a field to this View'), 'error'); } - $dimensions_field_options = array_merge(['' => $this->t(' - None -- ')],$dimensions_field_options); + $dimensions_field_options = array_merge(['' => ' - ' . $this->t('None') . ' -- '], $dimensions_field_options); $form['iiif_tile_field'] = [ '#title' => $this->t('Tile source field(s)'), @@ -637,13 +638,12 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#options' => $dimensions_field_options, ]; -$form['advanced']['custom_width_height']['width_field'] = [ - '#type' => 'select', - '#title' => $this->t('Custom width field'), - '#default_value' => $this->options['advanced']['custom_width_height']['width_field'], - '#options' => $dimensions_field_options, -]; - + $form['advanced']['custom_width_height']['width_field'] = [ + '#type' => 'select', + '#title' => $this->t('Custom width field'), + '#default_value' => $this->options['advanced']['custom_width_height']['width_field'], + '#options' => $dimensions_field_options, + ]; $form['advanced']['iiif_ocr_file_field'] = [ '#title' => $this->t('Structured OCR data file field'), @@ -667,7 +667,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#type' => 'textfield', '#title' => $this->t("Search endpoint path."), '#description' => $this->t("If there is a search endpoint to search within the book that returns IIIF annotations, put it here. Use %node substitution where needed.
E.g., paged-content-search/%node"), - '#default_value' => !empty($this->options['search_endpoint']) ?$this->options['search_endpoint'] : '', + '#default_value' => !empty($this->options['search_endpoint']) ? $this->options['search_endpoint'] : '', '#required' => FALSE, ]; } @@ -721,6 +721,16 @@ protected function getStructuredTextTerm() : ?TermInterface { return $this->structuredTextTerm; } + /** + * Store the image dimensions back onto the entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to store the dimensions on to. + * @param int $width + * The image's width. + * @param int $height + * The image's height. + */ protected function storeImageDimensions(EntityInterface $entity, $width, $height) { $height_field = !empty($this->options['advanced']['custom_width_height']['height_field']) ? $this->view->field[$this->options['advanced']['custom_width_height']['height_field']]->definition['field_name'] : 'field_height'; $width_field = !empty($this->options['advanced']['custom_width_height']['width_field']) ? $this->view->field[$this->options['advanced']['custom_width_height']['width_field']]->definition['field_name'] : 'field_width'; @@ -740,4 +750,5 @@ protected function storeImageDimensions(EntityInterface $entity, $width, $height $entity->save(); } } + } From d5f9261ab438c6bd617b8eb8c814da6bbb2c913a Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 20:42:35 -0300 Subject: [PATCH 31/39] Islandora IIIF: Remove unused schema entry. --- modules/islandora_iiif/config/schema/islandora_iiif.schema.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index e9bf48ca8..11fff4c71 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -28,9 +28,6 @@ views.style.iiif_manifest: structured_text_term_uri: type: string label: "Structured text term" - getdimensions_from_server: - type: boolean - label: "Retrieve image dimensions from IIIF server" search_endpoint: type: string label: "Search endpoint path" From facd6fbe74eddb5858cab7c74302459bc9c081b2 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 21:05:37 -0300 Subject: [PATCH 32/39] Islandora IIIF: Address PHPCS errors. --- modules/islandora_iiif/src/IiifInfo.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index a11a5e5a9..2a6c28189 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -11,7 +11,6 @@ use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\ServerException; -use Symfony\Component\HttpKernel\Event\RequestEvent; /** * Get IIIF related info for a given File or Image entity. @@ -128,16 +127,14 @@ public function getImageDimensions(FileInterface $file) { return FALSE; } -/** + /** * The IIIF base URL for an image. * * Visiting this URL will resolve to the full image resized to the maximum dimensions given. * - * @see https://iiif.io/api/image/2.1/ - * - * @param Drupal\file\FileInterface $image + * @param \Drupal\file\FileInterface $image * The image entity. - * @param int width + * @param int $width * The maximum width of the image to be returned. 0 for no constraint. * @param int $height * The maxim um height of the image to be returned. 0 for no contraint. @@ -145,7 +142,7 @@ public function getImageDimensions(FileInterface $file) { * @return string * The IIIF URl to retrieve the full image with the given max dimensions. */ - public function getImageWithMaxDimensions($image, $width = 0, $height = 0) { + public function getImageWithMaxDimensions(FileInterface $image, $width = 0, $height = 0) { $base_url = $this->baseUrl($image); return $base_url . "/full/!$width,$height/0/default.jpg"; From a3e76c750d2a47d28f0fb30ea094bfdc2ce6b90d Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 21:37:03 -0300 Subject: [PATCH 33/39] Islandora IIIF: Address PHPCS errors. --- .../Plugin/Action/MediaAttributesFromIiif.php | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index ef42e90b3..cdb67b1bd 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -6,9 +6,7 @@ use Drupal\Core\Action\ConfigurableActionBase; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; -use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -20,8 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides an action that can retrieve a large image file's - * dimensions from an IIIF server and save them to a media's fields. + * Provides an action that can retrieve a large image file's dimensions from an IIIF server and save them to a media's fields. * * @Action( * id = "media_attributes_from_iiif_action", @@ -34,23 +31,23 @@ class MediaAttributesFromIiif extends ConfigurableActionBase implements Containe /** * Config factory. * - * @var \Drupal\Core\Config\ConfigFactoryInterface + * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** - * Entity Field Manager + * Entity Field Manager. * * @var Drupal\Core\Entity\EntityFieldManagerInterface */ -protected $entityFieldManager; + protected $entityFieldManager; -/** - * Entity type Manager. - * - * @var Drupal\Core\Entity\EntityTypeManagerInterface - */ -protected $entityTypeManager; + /** + * Entity type Manager. + * + * @var Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; /** * The HTTP client. @@ -110,6 +107,8 @@ class MediaAttributesFromIiif extends ConfigurableActionBase implements Containe * Islandora media service. * @param \Drupal\Core\Logger\LoggerChannelInterface $channel * Logger channel. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * The entity field manager service. * @param Drupal\Core\Config\ConfigFactoryInterface $config_factory * Config factory. */ @@ -123,7 +122,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->utils = $islandora_utils; $this->mediaSource = $media_source; $this->logger = $channel; - $this->entityFieldManager =$entity_field_manager; + $this->entityFieldManager = $entity_field_manager; } /** @@ -131,19 +130,19 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity_type.manager'), - $container->get('datetime.time'), - $container->get('http_client'), - $container->get('islandora_iiif'), - $container->get('islandora.utils'), - $container->get('islandora.media_source_service'), - $container->get('logger.channel.islandora'), - $container->get('entity_field.manager'), - $container->get('config.factory') - ); + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('datetime.time'), + $container->get('http_client'), + $container->get('islandora_iiif'), + $container->get('islandora.utils'), + $container->get('islandora.media_source_service'), + $container->get('logger.channel.islandora'), + $container->get('entity_field.manager'), + $container->get('config.factory') + ); } /** @@ -153,13 +152,11 @@ public function execute($entity = NULL) { $width = $height = FALSE; // Get the original File media use term. - $source_term = $this->utils->getTermForUri($this->configuration['source_term_uri']); $source_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $source_term); if (!empty($source_mids)) { - foreach ($source_mids as $source_mid) { /** @@ -186,18 +183,19 @@ public function execute($entity = NULL) { } } - /** * {@inheritdoc} */ public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { - /** @var \Drupal\Core\Entity\EntityInterface $object */ + /** +* @var \Drupal\Core\Entity\EntityInterface $object +*/ return $object->access('update', $account, $return_as_object); } /** - * (@InheritDoc) + * {@inheritDoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { @@ -231,7 +229,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta return $form; } -/** + /** * {@inheritdoc} */ public function defaultConfiguration() { @@ -257,24 +255,30 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s } + /** + * Get all fields of an entity that are integer types. + * + * @return array + * The integer type fields. + */ protected function getIntegerFields() { - // get media types + // Get media types. $media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple(); $all_integer_fields = []; - foreach(array_keys($media_types) as $key => $value) { + foreach (array_keys($media_types) as $key => $value) { $fields = $this->entityFieldManager->getFieldDefinitions("media", $value); $integer_fields = array_filter( - $fields, - function ($field_value, $field_key) { - // only keep fields of type 'integer' - return (strpos($field_value->getType(), 'integer') > -1) - && is_a($field_value, '\Drupal\Core\Field\FieldConfigInterface') ; - }, ARRAY_FILTER_USE_BOTH - ); - foreach($integer_fields as $integer_field) { + $fields, + function ($field_value, $field_key) { + // Only keep fields of type 'integer'. + return (strpos($field_value->getType(), 'integer') > -1) + && is_a($field_value, '\Drupal\Core\Field\FieldConfigInterface'); + }, ARRAY_FILTER_USE_BOTH + ); + foreach ($integer_fields as $integer_field) { $all_integer_fields[$integer_field->id()] = $integer_field->getTargetEntityTypeId() - . ' -- ' . $integer_field->getTargetBundle() . ' -- ' . $integer_field->getLabel(); + . ' -- ' . $integer_field->getTargetBundle() . ' -- ' . $integer_field->getLabel(); } } @@ -282,16 +286,17 @@ function ($field_value, $field_key) { } /** - * Retrusn teh last part of a qualified field anme. + * Returns the last part of a qualified field name. * * @param string $field_id * The full field id, e.g., 'media.file.field_height'. + * * @return string * The short field name, e.g., 'field_height'. */ protected function getShortFieldName(string $field_id): string { - [$entity_type, $bundle, $field_name] = explode('.', $field_id); - return $field_name; + [$entity_type, $bundle, $field_name] = explode('.', $field_id); + return $field_name; } } From 923419a09e93b8b3d337b258e8b1c92d5b9a1cb0 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 21:43:46 -0300 Subject: [PATCH 34/39] Islandora IIIF: Fix PHPCS errors. --- modules/islandora_iiif/islandora_iiif.install | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install index f8e545d85..dc9af68bf 100644 --- a/modules/islandora_iiif/islandora_iiif.install +++ b/modules/islandora_iiif/islandora_iiif.install @@ -5,8 +5,6 @@ * Install/update hook implementations. */ -use Symfony\Component\Yaml\Yaml; - /** * Update config schema. */ @@ -15,29 +13,29 @@ function islandora_iiif_update_92002(&$sandbox) { /** * @var \Drupal\Core\Config\ConfigFactoryInterface */ - $config_factory = \Drupal::service('config.factory'); - $all_configs = $config_factory->listAll(); - $views_configs = array_values(array_filter($all_configs, function($config) { - return str_starts_with($config, 'views.view.'); - })); + $config_factory = \Drupal::service('config.factory'); + $all_configs = $config_factory->listAll(); + $views_configs = array_values(array_filter($all_configs, function ($config) { + return str_starts_with($config, 'views.view.'); + })); - foreach ($views_configs as $views_config_name) { - $needs_save = FALSE; - $view_config = $config_factory->getEditable($views_config_name); - $displays = $view_config->get('display'); - foreach ($displays as $display_name => $display) { - if ($display['display_plugin'] == 'rest_export' - && $display['display_options']['style']['type'] == 'iiif_manifest' - &&!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { + foreach ($views_configs as $views_config_name) { + $needs_save = FALSE; + $view_config = $config_factory->getEditable($views_config_name); + $displays = $view_config->get('display'); + foreach ($displays as $display_name => $display) { + if ($display['display_plugin'] == 'rest_export' + && $display['display_options']['style']['type'] == 'iiif_manifest' + &&!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { - $display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; - unset($display['display_options']['style']['options']['iiif_ocr_file_field']); - $view_config->set('display.' . $display_name . '.display_options.style.options', $display['display_options']['style']['options']); - $needs_save = TRUE; - } - } - if ($needs_save) { - $view_config->save(); + $display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; + unset($display['display_options']['style']['options']['iiif_ocr_file_field']); + $view_config->set('display.' . $display_name . '.display_options.style.options', $display['display_options']['style']['options']); + $needs_save = TRUE; } } + if ($needs_save) { + $view_config->save(); + } } +} From c91cd588191df0b39a3f58379fe1e038c4a80984 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Fri, 28 Jun 2024 22:02:24 -0300 Subject: [PATCH 35/39] Islandora IIIF: Fix PHPCS warnings. --- modules/islandora_iiif/src/IiifInfo.php | 2 +- .../src/Plugin/Action/MediaAttributesFromIiif.php | 2 +- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/islandora_iiif/src/IiifInfo.php b/modules/islandora_iiif/src/IiifInfo.php index 2a6c28189..f2b1138ad 100644 --- a/modules/islandora_iiif/src/IiifInfo.php +++ b/modules/islandora_iiif/src/IiifInfo.php @@ -130,7 +130,7 @@ public function getImageDimensions(FileInterface $file) { /** * The IIIF base URL for an image. * - * Visiting this URL will resolve to the full image resized to the maximum dimensions given. + * Visiting this URL resolves to the image resized to the maximum dimensions. * * @param \Drupal\file\FileInterface $image * The image entity. diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php index cdb67b1bd..cfe41f17c 100644 --- a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -18,7 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides an action that can retrieve a large image file's dimensions from an IIIF server and save them to a media's fields. + * Retrieve a large image file's dimensions and save them to a media's fields. * * @Action( * id = "media_attributes_from_iiif_action", diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 108d211f4..2f84e2fbb 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -594,7 +594,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $field_options[$field_name] = $field->adminLabel(); } else { - // Put it in the list of fields that could contain the custom width or height value. + // Put it in the list of fields that may contain the custom value. $dimensions_field_options[$field_name] = $field->adminLabel(); } } @@ -605,7 +605,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { You will need to add a field to this View'), 'error'); } - $dimensions_field_options = array_merge(['' => ' - ' . $this->t('None') . ' -- '], $dimensions_field_options); + $dimensions_field_options = array_merge(['' => ' - None -- '], $dimensions_field_options); $form['iiif_tile_field'] = [ '#title' => $this->t('Tile source field(s)'), From 7508ca7accdba7a4524892ea474e437b2f526d63 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 9 Jul 2024 21:26:14 -0300 Subject: [PATCH 36/39] Islandora IIIF: Address code review comment. --- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 2f84e2fbb..2c577f596 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -281,7 +281,7 @@ public function render() { * @return array * List of IIIF URLs to display in the Openseadragon viewer. */ - protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, $structured_text_term) { + protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id) { $canvases = []; foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) { $viewsField = $this->view->field[$iiif_tile_field]; From bdde016162974f3afa1ef828b3a7398809353039 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 10 Jul 2024 13:58:27 -0300 Subject: [PATCH 37/39] Update modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php Co-authored-by: Adam <607975+adam-vessey@users.noreply.github.com> --- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 2c577f596..79e7319b6 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -222,10 +222,6 @@ public function render() { $label = $this->t("IIIF Manifest"); } - /** - * @var \Drupal\taxonomy\TermInterface|null - */ - $structured_text_term = !empty($this->options['structured_text_term_uri']) ? $this->utils->getTermForUri($this->options['structured_text_term_uri']) : FALSE; // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ From e30bf3dc3da2ef400f1d59b79185f66458ebead1 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 10 Jul 2024 13:59:20 -0300 Subject: [PATCH 38/39] Update modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php Co-authored-by: Adam <607975+adam-vessey@users.noreply.github.com> --- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 79e7319b6..6e0393a3a 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -242,7 +242,7 @@ public function render() { // For each row in the View result. foreach ($this->view->result as $row) { // Add the IIIF URL to the image to print out as JSON. - $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id, $structured_text_term); + $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id); foreach ($canvases as $tile_source) { $json['sequences'][0]['canvases'][] = $tile_source; } From 240094e3d86d458e1249b3bb2a61ef4327e22822 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 11 Jul 2024 01:25:48 -0300 Subject: [PATCH 39/39] Islandora IIIF: Address PHPCS errors. --- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 6e0393a3a..526e34eb7 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -222,7 +222,6 @@ public function render() { $label = $this->t("IIIF Manifest"); } - // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ '@type' => 'sc:Manifest', @@ -271,8 +270,6 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". - * @param \Drupal\taxonomy\TermInterface|null $structured_text_term - * The term that structured text media references, if any. * * @return array * List of IIIF URLs to display in the Openseadragon viewer.