Moodul:CategoryForProfession

Selle mooduli dokumentatsiooni saab kirjutada asukohta Moodul:CategoryForProfession/doc.

local p = {}

local ListOfProfessions = mw.loadData('Module:ListOfProfessions')
local ListOfCountries = mw.loadData('Module:ListOfCountries')

local ProfessionsCategoryByAlphabet = {}
for k, v in pairs(ListOfProfessions) do
	ProfessionsCategoryByAlphabet[k] = '[[Kategorii:' .. v .. ' kirjamišton mödhe]]'
end

local ProfessionsCategoriesByCountry = {}
local QQQ = {}
for i, j in pairs(ListOfCountries) do
	QQQ = {}
	for k, v in pairs(ListOfProfessions) do
		QQQ[k] = '[[Kategorii:' .. v ..' ' .. j ..']]'
	end
	ProfessionsCategoriesByCountry[i] = QQQ
end

local function MakeGeneralCategories(professionsList, dict)
	local res = ''
	for i, prof in pairs(professionsList) do
		local cat=nil
		if(prof.mainsnak.datavalue) then -- если значение свойства "значение неизвестно", то datavalue nil
			id = 'Q' .. prof.mainsnak.datavalue.value['numeric-id']
			cat = dict[id];
		end
		if cat ~= nil then
			res = res .. cat;	
		end
	end
    return res
end

local function MakeCountriesCategories(countriesList, professionsList)
	local res = ''
	for i, cant in pairs(countriesList) do
		if cant.mainsnak.datavalue ~= nil then
			id = 'Q' .. cant.mainsnak.datavalue.value['numeric-id']
			local professionsCategories = ProfessionsCategoriesByCountry[id];
			if professionsCategories ~= nil then
				res = res .. MakeGeneralCategories(professionsList, professionsCategories);
			end
		end
	end
    return res
end

function p.mainFunction(frame)
	local result = ''
	local entity = mw.wikibase.getEntityObject()
	if entity ~= nil and entity.claims ~= nil then
		if entity.claims[ 'P106' ] ~= nil then
			professions = entity.claims[ 'P106' ]
			result = result .. MakeGeneralCategories(professions, ProfessionsCategoryByAlphabet)
			if entity.claims[ 'P27' ] ~= nil then
				countries = entity.claims[ 'P27' ]
				result = result .. MakeCountriesCategories(countries, professions)
			end
		end
		local args = {}
		if frame == mw.getCurrentFrame() then
			if frame.args['kategorijoita'] == nil then
				frame = frame:getParent();
			end
			if frame ~= nil then
				args = frame.args;
			end
		else
			args = frame
		end
		-- исключаем ненужные категории указанные в параметре через точку с запятой
    	local excude = args['kategorijoita'];
    	if excude ~= nil and excude ~= '' then
    		exclude = mw.text.split(excude, '%s*;%s*' )
			for i, v in ipairs(exclude) do 
				local cat = mw.ustring.gsub( v, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ); -- маскируем спецсимволы
				result = mw.ustring.gsub(result, '%[%[Kategorii:' .. cat .. '%]%]', '');	
			end
    	end;
	end
	return result
end

return p