Module:Sandbox/RexxS/Authors
--[[
Fetch authors from Wikidata
--]]
p = {}
function p.getAuthors(frame)
local args= frame.args
if not args.qid and not args[1] then
args = frame:getParent().args
end
local qid = mw.text.trim(args[1] or args.qid or "")
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return end
-- construct a table of tables:
-- key = series ordinal;
-- value = {name=author's name, orcid=author's orcid id, emp=author's employer}
local auths = {}
-- series ordinal = true when used
local ords = {}
-- list of employers
local emps = {}
-- keep track of values without series ordinals and max ordinal
local noord = 0
local maxord = 0
-- get authors that have entries
local prop50 = mw.wikibase.getBestStatements(qid, "P50")
for i, v in ipairs(prop50) do
if v.mainsnak.snaktype == "value" then
-- get author's qid
local nameid = v.mainsnak.datavalue.value.id
-- get author's name
local name = mw.wikibase.getLabel(nameid) or "No label"
-- get author's orcid id
local orcid
local orcidtbl = mw.wikibase.getBestStatements(nameid, "P496")[1]
if orcidtbl and orcidtbl.mainsnak.snaktype == "value" then
orcid = orcidtbl.mainsnak.datavalue.value
end
-- get author's employer
local emp
local emptbl = mw.wikibase.getBestStatements(nameid, "P108")[1]
if emptbl and emptbl.mainsnak.snaktype == "value" then
emp = mw.wikibase.getLabel(emptbl.mainsnak.datavalue.value.id)
end
if emp then emps[emp] = true end
-- get ordinal
local ordinal = noord
if v.qualifiers then
qualP1545 = v.qualifiers["P1545"][1]
if qualP1545 then
ordinal = tonumber(qualP1545.datavalue.value) or noord
end
end
if ordinal == noord then noord = noord -1 end
-- check for a duplicate ordinal
if ords[ordinal] then
repeat ordinal = ordinal + 1 until not ords[ordinal]
end
auths[ordinal] = {name = name, orcid = orcid, emp = emp}
ords[ordinal] = true
if ordinal > maxord then maxord = ordinal end
end
end
-- add author's name strings to the author's table
local propP2093 = mw.wikibase.getBestStatements(qid, "P2093")
for i, v in ipairs(propP2093) do
if v.mainsnak.snaktype == "value" then
local name = v.mainsnak.datavalue.value
local ordinal = noord
if v.qualifiers then
qualP1545 = v.qualifiers["P1545"][1]
if qualP1545 then
ordinal = tonumber(qualP1545.datavalue.value) or noord
end
end
if ordinal == noord then noord = noord -1 end
-- check for a duplicate ordinal
if ords[ordinal] then
repeat ordinal = ordinal + 1 until not ords[ordinal]
end
auths[ordinal] = {name = name}
ords[ordinal] = true
if ordinal > maxord then maxord = ordinal end
end
end
-- compact the auths table into a sequence
local authors = {}
for idx = 1, maxord do
if auths[idx] then table.insert(authors, auths[idx]) end
end
for idx = 0, noord, -1 do
if auths[idx] then table.insert(authors, auths[idx]) end
end
-- create a sequence of employers
local employers = {}
for k, v in pairs(emps) do
table.insert(employers, k)
end
-- construct some output
out = {}
for i, v in ipairs(authors) do
out[i] = v.name
if v.orcid then
out[i] = out[i] .. " [https://orcid.org/" .. v.orcid .. " ORCIDicon]"
end
if v.emp then
out[i] = out[i] .. " <sup>" .. v.emp .. "</sup>"
end
end
-- glue the list of authors together as a comma separated list
local returntxt = "<p>" .. table.concat(out, ", ")
-- add a list of employers
returntxt = returntxt .. "</p><p>" .. table.concat(employers, ", ") .. "</p>"
return returntxt
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.