Open main menu

Changes

Module:Wd

797 bytes added, 00:15, 12 September 2020
Avoid Lua errors should Wikidata not be available (tested via sandbox & testcases)
-- Original module located at [[:en:Module:Wd]], and [[:en:Module:Wd/i18n]] and [[:en:Module:Wd/aliasesP]].
local p = {}
local i18n
local function loadSubmodulesloadI18n(aliasesP, frame)
local title
end
if not i18n then i18n = i18n or require(title .. "/i18n") p.aliasesP = p.aliasesP or mw.loadDatainit(title .. "/aliasesP") end
end
page = "page",
date = "date"
}
 
local aliasesP = {
coord = "P625",
-----------------------
image = "P18",
author = "P50",
publisher = "P123",
importedFrom = "P143",
statedIn = "P248",
pages = "P304",
language = "P407",
hasPart = "P527",
publicationDate = "P577",
startTime = "P580",
endTime = "P582",
chapter = "P792",
retrieved = "P813",
referenceURL = "P854",
sectionVerseOrParagraph = "P958",
archiveURL = "P1065",
title = "P1476",
formatterURL = "P1630",
quote = "P1683",
shortName = "P1813",
definingFormula = "P2534",
archiveDate = "P2960",
inferredFrom = "P3452",
typeOfReference = "P3865",
column = "P3903"
}
local function replaceAlias(id)
if p.aliasesP[id] then id = p.aliasesP[id]
end
-- try short name first if requested
if short then
label = p._property{p.aliasesP.shortName, [p.args.eid] = id} -- get short name
if label == "" then
if not title then
if id:sub(1,1) == "Q" then
title = mw.wikibase.sitelinkgetSitelink(id)
elseif id:sub(1,1) == "P" then
-- properties have no sitelink, link to Wikidata instead
local attribute = nil
if (type == parameters.property or (type == parameters.qualifier and self.propertyID == p.aliasesP.hasPart)) and snak.property == p.aliasesP.definingFormula then
attribute = {qid = self.entityID}
end
return mw.getCurrentFrame():extensionTag("math", datavalue, attribute)
elseif subtype == 'external-id' and link then
local url = p._property{p.aliasesP.formatterURL, [p.args.eid] = snak.property} -- get formatter URL
if url ~= "" then
end
elseif datatype == 'monolingualtext' then
if anyLang then return datavalue['text'], datavalue['language'] elseif or datavalue['language'] == self.langCode then
return datavalue['text']
else
end
startTime = self:getSingleRawQualifier(claim, p.aliasesP.startTime)
if startTime and startTime ~= "" and startTime ~= " " then
startTimeY, startTimeM, startTimeD = parseDate(startTime)
end
endTime = self:getSingleRawQualifier(claim, p.aliasesP.endTime)
if endTime and endTime ~= "" and endTime ~= " " then
endTimeY, endTimeM, endTimeD = parseDate(endTime)
self.states[param].separator = self.separators["sep"..param] -- will be nil for param=="%p", which will be set separately
if flag:sub(-1) ~= 's' = p.claimCommands.property or flag == p.claimCommands.qualifier or flag == p.claimCommands.reference then
self.states[param].singleValue = true
end
local ref = {}
local version = 1 -- increase increment this each time the below logic is changed to avoid conflict errors
if statement.snaks then
-- don't include "imported from", which is added by a bot
if statement.snaks[p.aliasesP.importedFrom] then statement.snaks[p.aliasesP.importedFrom] = nil
end
-- don't include "inferred from", which is added by a bot
if statement.snaks[p.aliasesP.inferredFrom] then statement.snaks[p.aliasesP.inferredFrom] = nil
end
-- don't include "type of reference"
if statement.snaks[p.aliasesP.typeOfReference] then statement.snaks[p.aliasesP.typeOfReference] = nil
end
-- don't include "image" to prevent littering
if statement.snaks[p.aliasesP.image] then statement.snaks[p.aliasesP.image] = nil
end
-- don't include "language" if it is equal to the local one
if self:getReferenceDetail(statement.snaks, p.aliasesP.language) == self.conf.langName then statement.snaks[p.aliasesP.language] = nil
end
-- multiple authors may be given
if i == p.aliasesP.author then
params[i] = self:getReferenceDetails(statement.snaks, i, false, self.linked, true) -- link = true/false, anyLang = true
else
params[i] = {self:getReferenceDetail(statement.snaks, i, false, (self.linked or (i == p.aliasesP.statedIn)) and (statement.snaks[i][1].datatype ~= 'url'), true)} -- link = true/false, anyLang = true
end
-- get title of general template for citing web references
citeWeb = split(mw.wikibase.sitelinkgetSitelink(aliasesQ.citeWeb) or "", ":")[2] -- split off namespace from front
-- get title of template that expands stated-in references into citations
citeQ = split(mw.wikibase.sitelinkgetSitelink(aliasesQ.citeQ) or "", ":")[2] -- split off namespace from front
-- (1) use the general template for citing web references if there is a match and if at least both "reference URL" and "title" are present
if citeWeb and not citeMismatch['web'] and citeParams['web'][i18n['cite']['web'][p.aliasesP.referenceURL]] and citeParams['web'][i18n['cite']['web'][p.aliasesP.title]] then
useCite = citeWeb
useParams = citeParams['web']
-- (2) use the template that expands stated-in references into citations if there is a match and if at least "stated in" is present
elseif citeQ and not citeMismatch['q'] and citeParams['q'][i18n['cite']['q'][p.aliasesP.statedIn]] then
-- we need the raw "stated in" Q-identifier for the this template
citeParams['q'][i18n['cite']['q'][p.aliasesP.statedIn]] = self:getReferenceDetail(statement.snaks, p.aliasesP.statedIn, true) -- raw = true
useCite = citeQ
-- (3) else, do some default rendering of name-value pairs, but only if at least "stated in", "reference URL" or "title" is present
elseif params[p.aliasesP.statedIn] or params[p.aliasesP.referenceURL] or params[p.aliasesP.title] then
citeParams['default'] = {}
-- start by adding authors up front
if params[p.aliasesP.author] and #params[p.aliasesP.author] > 0 then citeParams['default'][#citeParams['default'] + 1] = table.concat(params[p.aliasesP.author], " & ")
end
-- combine "reference URL" and "title" into one link if both are present
if params[p.aliasesP.referenceURL] and params[p.aliasesP.title] then citeParams['default'][#citeParams['default'] + 1] = '[' .. params[p.aliasesP.referenceURL][1] .. ' "' .. params[p.aliasesP.title][1] .. '"]' elseif params[p.aliasesP.referenceURL] then citeParams['default'][#citeParams['default'] + 1] = params[p.aliasesP.referenceURL][1] elseif params[p.aliasesP.title] then citeParams['default'][#citeParams['default'] + 1] = '"' .. params[p.aliasesP.title][1] .. '"'
end
-- then add "stated in"
if params[p.aliasesP.statedIn] then citeParams['default'][#citeParams['default'] + 1] = "''" .. params[p.aliasesP.statedIn][1] .. "''"
end
-- remove previously added parameters so that they won't be added a second time
params[p.aliasesP.author] = nil params[p.aliasesP.referenceURL] = nil params[p.aliasesP.title] = nil params[p.aliasesP.statedIn] = nil
-- add the rest of the parameters
if value and self.linked then
if self.conf.entityID:sub(1,1) == "Q" then
title = mw.wikibase.sitelinkgetSitelink(self.conf.entityID)
elseif self.conf.entityID:sub(1,1) == "P" then
title = "d:Property:" .. self.conf.entityID
i = tostring(i)
if i:match('^[Pp]%d+$') or p.aliasesP[i] then
v = replaceSpecialChars(v)
if cfg.entityID:sub(1,1) == "Q" then
value = mw.wikibase.sitelinkgetSitelink(cfg.entityID)
end
end
elseif funcName == p.generalCommands.description then
cfg.entity value = mw.wikibase.getEntitygetDescription(cfg.entityID)  if cfg.entity.descriptions[cfg.langCode] then value = cfg.entity.descriptions[cfg.langCode].value end
else
local parsedFormat, formatParams
local args = copyTable(frame.args)
args.pointer = 1
loadSubmodulesloadI18n(aliasesP, frame)
return commandFunc(args, commandName)
end
args = copyTable(args)
args.pointer = 1
loadSubmodulesloadI18n(aliasesP)
return commandFunc(args, commandName)
end
-- main function that is supposed to be used by wrapper templates
function p.main(frame)
if not mw.wikibase then return nil end
 
local f, args
loadSubmodulesloadI18n(aliasesP, frame)
-- get the parent frame to take the arguments that were passed to the wrapper template
Anonymous user