Bug 26037: openlibrary.org is hit on every Koha requests
authorOwen Leonard <oleonard@myacpl.org>
Mon, 27 Jul 2020 12:26:41 +0000 (12:26 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 6 Aug 2020 09:37:07 +0000 (11:37 +0200)
This patch moves the inclusion of Open Library JavaScript from
opac-bottom.inc to the three templates where it might be used.

In addition, the Open Library JS has been modified to check for the
existence of the relevant markup on the page before executing a query.

Unrelated: The Open Library label is now hidden when the status returned
is "Not found." Previously this information was displayed.

To test, apply the patch and enable the OpenLibrarySearch system
preference.

 - Perform a search in the OPAC which will return results from Open
   Library. Some ISBNs: 014042382603950825520679436405.
   - On the search result page there should be an Open Library "Read
     now" button for each result that can be opened for reading in Open
     Library.
 - Add these titles to a list and view the list.
   - The "Read now" button should appear on the list page.
 - View the details for one of these titles.
   - The "Read now" button should appear on the page below the holdings.
 - Watch the browser's developer tools on these and other pages in the
   OPAC. There should be no errors in the console and no 404 errors in
   the network tab.

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt
koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js

index d4d8b23..d9bc539 100644 (file)
@@ -176,6 +176,10 @@ $.widget.bridge('uitooltip', $.ui.tooltip);
         var MSG_OVERDRIVE_DOWNLOAD_AS = _( "Download as: " );
         var MSG_OVERDRIVE_CANNOT_CHECKOUT = _("Item cannot be checked out. There are no available formats");
     [% END %]
+    [% IF OpenLibraryCovers || OpenLibrarySearch %]
+        var NO_OL_JACKET = _("No cover image available");
+        var OL_PREVIEW = _("Preview");
+    [% END %]
     [% IF ( OPACAmazonCoverImages || SyndeticsCoverImages ) %]
         $(window).load(function() {
             verify_images();
@@ -231,16 +235,6 @@ $.widget.bridge('uitooltip', $.ui.tooltip);
     </script>
 [% END %]
 
-[% IF OpenLibraryCovers || OpenLibrarySearch %]
-    [% Asset.js("js/openlibrary.js") | $raw %]
-    <script>
-    //<![CDATA[
-    var NO_OL_JACKET = _("No cover image available");
-    var OL_PREVIEW = _("Preview");
-    //]]>
-    </script>
-[% END %]
-
 [% IF OPACLocalCoverImages %]
     [% Asset.js("js/localcovers.js") | $raw %]
     <script>
index 9e89dea..cc32512 100644 (file)
     [% Asset.js("js/verovio.js") | $raw %]
 [% END %]
 [% Asset.js("lib/emoji-picker/js/emoji-picker-all.min.js") | $raw %]
+[% IF OpenLibraryCovers || OpenLibrarySearch %]
+    [% Asset.js("js/openlibrary.js") | $raw %]
+[% END %]
 <script>
     window.emojiPicker = new EmojiPicker({
       emojiable_selector: '[data-emojiable=true]',
index 3fd522c..e199aa7 100644 (file)
 [% Asset.js("js/authtoresults.js") | $raw %]
 [% Asset.js("lib/hc-sticky.js") | $raw %]
 [% IF ( OpacHighlightedWords ) %][% Asset.js("lib/jquery/plugins/jquery.highlight-3.js") | $raw %]
-[% END %]<script>
+[% END %]
+[% IF OpenLibraryCovers || OpenLibrarySearch %]
+    [% Asset.js("js/openlibrary.js") | $raw %]
+[% END %]
+
+<script>
 [% IF ( Koha.Preference( 'opacuserlogin' ) == 1 ) && ( Koha.Preference( 'RequestOnOpac' ) == 1 ) %]
 function holdMultiple() {
     var checkedBiblioNums = ""; // Separated by "/"
index ab9f776..46cbf0f 100644 (file)
 
                                                     [% INCLUDE 'title-actions-menu.inc' items=itemsloo %]
 
+                                                    [% INCLUDE "openlibrary-readapi.inc" bib = itemsloo %]
                                                     <!-- COinS / Openurl -->
                                                     <span class="Z3988" title="[% itemsloo.coins | html %]"></span>
                                                 </td>
 [% BLOCK jsinclude %]
 [% Asset.js("lib/jquery/plugins/jquery.checkboxes.min.js") | $raw %]
 [% Asset.js("lib/hc-sticky.js") | $raw %]
+[% IF OpenLibraryCovers || OpenLibrarySearch %]
+    [% Asset.js("js/openlibrary.js") | $raw %]
+[% END %]
 <script>
 var MSG_REMOVE_FROM_LIST = _("Are you sure you want to remove these items from the list?");
 var MSG_REMOVE_ONE_FROM_LIST = _("Are you sure you want to remove this item from the list?");
index c33778d..afd6e4c 100644 (file)
@@ -112,6 +112,8 @@ var ol_readapi_automator =
 // 'constants'
 var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys'];
 var magic_classname = 'ol_readapi_book';
+var ol_readapi_books = $("." + magic_classname );
+var result;
 
 // added to book divs to correlate with API results
 var magic_bookid = 'ol_bookid';
@@ -171,9 +173,8 @@ function make_read_button(bookdata) {
 function default_decorate_el_fn(el, bookdata) {
     // Note that 'bookdata' may be undefined, if the Read API call
     // didn't return results for this book
-    if (!bookdata) {
-        decoration = 'Not found';
-    } else {
+    var decoration;
+    if (bookdata) {
         decoration = make_read_button(bookdata);
     }
     if (decoration) {
@@ -216,15 +217,17 @@ function do_query(q, decorate_el_fn) {
                 });
 }
 
-// Do stuff
-var q = create_query();
-do_query(q);
+if( ol_readapi_books.length > 0 ){
+    // Do stuff
+    var q = create_query();
+    do_query(q);
 
-result = {
-    do_query: do_query,
-    create_query: create_query,
-    make_read_button: make_read_button
-};
+    result = {
+        do_query: do_query,
+        create_query: create_query,
+        make_read_button: make_read_button
+    };
+}
 
 return result;
 })(); // close anonymous scope