diff --git a/module_citeq.lua b/module_citeq.lua index 985a482..dc20f64 100644 --- a/module_citeq.lua +++ b/module_citeq.lua @@ -80,31 +80,8 @@ 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, namestyle) +local function makelink(v, out, link, maxpos, wdl) local label if v.mainsnak.snaktype == "value" then if v.mainsnak.datatype == "wikibase-item" then @@ -112,20 +89,11 @@ local function makelink(v, out, link, maxpos, wdl, namestyle) local sitelink = mw.wikibase.getSitelink(qnumber) if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors" - -- 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}) + -- 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}) if first_names then - first_names = getStyledNames(first_names, namestyle) + first_names = mw.text.trim(first_names:gsub(', ', ' ')) end if v.qualifiers and v.qualifiers.P1932 then @@ -194,7 +162,7 @@ local function makelink(v, out, link, maxpos, wdl, namestyle) last_name = v.qualifiers.P9688[1].datavalue.value end if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then - first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle) + first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' ')) end if last_name and first_names then @@ -221,14 +189,16 @@ local function makelink(v, out, link, maxpos, wdl, namestyle) last_name = v.qualifiers.P9688[1].datavalue.value end if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then - first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle) + first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' ')) end if last_name and first_names then - label = { + out[position] = { 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 "") @@ -305,7 +275,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, args.namestyle) + maxpos = makelink(v, out, link, maxpos, wdl) end end end @@ -313,7 +283,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.namestyle) + maxpos = makelink(v, out, link, maxpos, wdl) end end end @@ -407,11 +377,8 @@ local function yearFromDate(datestr) return datestr end --- Attempt to determine heuristically what we're citing -local function getTemplateFromInstance(qid, args) - local chapteritems = { - 'Q1980247', -- chapter - } +-- Attempt to determine heuristically if we're citing a news article +local function isNewsArticle(qid, args) local newsitems = { 'Q5707594', -- news article 'Q30070590', -- magazine article @@ -430,9 +397,7 @@ local function getTemplateFromInstance(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 'news' - elseif args['instance-of'] and in_array(args['instance-of'], chapteritems) then - return 'chapter' + return true end -- otherwise check multiple non-deprecated values local instance_of = mw.wikibase.getBestStatements(qid, 'P31') @@ -440,14 +405,12 @@ local function getTemplateFromInstance(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 'news' - elseif in_array(v.mainsnak.datavalue.value.id, chapteritems) then - return 'chapter' + return true end end end end - return '' + return false end -- Figure out a URL access value, from the "online access status" (P6954) qualifier of an identifier @@ -538,7 +501,6 @@ 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) @@ -815,20 +777,14 @@ 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 guessedType = getTemplateFromInstance(qid, citeq_args) - if guessedType == 'news' then + local is_news = isNewsArticle(qid, citeq_args) + if is_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'] @@ -993,8 +949,6 @@ 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"