Refactor makelink to always return metadata, fix name error links

This commit is contained in:
Jonathan Harker 2026-10-18 07:47:20 +13:00
parent d6a31b505e
commit 2929b04836

View file

@ -122,7 +122,7 @@ local function make_citeref(names, year)
end
-- function to fetch a value to display
local function makelink(v, out, link, maxpos, wdl, namestyle)
local function makelink(v, out, maxpos, wdl, namestyle)
local label
local item_lookup = false
if v.mainsnak.snaktype == "value" then
@ -176,21 +176,6 @@ local function makelink(v, out, link, maxpos, wdl, namestyle)
first_names = getStyledNames(first_names, namestyle)
end
if sitelink then
-- just the plain name,
-- but keep a record of the links, using the same index
if last_name and first_names then
out[position] = {
label = label,
last = last_name or nil,
first = first_names or nil,
qid = qnumber,
}
else
out[position] = label
end
link[position] = sitelink
else
if wdl then
-- show that there's a Wikidata entry available
out[position] = string.format(
@ -200,18 +185,13 @@ local function makelink(v, out, link, maxpos, wdl, namestyle)
cfg.i18n_t["errors"]["local-article-not-found"]
)
else
-- no Wikidata links wanted, so just give the plain label
if last_name and first_names then
out[position] = {
label = label,
last = last_name or nil,
first = first_names or nil,
link = sitelink or nil,
qid = qnumber,
}
else
out[position] = label
end
end
end
elseif v.mainsnak.datatype == "string" then
local position = maxpos + 1 -- Default to 'next' author.
@ -231,23 +211,17 @@ local function makelink(v, out, link, maxpos, wdl, namestyle)
first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle)
end
if last_name and first_names then
out[position] = {
label = v.mainsnak.datavalue.value,
last = last_name or nil,
first = first_names or nil,
}
else
out[position] = v.mainsnak.datavalue.value
end
else
-- not a wikibase-item or a string!
end
else
-- try the "object named as" qualifier, otherwise use the unknown author item
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value -- "object named as"
-- Names from "author given names" and "author last names" qualifiers
local last_name
local first_names
@ -257,14 +231,13 @@ local function makelink(v, out, link, maxpos, wdl, namestyle)
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle)
end
if last_name and first_names then
label = {
label = label,
label = v.qualifiers.P1932[1].datavalue.value, -- "object named as"
last = last_name or nil,
first = first_names or nil,
}
end
else
-- use anonymous
label = {
label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper) .. (cfg.i18n_t.trackingcats["unknown-author"] or ""),
qid = 'Q4233718',
@ -335,15 +308,13 @@ local function get_name_list (nl_type, args, qid, wdl)
end
-- So now we have something to return:
-- table 'out' is going to store the names(s):
-- and table 'link' will store any links to the name's article
-- table 'out' is going to store the name label, qid, first/last names, and link to the name's article
local out = {}
local link = {}
local maxpos = 0
if props and props[1] then
for k, v in pairs(props) do
if 'deprecated' ~= v.rank then -- ignore deprecated names
maxpos = makelink(v, out, link, maxpos, wdl, args['name-list-style'])
maxpos = makelink(v, out, maxpos, wdl, args['name-list-style'])
end
end
end
@ -351,7 +322,7 @@ local function get_name_list (nl_type, args, qid, wdl)
-- second properties
for k, v in pairs(fallback) do
if 'deprecated' ~= v.rank then -- ignore deprecated names
maxpos = makelink(v, out, link, maxpos, wdl, args['name-list-style'])
maxpos = makelink(v, out, maxpos, wdl, args['name-list-style'])
end
end
end
@ -366,35 +337,30 @@ local function get_name_list (nl_type, args, qid, wdl)
table.sort(keys) -- as they might be out of order
for i, k in ipairs(keys) do
names[i] = {}
local label
if out[k].label then
label = out[k].label
else
label = out[k]
end
local label = out[k].label
label = label:gsub(''', '\''); -- prevent cs1|2 multiple names categorization; replace html entity with the actual character
names[i].label = label
names[i].qid = out[k].qid or nil
mw.log(i .. " " .. k .. " " .. label)
if args[nl_type .. i] then -- name gets overwritten
-- pull corresponding -link only if overwritten name is same as WD name
if link[k] and (args[nl_type .. i] == label) then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or link[k] -- author-linkn or editor-linkn
names[i].link = link[k]
if out[k].link and (args[nl_type .. i] == label) then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or out[k].link -- author-linkn or editor-linkn
names[i].link = out[k].link
end
else -- name does not get overwritten, so pull name from WD
names[i].last = out[k].last or nil
names[i].first = out[k].first or nil
if out[k].first and out[k].last then
-- First/last names were found, so add them
args[nl_type .. '-last' .. i] = args[nl_type .. '-last' .. i] or out[k].last
args[nl_type .. '-first' .. i] = args[nl_type .. '-first' .. i] or out[k].first
names[i].last = out[k].last
names[i].first = out[k].first
else
args[nl_type .. i] = label
args[nl_type .. i] = out[k].label
end
if link[k] then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or link[k] -- author-linkn or editor-linkn
names[i].link = link[k]
if out[k].link then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or out[k].link -- author-linkn or editor-linkn
names[i].link = out[k].link
end
end
end
@ -858,11 +824,12 @@ local function _cite_q (citeq_args)
-- check only the names being displayed for missing first/last name errors
local missing_names = {}
for i, author in pairs(names.authors or {}) do
-- mw.logObject(author)
if not author.last and i <= (citeq_args['display-authors'] or author_count) then
if author.qid then
-- ignore anonymous
if author.qid ~= 'Q4233718' then
table.insert(missing_names, author.label .. "&nbsp;<span title='edit author in Wikidata'>[[File:Wikidata-logo.svg|16px|alt=|link=[[:d:" .. author.qid .. "]] ]]</span>")
table.insert(missing_names, author.label .. "&nbsp;<span title='edit author in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. author.qid .. "]] ]]</span>")
end
else
table.insert(missing_names, author.label)
@ -874,7 +841,7 @@ local function _cite_q (citeq_args)
if editor.qid then
-- ignore anonymous
if editor.qid ~= 'Q4233718' then
table.insert(missing_names, editor.label .. "&nbsp;<span title='edit editor in Wikidata'>[[File:Wikidata-logo.svg|16px|alt=|link=[[:d:" .. editor.qid .. "]] ]]</span>")
table.insert(missing_names, editor.label .. "&nbsp;<span title='edit editor in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. editor.qid .. "]] ]]</span>")
end
else
table.insert(missing_names, editor.label)
@ -886,7 +853,7 @@ local function _cite_q (citeq_args)
if translator.qid then
-- ignore anonymous
if translator.qid ~= 'Q4233718' then
table.insert(missing_names, translator.label .. "&nbsp;<span title='edit translator in Wikidata'>[[File:Wikidata-logo.svg|16px|alt=|link=[[:d:" .. translator.qid .. "]] ]]</span>")
table.insert(missing_names, translator.label .. "&nbsp;<span title='edit translator in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. translator.qid .. "]] ]]</span>")
end
else
table.insert(missing_names, translator.label)