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

en:Module:Wd 2017年12月4日 (月) 19:19(UTC)より
template>K-iczn
(en:Module:Wd 2017年10月22日 (日) 08:19(UTC)より)
template>K-iczn
(en:Module:Wd 2017年12月4日 (月) 19:19(UTC)より)
1行目: 1行目:
-- Original module located at [[:en:Module:Wd]] and [[:en:Module:Wd/i18n]].
-- Original module located at [[:en:Module:Wd]], [[:en:Module:Wd/i18n]] and [[:en:Module:Wd/aliasesP]].


local p = {}
local p = {}
local arg = ...
local arg = ...
local i18n
 
-- submodules to be loaded;
-- 'true' means: expose through table "p"
local submodules = {
i18n     = false,  -- internationalization
aliasesP = true    -- aliases for P-identifiers
}
 
-- non-exposed submodules will be stored in table "s"
local s = {}


function loadSubmodules(frame)
function loadSubmodules(frame)
-- internationalization
local title
if not i18n then
if frame then
if frame then
-- module invoked by page/template, get its title from frame
-- current module invoked by page/template, get its title from frame
i18n = require(frame:getTitle().."/i18n")
title = frame:getTitle()
else
-- current module included by other module, get its title from ...
title = arg
end
for i, v in pairs(submodules) do
if v then
if not p[i] then
p[i] = require(title.."/"..i)
end
else
else
-- module included by other module, get its title from ...
if not s[i] then
i18n = require(arg.."/i18n")
s[i] = require(title.."/"..i)
end
end
end
end
end
57行目: 77行目:
p.args = {
p.args = {
eid = "eid"
eid = "eid"
}
p.aliasesP = {
coord          = "P625",
---------------
author          = "P50",
publisher      = "P123",
importedFrom    = "P143",
statedIn        = "P248",
pages          = "P304",
publicationDate = "P577",
startTime      = "P580",
endTime        = "P582",
chapter        = "P792",
retrieved      = "P813",
referenceURL    = "P854",
archiveURL      = "P1065",
title          = "P1476",
quote          = "P1683",
shortName      = "P1813",
language        = "P2439",
archiveDate    = "P2960"
}
}


218行目: 216行目:


function errorText(code, param)  
function errorText(code, param)  
local text = i18n["errors"][code]
local text = s.i18n["errors"][code]
if param then text = mw.ustring.gsub(text, "$1", param) end
if param then text = mw.ustring.gsub(text, "$1", param) end
return text
return text
228行目: 226行目:


function replaceDecimalMark(num)
function replaceDecimalMark(num)
return mw.ustring.gsub(num, "[.]", i18n['numeric']['decimal-mark'], 1)
return mw.ustring.gsub(num, "[.]", s.i18n['numeric']['decimal-mark'], 1)
end
end


700行目: 698行目:
end
end
value = "[[File:Blue pencil.svg|frameless|text-top|10px|alt=" .. i18n['info']['edit-on-wikidata'] .. "|link=https://www.wikidata.org/wiki/" .. prefix .. self.entityID .. "?uselang=" .. self.langCode
value = "[[File:Blue pencil.svg|frameless|text-top|10px|alt=" .. s.i18n['info']['edit-on-wikidata'] .. "|link=https://www.wikidata.org/wiki/" .. prefix .. self.entityID .. "?uselang=" .. self.langCode
if self.propertyID then
if self.propertyID then
708行目: 706行目:
end
end
value = value .. "|" .. i18n['info']['edit-on-wikidata'] .. "]]"
value = value .. "|" .. s.i18n['info']['edit-on-wikidata'] .. "]]"
return front .. value .. back
return front .. value .. back
736行目: 734行目:
if not skip then
if not skip then
-- add <ref> tag with the reference's hash as its name (to deduplicate references)
-- add <ref> tag with the reference's hash as its name (to deduplicate references)
outString = outString .. mw.getCurrentFrame():extensionTag("ref", valuesArray[i][1], {name = "wikidata-" .. valuesArray[i].refHash})
outString = outString .. mw.getCurrentFrame():extensionTag("ref", valuesArray[i][1], {name = "wikidata-" .. valuesArray[i].refHash .. "-v" .. s.i18n['cite']['version']})
end
end
else
else
802行目: 800行目:
elseif subtype == 'math' and not raw then
elseif subtype == 'math' and not raw then
return mw.getCurrentFrame():extensionTag("math", datavalue)
return mw.getCurrentFrame():extensionTag("math", datavalue)
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
url = mw.ustring.gsub(url, "$1", datavalue)
return "[" .. url .. " " .. datavalue .. "]"
else
return datavalue
end
else
else
return datavalue
return datavalue
829行目: 836行目:
-- add delimiters for readability
-- add delimiters for readability
value = i18n.addDelimiters(value)
value = s.i18n.addDelimiters(value)
end
end
879行目: 886行目:
if not raw then
if not raw then
if precision == 6 then
if precision == 6 then
suffix = i18n['datetime']['suffixes']['millennium']
suffix = s.i18n['datetime']['suffixes']['millennium']
else
else
suffix = i18n['datetime']['suffixes']['century']
suffix = s.i18n['datetime']['suffixes']['century']
end
end
suffix = i18n.getOrdinalSuffix(yRound) .. suffix
suffix = s.i18n.getOrdinalSuffix(yRound) .. suffix
else
else
-- if not verbose, take the first year of the century/millennium
-- if not verbose, take the first year of the century/millennium
896行目: 903行目:
if not raw then
if not raw then
prefix = i18n['datetime']['prefixes']['decade-period']
prefix = s.i18n['datetime']['prefixes']['decade-period']
suffix = i18n['datetime']['suffixes']['decade-period']
suffix = s.i18n['datetime']['suffixes']['decade-period']
end
end
end
end
963行目: 970行目:
if not raw then
if not raw then
if precision == 3 then
if precision == 3 then
suffix = i18n['datetime']['suffixes']['million-years']
suffix = s.i18n['datetime']['suffixes']['million-years']
elseif precision == 0 then
elseif precision == 0 then
suffix = i18n['datetime']['suffixes']['billion-years']
suffix = s.i18n['datetime']['suffixes']['billion-years']
else
else
yRound = yRound * yFactor
yRound = yRound * yFactor
if yRound == 1 then
if yRound == 1 then
suffix = i18n['datetime']['suffixes']['year']
suffix = s.i18n['datetime']['suffixes']['year']
else
else
suffix = i18n['datetime']['suffixes']['years']
suffix = s.i18n['datetime']['suffixes']['years']
end
end
end
end
989行目: 996行目:
if not raw then
if not raw then
if link then
if link then
calendar = " ("..buildWikilink(i18n['datetime']['julian-calendar'], i18n['datetime']['julian'])..")"
calendar = " ("..buildWikilink(s.i18n['datetime']['julian-calendar'], s.i18n['datetime']['julian'])..")"
else
else
calendar = " ("..i18n['datetime']['julian']..")"
calendar = " ("..s.i18n['datetime']['julian']..")"
end
end
else
else
calendar = "/"..i18n['datetime']['julian']
calendar = "/"..s.i18n['datetime']['julian']
end
end
end
end
1,003行目: 1,010行目:
if sign < 0 then
if sign < 0 then
ce = i18n['datetime']['BCE']
ce = s.i18n['datetime']['BCE']
elseif precision <= 5 then
elseif precision <= 5 then
ce = i18n['datetime']['CE']
ce = s.i18n['datetime']['CE']
end
end
if ce then
if ce then
if link then
if link then
ce = buildWikilink(i18n['datetime']['common-era'], ce)
ce = buildWikilink(s.i18n['datetime']['common-era'], ce)
end
end
suffix = suffix .. " " .. ce
suffix = suffix .. " " .. ce
1,062行目: 1,069行目:
if not raw then
if not raw then
latDirectionN = i18n['coord']['latitude-north']
latDirectionN = s.i18n['coord']['latitude-north']
latDirectionS = i18n['coord']['latitude-south']
latDirectionS = s.i18n['coord']['latitude-south']
lonDirectionE = i18n['coord']['longitude-east']
lonDirectionE = s.i18n['coord']['longitude-east']
lonDirectionW = i18n['coord']['longitude-west']
lonDirectionW = s.i18n['coord']['longitude-west']
degSymbol = i18n['coord']['degrees']
degSymbol = s.i18n['coord']['degrees']
minSymbol = i18n['coord']['minutes']
minSymbol = s.i18n['coord']['minutes']
secSymbol = i18n['coord']['seconds']
secSymbol = s.i18n['coord']['seconds']
separator = i18n['coord']['separator']
separator = s.i18n['coord']['separator']
else
else
latDirectionN = latDirectionEN_N
latDirectionN = latDirectionEN_N
1,199行目: 1,206行目:
return " "  -- single space represents 'somevalue'
return " "  -- single space represents 'somevalue'
else
else
return i18n['values']['unknown']
return s.i18n['values']['unknown']
end
end
elseif snak.snaktype == 'novalue' and not noSpecial then
elseif snak.snaktype == 'novalue' and not noSpecial then
1,205行目: 1,212行目:
return ""  -- empty string represents 'novalue'
return ""  -- empty string represents 'novalue'
else
else
return i18n['values']['none']
return s.i18n['values']['none']
end
end
else
else
1,671行目: 1,678行目:


-- level 2 hook
-- level 2 hook
-- 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, template
local lang, key, citeWeb, citeQ, label
local authors = {}
local langParams = {p.aliasesP.language, p.aliasesP.languageOfWorkOrName}
local params = {}
local params = {}
local citeParams = {}
local citeParams = {['web'] = {}, ['q'] = {}}
local citeMismatch = {}
local useCite = nil
local useParams = nil
local value = ""
local value = ""
local ref = {}
local ref = {}
-- number of parameters that do not go along with "stated in"-sources other than web pages as per https://www.wikidata.org/wiki/Help:Sources;
-- these are parameters of properties other than "pages(s)" and "chapter" (for books) and "title" and "publication date" (for databases) and also "stated in"
local hasExtraParams = false
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 is added by a bot
if statement.snaks[p.aliasesP.importedFrom] then
if statement.snaks[p.aliasesP.importedFrom] then
statement.snaks[p.aliasesP.importedFrom] = nil
statement.snaks[p.aliasesP.importedFrom] = nil
end
end
-- not linked yet because we need the plain value for comparison first
-- don't include languages that are equal to the local one
language = self:getReferenceDetail(statement.snaks, p.aliasesP.language, false, false, false, true)  -- (noUnset = true)
for i, v in ipairs(langParams) do
if language then
lang = self:getReferenceDetail(statement.snaks, v)
-- not part of a "stated in"-source
hasExtraParams = true
-- only add language to the reference if it differs from the local one
if self.conf.langName == lang then
if self.conf.langName ~= language then
statement.snaks[v] = nil
if self.linked then
-- retrieve language again, but this time with link
params[p.aliasesP.language] = self:getReferenceDetail(statement.snaks, p.aliasesP.language, false, true)  -- link = true
else
params[p.aliasesP.language] = language
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
statement.snaks[p.aliasesP.language] = nil
end
authors = self:getReferenceDetails(statement.snaks, p.aliasesP.author, false, self.linked)  -- link = true/false
if #authors > 0 then
-- not part of a "stated in"-source
hasExtraParams = true
end
referenceURL = self:getReferenceDetail(statement.snaks, p.aliasesP.referenceURL)
if referenceURL then
-- not part of a "stated in"-source
hasExtraParams = true
end
end
-- the next two may be part of a "stated in"-source, so retrieve them already so that they won't count as hasExtraParams
-- retrieve all the parameters
title    = self:getReferenceDetail(statement.snaks, p.aliasesP.title, false, false, true) -- anyLang = true
for i in pairs(statement.snaks) do
statedIn = self:getReferenceDetail(statement.snaks, p.aliasesP.statedIn, false, true, false, true)  -- link = true, (noUnset = true)
label = ""
-- 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
if referenceURL and title and template then
citeParams[i18n['cite']['url']]    = referenceURL
citeParams[i18n['cite']['title']]  = title
citeParams[i18n['cite']['website']] = statedIn
citeParams[i18n['cite']['language']] = params[p.aliasesP.language]
-- 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
citeParams[i18n['cite']['date']]        = self:getReferenceDetail(statement.snaks, p.aliasesP.publicationDate)
if #params[i] == 0 then
citeParams[i18n['cite']['access-date']] = self:getReferenceDetail(statement.snaks, p.aliasesP.retrieved)
params[i] = nil
citeParams[i18n['cite']['archive-url']] = self:getReferenceDetail(statement.snaks, p.aliasesP.archiveURL)
else
citeParams[i18n['cite']['archive-date']] = self:getReferenceDetail(statement.snaks, p.aliasesP.archiveDate)
if statement.snaks[i][1].datatype == 'external-id' then
citeParams[i18n['cite']['publisher']]   = self:getReferenceDetail(statement.snaks, p.aliasesP.publisher, false, self.linked)  -- link = true/false
key = "external-id"
citeParams[i18n['cite']['quote']]        = self:getReferenceDetail(statement.snaks, p.aliasesP.quote, false, false, true)  -- anyLang = true
label = self.conf:getLabel(i)
citeParams[i18n['cite']['pages']]        = self:getReferenceDetail(statement.snaks, p.aliasesP.pages)
if label ~= "" then
label = label .. " "
end
else
key = i
end
-- add the parameter to each matching type of citation
for j in pairs(citeParams) do
-- do so if there was no mismatch with a previous parameter
if not citeMismatch[j] then
-- check if this parameter is not mismatching itself
if s.i18n['cite'][j][key] then
-- continue if an option is available in the corresponding cite template
if s.i18n['cite'][j][key] ~= "" then
citeParams[j][s.i18n['cite'][j][key]] = label .. params[i][1]
-- if there are multiple parameter values (authors), add those too
for k=2, #params[i] do
citeParams[j][s.i18n['cite'][j][key]..k] = label .. params[i][k]
end
end
else
citeMismatch[j] = true
end
end
end
end
end
-- get title of general template for citing web references
citeWeb = split(mw.wikibase.sitelink(aliasesQ.citeWeb) or "", ":")[2] -- split off namespace from front
-- get title of template that expands stated-in references into citations
citeQ = split(mw.wikibase.sitelink(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'][s.i18n['cite']['web'][p.aliasesP.referenceURL]] and citeParams['web'][s.i18n['cite']['web'][p.aliasesP.title]] then
useCite = citeWeb
useParams = citeParams['web']
for i, v in ipairs(authors) do
-- (2) use the template that expands stated-in references into citations if there is a match and if at least "stated in" is present
citeParams[i18n['cite']['author']..i] = v
elseif citeQ and not citeMismatch['q'] and citeParams['q'][s.i18n['cite']['q'][p.aliasesP.statedIn]] then
end
-- we need the raw "stated in" Q-identifier for the this template
citeParams['q'][s.i18n['cite']['q'][p.aliasesP.statedIn]] = self:getReferenceDetail(statement.snaks, p.aliasesP.statedIn, true)  -- raw = true
useCite = citeQ
useParams = citeParams['q']
end
if useCite and useParams then
-- if this module is being substituted then build a regular template call, otherwise expand the template
-- if this module is being substituted then build a regular template call, otherwise expand the template
if mw.isSubsting() then
if mw.isSubsting() then
for i, v in pairs(citeParams) do
for i, v in pairs(useParams) do
value = value .. "|" .. i .. "=" .. v
value = value .. "|" .. i .. "=" .. v
end
end
value = "{{" .. template .. value .. "}}"
value = "{{" .. useCite .. value .. "}}"
else
else
value = mw.getCurrentFrame():expandTemplate{title=template, args=citeParams}
value = mw.getCurrentFrame():expandTemplate{title=useCite, args=useParams}
end
end
else
-- we need the raw Q-identifier for the next template
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
-- (3) else, do some default rendering of name-value pairs, but only if at least "stated in", "reference URL" or "title" is present
params[p.aliasesP.pages]           = self:getReferenceDetail(statement.snaks, p.aliasesP.pages)
elseif params[p.aliasesP.statedIn] or params[p.aliasesP.referenceURL] or params[p.aliasesP.title] then
params[p.aliasesP.chapter]         = self:getReferenceDetail(statement.snaks, p.aliasesP.chapter)
citeParams['default'] = {}
params[p.aliasesP.publicationDate] = self:getReferenceDetail(statement.snaks, p.aliasesP.publicationDate)
-- 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
-- retrieve the rest of the parameters and make them count as hasExtraParams
-- combine "reference URL" and "title" into one link if both are present
for i in pairs(statement.snaks) do
if params[p.aliasesP.referenceURL] and params[p.aliasesP.title] then
params[i] = self:getReferenceDetail(statement.snaks, i, false, self.linked, true)  -- link = true/false, anyLang = true
citeParams['default'][#citeParams['default'] + 1] = '[' .. params[p.aliasesP.referenceURL][1] .. ' "' .. params[p.aliasesP.title][1] .. '"]'
hasExtraParams = true
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
end
-- get title of template that expands a given stated-in item
-- then add "stated in"
template = mw.wikibase.sitelink(aliasesQ.citeQ) or ""
if params[p.aliasesP.statedIn] then
template = split(template, ":")[2]  -- split off namespace from front
citeParams['default'][#citeParams['default'] + 1] = "''" .. params[p.aliasesP.statedIn][1] .. "''"
end
-- (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
-- remove previously added parameters so that they won't be added a second time
if statedInRaw and not hasExtraParams and template then
params[p.aliasesP.author] = nil
citeParams[i18n['cite']['pages-q']] = params[p.aliasesP.pages]
params[p.aliasesP.referenceURL] = nil
citeParams[i18n['cite']['chapter']] = params[p.aliasesP.chapter]
params[p.aliasesP.title] = nil
citeParams[i18n['cite']['date-q']]  = params[p.aliasesP.publicationDate]
params[p.aliasesP.statedIn] = nil
-- add the rest of the parameters
for i, v in pairs(params) do
i = self.conf:getLabel(i)
if mw.isSubsting() then
if i ~= "" then
for i, v in pairs(citeParams) do
citeParams['default'][#citeParams['default'] + 1] = i .. ": " .. v[1]
value = value .. "|" .. i .. "=" .. v
end
value = "{{" .. template .. "|" .. statedInRaw .. value .. "}}"
else
citeParams[1] = statedInRaw
value = mw.getCurrentFrame():expandTemplate{title=template, args=citeParams}
end
-- (3) else, do some default rendering of name-value pairs, but only if at least "stated in" or "reference URL" is present
elseif statedIn or referenceURL then
-- authors were already retrieved; start by adding them up front
if #authors > 0 then
citeParams[#citeParams + 1] = table.concat(authors, " & ")
end
-- combine "reference URL" and "title" into one link if both are present
if referenceURL and title then
citeParams[#citeParams + 1] = "[" .. referenceURL .. " " .. title .. "]"
elseif referenceURL then
citeParams[#citeParams + 1] = referenceURL
elseif title then
citeParams[#citeParams + 1] = title
end
if statedIn then
citeParams[#citeParams + 1] = statedIn
end
for i, v in pairs(params) do
i = self.conf:getLabel(i)
if i ~= "" then
citeParams[#citeParams + 1] = i .. ": " .. v
end
end
value = table.concat(citeParams, "; ")
if value ~= "" then
value = value .. "."
end
end
end
value = table.concat(citeParams['default'], "; ")
if value ~= "" then
value = value .. "."
end
end
end
end
1,847行目: 1,845行目:


-- gets a detail of one particular type for a reference
-- gets a detail of one particular type for a reference
function State:getReferenceDetail(snaks, dType, raw, link, anyLang, noUnset)
function State:getReferenceDetail(snaks, dType, raw, link, anyLang)
local switchLang = anyLang
local switchLang = anyLang
local value = nil
local value = nil
1,871行目: 1,869行目:
switchLang = not switchLang
switchLang = not switchLang
until anyLang and switchLang
until anyLang and switchLang
if not noUnset then
-- remove detail(s) to make it possible to get the rest of the details in one loop
snaks[dType] = nil
end
return value
return value
1,881行目: 1,874行目:


-- gets the details of one particular type for a reference
-- gets the details of one particular type for a reference
function State:getReferenceDetails(snaks, dType, raw, link, anyLang, noUnset)
function State:getReferenceDetails(snaks, dType, raw, link, anyLang)
local values = {}
local values = {}
1,891行目: 1,884行目:
-- if nil is returned then it will not be added to the table
-- if nil is returned then it will not be added to the table
values[#values + 1] = self.conf:getValue(v, raw, link, false, anyLang, false, true)  -- noSpecial = true
values[#values + 1] = self.conf:getValue(v, raw, link, false, anyLang, false, true)  -- noSpecial = true
end
if not noUnset then
-- remove detail(s) to make it possible to get the rest of the details in one loop
snaks[dType] = nil
end
end
匿名利用者