「モジュール: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)
if ID then
local args = {aliasesP.shortName}
return p._property({ID, aliasesP.shortName})  -- "property" is single
else
return p._property({aliasesP.shortName})  -- "property" is single
end
end
 
function getLabel(ID, args)
args = args or {}
if ID then
if ID then
args[#args + 1] = ID
args["eid"] = ID
end
end
return p._label(args)
return p._property(args) -- "property" is single
end
end


function Config:getEditIcon()
-- if ID == nil then item connected to current page is used
local value = ""
function Config:getLabel(ID, raw, link, short, pageTitle)
local prefix = ""
raw = raw or false
local front = " "
link = link or false
local back = ""
short = short or false
pageTitle = pageTitle or false
if self.entityID:sub(1,1) == "P" then
local label = nil
prefix = "Property:"
local title = nil
end
local prefix= ""
local lang
if self.editAtEnd then
if not raw or pageTitle then
front = '<span style="float:'
if not pageTitle then
-- (1) try short name
if self.langObj:isRTL() then
if short then
front = front .. 'left'
label = getShortName(ID)
else
front = front .. 'right'
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
front = front .. '">'
-- (3) try sitelink/title
back = '</span>'
if not label or link then
end
if not ID then
title = mw.title.getCurrentTitle().prefixedText
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
elseif ID:sub(1,1) == "Q" then
title = mw.wikibase.sitelink(ID)
if self.propertyID then
elseif label then
value = value .. "#" .. self.propertyID
-- 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 = value .. "|" .. i18n['info']['edit-on-wikidata'] .. "]]"
-- (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
return front .. value .. back
-- add optional link
end
if link and title then
 
label = buildWikilink(prefix .. title, label) -- label is never nil if title is not nil
-- used to create the final output string when it's all done, so that for references the
-- function extensionTag("ref", ...) is only called when they really ended up in the final output
function Config:concatValues(valuesArray)
local outString = ""
local j, skip
for i = 1, #valuesArray do
-- check if this is a reference
if valuesArray[i].refHash then
j = i - 1
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
end
-- if desired, add a clickable icon that may be used to edit the returned values on Wikidata
if not label then
if self.editable and outString ~= "" then
label = ""
outString = outString .. self:getEditIcon()
end
end
return outString
return label
end
end


function Config:convertUnit(unit, raw, link, short, unitOnly)
function Config:getEditIcon()
raw = raw or false
local value = ""
link = link or false
local prefix = ""
short = short or false
local front = " "
unitOnly = unitOnly or false
local back = ""
local args = {}
local space = " "
local label = ""
if unit == "" or unit == "1" then
if self.entityID:sub(1,1) == "P" then
return nil
prefix = "Property:"
end
end
if unitOnly then
if self.editAtEnd then
space = ""
front = '<span style="float:'
if self.langObj:isRTL() then
front = front .. 'left'
else
front = front .. 'right'
end
front = front .. '">'
back = '</span>'
end
end
itemID = parseWikidataURL(unit)
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 itemID then
if self.propertyID then
if itemID == aliasesQ.percentage then
value = value .. "#" .. self.propertyID
return "%"
else
if raw then
args[#args + 1] = flags.raw
end
if link then
args[#args + 1] = flags.linked
end
if short then
args[#args + 1] = flags.short
end
label = getLabel(itemID, args)
if label ~= "" then
return space .. label
end
end
end
end
return ""
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 lang
local value = ""
local title = nil
local prefix = ""
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
prefix = "Property:"
else
else
return '<strong class="error">' .. unknownDataTypeError(snak.datatype) .. '</strong>'
return '<strong class="error">' .. unknownDataTypeError(snak.datatype) .. '</strong>'
end
end
if raw then
return self:getLabel(itemID, raw, link, short)
if link then
return buildWikilink("d:" .. prefix .. itemID, itemID)
else
return itemID
end
end
if short then
value = getShortName(prefix .. itemID)
end
if value == "" then
value, lang = mw.wikibase.getLabelWithLang(itemID)
-- don't allow language fallback
if lang ~= self.langCode then
value = nil
end
end
if (link or value == nil) and snak.datatype == 'wikibase-item' then
title = mw.wikibase.sitelink(itemID)
end
if link then
if title then
value = buildWikilink(title, (value or title))
elseif not value or snak.datatype == 'wikibase-property' then
value = buildWikilink("d:" .. prefix .. itemID, (value or itemID))
end
elseif not value then
value = (title or itemID)
end
return value
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 = nil
local label = ""
local title = nil
local prefix= ""
local lang
local nextArg = args[1]
local nextArg = args[1]
2,319行目: 2,341行目:
end
end
if not _.curState.rawValue or _.pageTitle then
label = _:getLabel(ID, _.curState.rawValue, _.curState.linked, _.curState.shortName, _.pageTitle)
if not _.pageTitle then
-- (1) try short name
if _.curState.shortName then
if ID and ID:sub(1,1) == "P" then
label = getShortName("Property:" .. ID)
else
label = getShortName(ID)
end
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 ~= _.langCode then
label = nil
end
end
end
-- (3) try sitelink/title
if not label or _.curState.linked 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
-- (4) try entity ID (i.e. raw value)
if not label and not _.pageTitle then
if not ID then
label = _.entityID
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 _.curState.linked and title then
label = buildWikilink(prefix .. title, label)  -- label is never nil if title is not nil
end
if not label then
if _.editable and label ~= "" then
label = ""
elseif _.editable 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()
匿名利用者

案内メニュー