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/ | -- 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) | ||
local title | |||
if frame then | |||
-- current module invoked by page/template, get its title from frame | |||
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 | ||
if not s[i] then | |||
s[i] = require(title.."/"..i) | |||
end | |||
end | end | ||
end | end | ||
57行目: | 77行目: | ||
p.args = { | p.args = { | ||
eid = "eid" | eid = "eid" | ||
} | } | ||
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 | ||
function State:getReference(statement) | function State:getReference(statement) | ||
local | local lang, key, citeWeb, citeQ, label | ||
local | 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 = {} | ||
if statement.snaks then | if statement.snaks then | ||
-- don't include "imported from", which | -- 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 | ||
-- | -- don't include languages that are equal to the local one | ||
for i, v in ipairs(langParams) do | |||
lang = self:getReferenceDetail(statement.snaks, v) | |||
if self.conf.langName == lang then | |||
if self.conf.langName | statement.snaks[v] = nil | ||
end | end | ||
end | end | ||
-- the | -- retrieve all the parameters | ||
for i in pairs(statement.snaks) do | |||
label = "" | |||
-- 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 | |||
if #params[i] == 0 then | |||
params[i] = nil | |||
else | |||
if statement.snaks[i][1].datatype == 'external-id' then | |||
key = "external-id" | |||
label = self.conf:getLabel(i) | |||
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'] | |||
-- (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'][s.i18n['cite']['q'][p.aliasesP.statedIn]] then | |||
-- 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( | for i, v in pairs(useParams) do | ||
value = value .. "|" .. i .. "=" .. v | value = value .. "|" .. i .. "=" .. v | ||
end | end | ||
value = "{{" .. | value = "{{" .. useCite .. value .. "}}" | ||
else | else | ||
value = mw.getCurrentFrame():expandTemplate{title= | value = mw.getCurrentFrame():expandTemplate{title=useCite, args=useParams} | ||
end | end | ||
-- (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 | |||
params[ | 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 | 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 | |||
for i, v in pairs(params) do | |||
i = self.conf:getLabel(i) | |||
if | if i ~= "" then | ||
citeParams['default'][#citeParams['default'] + 1] = i .. ": " .. v[1] | |||
citeParams[ | |||
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 | 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 | ||
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 | 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 | end | ||