「モジュール:Wd」の版間の差分
ナビゲーションに移動
検索に移動
en:Module:Wd 2017年8月19日 (土) 20:45(UTC)より
template>K-iczn (en:Module:Wd 2017年7月18日 (火) 06:28(UTC)より) |
template>K-iczn (en:Module:Wd 2017年8月19日 (土) 20:45(UTC)より) |
||
95行目: | 95行目: | ||
cfg.entity = nil | cfg.entity = nil | ||
cfg.entityID = nil | |||
cfg.propertyID = nil | cfg.propertyID = nil | ||
cfg.propertyValue = nil | cfg.propertyValue = nil | ||
112行目: | 113行目: | ||
cfg.singleClaim = false | cfg.singleClaim = false | ||
cfg.sourcedOnly = false | cfg.sourcedOnly = false | ||
cfg.editable = false | |||
cfg.editAtEnd = false | |||
cfg.pageTitle = false | cfg.pageTitle = false | ||
147行目: | 150行目: | ||
stt.rawValue = false | stt.rawValue = false | ||
stt.shortName = false | stt.shortName = false | ||
stt.anyLanguage = false | |||
stt.singleValue = false | stt.singleValue = false | ||
258行目: | 262行目: | ||
end | end | ||
function parseWikidataURL(url) | |||
local i, j | |||
if url:match('^http[s]?://') then | |||
i, j = url:find("Q") | |||
if i then | |||
return url:sub(i) | |||
end | |||
end | |||
return nil | |||
end | |||
function parseDate(dateStr, precision) | |||
precision = precision or "d" | |||
return nil | |||
end | |||
function parseDate(dateStr, precision) | |||
precision = precision or "d" | |||
local i, j, index, ptr | local i, j, index, ptr | ||
local parts = {nil, nil, nil} | local parts = {nil, nil, nil} | ||
601行目: | 572行目: | ||
end | end | ||
function Config: | function Config:getEditIcon() | ||
local value = "" | |||
local | local prefix = "" | ||
local front = " " | |||
local back = "" | |||
if self.entityID:sub(1,1) == "P" then | |||
prefix = "Property:" | |||
end | |||
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 | |||
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 | |||
-- 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 | |||
if | -- 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 | |||
if | |||
end | end | ||
j = j - 1 | |||
end | end | ||
if not skip then | |||
end | -- 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 | end | ||
return | return outString | ||
end | end | ||
function Config: | function Config:convertUnit(unit, link) | ||
link = link or false | link = link or false | ||
local itemID, label, lang, title | |||
if unit == "" or unit == "1" then | |||
return nil | |||
end | |||
itemID = parseWikidataURL(unit) | |||
if | if itemID then | ||
if itemID == aliasesQ.percentage then | |||
if | return "%" | ||
else | |||
if | label, lang = mw.wikibase.getLabelWithLang(itemID) | ||
return " | -- don't allow language fallback | ||
if lang ~= self.langCode then | |||
return " | label = nil | ||
end | |||
title = nil | |||
if link or label == nil then | |||
title = mw.wikibase.sitelink(itemID) | |||
end | |||
if link then | |||
if title then | |||
return " " .. buildWikilink(title, (label or title)) | |||
end | |||
if not label then | |||
return " " .. buildWikilink("d:" .. itemID, itemID) | |||
end | end | ||
elseif snak.datatype == 'commonsMedia' then | end | ||
if link then | |||
if raw then | return " " .. (label or title or itemID) | ||
-- will render as a linked number like [1] | end | ||
return "[https://commons.wikimedia.org/wiki/File:" .. mw.ustring.gsub(snak.datavalue.value, " ", "_") .. "]" | end | ||
else | |||
return " " .. unit | |||
end | |||
function Config:getValue(snak, raw, link, short, anyLang, noSpecial) | |||
raw = raw or false | |||
link = link or false | |||
short = short or false | |||
anyLang = anyLang 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 | |||
if raw then | |||
-- will render as a linked number like [1] | |||
return "[" .. snak.datavalue.value .. "]" | |||
else | |||
return "[" .. snak.datavalue.value .. " " .. snak.datavalue.value .. "]" | |||
end | |||
elseif snak.datatype == 'commonsMedia' then | |||
if link then | |||
if raw then | |||
-- will render as a linked number like [1] | |||
return "[https://commons.wikimedia.org/wiki/File:" .. mw.ustring.gsub(snak.datavalue.value, " ", "_") .. "]" | |||
else | |||
return "[https://commons.wikimedia.org/wiki/File:" .. mw.ustring.gsub(snak.datavalue.value, " ", "_") .. " " .. snak.datavalue.value .. "]" | return "[https://commons.wikimedia.org/wiki/File:" .. mw.ustring.gsub(snak.datavalue.value, " ", "_") .. " " .. snak.datavalue.value .. "]" | ||
end | end | ||
1,052行目: | 1,095行目: | ||
local value = "" | local value = "" | ||
local title = nil | local title = nil | ||
local | local prefix = "" | ||
local itemID = snak.datavalue.value['numeric-id'] | |||
if snak.datatype == 'wikibase-item' then | |||
itemID = "Q" .. itemID | |||
elseif snak.datatype == 'wikibase-property' then | |||
itemID = "P" .. itemID | |||
prefix = "Property:" | |||
else | |||
return '<strong class="error">' .. unknownDataTypeError(snak.datatype) .. '</strong>' | |||
end | |||
if raw then | if raw then | ||
if link then | if link then | ||
return buildWikilink("d:" .. itemID, itemID) | return buildWikilink("d:" .. prefix .. itemID, itemID) | ||
else | else | ||
return itemID | return itemID | ||
1,063行目: | 1,116行目: | ||
if short then | if short then | ||
value = getShortName(itemID) | value = getShortName(prefix .. itemID) | ||
end | end | ||
1,075行目: | 1,128行目: | ||
end | end | ||
if link or value == nil then | if (link or value == nil) and snak.datatype == 'wikibase-item' then | ||
title = mw.wikibase.sitelink(itemID) | title = mw.wikibase.sitelink(itemID) | ||
end | end | ||
1,082行目: | 1,135行目: | ||
if title then | if title then | ||
value = buildWikilink(title, (value or title)) | value = buildWikilink(title, (value or title)) | ||
elseif not value then | elseif not value or snak.datatype == 'wikibase-property' then | ||
value = buildWikilink("d:" .. itemID, itemID) | value = buildWikilink("d:" .. prefix .. itemID, (value or itemID)) | ||
end | end | ||
elseif not value then | elseif not value then | ||
1,212行目: | 1,265行目: | ||
elseif flag == "short" then | elseif flag == "short" then | ||
self.curState.shortName = true | self.curState.shortName = true | ||
return true | |||
elseif flag == "multilanguage" then | |||
self.curState.anyLanguage = true | |||
return true | return true | ||
elseif flag == "mdy" then | elseif flag == "mdy" then | ||
1,221行目: | 1,277行目: | ||
elseif flag == "sourced" then | elseif flag == "sourced" then | ||
self.sourcedOnly = true | self.sourcedOnly = true | ||
return true | |||
elseif flag == "edit" then | |||
self.editable = true | |||
return true | |||
elseif flag == "edit@end" then | |||
self.editable = true | |||
self.editAtEnd = true | |||
return true | return true | ||
elseif flag == "best" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then | elseif flag == "best" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then | ||
1,477行目: | 1,540行目: | ||
-- level 1 hook | -- level 1 hook | ||
function State:getProperty(claim) | function State:getProperty(claim) | ||
local value = {self.conf:getValue(claim.mainsnak, self.rawValue, self.linked, self.shortName)} -- create one value object | local value = {self.conf:getValue(claim.mainsnak, self.rawValue, self.linked, self.shortName, self.anyLanguage)} -- create one value object | ||
if #value > 0 then | if #value > 0 then | ||
1,502行目: | 1,565行目: | ||
-- level 2 hook | -- level 2 hook | ||
function State:getQualifier(snak) | function State:getQualifier(snak) | ||
local value = {self.conf:getValue(snak, self.rawValue, self.linked, self.shortName)} -- create one value object | local value = {self.conf:getValue(snak, self.rawValue, self.linked, self.shortName, self.anyLanguage)} -- create one value object | ||
if #value > 0 then | if #value > 0 then | ||
1,964行目: | 2,027行目: | ||
-- check for optional entity ID of either item or property | -- check for optional entity ID of either item or property | ||
if nextArg:sub(1,1):upper() == "Q" then | if nextArg:sub(1,1):upper() == "Q" then | ||
_.entity = mw.wikibase.getEntity( | _.entityID = nextArg:upper() -- entity ID of an item was given | ||
_.entity = mw.wikibase.getEntity(_.entityID) -- get the item-entity based on the given ID | |||
_.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID | _.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID | ||
nextIndex = nextIndex + 1 | nextIndex = nextIndex + 1 | ||
elseif nextArg:sub(1,9):lower() == "property:" then | elseif nextArg:sub(1,9):lower() == "property:" then | ||
_.entityID = replaceAlias(mw.text.trim(nextArg:sub(10))):upper() -- entity ID of a property was given | |||
_.entity = mw.wikibase.getEntity( | _.entity = mw.wikibase.getEntity(_.entityID) -- get the property-entity based on the given ID | ||
_.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID | _.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID | ||
nextIndex = nextIndex + 1 | nextIndex = nextIndex + 1 | ||
else | else | ||
_. | _.entityID = mw.wikibase.getEntityIdForCurrentPage() -- no entity ID given, use item-entity connected to current page | ||
_.entity = mw.wikibase.getEntity() -- get the item-entity connected to the current page | |||
_.propertyID = nextArg -- property ID | _.propertyID = nextArg -- property ID | ||
end | end | ||
2,138行目: | 2,203行目: | ||
-- then iterate through the claims to collect values | -- then iterate through the claims to collect values | ||
return concatValues(_.states[parameters.property]:iterate(claims, hooks, State.claimMatches)) -- pass property state with level 1 hooks and matchHook | return _:concatValues(_.states[parameters.property]:iterate(claims, hooks, State.claimMatches)) -- pass property state with level 1 hooks and matchHook | ||
else | else | ||
return "" | return "" | ||
2,186行目: | 2,251行目: | ||
return "" | return "" | ||
end | end | ||
_.entityID = ID | |||
else | |||
_.entityID = mw.wikibase.getEntityIdForCurrentPage() | |||
end | end | ||
2,219行目: | 2,288行目: | ||
title = mw.title.getCurrentTitle().prefixedText | title = mw.title.getCurrentTitle().prefixedText | ||
elseif ID:sub(1,1) == "Q" then | elseif ID:sub(1,1) == "Q" then | ||
title = mw.wikibase.sitelink(ID) | title = mw.wikibase.sitelink(ID) | ||
elseif label then | elseif label then | ||
-- properties have no sitelink, 'title = ID' should only be set | -- properties have no sitelink, 'title = ID' should only be set | ||
-- if there is a 'label' already and if it needs to be linked; | -- 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 | -- otherwise, it would be 'label = title == ID' even if '_.pageTitle' is set | ||
title = ID | title = ID | ||
prefix = "d:Property:" | prefix = "d:Property:" | ||
end | end | ||
label = label or title | label = label or title | ||
end | end | ||
end | end | ||
-- (4) try entity ID (i.e. raw value) | -- (4) try entity ID (i.e. raw value) | ||
if not label and not _.pageTitle then | if not label and not _.pageTitle then | ||
if not ID then | if not ID then | ||
label = | label = _.entityID | ||
elseif mw.wikibase.getEntity(ID) then | elseif mw.wikibase.getEntity(ID) then | ||
label = ID | label = ID | ||
if ID:sub(1,1) == "P" then | if ID:sub(1,1) == "P" then | ||
prefix = "Property:" | prefix = "Property:" | ||
end | end | ||
end | end | ||
prefix = "d:" .. prefix | prefix = "d:" .. prefix | ||
title = label | 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 | end | ||
if not label then | if not label then | ||
label = "" | label = "" | ||
elseif _.editable then | |||
-- if desired, add a clickable icon that may be used to edit the returned value on Wikidata | |||
label = label .. _:getEditIcon() | |||
label = | |||
end | end | ||