From 924ba7adfeb23b5497e0992f490f95156ba44b28 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 May 2023 17:55:51 +0530 Subject: [PATCH] Content server: Allow disabling full text search via the web interface. Fixes #2020237 [[content server] Disable fulltext search activation via web interface](https://bugs.launchpad.net/calibre/+bug/2020237) --- src/calibre/srv/fts.py | 8 ++++++++ src/pyj/book_list/fts.pyj | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/calibre/srv/fts.py b/src/calibre/srv/fts.py index 5186f0f276..c3959d7cab 100644 --- a/src/calibre/srv/fts.py +++ b/src/calibre/srv/fts.py @@ -54,6 +54,14 @@ def fts_search(ctx, rd): return ans +@endpoint('/fts/disable', needs_db_write=True) +def fts_disable(ctx, rd): + db = get_library_data(ctx, rd)[0] + if db.is_fts_enabled(): + db.enable_fts(enabled=False) + return '' + + @endpoint('/fts/reindex', needs_db_write=True, methods=('POST',)) def fts_reindex(ctx, rd): db = get_library_data(ctx, rd)[0] diff --git a/src/pyj/book_list/fts.pyj b/src/pyj/book_list/fts.pyj index d3837b40e4..d806266d9c 100644 --- a/src/pyj/book_list/fts.pyj +++ b/src/pyj/book_list/fts.pyj @@ -8,7 +8,7 @@ from ajax import ajax, ajax_send from book_list.cover_grid import THUMBNAIL_MAX_HEIGHT, THUMBNAIL_MAX_WIDTH from book_list.globals import get_current_query, get_session_data from book_list.library_data import current_library_id, download_url -from book_list.router import back, open_book_url, push_state +from book_list.router import back, home, open_book_url, push_state from book_list.top_bar import create_top_bar from book_list.ui import set_panel_handler from book_list.views import create_image @@ -34,7 +34,9 @@ add_extra_css(def(): ) def component(name): - return document.getElementById(overall_container_id)?.querySelector(f'[data-component="{name}"]') + c = document.getElementById(overall_container_id) + if c: + return c.querySelector(f'[data-component="{name}"]') def showing_search_panel(): @@ -157,6 +159,8 @@ def clear_to_help(): container.appendChild(E.div( style='margin-top: 1ex', E.a(_('Re-index all books in this library'), class_='blue-link', href='javascript:void(0)', onclick=reindex_all), + E.span('\xa0\xa0'), + E.a(_('Disable full text search'), class_='blue-link', href='javascript:void(0)', onclick=disable_fts), )) container = container.firstChild fts_url = 'https://www.sqlite.org/fts5.html#full_text_query_syntax' @@ -245,6 +249,32 @@ def reindex_all(): ) +def disable_fts_backend(): + def on_response(end_type, xhr, ev): + if end_type is 'abort' or not showing_search_panel(): + return + if end_type is not 'load': + if xhr.status is 403: + return error_dialog(_('Permission denied'), _( + 'You do not have permission to disable FTS. Only logged in users with write permission are allowed to disable FTS.'), xhr.error_html) + return error_dialog(_('Disabling FTS failed'), _('Disabling FTS failed. Click "Show details" for more information.'), xhr.error_html) + info_dialog(_('Full text searching disabled'), _('Full text searching for this library has been disabled. In the future the entire library will have to be re-indexed when re-enabling full text searching.'), on_close=def(): + window.setTimeout(home) + ) + + ajax(f'fts/disable', on_response, bypass_cache=True).send() + + + +def disable_fts(): + question_dialog(_('Are you sure?'), _('Disabling full text search means that in the future the entire library will have to be re-indexed to use full text search. Are you sure you want to proceed?'), + def (yes): + if yes: + disable_fts_backend() + ) + + + def book_result_tile_clicked(ev): result_tile = ev.currentTarget bid = int(result_tile.dataset.bookId)