Modul:Failsafe

Aus skandinavien-wiki.net

Die Dokumentation für dieses Modul kann unter Modul:Failsafe/doc erstellt werden

-- module interface
local Failsafe = {
	suite  = 'Failsafe',
	serial = '2019-07-09',
	item   = 65306115
}

-- modul variable
local fs = {}

-- possible vaules of version
--  nil, false: no version required
--  string:     string of version required
--  table:      frame table (template, #invoke)
-- moduleInterface
--  table

function fs._failsafe( version, moduleInterface )
	if type( moduleInterface ) ~= 'table' then
		return false
	end

	local i, s, v

	-- check if version is a frame table
	if type( version ) == 'table' and type( version.args ) == 'table' then
		s = version.args[ 1 ]
		if not s and type( version.getParent ) == 'function' then
			i = version:getParent()
			if i then
				s = i.args[ 1 ]
			end
		end
		version = s
	end
	-- version should be a string
	if type( version ) == 'string' then
		-- check if empty string
		version = mw.text.trim( version )
		if version == '' then
			version = nil
		end
	else
		version = nil
	end

	-- getting version from Wikidata
	if version == 'wikidata' then
		version = nil
		i = moduleInterface.item
		if type( i ) == 'number' and i > 0 then
			s = mw.wikibase.getBestStatements( 'Q' .. i, 'P348' )
			if #s > 0 then
				for i = 1, #s, 1 do
					if s[ i ].mainsnak.snaktype == 'value' then
						v = s[ i ].mainsnak.datavalue.value
						if v ~= '' then
							return v
						end
					end
				end
			end
		end
	end

	if not moduleInterface.serial then
		return false
	elseif not version or version <= moduleInterface.serial then
		return moduleInterface.serial
	else
		return false
	end
end

-- not possible for Failsafe itself
-- function fs.Failsafe()
--	return Failsafe
-- end

function fs.failsafe( version )
	return fs._failsafe( version, Failsafe ) or ''
end

return fs