-- Version: 2021-10-19  
local p = {}  
require('strict')
local wdib = require('Module:WikidataIB')
local getValue = wdib._getValue
local getPropOfProp = wdib._getPropOfProp
local followQid = wdib._followQid
local getPropertyIDs = wdib._getPropertyIDs  
local i18n = {
["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper),
["unknown-author-trackingcat"] = "",
["ordinal"] = {
[1] = "st",
[2] = "nd",
[3] = "rd",
["default"] = "th"
},
["months"] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
},
}  
---
-- makeOrdinal needs to be internationalised along with the above i18n
-- takes cardinal number as a numeric and returns the ordinal as a string
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
---
p.makeOrdinal = function(cardinal)
local card = tonumber(cardinal)
if not card then return cardinal end
local ordsuffix = i18n.ordinal.default
if card % 10 == 1 then
ordsuffix = i18n.ordinal[1]
elseif card % 10 == 2 then
ordsuffix = i18n.ordinal[2]
elseif card % 10 == 3 then
ordsuffix = i18n.ordinal[3]
end
-- In English, 1, 21, 31, etc. use 'st', but 11, 111, etc. use 'th'
-- similarly for 12 and 13, etc.
if (card % 100 == 11) or (card % 100 == 12) or (card % 100 == 13) then
ordsuffix = i18n.ordinal.default
end
return card .. ordsuffix
end  
-- Table of simple properties that can be fetched in roughly the same way:
-- id = PXXX
-- maxvals = maximum number of multiple values (0 for all)
-- linked = "no" suppresses linking
-- populate_from_journal = true/false determines whether to look in a journal where the source is published
-- rank = "best", "preferred", normal, etc. determines how Wikidata ranks are treated
-- others = true - the value for the property goes to "others" section
local simple_properties = {
publisher = {id = "P123", maxvals = 1},
oclc = {id = "P243", maxvals = 1},
['publication-place'] = {id = "P291", maxvals = 0, linked = 'no'}, -- publication place (don't put into |place=; is treated specially in if both are given)
doi = {id = "P356", maxvals = 1}, -- take care of |doi-broken-date= (WD "reason for deprecation"/"stated as") and |doi-access= (WD "access status")?
issue = {id = "P433", maxvals = 0, populate_from_journal = true}, -- distinguish from |number= ("P1545"?) if both are given (still blocked by , but will be supported in the future)
pmid = {id = "P698", maxvals = 1},
-- gbooks = {id = "P675", maxvals = 1}, -- to be added to
-- ia = {id = "P724", maxvals = 1}, -- to be added to
arxiv = {id = "P818", maxvals = 1},
bibcode = {id = "P819", maxvals = 1}, -- take care of |bibcode-access=?
jstor = {id = "P888", maxvals = 1}, -- take care of |jstor-access=?
mr = {id = "P889", maxvals = 1},
rfc = {id = "P892", maxvals = 1},
zbl = {id = "P894", maxvals = 1},
ssrn = {id = "P893", maxvals = 1},
place = {id = "P1071", maxvals = 0, linked = 'no'}, -- written-at place
-- ['total-pages'] = {id = "P1104", maxvals = 0, linked = 'no'}, -- to be added to / COinS &rft.tpages=
-- coden = {id = "P1159", maxvals = 1}, -- to be added to / COinS &rft.coden=
s2cid = {id = "P8299", maxvals = 1}, -- take care of |s2cid-access=?
pmc = {id = "P932", maxvals = 1}, -- take care of |pmc-embargo-date= (WD "reason for deprecation")?
lccn = {id = "P1144", maxvals = 1},
hdl = {id = "P1184", maxvals = 1}, -- take care of |hdl-access=?
ismn = {id = "P1208", maxvals = 1},
journal = {id = "P1433", maxvals = 1},
citeseerx = {id = "P3784", maxvals = 1},
osti = {id = "P3894", maxvals = 1}, -- take care of |osti-access=?
biorxiv = {id = "P3951", maxvals = 1},
asin = {id = "P5749", maxvals = 1}, -- What about |asin-tld=? (WD examples resolve to .com at present, but may change)
-- ['catalog-number'] = {id = "P528", maxvals = 0}, -- to be added to / COinS &rft.artnum=
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?
-- 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
chapter = {id = "P792", maxvals = 1},
['publication-date'] = {id = "P577", maxvals = 1, populate_from_journal = true}, -- publication date (don't use |date=; is treated specially in if both are given.)
series = {id = "P179", maxvals = 1, populate_from_journal = true},
version = {id = "P348", maxvals = 0},
edition = {id = "P393", maxvals = 0},
volume = {id = "P478", maxvals = 0, populate_from_journal = true},
-- part = {id = "P1545"?, maxvals = 0}, -- to be added to / COinS &rft.part=
title = {id = "P1476", rank="p n"},
-- url = {id = "P953", maxvals = 1}, -- deal with this along with archive-url
pages = {id = "P304", maxvals = 0, populate_from_journal = true},
at = {id = "P958", maxvals = 0, populate_from_journal = true}, -- also incorporate lines (P7421) and columns (P3903) into this (cite map also supports |section=)
-- sheets = {id = "P7416", maxvals = 0, populate_from_journal = true},
-- interviewer = {id = "P?", maxvals = 0}, -- does **not** go to "others" section! Multiple interviewers should be n-enumerated
illustrator = {id = "P110", maxvals = 10, others = true}, -- goes to "others" section
-- foreword and afterword, when contributions to another author's work, are contributions so belong in |contribution=;
-- the writer's name goes in |contributor=; requires |title= and |author=
-- However, this might need to add support for multiple contributors and their roles to , see Help_talk:Citation_Style_1#Others
-- foreword = {id = "P2679", maxvals = 10, others = true}, -- goes to "others" section
-- afterword = {id = "P2680", maxvals = 10, others = true}, -- goes to "others" section
composer = {id = "P86", maxvals = 10, others = true}, -- goes to "others" section
animator = {id = "P6942", maxvals = 10, others = true}, -- goes to "others" section
director = {id = "P57", maxvals = 10, others = true}, -- goes to "others" section
screenwriter = {id = "P58", maxvals = 10, others = true}, -- goes to "others" section
signatory = {id = "P1891", maxvals = 10, others = true}, -- goes to "others" section
presenter = {id = "P371", maxvals = 10, others = true}, -- goes to "others" section
performer = {id = "P175", maxvals = 10, others = true}, -- goes to "others" section
}  
----------------------------< I S _ S E T >--------------------------------------------------------------
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.  
local function is_set( var )
return not (var == nil or var == '')
end  
----------------------------< I N _ A R R A Y >--------------------------------------------------------------
Whether needle is in haystack (taken from Module:Citation/CS1/Utilities)  
local function in_array( needle, haystack )
if needle == nil then
return false
end
for n, v in ipairs( haystack ) do
if v == needle then
return n
end
end
return false
end  
----------------------------< A C C E P T _ V A L U E >-------------------------------------------------------
Accept WD value by framing in ((...)) if param_val is equal to keyword; else pass-through WD value as is.  
local function accept_value( param_val, wd_val )
local val = param_val  
if val then
if in_array (val, {'accept', '))((', ':d:'}) then
val = '((' .. wd_val .. '))'
elseif '((accept))' == val then
val = 'accept'
elseif '()()' == val then
val = '))(('
elseif '((:d:))' == val then
val = ':d:'
else
val = wd_val
end
end  
return val
end  
-- function to fetch a value to display
local function makelink(v, out, link, maxpos, wdl)
local label
if v.mainsnak.snaktype == "value" then
if v.mainsnak.datatype == "wikibase-item" then
local qnumber = v.mainsnak.datavalue.value.id
local sitelink = mw.wikibase.getSitelink(qnumber)