Модуль:Песочница/DonRumata/GetEntityTest

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
local p = {}

local function DumpTable(t, fmt, delimiter, before, after, tab, tabspace)
    if type(t)=="table" then
        local f={};
        tabspace = tabspace .. tab;
        for k,v in pairs(t) do
            table.insert(f,string.format(tabspace .. (fmt or '%s = %s'), k, DumpTable(v, fmt, delimiter, before, after, tab, tabspace)))
        end;
        -- table.sort(f);
        return tabspace .. (before or '') .. table.concat(f, delimiter or '') .. tabspace .. (after or '')
    else 
    	return tostring(t)
    end
end

function p.sitelink(frame)
	if frame.args[1] then 
		return mw.wikibase.sitelink( frame.args[1] )
	else
		return '';
	end
end

function p.test(frame)
    local function DumpEntity()
    	local testObject = frame.args['object'] or false;
    	if type(testObject) == 'string' then
    		testObject = not (testObject == '' or testObject == '0' or testObject == 'false')
    	elseif type(testObject) ~= 'boolean' then
    		testObject = false;
    	end
	    local entity = nil;
	    local id = frame.args['id'];
	    if testObject then
	    	entity = mw.wikibase.getEntity(id);
	    else
	    	entity = mw.wikibase.getEntity();
	    end
	    return DumpTable(entity,"%s = %s",';','{','}', '  ', '\n')
	end
	local r, result = pcall(DumpEntity);
    if r then
    	return result;
    else
    	return '<strong class="error">Ошибка:'.. result .. '</strong>';
    end
end

function p.EnumPropertyValues(frame)
	local template = frame.args['template'] or '';
	if template == '' then
		return '<strong class="error">Шаблон не задан</strong>';
	end
	
	local param = frame.args['param'] or '';
	if param == '' then
		return '<strong class="error">Имя параметра шаблона не задано</strong>';
	end
	param = tonumber(param) or param; -- неименованный параметр
	
	local parentframe = frame:getParent();
	local parentargs = {};
	if parentframe ~= nil then
		for arg, val in pairs( parentframe.args ) do
			parentargs[arg] = val;
		end
	end
	
	local property = frame.args['property'] or '';
	local propertyid = mw.ustring.lower(property);
	if mw.ustring.find(propertyid, '^p%d+$', 1, false) == nil then
		return '<strong class="error">Имя свойства должно начинаться с P</strong>';
	end
	
	local id = frame.args['id'] or mw.wikibase.getEntityIdForCurrentPage();
	if id == nil then
		return '<strong class="error">Элемент Викиданных не найден</strong>';
	end
	local entity  = mw.wikibase.getEntity(id);
	local propertyvalues = entity:formatPropertyValues( property, { mw.wikibase.entity.claimRanks.RANK_NORMAL } )
	if propertyvalues.value == nil or propertyvalues.value == ''  then
		return ''
	end
	
	local values = mw.text.split(propertyvalues.value, ',');
	result = '';
	for i, value in ipairs(values) do
		local v = mw.text.trim(value);	
		parentargs[param] = v;
		mw.logObject(parentargs)
		result = result .. frame:expandTemplate{ title = template, args = parentargs }	
	end
	return result;
end
	
return p