「モジュール:Wd」の版間の差分

en:Module:Wd 2017年9月9日 (土) 16:25(UTC)より
template>K-iczn
(en:Module:Wd 2017年9月5日 (火) 20:13(UTC)より)
template>K-iczn
(en:Module:Wd 2017年9月9日 (土) 16:25(UTC)より)
18行目: 18行目:
end
end


local aliasesP = {
p.commands = {
property  = "property",
properties = "properties",
qualifier  = "qualifier",
qualifiers = "qualifiers",
reference  = "reference",
references = "references",
label      = "label",
title      = "title"
}
 
p.flags = {
linked        = "linked",
short        = "short",
raw          = "raw",
multilanguage = "multilanguage",
unit          = "unit",
-------------
preferred    = "preferred",
normal        = "normal",
deprecated    = "deprecated",
best          = "best",
future        = "future",
current      = "current",
former        = "former",
edit          = "edit",
editAtEnd    = "edit@end",
mdy          = "mdy",
single        = "single",
sourced      = "sourced",
}
 
p.args = {
eid = "eid"
}
 
p.aliasesP = {
coord          = "P625",
coord          = "P625",
---------------
---------------
42行目: 78行目:
local aliasesQ = {
local aliasesQ = {
percentage              = "Q11229",
percentage              = "Q11229",
prolepticJulianCalendar = "Q1985786"
prolepticJulianCalendar = "Q1985786",
}
citeWeb                = "Q5637226",
 
citeQ                  = "Q22321052"
local flags = {
linked        = "linked",
raw          = "raw",
short        = "short",
multilanguage = "multilanguage",
unit          = "unit",
mdy          = "mdy",
single        = "single",
sourced      = "sourced",
edit          = "edit",
editAtEnd    = "edit@end",
best          = "best",
preferred    = "preferred",
normal        = "normal",
deprecated    = "deprecated",
future        = "future",
current      = "current",
former        = "former"
}
}


178行目: 196行目:


function replaceAlias(ID)
function replaceAlias(ID)
if aliasesP[ID] then
if p.aliasesP[ID] then
ID = aliasesP[ID]
ID = p.aliasesP[ID]
end
end
281行目: 299行目:
return a1
return a1
end
function split(str, del)
local out = {}
local i, j = str:find(del)
if i and j then
out[1] = str:sub(1, i - 1)
out[2] = str:sub(j + 1)
else
out[1] = str
end
return out
end
end


function parseWikidataURL(url)
function parseWikidataURL(url)
local i, j
local ID
if url:match('^http[s]?://') then
if url:match('^http[s]?://') then
i, j = url:find("Q")
ID = split(url, "Q")
if i then
if ID[2] then
return url:sub(i)
return "Q" .. ID[2]
end
end
end
end
580行目: 612行目:
-- if ID == nil then item connected to current page is used
-- if ID == nil then item connected to current page is used
function getShortName(ID)
function getShortName(ID)
local args = {aliasesP.shortName}
local args = {p.aliasesP.shortName}
if ID then
if ID then
args["eid"] = ID
args[p.args.eid] = ID
end
end
1,232行目: 1,264行目:
local rankPos
local rankPos
if rank == flags.best then
if rank == p.flags.best then
self.bestRank = true
self.bestRank = true
self.flagBest = true  -- mark that 'best' flag was given
self.flagBest = true  -- mark that 'best' flag was given
1,238行目: 1,270行目:
end
end
if rank:sub(1,9) == flags.preferred then
if rank:sub(1,9) == p.flags.preferred then
rankPos = 1
rankPos = 1
elseif rank:sub(1,6) == flags.normal then
elseif rank:sub(1,6) == p.flags.normal then
rankPos = 2
rankPos = 2
elseif rank:sub(1,10) == flags.deprecated then
elseif rank:sub(1,10) == p.flags.deprecated then
rankPos = 3
rankPos = 3
else
else
1,271行目: 1,303行目:
local periodPos
local periodPos
if period == flags.future then
if period == p.flags.future then
periodPos = 1
periodPos = 1
elseif period == flags.current then
elseif period == p.flags.current then
periodPos = 2
periodPos = 2
elseif period == flags.former then
elseif period == p.flags.former then
periodPos = 3
periodPos = 3
else
else
1,297行目: 1,329行目:
end
end
if flag == flags.linked then
if flag == p.flags.linked then
self.curState.linked = true
self.curState.linked = true
return true
return true
elseif flag == flags.raw then
elseif flag == p.flags.raw then
self.curState.rawValue = true
self.curState.rawValue = true
1,309行目: 1,341行目:
return true
return true
elseif flag == flags.short then
elseif flag == p.flags.short then
self.curState.shortName = true
self.curState.shortName = true
return true
return true
elseif flag == flags.multilanguage then
elseif flag == p.flags.multilanguage then
self.curState.anyLanguage = true
self.curState.anyLanguage = true
return true
return true
elseif flag == flags.unit then
elseif flag == p.flags.unit then
self.curState.unitOnly = true
self.curState.unitOnly = true
return true
return true
elseif flag == flags.mdy then
elseif flag == p.flags.mdy then
self.mdyDate = true
self.mdyDate = true
return true
return true
elseif flag == flags.single then
elseif flag == p.flags.single then
self.singleClaim = true
self.singleClaim = true
return true
return true
elseif flag == flags.sourced then
elseif flag == p.flags.sourced then
self.sourcedOnly = true
self.sourcedOnly = true
return true
return true
elseif flag == flags.edit then
elseif flag == p.flags.edit then
self.editable = true
self.editable = true
return true
return true
elseif flag == flags.editAtEnd then
elseif flag == p.flags.editAtEnd then
self.editable = true
self.editable = true
self.editAtEnd = true
self.editAtEnd = true
return true
return true
elseif flag == flags.best or flag:match('^'..flags.preferred..'[+-]?$') or flag:match('^'..flags.normal..'[+-]?$') or flag:match('^'..flags.deprecated..'[+-]?$') then
elseif flag == p.flags.best or flag:match('^'..p.flags.preferred..'[+-]?$') or flag:match('^'..p.flags.normal..'[+-]?$') or flag:match('^'..p.flags.deprecated..'[+-]?$') then
self:setRank(flag)
self:setRank(flag)
return true
return true
elseif flag == flags.future or flag == flags.current or flag == flags.former then
elseif flag == p.flags.future or flag == p.flags.current or flag == p.flags.former then
self:setPeriod(flag)
self:setPeriod(flag)
return true
return true
1,357行目: 1,389行目:
end
end
if flag == "property" or flag == "properties" then
if flag == p.commands.property or flag == p.commands.properties then
param = parameters.property
param = parameters.property
elseif flag:match('^qualifier[s]?$') then
elseif flag == p.commands.qualifier or flag == p.commands.qualifiers then
self.states.qualifiersCount = self.states.qualifiersCount + 1
self.states.qualifiersCount = self.states.qualifiersCount + 1
param = parameters.qualifier .. self.states.qualifiersCount
param = parameters.qualifier .. self.states.qualifiersCount
self.separators["sep"..param] = {copyTable(defaultSeparators["sep%q\\d"])}
self.separators["sep"..param] = {copyTable(defaultSeparators["sep%q\\d"])}
elseif flag:match('^reference[s]?$') then
elseif flag == p.commands.reference or flag == p.commands.references then
param = parameters.reference
param = parameters.reference
else
else
1,434行目: 1,466行目:
local now = os.date('!*t')
local now = os.date('!*t')
startTime = self:getSingleRawQualifier(claim, aliasesP.startTime)
startTime = self:getSingleRawQualifier(claim, p.aliasesP.startTime)
if startTime and startTime ~= "" and startTime ~= " " then
if startTime and startTime ~= "" and startTime ~= " " then
startTimeY, startTimeM, startTimeD = parseDate(startTime)
startTimeY, startTimeM, startTimeD = parseDate(startTime)
end
end
endTime = self:getSingleRawQualifier(claim, aliasesP.endTime)
endTime = self:getSingleRawQualifier(claim, p.aliasesP.endTime)
if endTime and endTime ~= "" and endTime ~= " " then
if endTime and endTime ~= "" and endTime ~= " " then
endTimeY, endTimeM, endTimeD = parseDate(endTime)
endTimeY, endTimeM, endTimeD = parseDate(endTime)
1,670行目: 1,702行目:
-- logic determined based on https://www.wikidata.org/wiki/Help:Sources
-- logic determined based on https://www.wikidata.org/wiki/Help:Sources
function State:getReference(statement)
function State:getReference(statement)
local language, referenceURL, title, statedIn, statedInRaw
local language, referenceURL, title, statedIn, statedInRaw, template
local authors = {}
local authors = {}
local params = {}
local params = {}
1,683行目: 1,715行目:
if statement.snaks then
if statement.snaks then
-- don't include "imported from", which has been added by a bot
-- don't include "imported from", which has been added by a bot
if statement.snaks[aliasesP.importedFrom] then
if statement.snaks[p.aliasesP.importedFrom] then
statement.snaks[aliasesP.importedFrom] = nil
statement.snaks[p.aliasesP.importedFrom] = nil
end
end
-- not linked yet because we need the plain value for comparison first
-- not linked yet because we need the plain value for comparison first
language = self:getReferenceDetail(statement.snaks, aliasesP.language, false, false, false, true)  -- (noUnset = true)
language = self:getReferenceDetail(statement.snaks, p.aliasesP.language, false, false, false, true)  -- (noUnset = true)
if language then
if language then
-- not part of a "stated in"-source
-- not part of a "stated in"-source
1,697行目: 1,729行目:
if self.linked then
if self.linked then
-- retrieve language again, but this time with link
-- retrieve language again, but this time with link
params[aliasesP.language] = self:getReferenceDetail(statement.snaks, aliasesP.language, false, true)  -- link = true
params[p.aliasesP.language] = self:getReferenceDetail(statement.snaks, p.aliasesP.language, false, true)  -- link = true
else
else
params[aliasesP.language] = language
params[p.aliasesP.language] = language
end
end
end
end
-- we have to manually unset, since the first call to getReferenceDetail was with noUnset and the second call might not have happened
-- we have to manually unset, since the first call to getReferenceDetail was with noUnset and the second call might not have happened
statement.snaks[aliasesP.language] = nil
statement.snaks[p.aliasesP.language] = nil
end
end
authors = self:getReferenceDetails(statement.snaks, aliasesP.author, false, self.linked)  -- link = true/false
authors = self:getReferenceDetails(statement.snaks, p.aliasesP.author, false, self.linked)  -- link = true/false
if #authors > 0 then
if #authors > 0 then
-- not part of a "stated in"-source
-- not part of a "stated in"-source
1,713行目: 1,745行目:
end
end
referenceURL = self:getReferenceDetail(statement.snaks, aliasesP.referenceURL)
referenceURL = self:getReferenceDetail(statement.snaks, p.aliasesP.referenceURL)
if referenceURL then
if referenceURL then
-- not part of a "stated in"-source
-- not part of a "stated in"-source
1,720行目: 1,752行目:
-- the next two may be part of a "stated in"-source, so retrieve them already so that they won't count as hasExtraParams
-- the next two may be part of a "stated in"-source, so retrieve them already so that they won't count as hasExtraParams
title    = self:getReferenceDetail(statement.snaks, aliasesP.title, false, false, true)  -- anyLang = true
title    = self:getReferenceDetail(statement.snaks, p.aliasesP.title, false, false, true)  -- anyLang = true
statedIn = self:getReferenceDetail(statement.snaks, aliasesP.statedIn, false, true, false, true)  -- link = true, (noUnset = true)
statedIn = self:getReferenceDetail(statement.snaks, p.aliasesP.statedIn, false, true, false, true)  -- link = true, (noUnset = true)
-- get title of general template for citing web references
template = mw.wikibase.sitelink(aliasesQ.citeWeb) or ""
template = split(template, ":")[2]  -- split off namespace from front
-- (1) if both "reference URL" and "title" are present, then use the general template for citing web references
-- (1) if both "reference URL" and "title" are present, then use the general template for citing web references
if referenceURL and title and i18n['cite']['cite-web'] and i18n['cite']['cite-web'] ~= "" then
if referenceURL and title and template then
citeParams[i18n['cite']['url']]    = referenceURL
citeParams[i18n['cite']['url']]    = referenceURL
citeParams[i18n['cite']['title']]  = title
citeParams[i18n['cite']['title']]  = title
citeParams[i18n['cite']['website']] = statedIn
citeParams[i18n['cite']['website']] = statedIn
citeParams[i18n['cite']['language']] = params[aliasesP.language]
citeParams[i18n['cite']['language']] = params[p.aliasesP.language]
citeParams[i18n['cite']['date']]        = self:getReferenceDetail(statement.snaks, aliasesP.publicationDate)
citeParams[i18n['cite']['date']]        = self:getReferenceDetail(statement.snaks, p.aliasesP.publicationDate)
citeParams[i18n['cite']['access-date']]  = self:getReferenceDetail(statement.snaks, aliasesP.retrieved)
citeParams[i18n['cite']['access-date']]  = self:getReferenceDetail(statement.snaks, p.aliasesP.retrieved)
citeParams[i18n['cite']['archive-url']]  = self:getReferenceDetail(statement.snaks, aliasesP.archiveURL)
citeParams[i18n['cite']['archive-url']]  = self:getReferenceDetail(statement.snaks, p.aliasesP.archiveURL)
citeParams[i18n['cite']['archive-date']] = self:getReferenceDetail(statement.snaks, aliasesP.archiveDate)
citeParams[i18n['cite']['archive-date']] = self:getReferenceDetail(statement.snaks, p.aliasesP.archiveDate)
citeParams[i18n['cite']['publisher']]    = self:getReferenceDetail(statement.snaks, aliasesP.publisher, false, self.linked)  -- link = true/false
citeParams[i18n['cite']['publisher']]    = self:getReferenceDetail(statement.snaks, p.aliasesP.publisher, false, self.linked)  -- link = true/false
citeParams[i18n['cite']['quote']]        = self:getReferenceDetail(statement.snaks, aliasesP.quote, false, false, true)  -- anyLang = true
citeParams[i18n['cite']['quote']]        = self:getReferenceDetail(statement.snaks, p.aliasesP.quote, false, false, true)  -- anyLang = true
for i, v in ipairs(authors) do
for i, v in ipairs(authors) do
1,748行目: 1,784行目:
end
end
value = "{{" .. i18n['cite']['cite-web'] .. value .. "}}"
value = "{{" .. template .. value .. "}}"
else
else
value = mw.getCurrentFrame():expandTemplate{title=i18n['cite']['cite-web'], args=citeParams}
value = mw.getCurrentFrame():expandTemplate{title=template, args=citeParams}
end
end
else
else
-- we need the raw Q-identifier for the next template
-- we need the raw Q-identifier for the next template
statedInRaw = self:getReferenceDetail(statement.snaks, aliasesP.statedIn, true)  -- raw = true
statedInRaw = self:getReferenceDetail(statement.snaks, p.aliasesP.statedIn, true)  -- raw = true
-- the next three may be part of a "stated in"-source, so retrieve them already so that they won't count as hasExtraParams
-- the next three may be part of a "stated in"-source, so retrieve them already so that they won't count as hasExtraParams
params[aliasesP.pages]          = self:getReferenceDetail(statement.snaks, aliasesP.pages)
params[p.aliasesP.pages]          = self:getReferenceDetail(statement.snaks, p.aliasesP.pages)
params[aliasesP.chapter]        = self:getReferenceDetail(statement.snaks, aliasesP.chapter)
params[p.aliasesP.chapter]        = self:getReferenceDetail(statement.snaks, p.aliasesP.chapter)
params[aliasesP.publicationDate] = self:getReferenceDetail(statement.snaks, aliasesP.publicationDate)
params[p.aliasesP.publicationDate] = self:getReferenceDetail(statement.snaks, p.aliasesP.publicationDate)
-- retrieve the rest of the parameters and make them count as hasExtraParams
-- retrieve the rest of the parameters and make them count as hasExtraParams
1,767行目: 1,803行目:
end
end
-- (2) if "stated in" is present without any parameters not belonging to a "stated in"-source, then use this template which expands the stated-in item
-- get title of template that expands a given stated-in item
if statedInRaw and not hasExtraParams and i18n['cite']['cite-q'] and i18n['cite']['cite-q'] ~= "" then
template = mw.wikibase.sitelink(aliasesQ.citeQ) or ""
citeParams[i18n['cite']['pages']]  = params[aliasesP.pages]
template = split(template, ":")[2]  -- split off namespace from front
citeParams[i18n['cite']['chapter']] = params[aliasesP.chapter]
citeParams[i18n['cite']['date']]   = params[aliasesP.publicationDate]
-- (2) if "stated in" is present without any parameters not belonging to a "stated in"-source, then use the template that expands the stated-in item
if statedInRaw and not hasExtraParams and template then
citeParams[i18n['cite']['pages']]  = params[p.aliasesP.pages]
citeParams[i18n['cite']['chapter']] = params[p.aliasesP.chapter]
citeParams[i18n['cite']['date-q']] = params[p.aliasesP.publicationDate]
if mw.isSubsting() then
if mw.isSubsting() then
1,777行目: 1,817行目:
value = value .. "|" .. i .. "=" .. v
value = value .. "|" .. i .. "=" .. v
end
end
value = "{{" .. i18n['cite']['cite-q'] .. "|" .. statedInRaw .. value .. "}}"
value = "{{" .. template .. "|" .. statedInRaw .. value .. "}}"
else
else
citeParams[1] = statedInRaw
citeParams[1] = statedInRaw
value = mw.getCurrentFrame():expandTemplate{title=i18n['cite']['cite-q'], args=citeParams}
value = mw.getCurrentFrame():expandTemplate{title=template, args=citeParams}
end
end
-- (3) else, do some default rendering of name-value pairs, but only if at least "stated in" or "reference URL" is present
-- (3) else, do some default rendering of name-value pairs, but only if at least "stated in" or "reference URL" is present
1,997行目: 2,037行目:
function p._property(args)
function p._property(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "property")
return execCommand(args, p.commands.property)
end
end


2,007行目: 2,047行目:
function p._properties(args)
function p._properties(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "properties")
return execCommand(args, p.commands.properties)
end
end


2,017行目: 2,057行目:
function p._qualifier(args)
function p._qualifier(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "qualifier")
return execCommand(args, p.commands.qualifier)
end
end


2,027行目: 2,067行目:
function p._qualifiers(args)
function p._qualifiers(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "qualifiers")
return execCommand(args, p.commands.qualifiers)
end
end


2,037行目: 2,077行目:
function p._reference(args)
function p._reference(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "reference")
return execCommand(args, p.commands.reference)
end
end


2,047行目: 2,087行目:
function p._references(args)
function p._references(args)
loadSubmodules()
loadSubmodules()
return execCommand(args, "references")
return execCommand(args, p.commands.references)
end
end


2,085行目: 2,125行目:
else
else
-- no positional entity ID was given, so get entity ID from 'eid' if it was given
-- no positional entity ID was given, so get entity ID from 'eid' if it was given
if args["eid"] then
if args[p.args.eid] then
if args["eid"]:sub(1,1):upper() == "Q" then
if args[p.args.eid]:sub(1,1):upper() == "Q" then
_.entityID = args["eid"]:upper()   
_.entityID = args[p.args.eid]:upper()   
elseif args["eid"]:sub(1,9):lower() == "property:" then
elseif args[p.args.eid]:sub(1,9):lower() == "property:" then
_.entityID = replaceAlias(mw.text.trim(args["eid"]:sub(10))):upper()
_.entityID = replaceAlias(mw.text.trim(args[p.args.eid]:sub(10))):upper()
else
else
-- might be entity ID of a property, or a bogus entity ID that will fail later on
-- might be entity ID of a property, or a bogus entity ID that will fail later on
_.entityID = replaceAlias(args["eid"]):upper()
_.entityID = replaceAlias(args[p.args.eid]):upper()
if _.entityID == "" then
if _.entityID == "" then
2,237行目: 2,277行目:
-- must come AFTER defining the hooks
-- must come AFTER defining the hooks
if _.sourcedOnly and not _.states[parameters.reference] then
if _.sourcedOnly and not _.states[parameters.reference] then
_:processFlagOrCommand("reference")  -- use singular "reference" to minimize overhead
_:processFlagOrCommand(p.commands.reference)  -- use singular "reference" to minimize overhead
end
end
2,250行目: 2,290行目:
i = tostring(i)
i = tostring(i)
if i:match('^[Pp]%d+$') or aliasesP[i] then
if i:match('^[Pp]%d+$') or p.aliasesP[i] then
v = replaceSpecialChars(v)
v = replaceSpecialChars(v)
2,314行目: 2,354行目:
if ID == "" then
if ID == "" then
-- no positional entity ID was given, so get entity ID from 'eid' if it was given
-- no positional entity ID was given, so get entity ID from 'eid' if it was given
if args["eid"] then
if args[p.args.eid] then
if args["eid"]:sub(1,1):upper() == "Q" then
if args[p.args.eid]:sub(1,1):upper() == "Q" then
ID = args["eid"]:upper()   
ID = args[p.args.eid]:upper()   
elseif args["eid"]:sub(1,9):lower() == "property:" then
elseif args[p.args.eid]:sub(1,9):lower() == "property:" then
ID = replaceAlias(mw.text.trim(args["eid"]:sub(10))):upper()
ID = replaceAlias(mw.text.trim(args[p.args.eid]:sub(10))):upper()
else
else
-- might be entity ID of a property, or a bogus entity ID that will fail later on
-- might be entity ID of a property, or a bogus entity ID that will fail later on
ID = replaceAlias(args["eid"]):upper()
ID = replaceAlias(args[p.args.eid]):upper()
if ID == "" then
if ID == "" then
匿名利用者