@ -6,20 +6,20 @@ use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora_iiif\IiifInfo;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ResultRow;
use GuzzleHttp\Client;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;
/**
/**
* Provide serializer format for IIIF Manifest.
* Provide serializer format for IIIF Manifest.
@ -35,6 +35,14 @@ use Symfony\Component\HttpFoundation\Request;
*/
*/
class IIIFManifest extends StylePluginBase {
class IIIFManifest extends StylePluginBase {
/**
* Islandora utility functions.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
@ -59,6 +67,13 @@ class IIIFManifest extends StylePluginBase {
*/
*/
protected $serializer;
protected $serializer;
/**
* The IIIF Info service.
*
* @var \Drupal\islandora_iiif\IiifInfo
*/
protected $iiifInfo;
/**
/**
* The request service.
* The request service.
*
*
@ -104,7 +119,7 @@ class IIIFManifest extends StylePluginBase {
/**
/**
* {@inheritdoc}
* {@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) {
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);
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->serializer = $serializer;
$this->serializer = $serializer;
@ -115,6 +130,8 @@ class IIIFManifest extends StylePluginBase {
$this->httpClient = $http_client;
$this->httpClient = $http_client;
$this->messenger = $messenger;
$this->messenger = $messenger;
$this->moduleHandler = $moduleHandler;
$this->moduleHandler = $moduleHandler;
$this->utils = $utils;
$this->iiifInfo = $iiif_info;
}
}
/**
/**
@ -132,7 +149,9 @@ class IIIFManifest extends StylePluginBase {
$container->get('file_system'),
$container->get('file_system'),
$container->get('http_client'),
$container->get('http_client'),
$container->get('messenger'),
$container->get('messenger'),
$container->get('module_handler')
$container->get('module_handler'),
$container->get('islandora.utils'),
$container->get('islandora_iiif')
);
);
}
}
@ -163,6 +182,11 @@ class IIIFManifest extends StylePluginBase {
$content_path = implode('/', $url_components);
$content_path = implode('/', $url_components);
$iiif_base_id = $request_host . '/' . $content_path;
$iiif_base_id = $request_host . '/' . $content_path;
/**
* @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
// @see https://iiif.io/api/presentation/2.1/#manifest
$json += [
$json += [
'@type' => 'sc:Manifest',
'@type' => 'sc:Manifest',
@ -182,7 +206,7 @@ class IIIFManifest extends StylePluginBase {
// For each row in the View result.
// For each row in the View result.
foreach ($this->view->result as $row) {
foreach ($this->view->result as $row) {
// Add the IIIF URL to the image to print out as JSON.
// 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) {
foreach ($canvases as $tile_source) {
$json['sequences'][0]['canvases'][] = $tile_source;
$json['sequences'][0]['canvases'][] = $tile_source;
}
}
@ -192,6 +216,9 @@ class IIIFManifest extends StylePluginBase {
$content_type = 'json';
$content_type = 'json';
// Add a search endpoint if one is defined
$this->addSearchEndpoint($json, $url_components);
// Give other modules a chance to alter the manifest.
// Give other modules a chance to alter the manifest.
$this->moduleHandler->alter('islandora_iiif_manifest', $json, $this);
$this->moduleHandler->alter('islandora_iiif_manifest', $json, $this);
@ -208,11 +235,13 @@ class IIIFManifest extends StylePluginBase {
* @param string $iiif_base_id
* @param string $iiif_base_id
* The URL for the request, minus the last part of the URL,
* The URL for the request, minus the last part of the URL,
* which is likely "manifest".
* which is likely "manifest".
* @param \Drupal\taxonomy\TermInterface|null $structured_text_term
* The term that structured text media references, if any.
*
*
* @return array
* @return array
* List of IIIF URLs to display in the Openseadragon viewer.
* 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 = [];
$canvases = [];
foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) {
foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) {
$viewsField = $this->view->field[$iiif_tile_field];
$viewsField = $this->view->field[$iiif_tile_field];
@ -243,7 +272,10 @@ class IIIFManifest extends StylePluginBase {
$canvas_id = $iiif_base_id . '/canvas/' . $entity->id();
$canvas_id = $iiif_base_id . '/canvas/' . $entity->id();
$annotation_id = $iiif_base_id . '/annotation/' . $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;
}
$tmp_canvas = [
$tmp_canvas = [
// @see https://iiif.io/api/presentation/2.1/#canvas
// @see https://iiif.io/api/presentation/2.1/#canvas
@ -275,7 +307,7 @@ class IIIFManifest extends StylePluginBase {
],
],
];
];
if ($ocr_url = $this->getOcrUrl($entity, $row, $i )) {
if ($ocr_url = $this->getOcrUrl($entity, $structured_text_term )) {
$tmp_canvas['seeAlso'] = [
$tmp_canvas['seeAlso'] = [
'@id' => $ocr_url,
'@id' => $ocr_url,
'format' => 'text/vnd.hocr+html',
'format' => 'text/vnd.hocr+html',
@ -312,42 +344,70 @@ class IIIFManifest extends StylePluginBase {
* @return [string]
* @return [string]
* The width and height of the image.
* 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)
if (isset($image->width) & & is_numeric($image->width)
& & isset($image->height) & & is_numeric($image->height)) {
& & isset($image->height) & & is_numeric($image->height)) {
return [intval($image->width), intval($image->height)];
return [intval($image->width),
intval($image->height),
];
}
}
try {
if ($properties = $image->getProperties()
$info_json = $this->httpClient->get($iiif_url)->getBody();
& & isset($properties['width']) & & is_numeric($properties['width'])
$resource = json_decode($info_json, TRUE);
& & isset($properties['height']) & & is_numeric($properties['width'])) {
$width = $resource['width'];
return [intval($properties['width']),
$height = $resource['height'];
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.
$entity = $image->entity;
if (empty($width) || empty($height)) {
if ($entity->hasField('field_height') & & !$entity->get('field_height')->isEmpty()
// Get the image properties so we know the image width/height.
& & $entity->get('field_height')->value > 0
$properties = $image->getProperties();
& & $entity->hasField('field_width')
$width = isset($properties['width']) ? $properties['width'] : 0;
& & !$entity->get('field_width')->isEmpty()
$height = isset($properties['height']) ? $properties['height'] : 0;
& & $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
// 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.
// see if we can get the image size via PHP's core function.
if ($mime_type === 'image/tiff' & & !$width || !$height) {
$uri = $image->entity->getFileUri();
$uri = $image->entity->getFileUri();
$path = $this->fileSystem->realpath($uri);
$path = $this->fileSystem->realpath($uri);
if (!empty($path)) {
$image_size = getimagesize($path);
$image_size = getimagesize($path);
if ($image_size) {
if ($image_size) {
$width = $image_size[0];
return [intval($image_size[0]),
$height = $image_size[1];
intval($image_size[1]),
];
}
}
}
}
}
}
// 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 [$width, $height];
return [0, 0];
}
}
/**
/**
@ -355,29 +415,37 @@ class IIIFManifest extends StylePluginBase {
*
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity at the current row.
* The entity at the current row.
* @param \Drupal\views\ResultRow $row
* @param \Drupal\taxonomy\TermInterface|null $structured_text_term
* Result row.
* The term that structured text media references, if any.
* @param int $delta
* The delta in case there are multiple canvases on one media.
*
*
* @return string|false
* return String|FALSE
* The absolute URL of the current row's structured text,
* The absolute URL of the current row's structured text,
* or FALSE if none.
* or FALSE if none.
*/
*/
protected function getOcrUrl(EntityInterface $entity, ResultRow $row, $delta ) {
protected function getOcrUrl(EntityInterface $entity, $structured_text_term ) {
$ocr_url = FALSE;
$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['iiif_ocr_file_field']) ? array_filter(array_values($this->options['iiif_ocr_file_field'])) : [];
$ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL;
$ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL;
if ($ocrField) {
if ($ocrField) {
$ocr_entity = $ocrFi eld->getE ntity($row) ;
$ocr_entity = $entity;
$ocr_field_name = $ocrField->definition['field_name'];
$ocr_field_name = $ocrField->definition['field_name'];
if (!is_null($ocr_field_name)) {
if (!is_null($ocr_field_name)) {
$ocrs = $ocr_entity->{$ocr_field_name};
$ocrs = $ocr_entity->{$ocr_field_name};
$ocr = isset($ocrs[$delta]) ? $ocrs[$delta] : FALSE;
$ocr = $ocrs[0] ?? FALSE;
if ($ocr) {
$ocr_url = $ocr->entity->createFileUrl(FALSE);
$ocr_url = $ocr->entity->createFileUrl(FALSE);
}
}
}
}
elseif ($structured_text_term) {
$parent_node = $this->utils->getParentNode($entity);
$ocr_entity_array = $this->utils->getMediaReferencingNodeAndTerm($parent_node, $structured_text_term);
$ocr_entity_id = is_array($ocr_entity_array) ? array_shift($ocr_entity_array) : NULL;
$ocr_entity = $ocr_entity_id ? $this->entityTypeManager->getStorage('media')->load($ocr_entity_id) : NULL;
if ($ocr_entity) {
$ocr_file_source = $ocr_entity->getSource();
$ocr_fid = $ocr_file_source->getSourceFieldValue($ocr_entity);
$ocr_file = $this->entityTypeManager->getStorage('file')->load($ocr_fid);
$ocr_url = $ocr_file->createFileUrl(FALSE);
}
}
}
return $ocr_url;
return $ocr_url;
@ -411,6 +479,23 @@ class IIIFManifest extends StylePluginBase {
return $entity_title;
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}
* {@inheritdoc}
*/
*/
@ -479,10 +564,26 @@ class IIIFManifest extends StylePluginBase {
'#title' => $this->t('Structured OCR data file field'),
'#title' => $this->t('Structured OCR data file field'),
'#type' => 'checkboxes',
'#type' => 'checkboxes',
'#default_value' => $this->options['iiif_ocr_file_field'],
'#default_value' => $this->options['iiif_ocr_file_field'],
'#description' => $this->t('The source of structured OCR text for each entity.'),
'#description' => $this->t('The source of structured OCR text for each entity. If the term setting below is left blank, it will be the same entity as the source image '),
'#options' => $field_options,
'#options' => $field_options,
'#required' => FALSE,
'#required' => FALSE,
];
];
$form['structured_text_term'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => $this->t('Structured OCR text term'),
'#default_value' => $this->utils->getTermForUri($this->options['structured_text_term_uri']),
'#required' => FALSE,
'#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['search_endpoint'] = [
'#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 substitutions %node and %keywords.< br > E.g., paged-content-search/%node?search-in-pages=%keywords"),
'#default_value' => $this->options['search_endpoint'],
'#required' => FALSE,
];
}
}
/**
/**
@ -495,4 +596,25 @@ class IIIFManifest extends StylePluginBase {
return ['json' => 'json'];
return ['json' => 'json'];
}
}
/**
* Submit handler for options form.
*
* Used to store the structured text media term by URL instead of Ttid.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
// @codingStandardsIgnoreStart
public function submitOptionsForm(& $form, FormStateInterface $form_state) {
// @codingStandardsIgnoreEnd
$style_options = $form_state->getValue('style_options');
$tid = $style_options['structured_text_term'];
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
$style_options['structured_text_term_uri'] = $this->utils->getUriForTerm($term);
$form_state->setValue('style_options', $style_options);
parent::submitOptionsForm($form, $form_state);
}
}
}