From fcd865bb81b7f6617159ee6eee9db6d7a9fa9f67 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 14 Jan 2022 12:28:50 +0100 Subject: [PATCH] Revert removing of deprecated methods used by android wrapper. --- README.md | 12 ++++++-- include/reader.h | 63 ++++++++++++++++++++++++++++++++++++++++++ src/reader.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e25a77678..58d735fb4 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,14 @@ Meson. If you want statically linked libraries, you can add Depending of you system, `ninja` may be called `ninja-build`. +The android wrapper uses deprecated methods of libkiwix so it cannot be compiled +with `werror=true` (the default). So you must pass `-Dwerror=false` to meson: + +```bash +meson . build -Dwrapper=android -Dwerror=false +ninja -C build +``` + Testing ------- @@ -154,7 +162,7 @@ to use custom welcome page mention `customIndexPage` argument in `kiwix::interna to create a HTML template with custom JS you need to have a look at various OPDS based endpoints as mentioned [here](https://wiki.kiwix.org/wiki/OPDS) to load books. -To use JS provided by kiwix-serve you can use the following template to start with -> +To use JS provided by kiwix-serve you can use the following template to start with -> ``` @@ -184,7 +192,7 @@ To use JS provided by kiwix-serve you can use the following template to start wi - To get number of books listed add - `

` under body tag. - To add language select box add - `` under body tag. - To add language select box add - `` under body tag. -- To add search box for books use following form - +- To add search box for books use following form - ```
diff --git a/include/reader.h b/include/reader.h index ee5e2531a..12d06a114 100644 --- a/include/reader.h +++ b/include/reader.h @@ -329,6 +329,28 @@ class Reader */ Entry getEntryFromTitle(const std::string& title) const; + /** + * Search for entries with title starting with prefix (case sensitive). + * + * Suggestions are stored in an internal vector and can be retrieved using + * `getNextSuggestion` method. + * This method is not thread safe and is deprecated. Use : + * bool searchSuggestions(const string& prefix, + * unsigned int suggestionsCount, + * SuggestionsList_t& results); + * + * @param prefix The prefix to search. + * @param suggestionsCount How many suggestions to search for. + * @param reset If true, remove previous suggestions in the internal vector. + * If false, add suggestions to the internal vector + * (until internal vector size is suggestionCount (or no more + * suggestion)) + * @return True if some suggestions have been added to the internal vector. + */ + DEPRECATED bool searchSuggestions(const string& prefix, + unsigned int suggestionsCount, + const bool reset = true); + /** * Search for entries with title starting with prefix (case sensitive). * @@ -344,6 +366,28 @@ class Reader unsigned int suggestionsCount, SuggestionsList_t& resuls); + /** + * Search for entries for the given prefix. + * + * If the zim file has a internal fulltext index, the suggestions will be + * searched using it. + * Else the suggestions will be search using `searchSuggestions` while trying + * to be smart about case sensitivity (using `getTitleVariants`). + * + * In any case, suggestions are stored in an internal vector and can be + * retrieved using `getNextSuggestion` method. + * The internal vector will be reset. + * This method is not thread safe and is deprecated. Use : + * bool searchSuggestionsSmart(const string& prefix, + * unsigned int suggestionsCount, + * SuggestionsList_t& results); + * + * @param prefix The prefix to search for. + * @param suggestionsCount How many suggestions to search for. + */ + DEPRECATED bool searchSuggestionsSmart(const string& prefix, + unsigned int suggestionsCount); + /** * Search for entries for the given prefix. * @@ -390,6 +434,22 @@ class Reader */ std::vector getTitleVariants(const std::string& title) const; + /** + * Get the next suggestion title. + * + * @param[out] title the title of the suggestion. + * @return True if title has been set. + */ + DEPRECATED bool getNextSuggestion(string& title); + + /** + * Get the next suggestion title and url. + * + * @param[out] title the title of the suggestion. + * @param[out] url the url of the suggestion. + * @return True if title and url have been set. + */ + DEPRECATED bool getNextSuggestion(string& title, string& url); /** * Get if we can check zim file integrity (has a checksum). @@ -425,6 +485,9 @@ class Reader std::shared_ptr zimArchive; std::string zimFilePath; + SuggestionsList_t suggestions; + SuggestionsList_t::iterator suggestionsOffset; + private: std::map parseCounterMetadata() const; }; diff --git a/src/reader.cpp b/src/reader.cpp index 24b0a7ecb..7bcbe8707 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -273,6 +273,32 @@ bool Reader::hasFulltextIndex() const return zimArchive->hasFulltextIndex(); } +/* Search titles by prefix */ + +bool Reader::searchSuggestions(const string& prefix, + unsigned int suggestionsCount, + const bool reset) +{ + /* Reset the suggestions otherwise check if the suggestions number is less + * than the suggestionsCount */ + if (reset) { + this->suggestions.clear(); + this->suggestionsOffset = this->suggestions.begin(); + } else { + if (this->suggestions.size() > suggestionsCount) { + return false; + } + } + + auto ret = searchSuggestions(prefix, suggestionsCount, this->suggestions); + + /* Set the cursor to the begining */ + this->suggestionsOffset = this->suggestions.begin(); + + return ret; +} + + bool Reader::searchSuggestions(const string& prefix, unsigned int suggestionsCount, SuggestionsList_t& results) @@ -333,6 +359,19 @@ std::vector Reader::getTitleVariants( } +bool Reader::searchSuggestionsSmart(const string& prefix, + unsigned int suggestionsCount) +{ + this->suggestions.clear(); + this->suggestionsOffset = this->suggestions.begin(); + + auto ret = searchSuggestionsSmart(prefix, suggestionsCount, this->suggestions); + + this->suggestionsOffset = this->suggestions.begin(); + + return ret; +} + /* Try also a few variations of the prefix to have better results */ bool Reader::searchSuggestionsSmart(const string& prefix, unsigned int suggestionsCount, @@ -371,6 +410,38 @@ bool Reader::searchSuggestionsSmart(const string& prefix, return results.size() > 0; } +/* Get next suggestion */ +bool Reader::getNextSuggestion(string& title) +{ + if (this->suggestionsOffset != this->suggestions.end()) { + /* title */ + title = (*(this->suggestionsOffset)).getTitle(); + + /* increment the cursor for the next call */ + this->suggestionsOffset++; + + return true; + } + + return false; +} + +bool Reader::getNextSuggestion(string& title, string& url) +{ + if (this->suggestionsOffset != this->suggestions.end()) { + /* title */ + title = (*(this->suggestionsOffset)).getTitle(); + url = (*(this->suggestionsOffset)).getPath(); + + /* increment the cursor for the next call */ + this->suggestionsOffset++; + + return true; + } + + return false; +} + /* Check if the file has as checksum */ bool Reader::canCheckIntegrity() const {