Refactor cite_q arg processing, i18n string for QID error

This commit is contained in:
Trappist the monk 2026-11-05 11:48:00 +13:00 • committed by Jonathan Harker
parent 35496de2b6
commit 0da5fa4047
2 changed files with 31 additions and 24 deletions

View file

@ -726,6 +726,7 @@ local function _cite_q (citeq_args)
local wikisource_sitelink = mw.wikibase.getSitelink(qid, cfg.i18n_t.wikisource) or nil
if wikisource_sitelink then
slink = 's:'..wikisource_sitelink; -- use 's:' instead of ':s:' so that cs1|2 includes wikisource icon
citeq_args.via = citeq_args.via or '[[Wikisource]]'; -- set |via=[[Wikisource]] if not already set
wslink = true
end
end
@ -1120,42 +1121,46 @@ local function _cite_q (citeq_args)
return output .. erratumid .. eoc .. retract .. table.concat(opt_cat, ' ') .. cq_errors
end
--[[--------------------------< C I T E _ Q >------------------------------------------------------------------
template entry point
]]
local function cite_q (frame)
local args = {}
for k, v in pairs(frame:getParent().args) do
if v ~= "" then args[k] = v end
end
for k, v in pairs(frame.args) do
if v ~= "" then args[k] = v end
end
args.qid = args.qid or args[1] or ""
if args.qid == "" then return nil end
args[1] = nil
local args = require ('Module:Arguments').getArgs(frame);
local citesep = (args.citesep or "")
if citesep == "" then citesep = ", " end
args.qid = args.qid or args[1];
if not args.qid then return nil end -- this should return an error message?
args[1] = nil; -- unset; not needed
local citesep = args.citesep; -- separator string between separate cites of {{cite q|q..., q...}}; any need to keep this?
if not citesep then citesep = ", " end
citesep = citesep:gsub('"', '') -- strip double quotes after setting default to allow |citesep="" as a blank separator
args.citesep = nil
args.citesep = nil; -- unset; not needed
local tag = args.tag or ""
if tag == "" then tag = nil end
args.tag = nil
local tag = args.tag; -- for applying tags like <ref>...</ref>; <tag> is the name of the tag
-- if not tag then tag = nil end
args.tag = nil; -- unset; not needed
local list = args.list or ""
if list == "" then list = nil end
args.list = nil
local list = args.list -- undocumented |list= gets a template name; what templates are appropriate? added at [[Special:Diff/1004682832]]
-- if list == "" then list = nil end
args.list = nil; -- unset; not needed
args.language = args.language or args.lang
args.lang = nil
-- QID as normal form; emit error if not ^Q%d+$ (e.g. a Wikidata URL)
local qid_form = mw.ustring.match(mw.text.trim(args.qid), "^Q%d+$") or nil
if not qid_form then
return ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: QID parameter must be supplied as "Q" followed by digits, e.g. <code class="cs1-code">Q12345</code> ([[Template:Cite Q#Error messages|help]])</span> ' .. cfg.i18n_t.trackingcats["invalid-qid"]
-- local qid_form = mw.ustring.match(mw.text.trim(args.qid), "^Q%d+$") or nil
-- if not qid_form then
-- return ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: QID parameter must be supplied as "Q" followed by digits, e.g. <code class="cs1-code">Q12345</code> ([[Template:Cite Q#Error messages|help]])</span> ' .. cfg.i18n_t.trackingcats["invalid-qid"]
if not args.qid:match ('^Q%d+$') then
return cfg.i18n_t.errors.bad_qid_form .. cfg.i18n_t.trackingcats["invalid-qid"];
end
local cites = {}
for q in args.qid:gmatch("Q%d+") do
for q in args.qid:gmatch("Q%d+") do -- for processing separate cites of {{cite q|q..., q...}}; any need to keep this?
-- make a new copy of the arguments
local newargs = {}
for k, v in pairs(args) do
@ -1172,13 +1177,14 @@ local function cite_q (frame)
end
end
if list then
if list then -- undocumented |list= gets a template name; what templates are appropriate? added at [[Special:Diff/1004682832]]
return frame:expandTemplate{ title = list, args = cites }
else
return table.concat(cites, citesep)
end
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]

View file

@ -10,6 +10,7 @@ local i18n_t = {
["wdq"] = "[[WDQ (identifier)|Wikidata]]", -- go through "WDQ (identifier)" redirect to reduce clutter in "What links here" and improve reverse lookup. Keep in sync with {{QID}}.
["wikisource"] = "enwikisource",
["errors"] = {
bad_qid_form = ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: QID parameter must be supplied as "Q" followed by digits, e.g. <code class="cs1-code">Q12345</code> ([[Template:Cite Q#Error messages|help]])</span>',
["local-article-not-found"] = "Local article not found.",
["no-label-or-title"] = "No label or title -- debug: ",
missing_name = '%s&nbsp;<span title="edit %s in Wikidata">[[File:Edit icon on Wikisite.svg|14px|class=noviewer|alt=|link=[[:d:%s]] ]]</span>'; -- %s: <name>.label, '<name>', <name>.qid