「モジュール:Wd」の版間の差分
ナビゲーションに移動
検索に移動
en:Module:Wd 2017年9月5日 (火) 20:13(UTC)より
template>K-iczn (en:Module:Wd 2017年8月29日 (火) 11:01(UTC)より) |
template>K-iczn (en:Module:Wd 2017年9月5日 (火) 20:13(UTC)より) |
||
299行目: | 299行目: | ||
function parseDate(dateStr, precision) | function parseDate(dateStr, precision) | ||
precision = precision or "d" | precision = precision or "d" | ||
local i, j, index, ptr | local i, j, index, ptr | ||
local parts = {nil, nil, nil} | local parts = {nil, nil, nil} | ||
577行目: | 578行目: | ||
end | end | ||
-- if ID == nil then item connected to current page is used | |||
function getShortName(ID) | function getShortName(ID) | ||
local args = {aliasesP.shortName} | |||
if ID then | if ID then | ||
args[ | args["eid"] = ID | ||
end | end | ||
return p. | return p._property(args) -- "property" is single | ||
end | end | ||
function Config: | -- if ID == nil then item connected to current page is used | ||
function Config:getLabel(ID, raw, link, short, pageTitle) | |||
raw = raw or false | |||
link = link or false | |||
short = short or false | |||
pageTitle = pageTitle or false | |||
local label = nil | |||
local title = nil | |||
local prefix= "" | |||
local lang | |||
if | if not raw or pageTitle then | ||
if not pageTitle then | |||
-- (1) try short name | |||
if short then | |||
label = getShortName(ID) | |||
if label == "" then | |||
label = nil | |||
end | |||
end | |||
-- (2) try label | |||
if not label then | |||
label, lang = mw.wikibase.getLabelWithLang(ID) | |||
-- don't allow language fallback | |||
if lang ~= self.langCode then | |||
label = nil | |||
end | |||
end | |||
end | end | ||
-- (3) try sitelink/title | |||
if not label or link then | |||
if not ID then | |||
title = mw.title.getCurrentTitle().prefixedText | |||
elseif ID:sub(1,1) == "Q" then | |||
title = mw.wikibase.sitelink(ID) | |||
elseif label then | |||
-- properties have no sitelink, 'title = ID' should only be set | |||
-- if there is a 'label' already and if it needs to be linked; | |||
-- otherwise, it would be 'label = title == ID' even if 'pageTitle' is set | |||
title = ID | |||
prefix = "d:Property:" | |||
end | |||
label = label or title | |||
end | |||
end | end | ||
value = | -- (4) try entity ID (i.e. raw value) | ||
if not label and not pageTitle then | |||
if not ID then | |||
label = mw.wikibase.getEntityIdForCurrentPage() | |||
elseif mw.wikibase.getEntity(ID) then | |||
label = ID | |||
if ID:sub(1,1) == "P" then | |||
prefix = "Property:" | |||
end | |||
end | |||
prefix = "d:" .. prefix | |||
title = label | |||
end | |||
-- add optional link | |||
if link and title then | |||
label = buildWikilink(prefix .. title, label) -- label is never nil if title is not nil | |||
-- | |||
end | end | ||
if not label then | |||
label = "" | |||
end | end | ||
return | return label | ||
end | end | ||
function Config: | function Config:getEditIcon() | ||
local value = "" | |||
local prefix = "" | |||
local front = " " | |||
local back = "" | |||
local | |||
local | |||
local | |||
if | if self.entityID:sub(1,1) == "P" then | ||
prefix = "Property:" | |||
end | end | ||
if | if self.editAtEnd then | ||
front = '<span style="float:' | |||
if self.langObj:isRTL() then | |||
front = front .. 'left' | |||
else | |||
front = front .. 'right' | |||
end | |||
front = front .. '">' | |||
back = '</span>' | |||
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 | |||
if | if self.propertyID then | ||
value = value .. "#" .. self.propertyID | |||
end | end | ||
value = value .. "|" .. i18n['info']['edit-on-wikidata'] .. "]]" | |||
return front .. value .. back | |||
end | end | ||
function Config:getValue(snak, raw, link, short, anyLang, unitOnly, noSpecial) | -- used to create the final output string when it's all done, so that for references the | ||
raw = raw or false | -- function extensionTag("ref", ...) is only called when they really ended up in the final output | ||
link = link or false | function Config:concatValues(valuesArray) | ||
short = short or false | local outString = "" | ||
anyLang = anyLang or false | local j, skip | ||
unitOnly = unitOnly or false | |||
noSpecial = noSpecial or false | for i = 1, #valuesArray do | ||
-- check if this is a reference | |||
if snak.snaktype == 'value' then | if valuesArray[i].refHash then | ||
if snak.datavalue.type == 'string' then | j = i - 1 | ||
if snak.datatype == 'url' and link then | skip = false | ||
-- skip this reference if it is part of a continuous row of references that already contains the exact same reference | |||
while valuesArray[j] and valuesArray[j].refHash do | |||
if valuesArray[i].refHash == valuesArray[j].refHash then | |||
skip = true | |||
break | |||
end | |||
j = j - 1 | |||
end | |||
if not skip then | |||
-- 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}) | |||
end | |||
else | |||
outString = outString .. valuesArray[i][1] | |||
end | |||
end | |||
-- if desired, add a clickable icon that may be used to edit the returned values on Wikidata | |||
if self.editable and outString ~= "" then | |||
outString = outString .. self:getEditIcon() | |||
end | |||
return outString | |||
end | |||
function Config:convertUnit(unit, raw, link, short, unitOnly) | |||
raw = raw or false | |||
link = link or false | |||
short = short or false | |||
unitOnly = unitOnly or false | |||
local space = " " | |||
local label = "" | |||
if unit == "" or unit == "1" then | |||
return nil | |||
end | |||
if unitOnly then | |||
space = "" | |||
end | |||
itemID = parseWikidataURL(unit) | |||
if itemID then | |||
if itemID == aliasesQ.percentage then | |||
return "%" | |||
else | |||
label = self:getLabel(itemID, raw, link, short) | |||
if label ~= "" then | |||
return space .. label | |||
end | |||
end | |||
end | |||
return "" | |||
end | |||
function Config:getValue(snak, raw, link, short, anyLang, unitOnly, noSpecial) | |||
raw = raw or false | |||
link = link or false | |||
short = short or false | |||
anyLang = anyLang or false | |||
unitOnly = unitOnly or false | |||
noSpecial = noSpecial or false | |||
if snak.snaktype == 'value' then | |||
if snak.datavalue.type == 'string' then | |||
if snak.datatype == 'url' and link then | |||
-- create link explicitly | -- create link explicitly | ||
if raw then | if raw then | ||
1,113行目: | 1,178行目: | ||
return value | return value | ||
elseif snak.datavalue.type == 'wikibase-entityid' then | elseif snak.datavalue.type == 'wikibase-entityid' then | ||
local itemID = snak.datavalue.value['numeric-id'] | local itemID = snak.datavalue.value['numeric-id'] | ||
1,123行目: | 1,184行目: | ||
elseif snak.datatype == 'wikibase-property' then | elseif snak.datatype == 'wikibase-property' then | ||
itemID = "P" .. itemID | itemID = "P" .. itemID | ||
else | else | ||
return '<strong class="error">' .. unknownDataTypeError(snak.datatype) .. '</strong>' | return '<strong class="error">' .. unknownDataTypeError(snak.datatype) .. '</strong>' | ||
end | end | ||
return self:getLabel(itemID, raw, link, short) | |||
else | else | ||
return '<strong class="error">' .. unknownDataTypeError(snak.datavalue.type) .. '</strong>' | return '<strong class="error">' .. unknownDataTypeError(snak.datavalue.type) .. '</strong>' | ||
1,779行目: | 1,804行目: | ||
for i, v in pairs(params) do | for i, v in pairs(params) do | ||
i = getLabel(i) | i = self.conf:getLabel(i) | ||
if i ~= "" then | if i ~= "" then | ||
2,260行目: | 2,285行目: | ||
loadSubmodules() | loadSubmodules() | ||
local ID = nil | local ID = nil | ||
local label | local label = "" | ||
local nextArg = args[1] | local nextArg = args[1] | ||
2,319行目: | 2,341行目: | ||
end | end | ||
label = _:getLabel(ID, _.curState.rawValue, _.curState.linked, _.curState.shortName, _.pageTitle) | |||
if | if _.editable and label ~= "" then | ||
-- if desired, add a clickable icon that may be used to edit the returned value on Wikidata | -- if desired, add a clickable icon that may be used to edit the returned value on Wikidata | ||
label = label .. _:getEditIcon() | label = label .. _:getEditIcon() |