diff --git a/module_citeq.lua b/module_citeq.lua index 959332a..1b8d366 100644 --- a/module_citeq.lua +++ b/module_citeq.lua @@ -375,8 +375,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 @@ -395,7 +398,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') @@ -403,12 +408,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 @@ -775,14 +782,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']