モジュール:RexxS

-- 
-- Lua functions for personal use
-- 
p = {}
-- carousel returns one of a list of image filenames
-- the index of the one chosen increments every 'switchsecs'
-- which is a parameter giving the number of seconds between switches
-- 3600 would switch every hour
-- 43200 would be every 12 hours (the default)
-- 86400 would be daily
-- switchsecs=
-- carousel for 12 hours between switches
p.carousel = function(frame)
	local switchtime = tonumber(frame.args.switchsecs) or 43200
	if switchtime  seconds - default is 4
-- switchsecs=
-- wobble for 4 seconds between switches
p.wobble = function(frame)
	local switchtime = tonumber(frame.args.switchsecs) or 4
	if switchtime Warning: ' .. msg .. ' (this message is shown only in preview).'
	end
end
-- sandyrock returns a pseudo-random message inline
-- the style is customisable with the 'style' parameter
-- other messages may be added or substituted, just separate with a comma
p.sandyrock = function(frame)
	local style = frame.args.style or ""
	local msgs = {
		"You make Vogons look frivolous",
		"You're the poster-child for the phrase 'bureaucratic nightmare'",
		"How much did they pay for your life-story when they were filming Brazil?",
		"Discretion-ary sanctions? You are to 'discretion' what 'bull' is to 'china-shop'.",
		"Using you for Arbitration Enforcement is like employing King Herod as a baby-sitter."
		}
	local idx = os.time() % #msgs +1
	return  .. msgsidx .. 
end
-- getLink returns the label for a Qid linked to the article
p.getLink = function(frame)
	local itemID = mw.text.trim(frame.args1 or "")
	if itemID == "" then return end
	local sitelink = mw.wikibase.sitelink(itemID)
	local label = mw.wikibase.label(itemID)
	if not label then label = itemID end
	if sitelink then
		return "" .. label .. ""
	else
		return label
	end
end
-- getTitle returns the label for a Qid linked to the article, without the link that getLink returns
p.getTitle = function(frame)
local itemID = mw.text.trim(frame.args1 or "")
if itemID == "" then return end
local label = mw.wikibase.label(itemID)
if not label then label = itemID end
return label
end
-- getAT returns the article title for a Qid
p.getAT = function(frame)
	local itemID = mw.text.trim(frame.args1 or "")
	if itemID == "" then return end
	return mw.wikibase.sitelink(itemID)
end
-- getDescription returns the Wikidata item description for a Qid
p.getDescription = function(frame)
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	local desc = mw.wikibase.description(qid)
	if desc then return mw.text.nowiki(desc) else return nil end
end
-- getIdentifierQualifier returns the value of a qualifier for an Identifier
-- such as 'Art UK artist ID', P1367
-- the assumption is that one value exists for the property
-- and only one qualifier exists for that value
-- Constraint violations for P1367 are at:
-- https://www.wikidata.org/wiki/Wikidata:Database_reports/Constraint_violations/P1367#Single_value
-- Now in Module:WikidataIdentifiers
p.getIdentifierQualifier = function(frame)
	local propertyID = mw.text.trim(frame.args1 or "")
	-- The PropertyID of the qualifier
	-- whose value is to be returned is passed in named parameter |qual=
	local qualifierID = frame.args.qual
	
	-- Can take a named parameter |qid which is the Wikidata ID for the article.
	-- This will not normally be used because it's an expensive call.
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	local entity = mw.wikibase.getEntityObject(qid)
	local props
	if entity and entity.claims then
		props = entity.claimspropertyID
	end
	if props then
		-- Check that the first value of the property is an external id
		if props1.mainsnak.datatype == "external-id" then
			-- get any qualifiers of the first value of the property
			local quals = props1.qualifiers
			if quals and qualsqualifierID then
				-- check what the dataype of the first qualifier value is
				-- if it's quantity return the amount
				if qualsqualifierID1.datatype == "quantity" then
					return tonumber(qualsqualifierID1.datavalue.value.amount)
				end
				-- checks for other datatypes go here:
			end
			return "no qualifier"
		else
			return "not external id"
		end
		return "no property"
	end
	return "no claims"
end
-- getValueIndirect returns the value of a property of a value of a property
-- for example the 'headquarters location property, P159' (property1) may be a city (value 1)
-- that city should be a wikibase-entity with its own entry
-- that entry should have a 'country property, P17' (property2), which will have a value which is the country (value2)
-- the assumption is that one value (value1) exists for property1
-- and only one value exists for that property2 of value1
-- Constraint violations for P17 are at:
-- https://www.wikidata.org/wiki/Wikidata:Database_reports/Constraint_violations/P17#Single_value
-- This is intrinsically an expensive call
p.getValueIndirect = function(frame)
	local property1 = mw.text.trim(frame.args1 or "")
	local property2 = mw.text.trim(frame.args2 or "")
	
	-- Can take a named parameter |qid which is the Wikidata ID for the article.
	-- This will not normally be used because it's an expensive call.
	local qid1 = frame.args.qid
	if qid1 and (#qid1 == 0) then qid1 = nil end
	local entity1 = mw.wikibase.getEntityObject(qid1)
	local props1
	if entity1 and entity1.claims then
		props1 = entity1.claimsproperty1
	end
	if props1 then
		-- Check that the first value of the property is a wikibase-item
		if props11.mainsnak.datatype == "wikibase-item" then
			local qid2 = props11.mainsnak.datavalue.value.id
			local entity2 = mw.wikibase.getEntityObject(qid2)
			if entity2.claims then
				-- only need props2 if we want a more sophisticated parsing, e.g. mdy dates
				-- local props2 = entity2.claimsproperty2
				return entity2:formatPropertyValues(property2).value
			else
				return qid2 .. " has no claims."
			end
		else
			return "not wikibase-item: " .. props11.mainsnak.datatype --debug
		end
	end
	return "no claims"
end
-- checkPage creates a "title object" for the given title and namespace
-- named parameters:
-- art = title of article/page
-- ns = namespace number - defaults to 0 (mainspace) if omitted
-- returns 0 if page does not exist or a positive number if it does
-- art=
-- ns=
p.checkPage = function(frame)
	local article = mw.text.trim(frame.args.art or "")
	local ns = tonumber(frame.args.ns or 0)
	if mw.site.namespacesns then
		if article>"" then
			local t = mw.title.new(article, ns)
			return t.id
		end
		return "No article name given"
	end
	return "Invalid namespace"
end
-- checkRedirect creates a "title object" for the given title and namespace
-- named parameters:
-- art = title of article/page
-- ns = namespace number - defaults to 0 (mainspace) if omitted
-- returns Redirect / Not Redirect / Does not exist
-- art=
-- ns=
p.checkRedirect = function(frame)
	local article = mw.text.trim(frame.args.art or "")
	local ns = tonumber(frame.args.ns or 0)
	if mw.site.namespacesns then
		if article > "" then
			local t = mw.title.new(article, ns)
			if t.id > 0 then
				if t.isRedirect then
					return "Redirect"
				end
				return "Not Redirect"
			end
			return "Does not exist"
		end
		return "No article name given"
	end
	return "Invalid namespace"
end
-- getAuthors for Andy 
-- pass the Q-id of the source (book, etc.) in qid
-- returns a list in the form |author1=firstname secondname |author2= ...
p.getAuthors = function(frame)
	local propertyID = "P50"
	
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	
	-- wdlinks is a boolean passed to enable links to Wikidata when no article exists
	-- if "false" or "no" or "0" is passed set it false
	-- if nothing or an empty string is passed set it false
	local wdl = frame.args.wdlinks
	if wdl and (#wdl > 0) then
		wdl = wdl:lower()
		if (wdl == "false") or (wdl == "no") or (wdl == "0") then
			wdl = false
		else
			wdl = true
		end
	else
		-- wdl is empty, so
		wdl = false
	end
	
	local entity, props
	local entity = mw.wikibase.getEntity(qid)
	if entity and entity.claims then
		props = entity.claimspropertyID
	else
		-- there's no such entity or no claims for the entity
		return nil
	end
	
	-- Make sure it actually has the property requested
	if not props or not props1 then 
		return nil
	end
	
	-- So now we have something to return:
	-- table 'out' is going to to store the return value(s):
	local out = {}
	if props1.mainsnak.datavalue.type == "wikibase-entityid" then
		-- it's wiki-linked value, so output as link if possible
		for k, v in pairs(props) do
			local qnumber = "Q" .. v.mainsnak.datavalue.value"numeric-id"
			local sitelink = mw.wikibase.sitelink(qnumber)
			local label = mw.wikibase.label(qnumber)
			if label then
				label = mw.text.nowiki(label)
			else
				label = qnumber
			end
			if sitelink then
				out#out + 1 = "" .. label .. ""
			else
				-- no sitelink, so check first for a redirect with that label
				local artitle = mw.title.new(label, 0)
				if artitle.id > 0 then
					if artitle.isRedirect then
						-- no sitelink, but there's a redirect with the same title as the label; let's link to that
						out#out + 1 = "" .. label .. ""
					else
						-- no sitelink and not a redirect but an article exists with the same title as the label
						-- that's probably a dab page, so output the plain label
						out#out + 1 = label
					end
				else
					-- no article or redirect with the same title as the label
					if wdl then
						-- show that there's a Wikidata entry available
						out#out + 1 = ":d:Q" .. v.mainsnak.datavalue.value"numeric-id" .. "|" .. label .. " link="
					else
						-- no wikidata links wanted, so just give the plain label
						out#out + 1 = label
					end
				end
			end
		end
	else
		-- not a linkable article title
		out#out+1 = entity:formatPropertyValues(propertyID).value
	end
	
	-- if there's anything to return, then return a list
	-- in the form |author1=firstname secondname |author2= ...
	if #out > 0 then
		-- construct the list in the format we want
		for k,v in ipairs(out) do
			outk = "|author" .. k .. "=" .. v
		end
		return table.concat(out, " ")
	end
end
p.checkBlacklist = function(frame)
	local blacklist = frame.args.suppressfields
	local fieldname = frame.args.name
	if blacklist and fieldname then
		if blacklist:find(fieldname) then return nil end
		return true
	end
end
return p