Module:WikidataIB and Module:WikidataIB/sandbox1: Difference between pages

From HandWiki
(Difference between pages)
Page 1
Page 2
imported>Jworkorg
m (1 revision imported)
 
imported>Jworkorg
m (1 revision imported)
 
Line 1: Line 1:
-- Version: 2020-11-08
-- Version: 2020-09-22
-- 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
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: labelOrId(); donotlink[]
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt, shortname, lang, uselbl)
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt, shortname, lang)
lprefix = lprefix or "" -- toughen against nil values passed
lprefix = lprefix or "" -- toughen against nil values passed
lpostfix = lpostfix or ""
lpostfix = lpostfix or ""
Line 607: Line 606:
if sitelink then
if sitelink then
if not (dtxt or shortname) then
if not (dtxt or shortname) then
-- if sitelink and label are the same except for case, no need to process further
-- strip any namespace or dab from the sitelink
if sitelink:lower() ~= label:lower() then
local pos = sitelink:find(":") or 0
-- strip any namespace or dab from the sitelink
local slink = sitelink
local pos = sitelink:find(":") or 0
if pos > 0 then
local slink = sitelink
local prefix = sitelink:sub(1,pos-1)
if pos > 0 then
if mw.site.namespaces[prefix] then -- that prefix is a valid namespace, so remove it
local prefix = sitelink:sub(1,pos-1)
slink = sitelink:sub(pos+1)
if mw.site.namespaces[prefix] then -- that prefix is a valid namespace, so remove it
slink = sitelink:sub(pos+1)
end
end
-- remove stuff after commas or inside parentheses - ie. dabs
slink = slink:gsub("%s%(.+%)$", ""):gsub(",.+$", "")
-- if uselbl is false, use sitelink instead of label
if not uselbl then
--  use slink as display, preserving label case - find("^%u") is true for 1st char uppercase
if label:find("^%u") then
label = slink:gsub("^(%l)", string.upper)
else
label = slink:gsub("^(%u)", string.lower)
end
end
end
end
-- remove stuff after commas or inside parentheses - ie. dabs
slink = slink:gsub("%s%(.+%)$", ""):gsub(",.+$", "")
--  use that as label, preserving label case - find("^%u") is true for 1st char uppercase
if label:find("^%u") then
label = slink:gsub("^(%l)", string.upper)
else
label = slink:gsub("^(%u)", string.lower)
end
end
end
end
Line 806: Line 799:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local createicon = function(langcode, entityID, propertyID)
local createicon = function(langcode, entityID, propertyID)
local icon = "&nbsp;<span class='penicon'>[["
local icon = "&nbsp;[["
-- "&nbsp;<span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge
-- "<span data-bridge-edit-flow='overwrite' class='penicon'>&nbsp;[[" -> enable Wikidata Bridge
.. i18n["filespace"]
.. i18n["filespace"]
.. ":OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt="
.. ":OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt="
Line 814: Line 807:
.. "?uselang=" .. langcode
.. "?uselang=" .. langcode
if propertyID then icon = icon .. "#" .. propertyID end
if propertyID then icon = icon .. "#" .. propertyID end
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]</span>"
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]"
return icon
return icon
end
end
Line 927: Line 920:
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 948: Line 939:
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, lpre, lpost, pre, post, dtxt, shortname, args.lang)
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 2,204: Line 2,195:
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p._getPropOfProp = function(args)
p.getPropOfProp = function(frame)
-- parameter sets for commonly used groups of parameters
frame.args.reqranks = setRanks(frame.args.rank)
local paraset = tonumber(args.ps or args.parameterset or 0)
frame.args.langobj = findLang(frame.args.lang)
if paraset == 1 then
frame.args.lang = frame.args.langobj.code
-- a common setting
local args = frame.args
args.rank = "best"
args.fetchwikidata = "ALL"
args.onlysourced = "no"
args.noicon = "true"
elseif paraset == 2 then
-- equivalent to raw
args.rank = "best"
args.fetchwikidata = "ALL"
args.onlysourced = "no"
args.noicon = "true"
args.linked = "no"
args.pd = "true"
elseif paraset == 3 then
-- third set goes here
end
 
args.reqranks = setRanks(args.rank)
args.langobj = findLang(args.lang)
args.lang = args.langobj.code
local pid1 = args.prop1 or args.pid1 or ""
local pid1 = args.prop1 or args.pid1 or ""
local pid2 = args.prop2 or args.pid2 or ""
local pid2 = args.prop2 or args.pid2 or ""
local localval = mw.text.trim(args[1] or "")
local localval = mw.text.trim(args[1] or "")
if pid1 == "" or pid2 == "" then return nil end
if pid1 == "" or pid2 == "" then return nil end
 
local qid1, statements1 = parseInput(frame, localval, pid1)
local f = {}
f.args = args
local qid1, statements1 = parseInput(f, localval, pid1)
if not qid1 then return localval end
if not qid1 then return localval end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
local maxvals = tonumber(args.maxvals) or 0
Line 2,263: Line 2,231:
end -- of loop through values of property1
end -- of loop through values of property1
return assembleoutput(out, args, qid1, pid1)
return assembleoutput(out, args, qid1, pid1)
end
p.getPropOfProp = function(frame)
local args= frame.args
if not args.prop1 and not args.pid1 then
args = frame:getParent().args
if not args.prop1 and not args.pid1 then return i18n.errors["No property supplied"] end
end
return p._getPropOfProp(args)
end
end


Line 2,958: Line 2,916:
-- Dependencies: parseParam()
-- Dependencies: parseParam()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p._followQid = function(args)
p.followQid = function(frame)
local qid = (args.qid or ""):upper()
local qid = (frame.args.qid or ""):upper()
local all = parseParam(args.all, false)
local all = parseParam(frame.args.all, false)
if qid == "" then
if qid == "" then
qid = mw.wikibase.getEntityIdForCurrentPage()
qid = mw.wikibase.getEntityIdForCurrentPage()
Line 2,966: Line 2,924:
if not qid then return nil end
if not qid then return nil end
local out = {}
local out = {}
local props = (args.props or ""):upper()
local props = (frame.args.props or ""):upper()
if props ~= "" then
if props ~= "" then
for p in mw.text.gsplit(props, "%p") do -- split at punctuation and iterate
for p in mw.text.gsplit(props, "%p") do -- split at punctuation and iterate
Line 2,987: Line 2,945:
return qid
return qid
end
end
end
p.followQid = function(frame)
return p._followQid(frame.args)
end
end


Line 3,156: Line 3,110:
-- but it keeps the "edit at Wikidata" pen icon out of the microformat.
-- but it keeps the "edit at Wikidata" pen icon out of the microformat.
-- Usually it will take its url parameter directly from a Wikidata call:
-- Usually it will take its url parameter directly from a Wikidata call:
-- e.g. {{#invoke:WikidataIB |url2 |url={{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}} }}
-- e.g. {{#invoke:WikidataIB |url2 |url={{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
Line 3,169: Line 3,123:
prot = prot or url
prot = prot or url
addr = addr or ""
addr = addr or ""
-- strip trailing / from end of domain-only url and add <wbr/> before . and /
local disp, n = addr:gsub("%.", "<wbr/>%.")
local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>.")
return '<span class="url">[' .. prot .. addr .. " " .. disp .. "]</span>&nbsp;" .. icon
return '<span class="url">[' .. prot .. addr .. " " .. disp .. "]</span>&nbsp;" .. icon
end
end