Skip to content

Commit

Permalink
Use subrequest, clean some variables
Browse files Browse the repository at this point in the history
This is hard. But without creating a new cache so far my best choice.
Previously this was generating an infinite loop bc of accumulated requests. So far this works better but i have to double double check
  • Loading branch information
DiegoPino committed Aug 5, 2024
1 parent b7ec5bf commit cc9db93
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions src/Controller/IiifContentSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;


/**
Expand Down Expand Up @@ -190,7 +191,6 @@ function () use ($metadataexposeconfig_entity, $node) {
);

/* Coool beans, in a good way */

$canonical_url = $cacheabledata;
if ($canonical_url) {
$format = pathinfo($canonical_url, PATHINFO_BASENAME);
Expand All @@ -199,7 +199,7 @@ function () use ($metadataexposeconfig_entity, $node) {

$original_request = $this->requestStack->pop();
$subrequest = $original_request->duplicate(
NULL, NULL, NULL, NULL, NULL,
[], [], NULL, NULL, [],
$server_arguments
);
$exposed_metadata_route = $this->routeProvider->getRouteByName(
Expand All @@ -223,36 +223,38 @@ function () use ($metadataexposeconfig_entity, $node) {
/** @var $raw_inputbag \Symfony\Component\HttpFoundation\InputBag */
$raw_inputbag = $subrequest->attributes->all()['_raw_variables'];
$raw_inputbag->add(['format' => $format]);
$raw_inputbag->remove('page');
$raw_inputbag->remove('version');
$raw_inputbag->remove('mode');
$subrequest->attributes->set('_raw_variables', $raw_inputbag);
$subrequest->attributes->set('_controller', '\Drupal\format_strawberryfield\Controller\MetadataExposeDisplayController::castViaTwig');


// This is quite a trick. basically we get the current HTTP KERNEL
// And invoque a call directly. This has the benefit of using the whole caching mechanic
// The controller trick was nice. But not as nice as this.
/* @TODO Inject the http kernel service */
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
$kernel = \Drupal::getContainer()->get('http_kernel');
$response = $kernel->handle($subrequest);

/* This call is right but will never ever be cached. But i can cache at least the result of the processing */
$response = $kernel->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
/* This call is right was never ever being cached. So keeping as a comment.*/
/* @var $controller \Drupal\format_strawberryfield\Controller\MetadataExposeDisplayController */
/*
$controller = $this->classResolver->getInstanceFromDefinition(
'\Drupal\format_strawberryfield\Controller\MetadataExposeDisplayController'
);
$response = $controller->castViaTwig(
$node, $metadataexposeconfig_entity, $format
);
Restore the original request. We need it to return the right response for this search.
$this->requestStack->pop();
*/
// Restore the original request. We need it to return the right response for this search.
//$this->requestStack->pop();

$this->requestStack->push($original_request);

if ($response->isSuccessful()) {
$json_string = $response->getContent() ?? '{}';

$jsonArray = json_decode($json_string, TRUE);

if (json_last_error() == JSON_ERROR_NONE) {
if ($this->iiifConfig->get('iiif_content_search_validate_exposed')) {
$valid = FALSE;
Expand Down Expand Up @@ -356,16 +358,6 @@ function () use ($metadataexposeconfig_entity, $node) {
}
}

/* Expected structure independent if V2 or V3.
result = {array[345]}
0 = {array[3]}
width = {int} 464
height = {int} 782
img_canvas_pairs = {array[1]}
0 = {array[2]}
0 = "http://localhost:8183/iiif/2/bf0%2Fapplication-87758-0ad78298-d921-4f87-b0d8-104c1caf6cb1.pdf;1/full/full/0/default.jpg"
1 = "http://localhost:8001/do/975c85ef-4eb2-4e37-a044-078207a8e0dd/iiif/0ad78298-d921-4f87-b0d8-104c1caf6cb1/canvas/p1"
*/
$entries = [];
$paging_structure = [];
$uuid_uri_field = 'file_uuid';
Expand Down Expand Up @@ -416,7 +408,7 @@ function () use ($metadataexposeconfig_entity, $node) {
if ($version == "v1") {
$entries[] = [
"@id" => $current_url_clean
. "/annotation/anno-result/$i",
. "/{$page}/annotation/anno-result/$i",
"@type" => "oa:Annotation",
"motivation" => "painting",
"resource" => [
Expand All @@ -428,7 +420,7 @@ function () use ($metadataexposeconfig_entity, $node) {
} elseif ($version == "v2") {
$entries[] = [
"id" => $current_url_clean
. "/annotation/anno-result/$i",
. "/{$page}/annotation/anno-result/$i",
"type" => "Annotation",
"motivation" => "painting",
"body" => [
Expand Down Expand Up @@ -475,7 +467,7 @@ function () use ($metadataexposeconfig_entity, $node) {
if ($version == "v1") {
$entries[] = [
"@id" => $current_url_clean
. "/annotation/anno-result-time/$i",
. "/{$page}/annotation/anno-result-time/$i",
"@type" => "oa:Annotation",
"motivation" => $target_annotation ? "supplementing" : "painting",
"resource" => [
Expand All @@ -487,7 +479,7 @@ function () use ($metadataexposeconfig_entity, $node) {
} elseif ($version == "v2") {
$entries[] = [
"id" => $current_url_clean
. "/annotation/anno-result-time/$i",
. "/{$page}/annotation/anno-result-time/$i",
"type" => "Annotation",
"motivation" => $target_annotation ? "supplementing" : "painting",
"body" => [
Expand Down Expand Up @@ -539,7 +531,7 @@ function () use ($metadataexposeconfig_entity, $node) {
if ($version == "v1") {
$entries[] = [
"@id" => $current_url_clean
. "/annotation/anno-result-text/$i",
. "/{$page}/annotation/anno-result-text/$i",
"@type" => "oa:Annotation",
"motivation" => $target_annotation ? "supplementing" : "painting",
"resource" => [
Expand All @@ -551,7 +543,7 @@ function () use ($metadataexposeconfig_entity, $node) {
} elseif ($version == "v2") {
$entries[] = [
"id" => $current_url_clean
. "/annotation/anno-result-text/$i",
. "/{$page}/annotation/anno-result-text/$i",
"type" => "Annotation",
"motivation" => $target_annotation ? "supplementing" : "painting",
"body" => [
Expand All @@ -565,14 +557,15 @@ function () use ($metadataexposeconfig_entity, $node) {
}
}
}

}
}

if (count($entries) == 0) {
$results['total'] = 0;
$total = 0;
}
else {
$total = ($results['total'] ?? 0) + ($results_time['total'] ?? 0) + ($results_text['total'] ?? 0);
}
$total = ($results['total'] ?? 0) + ($results_time['total'] ?? 0) + ($results_text['total'] ?? 0);

if ($total > $this->iiifConfig->get('iiif_content_search_api_results_per_page')) {
$max_page = ceil($total/$this->iiifConfig->get('iiif_content_search_api_results_per_page')) - 1;
Expand All @@ -581,12 +574,12 @@ function () use ($metadataexposeconfig_entity, $node) {
"within" => [
"@type" => "sc:Layer",
"total" => $total,
"first" => $current_url_clean_no_page.'/0?='.urlencode($the_query_string),
"last" => $current_url_clean_no_page.'/'.$max_page .'?='.urlencode($the_query_string),
"first" => $current_url_clean_no_page.'/0?q='.urlencode($the_query_string),
"last" => $current_url_clean_no_page.'/'.$max_page .'?q='.urlencode($the_query_string),
]
];
if ($total > (($page+1) * $this->iiifConfig->get('iiif_content_search_api_results_per_page'))) {
$paging_structure["next"] = $current_url_clean_no_page.'/'.($page + 1).'?='.urlencode($the_query_string);
$paging_structure["next"] = $current_url_clean_no_page.'/'.($page + 1).'?q='.urlencode($the_query_string);
$paging_structure["startIndex"] = $page * $this->iiifConfig->get('iiif_content_search_api_results_per_page');
}
}
Expand All @@ -598,7 +591,7 @@ function () use ($metadataexposeconfig_entity, $node) {
"total" => $results['total'],
"first" =>
[
"id" => $current_url_clean_no_page.'/0?='.urlencode($the_query_string),
"id" => $current_url_clean_no_page.'/0?q='.urlencode($the_query_string),
"type" => "AnnotationPage",
],

Expand All @@ -611,7 +604,7 @@ function () use ($metadataexposeconfig_entity, $node) {
];
if ($total > (($page+1) * $this->iiifConfig->get('iiif_content_search_api_results_per_page'))) {
$paging_structure["next"] = [
"id" => $current_url_clean_no_page.'/'.($page + 1).'?='.urlencode($the_query_string),
"id" => $current_url_clean_no_page.'/'.($page + 1).'?q='.urlencode($the_query_string),
"type" => "AnnotationPage",
];
$paging_structure["startIndex"] = $page * $this->iiifConfig->get('iiif_content_search_api_results_per_page');
Expand Down Expand Up @@ -936,9 +929,6 @@ protected function flavorfromSolrIndex(string $term, array $processors, array $f
// remove the ID and the parent, not needed for file matching
unset($fields_to_retrieve['id']);
unset($fields_to_retrieve['parent_sequence_id']);
// Just in case something goes wrong with the returning region text
$region_text = $term;
$page_number_by_id = [];
if ($results->getResultCount() >= 1) {
// This applies to all searches with hits.
foreach ($results as $result) {
Expand Down Expand Up @@ -1031,7 +1021,7 @@ protected function flavorfromSolrIndex(string $term, array $processors, array $f
// $region_text like in a normal HOCR
// It is about time!
// Before and after. We will try to split the original text by the math
// If we end with more than 2 pieces, we can't be sure where it was found ..
// If we end with more than 2 pieces, we can't be sure where it was found
// so we set them '' ?
$before_and_after = explode($highlight[0]['text'], strip_tags($region_text));
$result_snippets_base['timespans'][] = [
Expand Down

0 comments on commit cc9db93

Please sign in to comment.