diff --git a/module_citeq.lua b/module_citeq.lua index 49d5110..21d07d7 100644 --- a/module_citeq.lua +++ b/module_citeq.lua @@ -88,7 +88,7 @@ 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" if v.qualifiers and v.qualifiers.P1932 then - label = v.qualifiers.P1932[1].datavalue.value + label = v.qualifiers.P1932[1].datavalue.value -- object named as else label = mw.wikibase.getLabel(qnumber) if label then @@ -293,6 +293,50 @@ local function wrap_nowiki(str, disable) return mw.text.nowiki(str or '') end +-- returns the first string sequence of 3 or more digits in a date string, or nil +local function yearFromDate(datestr) + if datestr then + return string.match(datestr, '%d%d%d+') + end + return datestr +end + +-- Attempt to determine heuristically if we're citing a news article +local function isNewsArticle(qid, args) + local newsitems = { + 'Q5707594', -- news article + 'Q30070590', -- magazine article + -- TODO: somehow use WHERE {?item (wdt:P31|wdt:P31/wdt:P279*) wd:Q5707594.} + -- main subclasses of news article: + 'Q87849013', -- AP article + 'Q2144639', -- feature story + 'Q139172839', -- top story + 'Q18339884', -- cover story + 'Q3326038', -- critical review + 'Q4202018', -- interview + 'Q309481', -- obituary + 'Q108970988', -- column + 'Q2602337', -- op-ed + 'Q59908', -- opinion piece + } + -- 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 + end + -- otherwise check multiple non-deprecated values + local instance_of = mw.wikibase.getBestStatements(qid, 'P31') + if instance_of and instance_of[1] then + 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 + end + end + end + end + return false +end + -- sort sequence table whose values are key-value pairs by key local function comp_key(a, b) local param_a, enum_a = a[1]:match ('(%D+)(%d+)$'); -- get param name and enumerator from @@ -594,13 +638,28 @@ 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) - if citeq_args.isbn then - template = template or "book" - citeq_args.asin = nil -- suppress ASIN if ISBN exists + local pubyear = yearFromDate(citeq_args['publication-date']) + 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 citeq_args.journal then template = template or "journal" + citeq_args['publication-date'] = pubyear or citeq_args['publication-date'] + -- articles can be published in books/proceedings, otherwise suppress publisher + if not citeq_args.isbn then + citeq_args.publisher = nil + end elseif is_thesis then template = template or "thesis" + elseif citeq_args.isbn then + template = template or "book" + citeq_args.asin = nil -- suppress ASIN if ISBN exists + citeq_args['publication-date'] = pubyear or citeq_args['publication-date'] elseif citeq_args.website then template = template or "web" end @@ -709,6 +768,9 @@ local function _cite_q (citeq_args) -- Disable CiteSeerX IDs for now citeq_args.citeseerx = nil + -- remove any remaining non-CS1 fields + citeq_args['instance-of'] = nil + -- render the template local lc_template = template and template:lower() or "citation" local output diff --git a/module_citeq_config.lua b/module_citeq_config.lua index 70d2667..8a53038 100644 --- a/module_citeq_config.lua +++ b/module_citeq_config.lua @@ -71,6 +71,7 @@ local simple_properties_t = { hdl = {id = "P1184", maxvals = 1}, -- take care of |hdl-access=? ismn = {id = "P1208", maxvals = 1}, journal = {id = "P1433", maxvals = 1}, + ['instance-of'] = {id = "P31", qidonly=true, maxvals = 5}, -- transient, non-CS1 thesis_degree = {id = "P13338", maxvals = 1}, -- transient, non-CS1 thesis_submitted_to = {id = "P4101", maxvals = 1}, -- transient, non-CS1 retracted_by = {id = "P5824", qidonly=true, maxvals = 1}, -- transient, non-CS1 @@ -82,7 +83,7 @@ local simple_properties_t = { asin = {id = "P5749", maxvals = 1}, -- What about |asin-tld=? (WD examples resolve to .com at present, but may change) ['article-number'] = {id = "P2322", maxvals = 1, populate_from_journal = true}, isbn = {id = "P212", maxvals = 1, populate_from_journal = true}, -- ISBN 13 - issn = {id = "P236", maxvals = 1, populate_from_journal = true}, -- distinguish from |eissn= for electronic issues? +-- issn = {id = "P236", maxvals = 1, populate_from_journal = true}, -- distinguish from |eissn= for electronic issues? -- jfm = {id = "P?", maxvals = 1}, -- Jahrbuch über die Fortschritte der Mathematik (not Zbl) -- sbn = {id = "P?", maxvals = 1}, -- Standard Book Number (predecessor of ISBN, not ICCU) -- message-id = {id = "P?", maxvals = 1}, -- Usenet message ID @@ -120,4 +121,4 @@ local simple_properties_t = { return { i18n_t = i18n_t, simple_properties_t = simple_properties_t, - } +}