Changes
Jump to navigation
Jump to search
Line 12:
Line 12:
− +
− +
Line 127:
Line 127:
− +
Line 148:
Line 148:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 300:
Line 325:
− +
Line 345:
Line 370:
− +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 401:
Line 440:
− +
+
Line 416:
Line 456:
− +
− +
− +
− -- If the optional parameter onlycat is true/yes/1 then only categories are returned
− -- defaults to false.
Line 429:
Line 467:
+
Line 478:
Line 517:
+
+
+
− +
− +
+
Line 491:
Line 534:
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 496:
Line 552:
− +
Line 523:
Line 579:
− +
Line 767:
Line 823:
+
+
+
+
+
+
− local dtype = propval.datatype or propval.mainsnak.datatype
Line 788:
Line 849:
− +
Line 812:
Line 873:
− +
Line 1,088:
Line 1,149:
− +
− +
− +
Line 1,101:
Line 1,162:
− elseif qualsonly and qualID then
− -- no value: it isn't sourced when only qualifiers are requested
− out[#out+1], mlt[#out+1] = rendersnak(v, args, linked, lprefix, lpostfix, prefix, postfix, uabbr) +
+
+
+
+
+
+
+
Line 1,139:
Line 1,205:
+
+
+
+
− qlist[#qlist + 1] = assembleoutput(propertyvalueandquals(v.qualifiers[v1], qargs), qargs) +
+
+
+
+
+
+
− +
− local ts = v1[1].datavalue.value.time+
− local dp = v1[1].datavalue.value.precision+
− qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
− else
Line 1,217:
Line 1,291:
− local langcode = args.langobj.code +
− langcode = mw.text.split( langcode, '-', true )[1]+
− +
−
Line 1,321:
Line 1,394:
− +
− -- or P276 (location) or P706 (located on terrain feature)+
Line 1,333:
Line 1,406:
+
+
− +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 1,351:
Line 1,451:
+
+
− +
− +
Line 1,394:
Line 1,496:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 1,552:
Line 1,724:
− +
+
Line 1,594:
Line 1,767:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 1,851:
Line 2,040:
− +
+
+
+
+
+
+
+
+
+
+
Line 1,871:
Line 2,070:
− +
− +
− +
− +
+
+
+
+
+
+
− +
− +
− +
− +
− +
− +
− +
− +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
− return globeid
− +
− +
− -- the Commons sitelink of the linked Wikidata item;+
−
− +
− +
− +
− +
− +
+
− +
− -- It takes an optional parameter |wiki= to determine which wiki is to be checked for a sitelink+
− -- If the parameter is blank, then it uses the local wiki.+
− +
−
− +
− return _getSitelink(frame.args.qid, frame.args.wiki or mw.text.trim(frame.args[1] or "")) +
+
+
− +
− +
− +
− +
− +
+
− +
− +
− +
− local sitelink = mw.wikibase.sitelink(itemID) +
− local label = labelOrId(itemID) +
− if sitelink then+
− +
+
− +
Line 1,942:
Line 2,300:
− +
− +
− +
+
+
+
+
− +
− +
− +
− if itemID == "" then return end
− local label = labelOrId(itemID)
− return label
− end
+
+
− -------------------------------------------------------------------------------+
− -- getAT has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=+
− -- If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text.
− -- If there is no sitelink, it returns nothing.
− -------------------------------------------------------------------------------
− -- Dependencies: none
− -------------------------------------------------------------------------------
− p.getAT = function(frame)
−
−
− return mw.wikibase.sitelink(itemID)
−
+
+
− -------------------------------------------------------------------------------+
− -- getDescription has the qid of a Wikidata entity passed as |qid=+
− -- (it defaults to the associated qid of the current article if omitted)+
− -- and a local parameter passed as the first unnamed parameter.
− -- Any local parameter passed (other than "Wikidata" or "none") becomes the return value.
− -- It returns the article description for the Wikidata entity if the local parameter is "Wikidata".
− -- Nothing is returned if the description doesn't exist or "none" is passed as the local parameter.
− -------------------------------------------------------------------------------
− -- Dependencies: none
− -------------------------------------------------------------------------------
− p.getDescription = function(frame)
−
− local itemID = mw.text.trim(frame.args.qid or "")
−
−
− return mw.wikibase.description(itemID)
− elseif desc:lower() == 'none' then
− return nil
− else
− return desc
− end
−
+
+
− -------------------------------------------------------------------------------+
− -- getAliases has the qid of a Wikidata entity passed as |qid=+
− -- (it defaults to the associated qid of the current article if omitted)+
− -- and a local parameter passed as the first unnamed parameter.
− -- It implements blacklisting and whitelisting with a field name of "alias" by default.
− -- Any local parameter passed becomes the return value.
− -- Otherwise it returns the aliases for the Wikidata entity with the usual list options.
− -- Nothing is returned if the aliases do not exist.
− -------------------------------------------------------------------------------
− -- Dependencies: findLang(); assembleoutput()
− -------------------------------------------------------------------------------
− p.getAliases = function(frame)
− local args = frame.args
−
− local fieldname = args.name or ""
− if fieldname == "" then fieldname = "alias" end
−
− local blacklist = args.suppressfields or args.spf or ""
− if blacklist:find(fieldname) then return nil end
−
− local localval = mw.text.trim(args[1] or "")
− if localval ~= "" then return localval end
−
− local whitelist = args.fetchwikidata or args.fwd or ""
− if whitelist == "" then whitelist = "NONE" end
− if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end
−
− local qid = mw.text.trim(args.qid or "")
− if qid == "" then qid = nil end
−
−
−
−
Line 2,074:
Line 2,379:
+
+
+
− +
+
+
+
+
+
+
+
Line 2,146:
Line 2,461:
− local label = labelOrId( frame.args.qid or frame.args[1] ) +
− return label
Line 2,164:
Line 2,478:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 2,214:
Line 2,542:
− +
− +
+
− +
Line 2,233:
Line 2,562:
− +
− +
+
+
+
+
− +
− +
+
+
+
Line 2,304:
Line 2,640:
+
Line 2,564:
Line 2,901:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Line 2,577:
Line 2,932:
+
+
+
+
+
+
+
+
Line 2,592:
Line 2,955:
+
Line 2,604:
Line 2,968:
+
+
copied from enwiki
local p = {}
local p = {}
local cdate = require("Module:Complex date")._complex_date
local cdate -- initialise as nil and only load _complex_date function if needed
-- [[Module:Complex date]] has the following dependencies:
-- [[Module:Complex date]] is loaded lazily and has the following dependencies:
-- Module:I18n/complex date, Module:ISOdate, Module:DateI18n (alternative for Module:Date),
-- Module:I18n/complex date, Module:ISOdate, Module:DateI18n (alternative for Module:Date),
-- Module:Formatnum, Module:I18n/date, Module:Yesno, Module:Linguistic, Module:Calendar
-- Module:Formatnum, Module:I18n/date, Module:Yesno, Module:Linguistic, Module:Calendar
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- findLang takes a "langcode" parameter if if supplied and valid
-- findLang takes a "langcode" parameter if supplied and valid
-- otherwise it tries to create it from the user's set language ({{int:lang}})
-- otherwise it tries to create it from the user's set language ({{int:lang}})
-- failing that it uses the wiki's content language.
-- failing that it uses the wiki's content language.
end
end
return langobj
return langobj
end
-------------------------------------------------------------------------------
-- _getItemLangCode takes a qid parameter (using the current page's qid if blank)
-- If the item for that qid has property country (P17) it looks at the first preferred value
-- If the country has an official language (P37), it looks at the first preferred value
-- If that official language has a language code (P424), it returns the first preferred value
-- Otherwise it returns nothing.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
local _getItemLangCode = function(qid)
qid = mw.text.trim(qid or ""):upper()
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return end
local prop17 = mw.wikibase.getBestStatements(qid, "P17")[1]
if not prop17 or prop17.mainsnak.snaktype ~= "value" then return end
local qid17 = prop17.mainsnak.datavalue.value.id
local prop37 = mw.wikibase.getBestStatements(qid17, "P37")[1]
if not prop37 or prop37.mainsnak.snaktype ~= "value" then return end
local qid37 = prop37.mainsnak.datavalue.value.id
local prop424 = mw.wikibase.getBestStatements(qid37, "P424")[1]
if not prop424 or prop424.mainsnak.snaktype ~= "value" then return end
return prop424.mainsnak.datavalue.value
end
end
-- Dependencies: findLang(); cdate(); dp[]
-- Dependencies: findLang(); cdate(); dp[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local dateFormat = function(timestamp, dprec, df, bcf, pd, qualifiers, lang, adj)
local dateFormat = function(timestamp, dprec, df, bcf, pd, qualifiers, lang, adj, model)
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
end
end
end
end
local fdate = cdate("", adj, tostring(iso), dp[dprec], bc, "", "", "", "", lang, 1)
-- deal with Julian dates:
-- no point in saying that dates before 1582 are Julian - they are by default
-- doesn't make sense for dates less precise than year
-- we can supress it by setting |plaindate, e.g. for use in constructing categories.
local calendarmodel = ""
if tonumber(year) > 1582
and dprec > 8
and not pd
and model == "http://www.wikidata.org/entity/Q1985786" then
calendarmodel = "julian"
end
if not cdate then
cdate = require("Module:Complex date")._complex_date
end
local fdate = cdate(calendarmodel, adj, tostring(iso), dp[dprec], bc, "", "", "", "", lang, 1)
-- this may have QuickStatements info appended to it in a div, so remove that
-- this may have QuickStatements info appended to it in a div, so remove that
fdate = fdate:gsub(' <div style="display: none;">[^<]*</div>', '')
fdate = fdate:gsub(' <div style="display: none;">[^<]*</div>', '')
local _getSitelink = function(qid, wiki)
local _getSitelink = function(qid, wiki)
qid = (qid or ""):upper()
qid = (qid or ""):upper()
if qid == "" then return nil end
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
wiki = wiki or ""
wiki = wiki or ""
local sitelink
local sitelink
-- _getCommonslink takes an optional qid of a Wikidata entity passed as |qid=
-- _getCommonslink takes an optional qid of a Wikidata entity passed as |qid=
-- It returns one of the following in order of preference:
-- It returns one of the following in order of preference:
-- the Commons sitelink of the linked Wikidata item;
-- the Commons sitelink of the Wikidata entity - but not if onlycat=true and it's not a category;
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-- the Commons sitelink of the topic's main category of the Wikidata entity;
-- the Commons category.
-- the Commons category of the Wikidata entity - unless fallback=false.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _getSitelink(); parseParam()
-- Dependencies: _getSitelink(); parseParam()
if not qid then return nil end
if not qid then return nil end
onlycat = parseParam(onlycat, false)
onlycat = parseParam(onlycat, false)
if fallback == "" then fallback = nil end
local sitelink = _getSitelink(qid, "commonswiki")
local sitelink = _getSitelink(qid, "commonswiki")
if onlycat and sitelink and sitelink:sub(1,9) ~= "Category:" then sitelink = nil end
if onlycat and sitelink and sitelink:sub(1,9) ~= "Category:" then sitelink = nil end
-- 4. Display unlinked label if it exists
-- 4. Display unlinked label if it exists
-- 5. Display entity-id for now to indicate a label could be provided
-- 5. Display entity-id for now to indicate a label could be provided
-- dtxt is text to be used instead of label, or nil.
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- lang is the current language code.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: labelOrId()
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt)
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 ""
prefix = prefix or ""
prefix = prefix or ""
postfix = postfix or ""
postfix = postfix or ""
lang = lang or "en" -- fallback to default if missing
local disp
local disp
local sitelink = mw.wikibase.sitelink(id)
local sitelink = mw.wikibase.sitelink(id)
if dtxt then
if dtxt then
label, islabel = dtxt, true
label, islabel = dtxt, true
elseif shortname then
-- see if there is a shortname in our language, and set label to it
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P1813") ) do
if v.mainsnak.datavalue.value.language == lang then
label, islabel = v.mainsnak.datavalue.value.text, true
break
end -- test for language match
end -- loop through values of short name
-- if we have no label set, then there was no shortname available
if not islabel then
label, islabel = labelOrId(id)
shortname = false
end
else
else
label, islabel = labelOrId(id)
label, islabel = labelOrId(id)
if mw.site.siteName ~= "Wikimedia Commons" then
if mw.site.siteName ~= "Wikimedia Commons" then
if sitelink then
if sitelink then
if not dtxt then
if not (dtxt or shortname) then
-- strip any namespace or dab from the sitelink and use that as label
-- strip any namespace or dab from the sitelink and use that as label
local pos = sitelink:find(":") or 0
local pos = sitelink:find(":") or 0
else
else
local ccat = mw.wikibase.getBestStatements(id, "P373")[1]
local ccat = mw.wikibase.getBestStatements(id, "P373")[1]
if ccat then
if ccat and ccat.mainsnak.datavalue then
ccat = ccat.mainsnak.datavalue.value
ccat = ccat.mainsnak.datavalue.value
disp = "[[" .. lprefix .. "Category:" .. ccat .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
disp = "[[" .. lprefix .. "Category:" .. ccat .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
pre = pre or ""
pre = pre or ""
post = post or ""
post = post or ""
args.lang = args.lang or findLang().code
-- allow values to display a fixed text instead of label
local dtxt = args.displaytext or args.dt
local dtxt = args.displaytext or args.dt
if dtxt == "" then dtxt = nil end
if dtxt == "" then dtxt = nil end
-- switch to use display of short name (P1813) instead of label
local shortname = args.shortname or args.sn
shortname = parseParam(shortname, false)
local snak = propval.mainsnak or propval
local snak = propval.mainsnak or propval
local dtype = snak.datatype
local dv = snak.datavalue
local dv = snak.datavalue
dv = dv and dv.value
dv = dv and dv.value
-- value and monolingual text language code returned
-- value and monolingual text language code returned
local qnumber = dv.id
local qnumber = dv.id
if linked then
if linked then
val = linkedItem(qnumber, lpre, lpost, pre, post, dtxt)
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
-- qualifiers (if any) is a nested table or nil
-- qualifiers (if any) is a nested table or nil
-- lang is given, or user language, or site language
-- lang is given, or user language, or site language
val = dateFormat(dv.time, dv.precision, args.df, args.bc, args.pd, propval.qualifiers, args.lang)
val = dateFormat(dv.time, dv.precision, args.df, args.bc, args.pd, propval.qualifiers, args.lang, "", dv.calendarmodel)
------------------------------------
------------------------------------
-- data types which are strings:
-- data types which are strings:
args.pd = pd
args.pd = pd
local lang = args.lang
local lang = args.lang or findlang().code
-- all proper values of a Wikidata property will be the same type as the first
-- all proper values of a Wikidata property will be the same type as the first
-- qualifiers don't have a mainsnak, properties do
-- qualifiers don't have a mainsnak, properties do
local datatype = objproperty[1].datatype or objproperty[1].mainsnak.datatype
local datatype = objproperty[1].datatype or objproperty[1].mainsnak.datatype
-- out holds the values for this property
-- out[] holds the values for this property
-- mlt holds the language code if the datatype is monolingual text
-- mlt[] holds the language code if the datatype is monolingual text
local out = {}
local out = {}
local mlt = {}
local mlt = {}
-- no value: it isn't sourced when onlysourced=true
-- no value: it isn't sourced when onlysourced=true
hasvalue = false
hasvalue = false
else
else
local val, lcode = rendersnak(v, args, linked, lprefix, lpostfix, prefix, postfix, uabbr)
if not val then
hasvalue = false -- rank doesn't match
elseif qualsonly and qualID then
-- suppress value returned: only qualifiers are requested
else
out[#out+1], mlt[#out+1] = val, lcode
end
end
end
local dp = v.qualifiers[v1][1].datavalue.value.precision
local dp = v.qualifiers[v1][1].datavalue.value.precision
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
elseif v1 == "P1319" then
local ts = v.qualifiers[v1][1].datavalue.value.time
local dp = v.qualifiers[v1][1].datavalue.value.precision
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "after")
else
else
local q = assembleoutput(propertyvalueandquals(v.qualifiers[v1], qargs), qargs)
-- we already deal with circa via 'sourcing circumstances'
-- either linked or unlinked *** internationalise later ***
if q ~= "circa" and not q:find("circa]]") then
qlist[#qlist + 1] = q
end
end
end
end
end
else
else
-- are there cases where qualifiers-order doesn't exist?
local ql = propertyvalueandquals(v.qualifiers, qargs)
local ql = propertyvalueandquals(v.qualifiers, qargs)
for k1, v1 in ipairs(ql) do
for k1, v1 in ipairs(ql) do
if k1 == "P1326" then
-- we already deal with circa via 'sourcing circumstances'
-- either linked or unlinked *** internationalise later ***
if v1 ~= "circa" and not v1:find("circa]]") then
qlist[#qlist + 1] = v1
qlist[#qlist + 1] = v1
end
end
-- otherwise look through the fallback languages for a match
-- otherwise look through the fallback languages for a match
if datatype == "monolingualtext" and #out >1 then
if datatype == "monolingualtext" and #out >1 then
lang = mw.text.split( lang, '-', true )[1]
local fbtbl = mw.language.getFallbacksFor( lang )
local fbtbl = mw.language.getFallbacksFor( langcode )
table.insert( fbtbl, 1, lang )
table.insert( fbtbl, 1, langcode )
local bestval = ""
local bestval = ""
local found = false
local found = false
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- _location takes Q-id and follows P131 (located in the administrative territorial entity)
-- _location takes Q-id and follows P276 (location)
-- or P131 (located in the administrative territorial entity) or P706 (located on terrain feature)
-- from the initial item to higher level territories/locations until it reaches the highest.
-- from the initial item to higher level territories/locations until it reaches the highest.
-- An optional boolean, 'first', determines whether the first item is returned (default: false).
-- An optional boolean, 'first', determines whether the first item is returned (default: false).
first = parseParam(first, false)
first = parseParam(first, false)
skip = parseParam(skip, false)
skip = parseParam(skip, false)
local locs = {"P276", "P131", "P706"}
local out = {}
local out = {}
local langcode = findLang():getCode()
local langcode = findLang():getCode()
local finished = false
local finished = false
local count = 0
local count = 0
local prevqid = ""
repeat
repeat
local prop = mw.wikibase.getBestStatements(qid, "P131")[1] or mw.wikibase.getBestStatements(qid, "P276")[1] or mw.wikibase.getBestStatements(qid, "P706")[1]
local prop
for i1, v1 in ipairs(locs) do
local proptbl = mw.wikibase.getBestStatements(qid, v1)
if #proptbl > 1 then
-- there is more than one parent location
for i2, v2 in ipairs(proptbl) do
parttbl = v2.qualifiers and v2.qualifiers.P518
if parttbl then
-- this parent location has qualifier 'applies to part' (P518)
for i3, v3 in ipairs(parttbl) do
if v3.snaktype == "value" and v3.datavalue.value.id == prevqid then
-- it has a value equal to the previous location
prop = proptbl[i2]
break
end -- of test for matching last location
end -- of loop through values of 'applies to part'
end
end -- of loop through parent locations
-- fallback to second value if match not found
prop = prop or proptbl[2]
elseif #proptbl > 0 then
prop = proptbl[1]
end
if prop then break end
end
-- check if it's an instance of (P31) a country (Q6256) and terminate the chain if it is
-- check if it's an instance of (P31) a country (Q6256) and terminate the chain if it is
local inst = mw.wikibase.getAllStatements(qid, "P31")
local inst = mw.wikibase.getAllStatements(qid, "P31")
end
end
end
end
-- get the name of this location and update qid to point to the parent location
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
out[#out+1] = linkedItem(qid, ":", "", "", "") -- get a linked value if we can
end
end
qid = prop.mainsnak.datavalue.value.id
qid, prevqid = prop.mainsnak.datavalue.value.id, qid
else
else
-- This is top-level location, so get short name except when this is the first item
-- This is top-level location, so get short name except when this is the first item
-- Use full label if there's no short name or this is the first item
-- Use full label if there's no short name or this is the first item
local prop1813 = mw.wikibase.getAllStatements(qid, "P1813")
local prop1813 = mw.wikibase.getAllStatements(qid, "P1813")
count = count + 1
count = count + 1
until finished or count >= 10 -- limit to 10 levels to avoid infinite loops
until finished or count >= 10 -- limit to 10 levels to avoid infinite loops
-- remove the first location if not quired
if not first then table.remove(out, 1) end
if not first then table.remove(out, 1) end
-- we might have duplicate text for consecutive locations, so remove them
if #out > 2 then
local plain = {}
for i, v in ipairs(out) do
-- strip any links
plain[i] = v:gsub("^%[%[[^|]*|", ""):gsub("]]$", "")
end
local idx = 2
repeat
if plain[idx] == plain[idx-1] then
-- duplicate found
local removeidx = 0
if (plain[idx] ~= out[idx]) and (plain[idx-1] == out[idx-1]) then
-- only second one is linked, so drop the first
removeidx = idx - 1
elseif (plain[idx] == out[idx]) and (plain[idx-1] ~= out[idx-1]) then
-- only first one is linked, so drop the second
removeidx = idx
else
-- pick one
removeidx = idx - (os.time()%2)
end
table.remove(out, removeidx)
table.remove(plain, removeidx)
else
idx = idx +1
end
until idx >= #out
end
return out
return out
end
-------------------------------------------------------------------------------
-- _getsumofparts scans the property 'has part' (P527) for values matching a list.
-- The list (args.vlist) consists of a string of Qids separated by spaces or any usual punctuation.
-- If the matched values have a qualifer 'quantity' (P1114), those quantites are summed.
-- The sum is returned as a number (i.e. 0 if none)
-- a table of arguments is supplied implementing the usual parameters.
-------------------------------------------------------------------------------
-- Dependencies: setRanks; parseParam; parseInput; sourced; assembleoutput;
-------------------------------------------------------------------------------
local _getsumofparts = function(args)
local vallist = (args.vlist or ""):upper()
if vallist == "" then return end
args.reqranks = setRanks(args.rank)
local f = {}
f.args = args
local qid, props = parseInput(f, "", "P527")
if not qid then return 0 end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local sum = 0
for k1, v1 in ipairs(props) do
if (onlysrc == false or sourced(v1))
and v1.mainsnak.snaktype == "value"
and v1.mainsnak.datavalue.type == "wikibase-entityid"
and vallist:match( v1.mainsnak.datavalue.value.id )
and v1.qualifiers
then
local quals = v1.qualifiers["P1114"]
if quals then
for k2, v2 in ipairs(quals) do
sum = sum + v2.datavalue.value.amount
end
end
end
end
return sum
end
end
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)
-- set a language object in the frame.args table
-- set a language object and language code in the frame.args table
frame.args.langobj = findLang(frame.args.lang)
frame.args.langobj = findLang(frame.args.lang)
frame.args.lang = frame.args.langobj.code
-- set the requested ranks flags
-- set the requested ranks flags
end -- of test for success
end -- of test for success
return nil
return nil
end
-------------------------------------------------------------------------------
-- getSumOfParts scans the property 'has part' (P527) for values matching a list.
-- The list is passed in parameter vlist.
-- It consists of a string of Qids separated by spaces or any usual punctuation.
-- If the matched values have a qualifier 'quantity' (P1114), those quantities are summed.
-- The sum is returned as a number or nothing if zero.
-------------------------------------------------------------------------------
-- Dependencies: _getsumofparts;
-------------------------------------------------------------------------------
p.getSumOfParts = function(frame)
local sum = _getsumofparts(frame.args)
if sum == 0 then return end
return sum
end
end
local qid3 = statements2[1].mainsnak.datavalue.value.id
local qid3 = statements2[1].mainsnak.datavalue.value.id
local sitelink = mw.wikibase.sitelink(qid3)
local sitelink = mw.wikibase.sitelink(qid3)
sitelink = sitelink or mw.wikibase.label(qid3)
-- if there's no local sitelink, create the sitelink from English label
if not sitelink then
local lbl = mw.wikibase.getLabelByLang(qid3, "en")
if lbl then
if lbl:sub(1,9) == "Category:" then
sitelink = mw.text.nowiki(lbl)
else
sitelink = "Category:" .. mw.text.nowiki(lbl)
end
end
end
if sitelink then
if sitelink then
if sk ~= "" then
if sk ~= "" then
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getGlobe takes an optional qid of a Wikidata entity passed as |qid=
-- getIntersectCat takes most of the usual parameters.
-- otherwise it uses the linked item for the current page.
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-- If returns the Qid of the globe used in P625 (coordinate location),
-- It takes two properties, |prop1 and |prop2 (e.g. occupation and country of citizenship)
-- or nil if there isn't one.
-- Each property's value is a wiki-base entity
-- For each value of the first parameter (ranks implemented) it fetches the value's main category
-- and then each value of the second parameter (possibly substituting a simpler description)
-- then it returns all of the categories representing the intersection of those properties,
-- (e.g. Category:Actors from Canada). A joining term may be supplied (e.g. |join=from).
-- The item's P734 (family name) is the sort key, or no sort key if there is no family name.
-- The sort key may be overridden by the parameter |sortkey (alias |sk).
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getGlobe = function(frame)
p.getIntersectCat = function(frame)
local qid = frame.args.qid or frame.args[1] or ""
frame.args.reqranks = setRanks(frame.args.rank)
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
frame.args.langobj = findLang(frame.args.lang)
local coords = mw.wikibase.getBestStatements(qid, "P625")[1]
frame.args.lang = frame.args.langobj.code
local globeid
local args = frame.args
if coords and coords.mainsnak.snaktype == "value" then
args.sep = " "
globeid = coords.mainsnak.datavalue.value.globe:match("(Q%d+)")
args.linked = "no"
local pid1 = args.prop1 or "P106"
local pid2 = args.prop2 or "P27"
if pid1 == "" or pid2 == "" then return nil end
local qid, statements1 = parseInput(frame, "", pid1)
if not qid then return nil end
local qid, statements2 = parseInput(frame, "", pid2)
if not qid then return nil end
-- topics like countries may have different names in categories from their label in Wikidata
local subs_exists, subs = pcall(mw.loadData, "Module:WikidataIB/subs")
local join = args.join or ""
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
-- linkprefix (strip quotes)
local lp = (args.linkprefix or args.lp or ""):gsub('"', '')
-- sort key (strip quotes, hyphens and periods):
local sk = (args.sortkey or args.sk or ""):gsub('["-.]', '')
-- family name:
local famname = ""
if sk == "" then
local p734 = mw.wikibase.getBestStatements(qid, "P734")[1]
local p734id = p734 and p734.mainsnak.snaktype == "value" and p734.mainsnak.datavalue.value.id or ""
famname = mw.wikibase.sitelink(p734id) or ""
-- strip namespace and disambigation
local pos = famname:find(":") or 0
famname = famname:sub(pos+1):gsub("%s%(.+%)$", "")
if famname == "" then
local lbl = mw.wikibase.label(p734id)
famname = lbl and mw.text.nowiki(lbl) or ""
end
end
local cat1 = {}
for k, v in ipairs(statements1) do
if not onlysrc or sourced(v) then
-- get the ID representing the value of the property
local pvalID = (v.mainsnak.snaktype == "value") and v.mainsnak.datavalue.value.id
if pvalID then
-- get the topic's main category (P910) for that entity
local p910 = mw.wikibase.getBestStatements(pvalID, "P910")[1]
if p910 and p910.mainsnak.snaktype == "value" then
local tmcID = p910.mainsnak.datavalue.value.id
-- use sitelink or the English label for the cat
local cat = mw.wikibase.sitelink(tmcID)
if not cat then
local lbl = mw.wikibase.getLabelByLang(tmcID, "en")
if lbl then
if lbl:sub(1,9) == "Category:" then
cat = mw.text.nowiki(lbl)
else
cat = "Category:" .. mw.text.nowiki(lbl)
end
end
end
cat1[#cat1+1] = cat
end -- of test for topic's main category exists
end -- of test for property has vaild value
end -- of test for sourced
if maxvals > 0 and #cat1 >= maxvals then break end
end
local cat2 = {}
for k, v in ipairs(statements2) do
if not onlysrc or sourced(v) then
local cat = rendersnak(v, args)
if subs[cat] then cat = subs[cat] end
cat2[#cat2+1] = cat
end
if maxvals > 0 and #cat2 >= maxvals then break end
end
out = {}
for k1, v1 in ipairs(cat1) do
for k2, v2 in ipairs(cat2) do
if sk ~= "" then
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "|" .. sk .. "]]"
elseif famname ~= "" then
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "|" .. famname .. "]]"
else
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "]]"
end -- of check for sort keys
end
end
args.noicon = "true"
return assembleoutput(out, args, qid, pid1)
end
-------------------------------------------------------------------------------
-- getGlobe takes an optional qid of a Wikidata entity passed as |qid=
-- otherwise it uses the linked item for the current page.
-- If returns the Qid of the globe used in P625 (coordinate location),
-- or nil if there isn't one.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getGlobe = function(frame)
local qid = frame.args.qid or frame.args[1] or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
local coords = mw.wikibase.getBestStatements(qid, "P625")[1]
local globeid
if coords and coords.mainsnak.snaktype == "value" then
globeid = coords.mainsnak.datavalue.value.globe:match("(Q%d+)")
end
return globeid
end
-------------------------------------------------------------------------------
-- getCommonsLink takes an optional qid of a Wikidata entity passed as |qid=
-- It returns one of the following in order of preference:
-- the Commons sitelink of the linked Wikidata item;
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-------------------------------------------------------------------------------
-- Dependencies: _getCommonslink(); _getSitelink(); parseParam()
-------------------------------------------------------------------------------
p.getCommonsLink = function(frame)
local oc = frame.args.onlycat or frame.args.onlycategories
local fb = parseParam(frame.args.fallback or frame.args.fb, true)
return _getCommonslink(frame.args.qid, oc, fb)
end
-------------------------------------------------------------------------------
-- getSitelink takes the qid of a Wikidata entity passed as |qid=
-- It takes an optional parameter |wiki= to determine which wiki is to be checked for a sitelink
-- If the parameter is blank, then it uses the local wiki.
-- If there is a sitelink to an article available, it returns the plain text link to the article
-- If there is no sitelink, it returns nil.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getSiteLink = function(frame)
return _getSitelink(frame.args.qid, frame.args.wiki or mw.text.trim(frame.args[1] or ""))
end
-------------------------------------------------------------------------------
-- getLink has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- If there is a sitelink to an article on the local Wiki, it returns a link to the article
-- with the Wikidata label as the displayed text.
-- If there is no sitelink, it returns the label as plain text.
-- If there is no label in the local language, it displays the qid instead.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getLink = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
if itemID == "" then return end
local sitelink = mw.wikibase.sitelink(itemID)
local label = labelOrId(itemID)
if sitelink then
return "[[:" .. sitelink .. "|" .. label .. "]]"
else
return label
end
end
end
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getCommonsLink takes an optional qid of a Wikidata entity passed as |qid=
-- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- It returns one of the following in order of preference:
-- It returns the Wikidata label for the local language as plain text.
-- If there is no label in the local language, it displays the qid instead.
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _getCommonslink(); _getSitelink(); parseParam()
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getCommonsLink = function(frame)
p.getLabel = function(frame)
local oc = frame.args.onlycat or frame.args.onlycategories
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
local fb = parseParam(frame.args.fallback or frame.args.fb, true)
if itemID == "" then return end
return _getCommonslink(frame.args.qid, oc, fb)
local label = labelOrId(itemID)
return label
end
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getSitelink takes the qid of a Wikidata entity passed as |qid=
-- getAT (Article Title)
-- has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text.
-- If there is a sitelink to an article available, it returns the plain text link to the article
-- If there is no sitelink, it returns nothing.
-- If there is no sitelink, it returns nil.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getSiteLink = function(frame)
p.getAT = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
if itemID == "" then return end
return mw.wikibase.sitelink(itemID)
end
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getLink has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- getDescription has the qid of a Wikidata entity passed as |qid=
-- If there is a sitelink to an article on the local Wiki, it returns a link to the article
-- (it defaults to the associated qid of the current article if omitted)
-- with the Wikidata label as the displayed text.
-- and a local parameter passed as the first unnamed parameter.
-- If there is no sitelink, it returns the label as plain text.
-- Any local parameter passed (other than "Wikidata" or "none") becomes the return value.
-- If there is no label in the local language, it displays the qid instead.
-- It returns the article description for the Wikidata entity if the local parameter is "Wikidata".
-- Nothing is returned if the description doesn't exist or "none" is passed as the local parameter.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getLink = function(frame)
p.getDescription = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
local desc = mw.text.trim(frame.args[1] or "")
if itemID == "" then return end
local itemID = mw.text.trim(frame.args.qid or "")
if itemID == "" then itemID = nil end
if desc:lower() == 'wikidata' then
return mw.wikibase.description(itemID)
return "[[:" .. sitelink .. "|" .. label .. "]]"
elseif desc:lower() == 'none' then
return nil
else
else
return label
return desc
end
end
end
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- getAliases has the qid of a Wikidata entity passed as |qid=
-- It returns the Wikidata label for the local language as plain text.
-- (it defaults to the associated qid of the current article if omitted)
-- If there is no label in the local language, it displays the qid instead.
-- and a local parameter passed as the first unnamed parameter.
-- It implements blacklisting and whitelisting with a field name of "alias" by default.
-- Any local parameter passed becomes the return value.
-- Otherwise it returns the aliases for the Wikidata entity with the usual list options.
-- Nothing is returned if the aliases do not exist.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: findLang(); assembleoutput()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getLabel = function(frame)
p.getAliases = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
local args = frame.args
local fieldname = args.name or ""
if fieldname == "" then fieldname = "alias" end
local blacklist = args.suppressfields or args.spf or ""
if blacklist:find(fieldname) then return nil end
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
if itemID == "" then return end
end
local localval = mw.text.trim(args[1] or "")
if localval ~= "" then return localval end
local whitelist = args.fetchwikidata or args.fwd or ""
if whitelist == "" then whitelist = "NONE" end
if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end
local desc = mw.text.trim(frame.args[1] or "")
if itemID == "" then itemID = nil end
if desc:lower() == 'wikidata' then
end
local qid = mw.text.trim(args.qid or "")
if qid == "" then qid = nil end
local entity = mw.wikibase.getEntity(qid)
if not entity then return nil end
local aliases = entity.aliases
local entity = mw.wikibase.getEntity(qid)
if not entity then return nil end
local aliases = entity.aliases
if not aliases then return nil end
if not aliases then return nil end
if not qid then qid= mw.wikibase.getEntityIdForCurrentPage() end
if not qid then qid= mw.wikibase.getEntityIdForCurrentPage() end
-- optional boolean parameter first toggles the display of the first item
-- optional boolean parameter first toggles the display of the first item
-- optional boolean parameter skip toggles the display to skip to the last item
-- optional boolean parameter skip toggles the display to skip to the last item
-- parameter debug=<y/n> (default 'n') adds error msg if not a location
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _location();
-- Dependencies: _location();
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.location = function(frame)
p.location = function(frame)
local debug = (frame.args.debug or ""):sub(1, 1):lower()
if debug == "" then debug = "n" end
local qid = mw.text.trim(frame.args.qid or frame.args[1] or ""):upper()
local qid = mw.text.trim(frame.args.qid or frame.args[1] or ""):upper()
if qid == "" then return "No id supplied" end
if qid == "" then qid=mw.wikibase.getEntityIdForCurrentPage() end
if not qid then
if debug ~= "n" then
return i18n.errors["entity-not-found"]
else
return nil
end
end
local first = mw.text.trim(frame.args.first or "")
local first = mw.text.trim(frame.args.first or "")
local skip = mw.text.trim(frame.args.skip or "")
local skip = mw.text.trim(frame.args.skip or "")
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.labelorid = function(frame)
p.labelorid = function(frame)
return labelOrId( frame.args.qid or frame.args[1] )
end
end
end
end
return langcode
return langcode
end
-------------------------------------------------------------------------------
-- getItemLangCode takes a qid parameter (using the current page's qid if blank)
-- If the item for that qid has property country (P17) it looks at the first preferred value
-- If the country has an official language (P37), it looks at the first preferred value
-- If that official language has a language code (P424), it returns the first preferred value
-- Otherwise it returns nothing.
-------------------------------------------------------------------------------
-- Dependencies: _getItemLangCode()
-------------------------------------------------------------------------------
p.getItemLangCode = function(frame)
return _getItemLangCode(frame.args.qid or frame.args[1])
end
end
-- If props is given, the Wikidata item for the qid is examined for each property in turn.
-- If props is given, the Wikidata item for the qid is examined for each property in turn.
-- If that property contains a value that is another Wikibase-item, that item's qid is returned,
-- If that property contains a value that is another Wikibase-item, that item's qid is returned,
-- and the search terminates.
-- and the search terminates, unless |all=y when all of the qids are returned, sparated by spaces.
-- If props is not given, the qid is returned.
-- If props is not given, the qid is returned.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.followQid = function(frame)
p.followQid = function(frame)
local qid = (frame.args.qid or ""):upper()
local qid = (frame.args.qid or ""):upper()
local all = parseParam(frame.args.all, false)
if qid == "" then
if qid == "" then
qid = mw.wikibase.getEntityIdForCurrentPage()
qid = mw.wikibase.getEntityIdForCurrentPage()
end
end
if not qid then return nil end
if not qid then return nil end
local out = {}
local props = (frame.args.props or ""):upper()
local props = (frame.args.props or ""):upper()
if props ~= "" then
if props ~= "" then
local linkedid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
local linkedid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
if linkedid then
if linkedid then
return linkedid
if all then
end
out[#out+1] = linkedid
else
return linkedid
end -- test for all or just the first one found
end -- test for value exists for that property
end -- loop through values of property to follow
end -- loop through values of property to follow
end -- loop through list of properties to follow
end -- loop through list of properties to follow
end
end
if #out > 0 then
return qid
return table.concat(out, " ")
else
return qid
end
end
end
return lang:formatNum( num )
return lang:formatNum( num )
end
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
table.sort( out )
table.sort( out )
return table.concat(out, "; ")
return table.concat(out, "; ")
end
-------------------------------------------------------------------------------
-- checkValidity checks whether the first unnamed parameter represents a valid entity-id,
-- that is, something like Q1235 or P123.
-- It returns the strings "true" or "false".
-- Change false to nil to return "true" or "" (easier to test with #if:).
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
function p.checkValidity(frame)
local id = mw.text.trim(frame.args[1] or "")
if mw.wikibase.isValidEntityId(id) then
return true
else
return false
end
end
end
-- getCoords
-- getCoords
-- getQualifierValue
-- getQualifierValue
-- getSumOfParts
-- getValueByQual
-- getValueByQual
-- getValueByLang
-- getValueByLang
-- getValueByRefSource
-- getPropOfProp
-- getAwardCat
-- getIntersectCat
-- getGlobe
-- getCommonsLink
-- getSiteLink
-- getLink
-- getLink
-- getLabel
-- getLabel
-- getLang
-- getLang
-- findLanguage
-- findLanguage
-- getQID
-- getQID
-- followQid
-- followQid
-- getAllDescriptions
-- getAllDescriptions
-- getAllAliases
-- getAllAliases
-- showNoLinks
-- checkValidity
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------