Modulo:Cite Q: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
MRZQ (discussione | contributi)
created
 
aggiorno modulo
Riga 1:
require('Module:No globals')
local wdib = require('Module:WikidataIB')
local getValue = wdib._getValue
local getPropOfProp = wdib._getPropOfProp
local followQid = wdib._followQid
 
local simple_properties = { -- PXXX, is multiple?, linked?
publisher = {id="P123", maxvals=1},
oclc = {id="P243", maxvals=1},
place = {id="P291", maxvals=0,linked='no'},
doi = {id="P356", maxvals=1},
issue = {id="P433", maxvals=1, populate_from_journal = true},
pmid = {id="P698", maxvals=1},
arxiv = {id="P818", maxvals=1},
bibcode = {id="P819", maxvals=1},
jstor = {id="P888", maxvals=1},
mr = {id="P889", maxvals=1},
ssrn = {id="P893", maxvals=1},
pmc = {id="P932", maxvals=1},
lccn = {id="P1144", maxvals=1},
hdl = {id="P1184", maxvals=1},
ismn = {id="P1208", maxvals=1},
journal = {id="P1433", maxvals=1},
citeseerx = {id="P3784", maxvals=1},
osti = {id="P3894", maxvals=1},
biorxiv = {id="P3951", maxvals=1},
isbn = {id="P212", maxvals=1, populate_from_journal = true}, -- ISBN 13
issn = {id="P236", maxvals=1, populate_from_journal = true},
chapter = {id="P792", maxvals=1},
["date"] = {id="P577", maxvals=1, populate_from_journal = true},
series = {id="P179", maxvals=1, populate_from_journal = true},
volume = {id="P478", maxvals=1, populate_from_journal = true},
title = {id="P1476", maxvals=1},
url = {id="P953", maxvals=1}, -- full work available at
pages = {id="P304", maxvals=1, populate_from_journal = true},
translator = {id="P655", maxvals=0}, -- does **not** go to "others" section!
illustrator = {id="P110", maxvals=0, others=true}, -- goes to "others" section
composer = {id="P86", maxvals=0, others=true}, -- goes to "others" section
animator = {id="P6942", maxvals=0, others=true}, -- goes to "others" section
director = {id="P57", maxvals=0, others=true}, -- goes to "others" section
screenwriter = {id="P58", maxvals=0, others=true}, -- goes to "others" section
}
 
local citeq = {}
Line 161 ⟶ 202:
 
]]
 
local function wrap_nowiki(str)
return mw.text.nowiki(str or '')
end
 
function citeq.cite_q (frame)
local citeq_args = {};
local qid;
local wdl;
 
local for pframek, =v in pairs(frame:getParent().args) do
citeq_args[k] = v
local args = pframe.args; -- first get parent frame arguments - these from the template call
end
 
for k, v in pairs (frame.args) do -- copy named parameters and their values into citeq_args
citeq_args[k] = v
if type( k ) == 'string' then -- numbered parameters ignored
end
citeq_args[k] = v;
local qid = citeq_args.qid
local wdl = citeq_args.wdl
citeq_args.qid = nil
citeq_args.wdl = nil
local oth = {}
citeq_args.language = citeq_args.language or getPropOfProp( {qid = qid, prop1 = "P407", prop2 = "P218", ps = 1} )
if citeq_args.language == '' then
citeq_args.language = nil
end
if not citeq_args.language then
-- try fallback to journal's language
local journal_qid = followQid({qid = qid, props = "P1433"})
citeq_args.language = journal_qid and getPropOfProp( {qid = journal_qid, prop1 = "P407", prop2 = "P218", ps = 1} )
end
for name, data in pairs(simple_properties) do
citeq_args[name] = getValue( {data.id, ps = 1, qid = qid, maxvals=data.maxvals, linked=data.linked, citeq_args[name] } )
if data.populate_from_journal then
citeq_args[name] = getValue( {"P1433", ps = 1, qid = qid, maxvals=0, citeq_args[name], qual=data.id, qualsonly='yes'} )
citeq_args[name] = citeq_args[name] or getPropOfProp({qid = qid, prop1 = "P1433", prop2 = data.id, maxvals=data.maxvals, ps = 1})
end
if citeq_args[name] and citeq_args[name]:find('[[Category:Articles with missing Wikidata information]]', 1, true) then
-- try fallback to work's native language
citeq_args[name] = getValue( {data.id, ps = 1, qid = qid, maxvals=data.maxvals, linked="no", lang = citeq_args.language } )
if citeq_args[name]:find('^Q%d+$') then -- qid was returned
-- try fallback to qid's native language
local qid_language = getPropOfProp( {qid = citeq_args[name], prop1 = "P407", prop2 = "P218", ps = 1} )
citeq_args[name] = getValue( {data.id, ps = 1, qid = qid, maxvals=data.maxvals, linked="no", lang = qid_language } )
if citeq_args[name]:find('^Q%d+$') then -- qid was returned again
citeq_args[name] = nil
end
end
end
if data.others then
oth[#oth+1] = citeq_args[name] and (name:gsub("^%l", string.upper) .. ": " .. citeq_args[name])
citeq_args[name] = nil
end
end
citeq_args.others = citeq_args.others or table.concat(oth, ". ")
 
citeq_args.journal = citeq_args.journal and citeq_args.journal:gsub("^''", ""):gsub("''$", ""):gsub("|''", "|"):gsub("'']]", "]]")
 
citeq_args.ol = (getValue( {"P648", ps = 1, qid = qid, maxvals=1, citeq_args.ol } ) or ''):gsub("^OL(.+)$", "%1")
citeq_args.biorxiv = citeq_args.biorxiv and ("10.1101/" .. citeq_args.biorxiv)
 
citeq_args.isbn = getValue( {"P957", ps = 1, qid = qid, maxvals=0, citeq_args.isbn } ) -- try ISBN 10
citeq_args.url = getValue( {"P856", ps = 1, qid = qid, maxvals=0, citeq_args.url } ) -- try official website
args = frame.args; -- now get frame arguments (from the template wikisource)
citeq_args.url = getValue( {"P2699", ps = 1, qid = qid, maxvals=0, citeq_args.url } ) -- try url
 
local slink = mw.wikibase.getSitelink(qid)
for k, v in pairs (args) do -- copy args into citeq_args
local label = mw.wikibase.getLabel(qid) or citeq_args.language and mw.wikibase.getLabelByLang(qid, citeq_args.language)
if 'qid' == k then -- don't copy qid
if citeq_args.title then
qid = v; -- save its value
if slink then
elseif 'wdlinks' == k then -- don't copy wdlinks
citeq_args.url = nil
wdl = v; -- save its value
citeq_args.title = '[[' .. slink .. '|' .. wrap_nowiki(citeq_args.title) .. ']]'
else
citeq_args.title = wrap_nowiki(citeq_args.title)
citeq_args[k] = v -- but copy everything else
end
else
if slink then
citeq_args.url = nil
if slink:lower() == label:lower() then
citeq_args.title = '[[' .. slink .. ']]'
else
citeq_args.title = '[[' .. slink .. '|' .. wrap_nowiki(slink:gsub("%s%(.+%)$", ""):gsub(",.+$", "")) .. ']]'
end
else
citeq_args.title = wrap_nowiki(label)
end
end
 
if citeq_args.p or citeq_args.page then
citeq_args.pages = nil
end
if citeq_args.pages then
local _, count = string.gsub(citeq_args.pages, " %d+", "")
if count == 1 then
citeq_args.p = citeq_args.pages
citeq_args.pages = nil
end
end
 
for k, v in pairs(citeq_args) do
if v == 'unset' or type(k) ~= 'string' then
citeq_args[k] = nil
end
end
Line 196 ⟶ 317:
get_name_list ('editor', citeq_args, qid, wdl); -- modify citeq_args table with editors from wikidata
end
end
local author_count = 0
for k, v in pairs(citeq_args) do
if k:find("^author%d+$") then
author_count = author_count + 1
end
end
if author_count > 8 then -- convention in astronomy journals
citeq_args['display-authors'] = citeq_args['display-authors'] or 3
end
local editor_count = 0
for k, v in pairs(citeq_args) do
if k:find("^editor%d+$") then
editor_count = editor_count + 1
end
end
if editor_count > 8 then -- convention in astronomy journals
citeq_args['display-editors'] = citeq_args['display-editors'] or 3
end
 
returnlocal returntext = frame:expandTemplate{title = 'citation', args = citeq_args}; -- render the citationtemplate
if citeq_args.mode ~= 'cs1' then
returntext = returntext .. ','
end
returntext = returntext .. ' [[Wikidata]] [[:d:' .. qid .. '|' .. qid .. ']]'
return returntext
end