Initial support for chapters from P31 instance-of to Cite book

This commit is contained in:
Jonathan Harker 2026-09-24 14:31:00 +12:00
parent 2ed4bf2b7c
commit 1df4c6cb25

View file

@ -375,8 +375,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
@ -395,7 +398,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')
@ -403,12 +408,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
@ -775,14 +782,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']