Module:WikidataIB: Difference between revisions

Jump to navigation Jump to search
m (1 revision imported: Wikipedia article on Nicotine templates needed)
(hide pen icon for unconfirmed users)
Line 1: Line 1:
-- Version: 2020-12-08
-- Version: 2020-12-15
-- Module to implement use of a blacklist and whitelist for infobox fields
-- Module to implement use of a blacklist and whitelist for infobox fields
-- Can take a named parameter |qid which is the Wikidata ID for the article
-- Can take a named parameter |qid which is the Wikidata ID for the article
Line 565: Line 565:
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- lang is the current language code.
-- lang is the current language code.
-- uselbl is boolean switch to display the label
-- uselbl is boolean switch to force display of the label instead of the sitelink (default: false)
-- linkredir is boolean switch to allow linking to a redirect (default: false)
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: labelOrId(); donotlink[]
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt, shortname, lang, uselbl)
local linkedItem = function(id, args)
lprefix = lprefix or "" -- toughen against nil values passed
local lprefix = args.lprefix or "" -- toughen against nil values passed
lpostfix = lpostfix or ""
local lpostfix = args.lpostfix or ""
prefix = prefix or ""
local prefix = args.prefix or ""
postfix = postfix or ""
local postfix = args.postfix or ""
lang = lang or "en" -- fallback to default if missing
local dtxt = args.dtxt
local shortname = args.shortname
local lang = args.lang or "en" -- fallback to default if missing
local uselbl = args.uselabel or args.uselbl
uselbl = parseParam(uselbl, false)
local linkredir = args.linkredir
linkredir = parseParam(linkredir, false)
-- see if item might need italics or quotes
-- see if item might need italics or quotes
local fmt = ""
local fmt = ""
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P31") ) do
if next(formats) then
if v.mainsnak.datavalue and formats[v.mainsnak.datavalue.value.id] then
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P31") ) do
fmt = formats[v.mainsnak.datavalue.value.id]
if v.mainsnak.datavalue and formats[v.mainsnak.datavalue.value.id] then
break -- pick the first match
fmt = formats[v.mainsnak.datavalue.value.id]
break -- pick the first match
end
end
end
end
end
Line 613: Line 622:
local slink = sitelink
local slink = sitelink
if pos > 0 then
if pos > 0 then
local prefix = sitelink:sub(1,pos-1)
local pfx = sitelink:sub(1,pos-1)
if mw.site.namespaces[prefix] then -- that prefix is a valid namespace, so remove it
if mw.site.namespaces[pfx] then -- that prefix is a valid namespace, so remove it
slink = sitelink:sub(pos+1)
slink = sitelink:sub(pos+1)
end
end
Line 637: Line 646:
end
end
elseif islabel then
elseif islabel then
-- no sitelink, label exists, so check if a redirect with that title exists
-- no sitelink, label exists, so check if a redirect with that title exists, if linkredir is true
local artitle = mw.title.new(label, 0) -- only nil if label has invalid chars
-- display plain label by default
if not donotlink[label] and artitle and artitle.redirectTarget then
disp = prefix .. fmt .. label .. fmt .. postfix
-- there's a redirect with the same title as the label, so let's link to that
if linkredir then
disp = "[[".. lprefix .. label .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]"
local artitle = mw.title.new(label, 0) -- only nil if label has invalid chars
else
if not donotlink[label] and artitle and artitle.redirectTarget then
-- either (donotlink is true) or (no sitelink, label exists, not redirect)
-- there's a redirect with the same title as the label, so let's link to that
-- so output unlinked label with italics or quotes as needed
disp = "[[".. lprefix .. label .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]"
disp = prefix .. fmt .. label .. fmt .. postfix
end
end -- test if article title exists as redirect on current Wiki
end -- test if article title exists as redirect on current Wiki
else
else
Line 811: Line 820:
if not entityID or entityID == "" then entityID= mw.wikibase.getEntityIdForCurrentPage() end
if not entityID or entityID == "" then entityID= mw.wikibase.getEntityIdForCurrentPage() end
propertyID = propertyID or ""
propertyID = propertyID or ""
local icon = "&nbsp;<span class='penicon'>[["
local icon = "&nbsp;<span class='penicon autoconfirmed-show'>[["
-- "&nbsp;<span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge
-- "&nbsp;<span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge
.. i18n["filespace"]
.. i18n["filespace"]
Line 932: Line 941:
local shortname = args.shortname or args.sn
local shortname = args.shortname or args.sn
shortname = parseParam(shortname, false)
shortname = parseParam(shortname, false)
local uselbl = args.uselabel or args.uselbl
uselbl = parseParam(uselbl, false)
local snak = propval.mainsnak or propval
local snak = propval.mainsnak or propval
local dtype = snak.datatype
local dtype = snak.datatype
Line 953: Line 960:
local qnumber = dv.id
local qnumber = dv.id
if linked then
if linked then
val = linkedItem(qnumber, lpre, lpost, pre, post, dtxt, shortname, args.lang, uselbl)
val = linkedItem(qnumber, args)
else -- no link wanted so check for display-text, otherwise test for lang code
else -- no link wanted so check for display-text, otherwise test for lang code
local label, islabel
local label, islabel
Line 1,586: Line 1,593:
if prop and prop.mainsnak.datavalue then
if prop and prop.mainsnak.datavalue then
if not skip or count == 0 then
if not skip or count == 0 then
out[#out+1] = linkedItem(qid, ":", "", "", "") -- get a linked value if we can
local args = { lprefix = ":" }
out[#out+1] = linkedItem(qid, args) -- get a linked value if we can
end
end
qid, prevqid = prop.mainsnak.datavalue.value.id, qid
qid, prevqid = prop.mainsnak.datavalue.value.id, qid
Line 1,826: Line 1,834:


-- if there is a 'display' parameter supplied, use it
-- if there is a 'display' parameter supplied, use it
-- otherwise default to "inline, title"
-- otherwise default to nothing
local disp = frame.args.display or ""
local disp = frame.args.display or ""
if disp == "" then
if disp == "" then
Line 3,166: Line 3,174:
local txt = frame.args.url or ""
local txt = frame.args.url or ""
if txt == "" then return nil end
if txt == "" then return nil end
-- extract any icon
local url, icon = txt:match("(.+)&nbsp;(.+)")
local url, icon = txt:match("(.+)&nbsp;(.+)")
url = url or txt
-- make sure there's at least a space at the end
url = (url or txt) .. " "
icon = icon or ""
icon = icon or ""
local prot, addr = url:match("(http[s]*://)(.+)")
-- extract any protocol like https://
prot = prot or url
local prot = url:match("(https*://).+[ \"\']")
addr = addr or ""
-- extract address
local addr = ""
if prot then
addr = url:match("https*://(.+)[ \"\']") or " "
else
prot = "//"
addr = url:match("[^%p%s]+%.(.+)[ \"\']") or " "
end
-- strip trailing / from end of domain-only url and add <wbr/> before . and /
-- strip trailing / from end of domain-only url and add <wbr/> before . and /
local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>.")
local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>.")