Jump to content
Wikipedia The Free Encyclopedia

Module:Sandbox/RexxS/AA

From Wikipedia, the free encyclopedia
Module documentation[create] [purge]
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
 -- Testing for arbitrary access
 -- Intended to be:
 -- Use : {{#invoke:Wikidata|getValueFromID|<ID>|<Property>|FETCH_WIKIDATA}}
 -- E.g.: {{#invoke:Wikidata|getValueFromID|Q151973|P26|FETCH_WIKIDATA}} - to fetch value of 'spouse' (P26) from 'Richard Burton' (Q151973)
 -- While in sandbox:
 -- Use : {{#invoke:Sandbox/RexxS/AA|getValueFromID|<ID>|<Property>|FETCH_WIKIDATA}}
 -- E.g.: {{#invoke:Sandbox/RexxS/AA|getValueFromID|Q151973|P26|FETCH_WIKIDATA}} - to fetch value of 'spouse' (P26) from 'Richard Burton' (Q151973)

 local p = {}

 -- This is used to get a value, or a comma separated list of them if multiple values exist
 p.getValueFromID = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	local propertyID = mw.text.trim(frame.args[2] or "")
	local input_parm = mw.text.trim(frame.args[3] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity(itemID)
		local claims = entity.claims[propertyID]
		if claims then
			-- if wiki-linked value output as link if possible
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				local out = {}
				for k, v in pairs(claims) do
					local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
							
					if sitelink then
						out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
					else
						out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>"
					end
				end
				return table.concat(out, ", ")
			else
				return entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
			end
		else
			return ""
		end
	else
		return input_parm
	end
 end


 -- A function to return the QID of a property value, rather than its text label
 -- May be useful for constructing chains of calls to get properties of properties, etc.
 -- It returns the QID of only the first property value if more than one
 -- Use like this: {{#invoke:Sandbox/RexxS/AA|getQIDFromID|Q151973|P26|FETCH_WIKIDATA}}
 -- That will fetch the QID of the first value for the spouse (P26) of Richard Burton (Q151973)
 -- Returns an empty string if the value doesn't exist or has no QID.

 p.getQIDFromID = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	local propertyID = mw.text.trim(frame.args[2] or "")
	local input_parm = mw.text.trim(frame.args[3] or "")

	local entity = mw.wikibase.getEntity(itemID)
	local claims = entity.claims[propertyID]
	if claims then
		-- if wiki-linked value return the QID of the first value of the property
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
			return "Q" .. claims[1].mainsnak.datavalue.value["numeric-id"]
		else
			return ""
		end
	else
		return ""
	end
 end


 return p

AltStyle によって変換されたページ (->オリジナル) /