Compare commits

..

3 commits

View file

@ -80,8 +80,31 @@ local function accept_value( param_val, wd_val )
return val
end
-- Split string of comma-separated first names into table of initials
local function getInitials(str)
local initials = {}
for _, word in pairs(mw.text.split(str, ' ', true)) do
local initial = mw.text.trim(word)
if #initial > 0 then
table.insert(initials, initial:sub(1, 1):upper())
end
end
return initials
end
-- Return initials given a style (e.g. Vancouver or APA)
local function getStyledNames(names, style)
if style == 'Vancouver' then
return table.concat(getInitials(names))
elseif style == 'APA' then
return table.concat(getInitials(names), '. ') .. '.'
else
return mw.text.trim(names:gsub(', ', ' '))
end
end
-- function to fetch a value to display
local function makelink(v, out, link, maxpos, wdl)
local function makelink(v, out, link, maxpos, wdl, namestyle)
local label
if v.mainsnak.snaktype == "value" then
if v.mainsnak.datatype == "wikibase-item" then
@ -89,11 +112,20 @@ local function makelink(v, out, link, maxpos, wdl)
local sitelink = mw.wikibase.getSitelink(qnumber)
if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors"
-- Last name, first name(s)
local last_name = getValue({"P734", qid=qnumber, maxvals=1, ps=2})
local first_names = getValue({"P735", qid=qnumber, maxvals=0, ps=2})
-- Last name, first name(s) from "author given names" and "author last names" qualifiers
local last_name
local first_names
if v.qualifiers and v.qualifiers.P9688 and v.qualifiers.P9688[1] then
last_name = v.qualifiers.P9688[1].datavalue.value
end
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
first_names = v.qualifiers.P9687[1].datavalue.value
end
-- Otherwise, get last name, first name(s) from author item properties
last_name = last_name or getValue({"P734", qid=qnumber, maxvals=1, ps=2})
first_names = first_names or getValue({"P735", qid=qnumber, maxvals=0, ps=2})
if first_names then
first_names = mw.text.trim(first_names:gsub(', ', ' '))
first_names = getStyledNames(first_names, namestyle)
end
if v.qualifiers and v.qualifiers.P1932 then
@ -162,7 +194,7 @@ local function makelink(v, out, link, maxpos, wdl)
last_name = v.qualifiers.P9688[1].datavalue.value
end
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' '))
first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle)
end
if last_name and first_names then
@ -189,16 +221,14 @@ local function makelink(v, out, link, maxpos, wdl)
last_name = v.qualifiers.P9688[1].datavalue.value
end
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' '))
first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle)
end
if last_name and first_names then
out[position] = {
label = {
label = label,
last = last_name or nil,
first = first_names or nil,
}
else
out[position] = label
end
else
label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper) .. (cfg.i18n_t.trackingcats["unknown-author"] or "")
@ -275,7 +305,7 @@ local function get_name_list (nl_type, args, qid, wdl)
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)
maxpos = makelink(v, out, link, maxpos, wdl, args.namestyle)
end
end
end
@ -283,7 +313,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)
maxpos = makelink(v, out, link, maxpos, wdl, args.namestyle)
end
end
end
@ -377,8 +407,11 @@ local function yearFromDate(datestr)
return datestr
end
-- Attempt to determine heuristically if we're citing a news article
local function isNewsArticle(qid, args)
-- Attempt to determine heuristically what we're citing
local function getTemplateFromInstance(qid, args)
local chapteritems = {
'Q1980247', -- chapter
}
local newsitems = {
'Q5707594', -- news article
'Q30070590', -- magazine article
@ -397,7 +430,9 @@ local function isNewsArticle(qid, args)
}
-- most of the time a cheap lookup to a single value will work
if args['instance-of'] and in_array(args['instance-of'], newsitems) then
return true
return 'news'
elseif args['instance-of'] and in_array(args['instance-of'], chapteritems) then
return 'chapter'
end
-- otherwise check multiple non-deprecated values
local instance_of = mw.wikibase.getBestStatements(qid, 'P31')
@ -405,12 +440,14 @@ local function isNewsArticle(qid, args)
for k, v in pairs(instance_of) do
if v.mainsnak.snaktype == "value" and v.mainsnak.datatype == "wikibase-item" then
if in_array(v.mainsnak.datavalue.value.id, newsitems) then
return true
return 'news'
elseif in_array(v.mainsnak.datavalue.value.id, chapteritems) then
return 'chapter'
end
end
end
end
return false
return ''
end
-- Figure out a URL access value, from the "online access status" (P6954) qualifier of an identifier
@ -501,6 +538,7 @@ local function _cite_q (citeq_args)
citeq_args.qid = nil
citeq_args.wdl = nil
citeq_args.template = nil
citeq_args.namestyle = citeq_args.namestyle or citeq_args['name-style'] or nil
-- if title supplied, flag to not read html title
local titleforced = (citeq_args.title ~= nil)
@ -777,14 +815,20 @@ local function _cite_q (citeq_args)
-- code to make a guess what template to use from the supplied parameters
-- (first draft for proof-of-concept)
local pubyear = yearFromDate(citeq_args['publication-date'])
local is_news = isNewsArticle(qid, citeq_args)
if is_news then
local guessedType = getTemplateFromInstance(qid, citeq_args)
if guessedType == 'news' then
template = template or "news"
-- some items use P123 (publisher) as the newspaper instead of P1433 (published in)
if citeq_args.publisher and not citeq_args.journal then
citeq_args.journal = citeq_args.publisher
citeq_args.publisher = nil
end
elseif guessedType == 'chapter' then
template = template or "book"
citeq_args['publication-date'] = pubyear or citeq_args['publication-date']
citeq_args.chapter = citeq_args.title
citeq_args.title = citeq_args.journal
citeq_args.journal = nil
elseif citeq_args.journal then
template = template or "journal"
citeq_args['publication-date'] = pubyear or citeq_args['publication-date']
@ -949,6 +993,8 @@ local function _cite_q (citeq_args)
-- remove any remaining non-CS1 fields
citeq_args['instance-of'] = nil
citeq_args.namestyle = nil
citeq_args['name-style'] = nil
-- render the template
local lc_template = template and template:lower() or "citation"