Skip to content

Commit

Permalink
Merge pull request #1197 from NatLibFi/fix-search-errors-waypoint-eas…
Browse files Browse the repository at this point in the history
…yrdf-http

Fixes for searches, ajax, waypoints, easyrdf HTTP client errors
  • Loading branch information
osma authored Sep 9, 2021
2 parents e2f4079 + ee5e40f commit 4b4359d
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 30 deletions.
29 changes: 23 additions & 6 deletions controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,36 @@ public function invokeGlobalSearch($request)
$vocabObjects = array();
if ($vocids) {
foreach($vocids as $vocid) {
$vocabObjects[] = $this->model->getVocabulary($vocid);
try {
$vocabObjects[] = $this->model->getVocabulary($vocid);
} catch (ValueError $e) {
// fail fast with an error page if the vocabulary cannot be found
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
header("HTTP/1.0 400 Bad Request");
$this->invokeGenericErrorPage($request, $e->getMessage());
return;
}
}
}
$parameters->setVocabularies($vocabObjects);

$counts = null;
$searchResults = null;
$errored = false;

try {
$countAndResults = $this->model->searchConceptsAndInfo($parameters);
$counts = $countAndResults['count'];
$searchResults = $countAndResults['results'];
} catch (Exception $e) {
header("HTTP/1.0 404 Not Found");
$errored = true;
header("HTTP/1.0 500 Internal Server Error");
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
$this->invokeGenericErrorPage($request, $e->getMessage());
return;
}
$counts = $countAndResults['count'];
$searchResults = $countAndResults['results'];
$vocabList = $this->model->getVocabularyList();
$sortedVocabs = $this->model->getVocabularyList(false, true);
$langList = $this->model->getLanguages($lang);
Expand All @@ -357,6 +370,7 @@ public function invokeGlobalSearch($request)
'search_results' => $searchResults,
'rest' => $parameters->getOffset()>0,
'global_search' => true,
'search_failed' => $errored,
'term' => $request->getQueryParamRaw('q'),
'lang_list' => $langList,
'vocabs' => str_replace(' ', '+', $vocabs),
Expand All @@ -375,6 +389,7 @@ public function invokeVocabularySearch($request)
$template = $this->twig->loadTemplate('vocab-search-listing.twig');
$this->setLanguageProperties($request->getLang());
$vocab = $request->getVocab();
$searchResults = null;
try {
$vocabTypes = $this->model->getTypes($request->getVocabid(), $request->getLang());
} catch (Exception $e) {
Expand All @@ -388,6 +403,7 @@ public function invokeVocabularySearch($request)
'languages' => $this->languages,
'vocab' => $vocab,
'request' => $request,
'search_results' => $searchResults
));

return;
Expand All @@ -411,6 +427,7 @@ public function invokeVocabularySearch($request)
'languages' => $this->languages,
'vocab' => $vocab,
'term' => $request->getQueryParam('q'),
'search_results' => $searchResults
));
return;
}
Expand Down
25 changes: 18 additions & 7 deletions model/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ public function __construct($model, $resource)
protected function getExternalLabel($exvoc, $exuri, $lang)
{
if ($exvoc) {
$exsparql = $exvoc->getSparql();
$results = $exsparql->queryLabel($exuri, $lang);

return isset($results[$lang]) ? $results[$lang] : null;
try {
$exsparql = $exvoc->getSparql();
$results = $exsparql->queryLabel($exuri, $lang);
return isset($results[$lang]) ? $results[$lang] : null;
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
}
}
return null;
}
Expand All @@ -63,9 +68,15 @@ protected function getExternalLabel($exvoc, $exuri, $lang)
protected function getExternalNotation($exvoc, $exuri)
{
if ($exvoc) {
$exsparql = $exvoc->getSparql();
$results = $exsparql->queryNotation($exuri);
return isset($results) ? $results : null;
try {
$exsparql = $exvoc->getSparql();
$results = $exsparql->queryNotation($exuri);
return isset($results) ? $results : null;
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
if ($this->model->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
}
}
return null;
}
Expand Down
33 changes: 24 additions & 9 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function searchConcepts($params)
try {
$hitvoc = $this->getVocabularyByGraph($hit['graph']);
$hit['vocab'] = $hitvoc->getId();
} catch (Exception $e) {
} catch (ValueError $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
$hitvoc = null;
$hit['vocab'] = "???";
Expand All @@ -255,7 +255,7 @@ public function searchConcepts($params)

$hit['voc'] = $hitvoc;

if (!$hitvoc->containsURI($hit['uri'])) {
if ($hitvoc === null || !$hitvoc->containsURI($hit['uri'])) {
// if uri is a external vocab uri that is included in the current vocab
$realvoc = $this->guessVocabularyFromURI($hit['uri'], $voc !== null ? $voc->getId() : null);
if ($realvoc !== $hitvoc) {
Expand Down Expand Up @@ -470,11 +470,18 @@ private function disambiguateVocabulary($vocabs, $uri, $preferredVocabId = null)
if($preferredVocabId != null) {
foreach ($vocabs as $vocab) {
if($vocab->getId() == $preferredVocabId) {
// double check that a label exists in the preferred vocabulary
if ($vocab->getConceptLabel($uri, null) !== null) {
return $vocab;
} else {
// not found in preferred vocabulary, fall back to next method
try {
// double check that a label exists in the preferred vocabulary
if ($vocab->getConceptLabel($uri, null) !== null) {
return $vocab;
} else {
// not found in preferred vocabulary, fall back to next method
break;
}
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
if ($this->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
break;
}
}
Expand All @@ -483,8 +490,16 @@ private function disambiguateVocabulary($vocabs, $uri, $preferredVocabId = null)

// no preferred vocabulary, or it was not found, search in which vocabulary the concept has a label
foreach ($vocabs as $vocab) {
if ($vocab->getConceptLabel($uri, null) !== null)
return $vocab;
try {
if ($vocab->getConceptLabel($uri, null) !== null) {
return $vocab;
}
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
if ($this->getConfig()->getLogCaughtExceptions()) {
error_log('Caught exception: ' . $e->getMessage());
}
break;
}
}

// if the URI couldn't be found, fall back to the first vocabulary
Expand Down
19 changes: 19 additions & 0 deletions resource/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ ul.dropdown-menu > li:last-child > input {
margin: 10px 0px;
}

.search-result-listing > .alert > .btn-default {
border-radius: 0;
}

.search-result-listing .search-count {
text-align: left;
margin-left: 15px;
Expand Down Expand Up @@ -1068,6 +1072,21 @@ div#sidebar-grey > div > div > form.search-options {
top: 10px;
}

.search-result-listing .alert .btn-default {
position: unset;
vertical-align: unset;
margin-left: 15px;
font-size: 18px;
}

.alert h4, .alert h3 {
display: inline;
}

.search-result-listing .alert-warning h4 {
display: block;
}

.search-options .mCSB_container > li > a > .radio {
margin: 0;
padding-left: 25px;
Expand Down
25 changes: 21 additions & 4 deletions resource/js/docready.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ $(function() { // DOCUMENT READY
$('.search-result-listing').append($ready);
}
else {
$trigger.waypoint(function() { waypointCallback(); }, options);
$trigger.waypoint(function() { waypointCallback(this); }, options);
}
}

Expand Down Expand Up @@ -894,9 +894,13 @@ $(function() { // DOCUMENT READY
changeOffset += 200;
}

function waypointCallback() {
function waypointCallback(waypoint) {
if ($('.search-result-listing > p .spinner,.search-result-listing .alert-danger').length > 0) {
return false;
}
var number_of_hits = $(".search-result").length;
if (number_of_hits < parseInt($('.search-count p').text().substr(0, $('.search-count p').text().indexOf(' ')), 10)) { $('.search-result-listing').append($loading);
if (number_of_hits < parseInt($('.search-count p').text().substr(0, $('.search-count p').text().indexOf(' ')), 10)) {
$('.search-result-listing').append($loading);
var typeLimit = $('#type-limit').val();
var schemeLimit = $('#scheme-limit').val();
var groupLimit = $('#group-limit').val();
Expand All @@ -917,7 +921,20 @@ $(function() { // DOCUMENT READY
$('.search-result-listing').append($ready);
return false;
}
$('.search-result:nth-last-of-type(4)').waypoint(function() { waypointCallback(); }, options );
waypoint.destroy();
$('.search-result:nth-last-of-type(4)').waypoint(function() { waypointCallback(this); }, options );
},
error: function(jqXHR, textStatus, errorThrown) {
$loading.detach();
var $failedSearch = $('<div class="alert alert-danger"><h4>' + loading_failed_text + '</h4></div>');
var $retryButton = $('<button class="btn btn-default" type="button">' + loading_retry_text + '</button>');
$retryButton.on('click', function () {
$failedSearch.remove();
waypointCallback(waypoint);
return false;
});
$failedSearch.append($retryButton)
$('.search-result-listing').append($failedSearch);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion resource/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ function loadLimitedResults(parameters) {
clearResultsAndAddSpinner();
$.ajax({
data: parameters,
success : function(data) {
complete : function(jqXHR, textStatus) {
var data = jqXHR.responseText;
var response = $('.search-result-listing', data).html();
if (window.history.pushState) { window.history.pushState({url: this.url}, '', this.url); }
$('.search-result-listing').append(response);
Expand Down
Binary file modified resource/translations/en/LC_MESSAGES/skosmos.mo
Binary file not shown.
Binary file modified resource/translations/fi/LC_MESSAGES/skosmos.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions resource/translations/skosmos_en.po
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,9 @@ msgstr "Error: Search failed!"

msgid "Error: Failed to retrieve vocabulary information!"
msgstr "Error: Failed to retrieve vocabulary information!"

msgid "Error: Loading more items failed!"
msgstr "Error: Loading more items failed!"

msgid "Retry"
msgstr "Retry"
6 changes: 6 additions & 0 deletions resource/translations/skosmos_fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,9 @@ msgstr "Virhe: Haku epäonnistui!"

msgid "Error: Failed to retrieve vocabulary information!"
msgstr "Virhe: Sanaston tietojen haku epäonnistui!"

msgid "Error: Loading more items failed!"
msgstr "Virhe: Lisätulosten haku epäonnistui!"

msgid "Retry"
msgstr "Yritä uudelleen"
6 changes: 6 additions & 0 deletions resource/translations/skosmos_sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -857,3 +857,9 @@ msgstr "Fel: Sökningen misslyckades!"

msgid "Error: Failed to retrieve vocabulary information!"
msgstr "Fel: Information om vokabulären kunde inte hämtas!"

msgid "Error: Loading more items failed!"
msgstr "Fel: Flera resultat kunde inte hämtas!"

msgid "Retry"
msgstr "Försök igen"
Binary file modified resource/translations/sv/LC_MESSAGES/skosmos.mo
Binary file not shown.
2 changes: 2 additions & 0 deletions view/scripts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!-- translations needed in javascript -->
var noResultsTranslation = "{% trans %}No results{% endtrans %}";
var loading_text = "{% trans %}Loading more items{% endtrans %}";
var loading_failed_text = "{% trans %}Error: Loading more items failed!{% endtrans %}";
var loading_retry_text = "{% trans %}Retry{% endtrans %}";
var jstree_loading = "{% trans %}Loading{% endtrans %} ...";
var results_disp = "{% trans %}All %d results displayed{% endtrans %}";
var all_vocabs = "{% trans %}from all{% endtrans %}";
Expand Down
2 changes: 1 addition & 1 deletion view/search-result.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="search-count{% if limit_type or limit_parent or limit_group or limit_scheme %} search-limited{% endif %}"><p>{% trans %}{{ search_count }} results for '{{ term }}'{% endtrans %}{% if limit_type %}, {% trans "limited to type" %} '{% for type in limit_type %}{{ type }}{% if loop.last == false %}, {% endif %}{% endfor %}'{% endif %}{% if limit_parent %}, {% trans "limited to parent" %} '{{ limit_parent }}'{% endif %}{% if limit_group %}, {% trans "limited to group" %} '{{ limit_group }}'{% endif %}{% if limit_scheme %}, {% trans "limited to scheme" %} '{% for scheme in limit_scheme %}{{ scheme }}{% if loop.last == false %}, {% endif %}{% endfor %}'{% endif %}</p>
</div>{% if limit_type or limit_parent or limit_group or limit_scheme %}<button type="button" class="btn btn-default" id="remove-limits">{% trans "Clear limitations" %}</button>{% endif %}
{% endif %}
{% if search_results is defined and search_results|length == 0 %}<p class="no-results">{% trans 'The search provided no results.' %}</p>{% endif %}
{% if search_results is not null and search_results|length == 0 %}<p class="no-results">{% trans 'The search provided no results.' %}</p>{% endif %}
{% for concept in search_results %} {# loop through the hits #}
<div class="search-result">
{% spaceless %}
Expand Down
4 changes: 2 additions & 2 deletions view/vocab-search-listing.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<div class="search-result-listing">
<h2 class="sr-only">{% trans "Search results" %}</h2>
{% include 'search-result.twig' %}
{% if search_results is not defined and not global_search %}
{% if search_results is null %}
<div class="alert alert-danger">
{% if request.vocabid == 'null' %}
{% if request.vocabid == 'null' and not global_search %}
<h4>{% trans %}Error: Requested vocabulary not found!{% endtrans %}</h4>
{% else %}
<h4>{% trans %}Error: Search failed!{% endtrans %}</h4>
Expand Down

0 comments on commit 4b4359d

Please sign in to comment.