Compare commits
3 commits
280095ed0f
...
6f13edba6e
| Author | SHA1 | Date | |
|---|---|---|---|
| 6f13edba6e | |||
| 1df4c6cb25 | |||
| 2ed4bf2b7c |
1 changed files with 65 additions and 19 deletions
|
|
@ -80,8 +80,31 @@ local function accept_value( param_val, wd_val )
|
||||||
return val
|
return val
|
||||||
end
|
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
|
-- 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
|
local label
|
||||||
if v.mainsnak.snaktype == "value" then
|
if v.mainsnak.snaktype == "value" then
|
||||||
if v.mainsnak.datatype == "wikibase-item" 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)
|
local sitelink = mw.wikibase.getSitelink(qnumber)
|
||||||
if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors"
|
if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors"
|
||||||
|
|
||||||
-- Last name, first name(s)
|
-- Last name, first name(s) from "author given names" and "author last names" qualifiers
|
||||||
local last_name = getValue({"P734", qid=qnumber, maxvals=1, ps=2})
|
local last_name
|
||||||
local first_names = getValue({"P735", qid=qnumber, maxvals=0, ps=2})
|
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
|
if first_names then
|
||||||
first_names = mw.text.trim(first_names:gsub(', ', ' '))
|
first_names = getStyledNames(first_names, namestyle)
|
||||||
end
|
end
|
||||||
|
|
||||||
if v.qualifiers and v.qualifiers.P1932 then
|
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
|
last_name = v.qualifiers.P9688[1].datavalue.value
|
||||||
end
|
end
|
||||||
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
|
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
|
end
|
||||||
|
|
||||||
if last_name and first_names then
|
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
|
last_name = v.qualifiers.P9688[1].datavalue.value
|
||||||
end
|
end
|
||||||
if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then
|
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
|
end
|
||||||
if last_name and first_names then
|
if last_name and first_names then
|
||||||
out[position] = {
|
label = {
|
||||||
label = label,
|
label = label,
|
||||||
last = last_name or nil,
|
last = last_name or nil,
|
||||||
first = first_names or nil,
|
first = first_names or nil,
|
||||||
}
|
}
|
||||||
else
|
|
||||||
out[position] = label
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
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) .. (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
|
if props and props[1] then
|
||||||
for k, v in pairs(props) do
|
for k, v in pairs(props) do
|
||||||
if 'deprecated' ~= v.rank then -- ignore deprecated names
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -283,7 +313,7 @@ local function get_name_list (nl_type, args, qid, wdl)
|
||||||
-- second properties
|
-- second properties
|
||||||
for k, v in pairs(fallback) do
|
for k, v in pairs(fallback) do
|
||||||
if 'deprecated' ~= v.rank then -- ignore deprecated names
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -377,8 +407,11 @@ local function yearFromDate(datestr)
|
||||||
return datestr
|
return datestr
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Attempt to determine heuristically if we're citing a news article
|
-- Attempt to determine heuristically what we're citing
|
||||||
local function isNewsArticle(qid, args)
|
local function getTemplateFromInstance(qid, args)
|
||||||
|
local chapteritems = {
|
||||||
|
'Q1980247', -- chapter
|
||||||
|
}
|
||||||
local newsitems = {
|
local newsitems = {
|
||||||
'Q5707594', -- news article
|
'Q5707594', -- news article
|
||||||
'Q30070590', -- magazine 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
|
-- 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
|
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
|
end
|
||||||
-- otherwise check multiple non-deprecated values
|
-- otherwise check multiple non-deprecated values
|
||||||
local instance_of = mw.wikibase.getBestStatements(qid, 'P31')
|
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
|
for k, v in pairs(instance_of) do
|
||||||
if v.mainsnak.snaktype == "value" and v.mainsnak.datatype == "wikibase-item" then
|
if v.mainsnak.snaktype == "value" and v.mainsnak.datatype == "wikibase-item" then
|
||||||
if in_array(v.mainsnak.datavalue.value.id, newsitems) 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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return ''
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Figure out a URL access value, from the "online access status" (P6954) qualifier of an identifier
|
-- 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.qid = nil
|
||||||
citeq_args.wdl = nil
|
citeq_args.wdl = nil
|
||||||
citeq_args.template = 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
|
-- if title supplied, flag to not read html title
|
||||||
local titleforced = (citeq_args.title ~= nil)
|
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
|
-- code to make a guess what template to use from the supplied parameters
|
||||||
-- (first draft for proof-of-concept)
|
-- (first draft for proof-of-concept)
|
||||||
local pubyear = yearFromDate(citeq_args['publication-date'])
|
local pubyear = yearFromDate(citeq_args['publication-date'])
|
||||||
local is_news = isNewsArticle(qid, citeq_args)
|
local guessedType = getTemplateFromInstance(qid, citeq_args)
|
||||||
if is_news then
|
if guessedType == 'news' then
|
||||||
template = template or "news"
|
template = template or "news"
|
||||||
-- some items use P123 (publisher) as the newspaper instead of P1433 (published in)
|
-- some items use P123 (publisher) as the newspaper instead of P1433 (published in)
|
||||||
if citeq_args.publisher and not citeq_args.journal then
|
if citeq_args.publisher and not citeq_args.journal then
|
||||||
citeq_args.journal = citeq_args.publisher
|
citeq_args.journal = citeq_args.publisher
|
||||||
citeq_args.publisher = nil
|
citeq_args.publisher = nil
|
||||||
end
|
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
|
elseif citeq_args.journal then
|
||||||
template = template or "journal"
|
template = template or "journal"
|
||||||
citeq_args['publication-date'] = pubyear or citeq_args['publication-date']
|
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
|
-- remove any remaining non-CS1 fields
|
||||||
citeq_args['instance-of'] = nil
|
citeq_args['instance-of'] = nil
|
||||||
|
citeq_args.namestyle = nil
|
||||||
|
citeq_args['name-style'] = nil
|
||||||
|
|
||||||
-- render the template
|
-- render the template
|
||||||
local lc_template = template and template:lower() or "citation"
|
local lc_template = template and template:lower() or "citation"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue