Wikipedia: rework wiki languages internal handling

Fix various issues with stacked Wikipedia lookup results,
and follow up lookups when invoked from Wikipedia lookup
history (where going to a search for language LL would
not use language LL when hitting "Full wikipedia").
reviewable/pr8694/r1
poire-z 2 years ago
parent dccd0da4cc
commit 0216493c43

@ -947,7 +947,7 @@ end
function ReaderDictionary:showDict(word, results, boxes, link) function ReaderDictionary:showDict(word, results, boxes, link)
if results and results[1] then if results and results[1] then
logger.dbg("showing quick lookup window", word, results) logger.dbg("showing quick lookup window", #self.dict_window_list+1, ":", word, results)
self.dict_window = DictQuickLookup:new{ self.dict_window = DictQuickLookup:new{
window_list = self.dict_window_list, window_list = self.dict_window_list,
ui = self.ui, ui = self.ui,
@ -962,7 +962,6 @@ function ReaderDictionary:showDict(word, results, boxes, link)
preferred_dictionaries = self.preferred_dictionaries, preferred_dictionaries = self.preferred_dictionaries,
-- differentiate between dict and wiki -- differentiate between dict and wiki
is_wiki = self.is_wiki, is_wiki = self.is_wiki,
wiki_languages = self.wiki_languages,
refresh_callback = function() refresh_callback = function()
if self.view then if self.view then
-- update info in footer (time, battery, etc) -- update info in footer (time, battery, etc)

@ -24,11 +24,11 @@ local wikipedia_history = nil
local ReaderWikipedia = ReaderDictionary:extend{ local ReaderWikipedia = ReaderDictionary:extend{
-- identify itself -- identify itself
is_wiki = true, is_wiki = true,
wiki_languages = {},
disable_history = G_reader_settings:isTrue("wikipedia_disable_history"), disable_history = G_reader_settings:isTrue("wikipedia_disable_history"),
} }
function ReaderWikipedia:init() function ReaderWikipedia:init()
self.wiki_languages = {}
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
if not wikipedia_history then if not wikipedia_history then
wikipedia_history = LuaData:open(DataStorage:getSettingsDir() .. "/wikipedia_history.lua", { name = "WikipediaHistory" }) wikipedia_history = LuaData:open(DataStorage:getSettingsDir() .. "/wikipedia_history.lua", { name = "WikipediaHistory" })
@ -348,7 +348,8 @@ function ReaderWikipedia:initLanguages(word)
-- Fill self.wiki_languages with languages to propose -- Fill self.wiki_languages with languages to propose
local wikipedia_languages = G_reader_settings:readSetting("wikipedia_languages") local wikipedia_languages = G_reader_settings:readSetting("wikipedia_languages")
if type(wikipedia_languages) == "table" and #wikipedia_languages > 0 then if type(wikipedia_languages) == "table" and #wikipedia_languages > 0 then
-- use this setting, no need to guess -- use this setting, no need to guess: we reference the setting table, so
-- any update to it will have it saved in settings
self.wiki_languages = wikipedia_languages self.wiki_languages = wikipedia_languages
else else
-- guess some languages -- guess some languages
@ -402,10 +403,11 @@ function ReaderWikipedia:lookupWikipedia(word, is_sane, box, get_fullpage, force
self:initLanguages(word) self:initLanguages(word)
local lang local lang
if forced_lang then if forced_lang then
-- use provided lang (from readerlink when noticing that an external link is a wikipedia url) -- use provided lang (from readerlink when noticing that an external link is a wikipedia url,
-- of from Wikipedia lookup history, or when switching to next language in DictQuickLookup)
lang = forced_lang lang = forced_lang
else else
-- use first lang from self.wiki_languages, which may have been rotated by DictQuickLookup -- use first lang from self.wiki_languages
lang = self.wiki_languages[1] lang = self.wiki_languages[1]
end end
logger.dbg("lookup word:", word, box, get_fullpage) logger.dbg("lookup word:", word, box, get_fullpage)
@ -524,11 +526,65 @@ function ReaderWikipedia:lookupWikipedia(word, is_sane, box, get_fullpage, force
lang = lang, lang = lang,
} }
} }
-- Also put this as a k/v into the results array: if we end up with this
-- after lang rotation, DictQuickLookup will not update this lang rotation.
results.no_result = true
logger.dbg("dummy result table:", word, results) logger.dbg("dummy result table:", word, results)
end end
self:showDict(word, results, box) self:showDict(word, results, box)
end end
function ReaderWikipedia:getWikiLanguages(first_lang)
-- Always return a copy of ours
local wiki_languages = {unpack(self.wiki_languages)}
local is_first_lang = first_lang == wiki_languages[1]
if not is_first_lang then
-- return a wiki_languages with requested lang at first
if util.arrayContains(wiki_languages, first_lang) then
-- first_lang in the list: rotate until it is first
while wiki_languages[1] ~= first_lang do
table.insert(wiki_languages, table.remove(wiki_languages, 1))
end
else
-- first_lang not in the list: add it first
table.insert(wiki_languages, 1, first_lang)
end
end
local update_wiki_languages_on_close = false
if self.dict_window_list.rotated_update_wiki_languages_on_close ~= nil then
-- Flag set by DictQuickLookup when rotating, forwarding the flag
-- of the rotated out DictQuickLookup instance: trust it
update_wiki_languages_on_close = self.dict_window_list.rotated_update_wiki_languages_on_close
self.dict_window_list.rotated_update_wiki_languages_on_close = nil
else
-- Not a rotation. Only if it's the first request with the current
-- first language, we will have it (and any lang rotation from it)
-- update the main ReaderWikipedia.wiki_languages. That is, queries
-- from Wikipedia url links for another language, or from Wikipedia
-- lookup history with other languages (and any lang rotation made
-- from them) won't update it.
if is_first_lang then
update_wiki_languages_on_close = true
for i=1, #self.dict_window_list-1 do -- (ignore the last one, which is the one calling this)
if self.dict_window_list[i].is_wiki then
-- Another upper Wikipedia result: only this one may update it
update_wiki_languages_on_close = false
break
end
end
end
end
return wiki_languages, update_wiki_languages_on_close
end
function ReaderWikipedia:onUpdateWikiLanguages(wiki_languages)
-- Update our self.wiki_languages in-place
while table.remove(self.wiki_languages) do end
for _, lang in ipairs(wiki_languages) do
table.insert(self.wiki_languages, lang)
end
end
-- override onSaveSettings in ReaderDictionary -- override onSaveSettings in ReaderDictionary
function ReaderWikipedia:onSaveSettings() function ReaderWikipedia:onSaveSettings()
end end

@ -152,23 +152,19 @@ function DictQuickLookup:init()
}, },
-- callback function when HoldReleaseText is handled as args -- callback function when HoldReleaseText is handled as args
args = function(text, hold_duration) args = function(text, hold_duration)
local lookup_target -- do this lookup in the same domain (dict/wikipedia)
if hold_duration < TimeVal:new{ sec = 3, usec = 0 } then local lookup_wikipedia = self.is_wiki
-- do this lookup in the same domain (dict/wikipedia) if hold_duration >= TimeVal:new{ sec = 3, usec = 0 } then
lookup_target = self.is_wiki and "LookupWikipedia" or "LookupWord"
else
-- but allow switching domain with a long hold -- but allow switching domain with a long hold
lookup_target = self.is_wiki and "LookupWord" or "LookupWikipedia" lookup_wikipedia = not lookup_wikipedia
end end
if lookup_target == "LookupWikipedia" then -- We don't pass self.highlight to subsequent lookup, we want the
self:resyncWikiLanguages() -- first to be the only one to unhighlight selection when closed
if lookup_wikipedia then
self:lookupWikipedia(false, text)
else
self.ui:handleEvent(Event:new("LookupWord", text))
end end
self.ui:handleEvent(
-- don't pass self.highlight to subsequent lookup, we want
-- the first to be the only one to unhighlight selection
-- when closed
Event:new(lookup_target, text)
)
end end
}, },
-- These will be forwarded to MovableContainer after some checks -- These will be forwarded to MovableContainer after some checks
@ -185,9 +181,9 @@ function DictQuickLookup:init()
-- And here comes the initial widget layout... -- And here comes the initial widget layout...
if self.is_wiki then if self.is_wiki then
-- Keep a copy of self.wiki_languages for use -- Get a copy of ReaderWikipedia.wiki_languages, with the current result
-- by DictQuickLookup:resyncWikiLanguages() -- lang first (rotated, or added)
self.wiki_languages_copy = self.wiki_languages and {unpack(self.wiki_languages)} or nil self.wiki_languages, self.update_wiki_languages_on_close = self.ui.wikipedia:getWikiLanguages(self.lang)
end end
-- Bigger window if fullpage Wikipedia article being shown, -- Bigger window if fullpage Wikipedia article being shown,
@ -319,7 +315,7 @@ function DictQuickLookup:init()
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
-- if forced_lang was specified, it may not be in our wiki_languages, -- if forced_lang was specified, it may not be in our wiki_languages,
-- but ReaderWikipedia will have put it in result.lang -- but ReaderWikipedia will have put it in result.lang
local lang = self.lang or self.wiki_languages_copy[1] local lang = self.lang or self.wiki_languages[1]
-- Find a directory to save file into -- Find a directory to save file into
local dir local dir
if G_reader_settings:isTrue("wikipedia_save_in_book_dir") and not self:isDocless() then if G_reader_settings:isTrue("wikipedia_save_in_book_dir") and not self:isDocless() then
@ -386,7 +382,7 @@ function DictQuickLookup:init()
id = "close", id = "close",
text = _("Close"), text = _("Close"),
callback = function() callback = function()
UIManager:close(self) self:onClose()
end, end,
}, },
}, },
@ -473,12 +469,14 @@ function DictQuickLookup:init()
enabled = self:canSearch(), enabled = self:canSearch(),
callback = function() callback = function()
if self.is_wiki then if self.is_wiki then
self:resyncWikiLanguages(true) -- rotate & resync them -- We're rotating: forward this flag from the one we're closing so
UIManager:close(self) -- that ReaderWikipedia can give it to the one we'll be showing
self:lookupWikipedia() self.window_list.rotated_update_wiki_languages_on_close = self.update_wiki_languages_on_close
self:lookupWikipedia(false, nil, nil, self.wiki_languages[2])
self:onClose(true)
else else
self.ui:handleEvent(Event:new("HighlightSearch")) self.ui:handleEvent(Event:new("HighlightSearch"))
UIManager:close(self) self:onClose(true) -- don't unhighlight (or we might erase a search hit)
end end
end, end,
}, },
@ -486,7 +484,6 @@ function DictQuickLookup:init()
id = "close", id = "close",
text = _("Close"), text = _("Close"),
callback = function() callback = function()
-- UIManager:close(self)
self:onClose() self:onClose()
end, end,
}, },
@ -1094,7 +1091,7 @@ function DictQuickLookup:onTap(arg, ges_ev)
return true return true
end end
function DictQuickLookup:onClose() function DictQuickLookup:onClose(no_clear)
UIManager:close(self) UIManager:close(self)
for i = #self.window_list, 1, -1 do for i = #self.window_list, 1, -1 do
local window = self.window_list[i] local window = self.window_list[i]
@ -1102,7 +1099,13 @@ function DictQuickLookup:onClose()
table.remove(self.window_list, i) table.remove(self.window_list, i)
end end
end end
if self.highlight then if self.update_wiki_languages_on_close then
-- except if we got no result for current language
if not self.results.no_result then
self.ui:handleEvent(Event:new("UpdateWikiLanguages", self.wiki_languages))
end
end
if self.highlight and not no_clear then
-- delay unhighlight of selection, so we can see where we stopped when -- delay unhighlight of selection, so we can see where we stopped when
-- back from our journey into dictionary or wikipedia -- back from our journey into dictionary or wikipedia
local clear_id = self.highlight:getClearId() local clear_id = self.highlight:getClearId()
@ -1114,18 +1117,8 @@ function DictQuickLookup:onClose()
end end
function DictQuickLookup:onHoldClose(no_clear) function DictQuickLookup:onHoldClose(no_clear)
self:onClose() while #self.window_list > 0 do
for i = #self.window_list, 1, -1 do self.window_list[#self.window_list]:onClose(no_clear)
local window = self.window_list[i]
-- if one holds a highlight, let's clear it like in onClose()
if window.highlight and not no_clear then
local clear_id = window.highlight:getClearId()
UIManager:scheduleIn(0.5, function()
window.highlight:clear(clear_id)
end)
end
UIManager:close(window)
table.remove(self.window_list, i)
end end
return true return true
end end
@ -1258,15 +1251,12 @@ end
function DictQuickLookup:inputLookup() function DictQuickLookup:inputLookup()
local word = self.input_dialog:getInputText() local word = self.input_dialog:getInputText()
if word and word ~= "" then if word and word ~= "" then
local event -- Trust that input text does not need any cleaning (allows querying for "-suffix")
if self.is_wiki then if self.is_wiki then
event = "LookupWikipedia" self:lookupWikipedia(false, word, true)
self:resyncWikiLanguages()
else else
event = "LookupWord" self.ui:handleEvent(Event:new("LookupWord", word, true))
end end
-- Trust that input text does not need any cleaning (allows querying for "-suffix")
self.ui:handleEvent(Event:new(event, word, true))
end end
end end
@ -1274,40 +1264,32 @@ function DictQuickLookup:closeInputDialog()
UIManager:close(self.input_dialog) UIManager:close(self.input_dialog)
end end
function DictQuickLookup:resyncWikiLanguages(rotate) function DictQuickLookup:lookupWikipedia(get_fullpage, word, is_sane, lang)
-- Resync the current language or rotate it from its state when if not lang then
-- this window was created (we may have rotated it later in other -- Use the lang of the current or nearest is_wiki DictQuickLookup.
-- wikipedia windows that we closed and went back here, and its -- Otherwise, first lang in ReaderWikipedia.wiki_languages will be used.
-- state would not be what the wikipedia language button is showing. for i = #self.window_list, 1, -1 do
if not self.wiki_languages_copy then local window = self.window_list[i]
return if window.is_wiki and window.lang then
end lang = window.lang
if rotate then break
-- rotate our saved wiki_languages copy end
local current_lang = table.remove(self.wiki_languages_copy, 1) end
table.insert(self.wiki_languages_copy, current_lang)
end
-- re-set self.wiki_languages with original (possibly rotated) items
for i, lang in ipairs(self.wiki_languages_copy) do
self.wiki_languages[i] = lang
end end
end if not word then
if get_fullpage then
function DictQuickLookup:lookupWikipedia(get_fullpage) -- we use the word of the displayed result's definition, which
local word -- is the exact title of the full wikipedia page
local is_sane word = self.lookupword
if get_fullpage then is_sane = true
-- we use the word of the displayed result's definition, which else
-- is the exact title of the full wikipedia page -- we use the original word that was querried
word = self.lookupword word = self.word
is_sane = true is_sane = false
else end
-- we use the original word that was querried
word = self.word
is_sane = false
end end
self:resyncWikiLanguages() -- Keep providing self.word_boxes so new windows keep being positionned to not hide it
self.ui:handleEvent(Event:new("LookupWikipedia", word, is_sane, self.word_box, get_fullpage)) self.ui:handleEvent(Event:new("LookupWikipedia", word, is_sane, self.word_boxes, get_fullpage, lang))
end end
return DictQuickLookup return DictQuickLookup

Loading…
Cancel
Save