Module:Autres projets/Bac à sable

 Documentation[créer] [purger]
-- dumpPath() est un tostring() avancé. Il est récursif et affiche 1 ligne par valeur signifiante.
-- Par exemple, dumpPath('entity', mw.wikibase.getEntity()) retourne:
--   entity.type=string:item
--   entity.sitelinks.nlwiki.title=string:Nachtorchis
--   entity.sitelinks.nlwiki.site=string:nlwiki
--   entity.sitelinks.plwiki.title=string:Podkolan
--   entity.sitelinks.plwiki.site=string:plwiki
--   ...
--   entity.descriptions.es.language=string:es
--   entity.descriptions.es.value=string:'género de plantas de la familia Orchidaceae'
--   entity.descriptions.de.language=string:de
--   entity.descriptions.de.value=string:'Gattung der Familie der Orchideen (Orchidaceae)'
--   ...
--   entity.id=string:Q161849
--   entity.claims.p910[0].mainsnak.snaktype=string:value
--   entity.claims.p910[0].mainsnak.property=string:P910
--   entity.claims.p910[0].mainsnak.datavalue.value.numeric-id=number:8765698
--   entity.claims.p910[0].mainsnak.datavalue.value.entity-type=string:item
--   entity.claims.p910[0].mainsnak.datavalue.type=string:wikibase-entityid
--   entity.claims.p910[0].type=string:statement
--   entity.claims.p910[0].id=string:Q161849$10B4A9CE-CF8C-4D2D-A0E0-AE494A71EBE1
function dumpPath(path, value)
	local visited, lines, bytes = {}, {}, 0
	local function visit(path, value)
		-- check quota to stop early
		if bytes > 60000 then return end
		if type(value) ~= 'number' then
			if type(value) == 'table' then
				-- Because some objects expose non idempotent properties, if an object sets
				-- its own __tostring() metamethod (e.g. mw.title objects), use it, and don't
				-- recurse inside its keys. Otherwise this could create an infinite loop, such as
				-- between talkPageTitle() and subjectPageTitle() which don't use a cache to make
				-- sure they will return the same object reference via distinct access paths.
				if getmetatable(value).__tostring == nil then
					-- make sure we'll not recurse in an already visited table reference
					if visited[value] then return end
					visited[value] = true
					-- now perform the recursion in each key
					for key, value in pairs(value) do
						if type(key) == 'string' then
							if key:find('^[%a_][%w_]*$') == 1 then
								visit(path .. '.' .. key, value)
							else
								visit(path .. "['" .. key:gsub('\\', '\\\\'):gsub("'", "\'") .. "']", value)
							end
						elseif type(key) == 'number' then
							visit(path .. '[' .. key .. ']', value)
						end
					end
					return
				end
			end
			value =  "'" .. tostring(value):gsub("\\", "\\\\"):gsub("'", "\'") .. "'"
		end
		value = path .. '=' .. value .. '<br />\n'
		table.insert(lines, value)
		-- update quota
		bytes = bytes + #value
	end
	visit(path, value)
	-- it saves lots of resources to concatenate all lines at once instead of incrementally
	return table.concat(lines)
end

-- getProperty(entity, true, {'claims','P301',0,'mainsnak','datavalue','value'}) retourne la même chose que
-- entity.claims.P301[0].mainsnak.datavalue.value, sauf qu'il ne génère pas d'erreur.
-- Au lieu de lever une erreur il retourne nil si not verbose ou une explication (comme 'entity.claims.P301[0]=nil') si verbose
function getProperty(entity, verbose, propertyPath)
	local property = entity
	local path = ''
	for _, key in ipairs(propertyPath) do
		if not property then break end
		property = property[key]
		if verbose then
			if type(key) == 'number' then
				path = path .. '[' .. key .. ']'
			else
				key = tostring(key)
				if key:find('^[%a_][%w_]*$') == 1 then
					path = path .. '.' .. key
				else
					path = path .. "['" .. key:gsub('\\', '\\\\'):gsub("'", "\'") .. "']"
				end
			end
		end
	end
	if property ~= nil then
		return property
	elseif verbose then
		return currentPath .. "=nil"
	else
		return nil
	end
end

function getWikidataCommons()
	local entity = mw.wikibase.getEntity()
	local wikidatacommons = getProperty(entity, false, {'claims', 'P373' , 0, 'mainsnak', 'datavalue', 'value'})
	if wikidatacommons ~= nil then
		return 'Category:' .. wikidatacommons
	end
	local wikidatacommons = getProperty(entity, false, {'sitelinks', 'commonswiki' , 'title'})
	if wikidatacommons ~= nil then
		return wikidatacommons
	end
	return nil
	--return dumpPath('entity', entity)
end

function getCommons(args)
	local commons = args.commons
	if commons == nil or commons == '' then
		commons = getWikidataCommons()
	end
	if commons ~= nil and commons ~= '' then
		local commonstitre = args.commonstitre
		if commonstitre == nil or commonstitre == '' then
			commonstitre = mw.title.getCurrentTitle().fullText
		end
		return '[https://commons.wikimedia.org/wiki/' .. commons:gsub(' ', '_') .. '?uselang=fr ' .. commonstitre .. ']'
	end
	return nil
end

function test()
	return dumpPath('mw.title.getCurrentTitle()',mw.title.getCurrentTitle())
end

local p = {}

function p.getCommons(frame)
	return getCommons(frame.args)
end

function p.test(frame)
	return test(frame.args)
end

return p