Difference between revisions of "Module:RexxS"

From World Samba
Jump to: navigation, search
(we're looking for a wikibase-item, not an external id)
m (1 revision imported)
(No difference)

Latest revision as of 18:41, 4 March 2017

Documentation for this module may be created at Module:RexxS/doc

-- 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
-- {{#invoke:RexxS|carousel|switchsecs=<number-of-seconds>}}
-- {{#invoke:RexxS|carousel}} for 12 hours between switches
p.carousel = function(frame)
	local switchtime = tonumber(frame.args.switchsecs) or 43200
	if switchtime < 1 then switchtime = 43200 end
	local imgs = {
		"Pelicans 11.3.2007.jpg",
		"Great Blue Heron and immature Bald Eagle on the Platte River.jpg",
		"Green Heron4.jpg",
		"Canada Goose mating ritual2.jpg",
		"Flock of Cedar Waxwings3.jpg",
		"Lightning 7.11.2008.jpg",
		"Ursus americanus.jpg",
		"Stone Creek Nebraska.jpg",
		"Grus canadensis2.jpg",
		"Scaphirhynchus platorynchus 6.14.2014a.jpg",
		"Bison Bull in Nebraska.jpg",
		"Horses and thunderstorm1.jpg",
		"Lillypads at Desoto.jpg",
		"Steamboat Geyser.jpg",
		"Lake view from Beartooth Pass.jpg",
		"Tetons from Togwotee Pass.jpg",
		"Inspiration Point.jpg",
		"Flowers b grow to 6 feet.jpg",
		"Storm Front2.jpg"
	local numimgs = #imgs
	local now = math.floor(os.time()/switchtime)
	local idx = now % numimgs +1
	return imgs[idx]

-- wobble returns CSS which rotates the container
-- possible angles are -4, -2, 0, 2, 4 degrees
-- a different angle is selected every <switchtime> seconds - default is 4
-- {{#invoke:RexxS|wobble|switchsecs=<number-of-seconds>}}
-- {{#invoke:RexxS|wobble}} for 4 seconds between switches
p.wobble = function(frame)
	local switchtime = tonumber(frame.args.switchsecs) or 4
	if switchtime < 1 then switchtime = 4 end
	local now = math.floor(os.time()/switchtime)
	local angle = 2 * (now % 5 -2 )
	return "-moz-transform:rotate(" .. angle .. "deg);-webkit-transform:rotate(" .. angle .. "deg); transform:rotate(" .. angle .. "deg);"

-- prevwarn returns a hatnote-style warning message in red
-- the customisable text of the warning is passed in the parameter 'message'
-- it only returns the warning in preview mode
-- note that a blank {{REVISIONID}} is a way of identifying preview mode.
-- {{#invoke:RexxS|prevwarn|message=the religion parameter will be removed soon.}}
p.prevwarn = function(frame)
	local msg = frame.args.message
	if frame:preprocess( "{{REVISIONID}}" ) == "" then
		return '<div class="hatnote" style="color:red"><strong>Warning:</strong> ' .. msg .. ' (this message is shown only in preview).</div>'

-- 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
-- {{#invoke:RexxS|sandyrock|style=color:#C00;}}
p.sandyrock = function(frame)
	local style = frame.args.style
	local msgs = {
		"You make [[Vogon]]s 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]]?"
	local idx = os.time() % #msgs +1
	return '<span style="'.. style .. '">' .. msgs[idx] .. '</span>'

-- getLink returns the label for a Qid linked to the article
p.getLink = function(frame)
	local itemID = mw.text.trim(frame.args[1] 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 "[[" .. sitelink .. "|" .. label .. "]]"
		return label

-- getAT returns the article title for a Qid
p.getAT = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	if itemID == "" then return end
	return mw.wikibase.sitelink(itemID)

-- 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.args[1] 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.claims[propertyID]
	if props then
		-- Check that the first value of the property is an external id
		if props[1].mainsnak.datatype == "external-id" then
			-- get any qualifiers of the first value of the property
			local quals = props[1].qualifiers
			if quals and quals[qualifierID] then
				-- check what the dataype of the first qualifier value is
				-- if it's quantity return the amount
				if quals[qualifierID][1].datatype == "quantity" then
					return tonumber(quals[qualifierID][1].datavalue.value.amount)
				-- checks for other datatypes go here:
			return "no qualifier"
			return "not external id"
		return "no property"
	return "no claims"

-- 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.args[1] or "")
	local property2 = mw.text.trim(frame.args[2] 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.claims[property1]
	if props1 then
		-- Check that the first value of the property is a wikibase-item
		if props1[1].mainsnak.datatype == "wikibase-item" then
			local qid2 = props1[1].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.claims[property2]
				return entity2:formatPropertyValues(property2).value
				return qid2 .. " has no claims."
			return "not wikibase-item: " .. props1[1].mainsnak.datatype --debug
	return "no claims"

return p