Refactor tracking categories to a set-like table, separate from error output

This commit is contained in:
Jonathan Harker 2026-10-20 12:34:18 +13:00
parent 0dfb7e26e1
commit a12122b03f

View file

@ -9,7 +9,8 @@ local cite_cfg = mw.loadData ('Module:Cite/config')
local cfg = mw.loadData ('Module:Cite_Q/config') local cfg = mw.loadData ('Module:Cite_Q/config')
local cs1_module = 'Module:Citation/CS1' -- don't load the module yet, may not be needed local cs1_module = 'Module:Citation/CS1' -- don't load the module yet, may not be needed
local cs1_cfg = mw.loadData('Module:Citation/CS1/Configuration') local cs1_cfg = mw.loadData('Module:Citation/CS1/Configuration')
local opt_cat = '' local opt_cat = {}
local cq_errors = ''
local max_authors_displayed = 10 -- Don't look up all 500+ author items (is this in CS1 config somewhere?) local max_authors_displayed = 10 -- Don't look up all 500+ author items (is this in CS1 config somewhere?)
@ -59,6 +60,14 @@ local function in_array( needle, haystack )
return false return false
end end
--[[--------------------------< T A B L E S E T _ A D D >------------------------------------------------------
Add needle to the haystack if it's not already there.
]]
local function tableset_add(haystack, needle)
if needle == nil or in_array(needle, haystack) then return false end
table.insert(haystack, needle)
return true
end
--[[--------------------------< A C C E P T _ V A L U E >------------------------------------------------------- --[[--------------------------< A C C E P T _ V A L U E >-------------------------------------------------------
Accept WD value by framing in ((...)) if param_val is equal to keyword; else pass-through WD value as is. Accept WD value by framing in ((...)) if param_val is equal to keyword; else pass-through WD value as is.
@ -252,8 +261,9 @@ local function makelink(v, out, maxpos, wdl, namestyle)
} }
else else
-- use anonymous -- use anonymous
tableset_add(opt_cat, cfg.i18n_t.trackingcats["unknown-author"])
label = { label = {
label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper) .. (cfg.i18n_t.trackingcats["unknown-author"] or ""), label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper),
qid = 'Q4233718', qid = 'Q4233718',
} }
end end
@ -886,11 +896,10 @@ local function _cite_q (citeq_args)
end end
-- add category and display error if name errors found -- add category and display error if name errors found
local name_cat = cfg.i18n_t.trackingcats['missing-author-first-last-names'] if #missing_names > 0 and not in_array('missing-author-first-last-names', opt_cat) then
if #missing_names > 0 and not string.find(opt_cat, name_cat, 1, true) then tableset_add(opt_cat, cfg.i18n_t.trackingcats['missing-author-first-last-names'])
opt_cat = opt_cat .. name_cat
-- TODO: format and i18n this string -- TODO: format and i18n this string
opt_cat = opt_cat .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]])</span> ' cq_errors = cq_errors .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]])</span> '
end end
-- change edition to ordinal if it's set and numeric -- change edition to ordinal if it's set and numeric
@ -913,7 +922,7 @@ local function _cite_q (citeq_args)
else else
local tpara = frame:expandTemplate{ title = 'para', args = {'ref', citeq_args.ref} } local tpara = frame:expandTemplate{ title = 'para', args = {'ref', citeq_args.ref} }
-- TODO: format and i18n this string -- TODO: format and i18n this string
opt_cat = opt_cat .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: supplied ' .. tpara .. ' does not match value generated from Wikidata: <code class="cs1-code">' .. citeref .. '</code> ([[Template:Cite Q#Error messages|help]])</span> ' cq_errors = cq_errors .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: supplied ' .. tpara .. ' does not match value generated from Wikidata: <code class="cs1-code">' .. citeref .. '</code> ([[Template:Cite Q#Error messages|help]])</span> '
end end
end end
@ -978,7 +987,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has been retracted -- Indicator if item has been retracted
local retract = '' local retract = ''
if citeq_args.retracted_by then if citeq_args.retracted_by then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.retracted tableset_add(opt_cat, cfg.i18n_t.trackingcats.retracted)
local retract_id = string.gsub(citeq_args.retracted_by, 'Q', '') local retract_id = string.gsub(citeq_args.retracted_by, 'Q', '')
local retr_params = { local retr_params = {
citeq = 'yes', citeq = 'yes',
@ -1033,7 +1042,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has corrigendum/erratum -- Indicator if item has corrigendum/erratum
local erratumid = "" local erratumid = ""
if citeq_args.erratum then if citeq_args.erratum then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.erratum tableset_add(opt_cat, cfg.i18n_t.trackingcats.erratum)
local erratum_params = { local erratum_params = {
citeq = 'yes', citeq = 'yes',
id = frame:expandTemplate{ id = frame:expandTemplate{
@ -1059,7 +1068,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has an expression of concern notice (P14891) -- Indicator if item has an expression of concern notice (P14891)
local eoc = "" local eoc = ""
if citeq_args['expression-of-concern'] then if citeq_args['expression-of-concern'] then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats['expression-of-concern'] tableset_add(opt_cat, cfg.i18n_t.trackingcats['expression-of-concern'])
local eoc_params = { local eoc_params = {
citeq = 'yes', citeq = 'yes',
id = frame:expandTemplate{ id = frame:expandTemplate{
@ -1083,11 +1092,11 @@ local function _cite_q (citeq_args)
citeq_args['eoc-intentional'] = nil citeq_args['eoc-intentional'] = nil
if citeq_args.replaced_by then if citeq_args.replaced_by then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.replaced tableset_add(opt_cat, cfg.i18n_t.trackingcats.replaced)
citeq_args.replaced_by = nil citeq_args.replaced_by = nil
end end
if not citeq_args.title then -- emit category link if missing title if not citeq_args.title then -- emit category link if missing title
opt_cat = opt_cat .. cfg.i18n_t.trackingcats['no-label-or-title']; tableset_add(opt_cat, cfg.i18n_t.trackingcats['no-label-or-title'])
end end
-- Disable CiteSeerX IDs for now -- Disable CiteSeerX IDs for now
@ -1108,9 +1117,10 @@ local function _cite_q (citeq_args)
else else
output = frame:expandTemplate{ -- render the template output = frame:expandTemplate{ -- render the template
title = "Cite "..template, args = citeq_args title = "Cite "..template, args = citeq_args
} .. cfg.i18n_t.trackingcats["unknown-template"] }
tableset_add(opt_cat, cfg.i18n_t.trackingcats["unknown-template"])
end end
return output .. erratumid .. eoc .. retract .. opt_cat return output .. erratumid .. eoc .. retract .. table.concat(opt_cat, ' ') .. cq_errors
end end
local function cite_q (frame) local function cite_q (frame)