Module:විකිමාධ්‍ය දින පොත

Wikibooks වෙතින්
local p = {}

local function loop_log_id_list( mode )
	local i = 0
	local result = '0000000000'
	local book_name = 'විකිමාධ්‍ය දින පොත'
	local page_title = 'විකිමාධ්‍ය දින පොත'
 
	for i = 0, 10000000000, 1 do
		result = string.format("%010.0f", i)
		page_title = book_name..'/'..result
		if  mw.title.new( page_title ).exists then
			-- මෙම යොමුව පවතියි. ඊලඟ එක බලන්න.
		else
			if ( mode == 'වත්මන්' ) then
				result = string.format("%010.0f", i-1)
			else
				-- නිවැරදි අගය දැනටමත් result තුළ ඇත
			end
			break
		end
	end
	return result
end

local function get_previous_log_id_internal( id )
	local pattern = '(%d+)$'
	local n = nil
	local result = '0000000000'

	if (id == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'වත්මන්' )
	else
		n = string.match( id, pattern )
	end

	if (n == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'වත්මන්' )
	else
		-- පෙර නම් එකක් අඩු කරන්න
		r = n-1
		if (r < 0) then
			-- සෘණ අගයක් පැමිණ ඇත. පෙර අගය ම ලබා දී බලමු
			result = string.format("%010.0f", n)
		else
			result = string.format("%010.0f", r)
		end
	end
	return result
end

local function get_log_data_internal( id, vyaapruthiya, vargaya )
--	local vyaapruthiya = frame.args['ව්‍යාපෘතිය']
--	local vargaya = frame.args['වර්ගය']
--	local id = p.extract_log_id_from_title(frame)

	local result = 0

	local data = mw.loadData("Module:විකිමාධ්‍ය දින පොත/දත්ත/"..id)
	if vargaya == "අන්තර්ගතය" 
		or vargaya == "පිටු"
		or vargaya == "දෝෂ ප්‍රවර්ග"
		or vargaya == "දෝෂ පිටු"
		or vargaya == "විශේෂ"
	then
		result = data[vyaapruthiya][vargaya]
		-- සිංහල දත්ත වල කොමාව, දශම සළකුණ ලෙස භාවිතා නොවේ.
		-- එම නිසා ගණනය කිරීම් වල දී කොමා සළකුණ අවශ්‍ය නොවේ.
		-- result = mw.ustring.gsub( result, ",", "" );
	end
	return result
end


function p.get_next_log_id( frame )
	local s = frame.args['s']
	local pattern = '(%d+)$'
	local n = nil
	local result = '0000000000'

	if (s == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'ඊලඟ' )
	else
		n = string.match( s, pattern )
	end

	if (n == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'ඊලඟ' )
	else
		-- පෙර නම් එකක් එකතු කරන්න
		result = string.format("%010.0f", n+1)
	end
	return result
end

function p.get_active_log_id( frame )
	return loop_log_id_list( 'වත්මන්' )
end

function p.get_previous_log_id( frame )
	local s = frame.args['s']
	local pattern = '(%d+)$'
	local n = nil
	local r = 0
	local result = '0000000000'

	if (s == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'වත්මන්' )
	else
		n = string.match( s, pattern )
	end

	if (n == nil) then
		-- විශේෂ අවස්ථාව
		result = loop_log_id_list( 'වත්මන්' )
	else
		-- පෙර නම් එකක් අඩු කරන්න
		r = n-1
		if (r < 0) then
			-- සෘණ අගයක් පැමිණ ඇත. පෙර අගය ම ලබා දී බලමු
			result = string.format("%010.0f", n)
		else
			result = string.format("%010.0f", r)
		end
	end
	return result
end

function p.get_log_data_diff( frame )
	local v = {}
	v["වර්ගය"] = frame.args['වර්ගය']
	local result = p.get_log_data_diff_value( frame )

	if v["වර්ගය"] == 'දෝෂ ප්‍රවර්ග' or v["වර්ගය"] == 'දෝෂ පිටු' then
		v["වාසි සැකිල්ල"] = 'වාසි පැත්තට අඩු'
	else
		v["වාසි සැකිල්ල"] = 'වාසි පැත්තට වැඩි'
	end

	if result > 0 then
		result = frame:expandTemplate{ title = v["වාසි සැකිල්ල"], args = { result } }	
	elseif result < 0 then
		result = frame:expandTemplate{ title = v["වාසි සැකිල්ල"], args = { result } }	
	else
		result = frame:expandTemplate{ title = 'නොවෙනස්', args = { result, label = '+0' } }
	end
	return result
end

function p.get_log_data_diff_value( frame )
	local vyaapruthiya = frame.args['ව්‍යාපෘතිය']
	local vargaya = frame.args['වර්ගය']
	local mema_id = p.extract_log_id_from_title(frame)
	local pera_id = get_previous_log_id_internal( mema_id )
	local result = '+0'

	local mema_dhaththa = 0
	local pera_dhaththa = 0

	mema_dhaththa = get_log_data_internal( mema_id, vyaapruthiya, vargaya )
	pera_dhaththa = get_log_data_internal( pera_id, vyaapruthiya, vargaya )

	result = mema_dhaththa - pera_dhaththa

	return result
end

function p.get_log_data( frame )
	local vyaapruthiya = frame.args['ව්‍යාපෘතිය']
	local vargaya = frame.args['වර්ගය']
	local id = p.extract_log_id_from_title(frame)

	local result = get_log_data_internal( id, vyaapruthiya, vargaya )

--	local data = mw.loadData("Module:විකිමාධ්‍ය දින පොත/දත්ත/"..id)
--	if vargaya == "අන්තර්ගතය" 
--		or vargaya == "පිටු"
--		or vargaya == "දෝෂ ප්‍රවර්ග"
--		or vargaya == "දෝෂ පිටු"
--		or vargaya == "විශේෂ"
--	then
--		result = data[vyaapruthiya][vargaya]
--	end
	return result
end

function p.extract_log_id_from_title( frame )
	local s = frame.args['s']
	local pattern = '(%d+)$'
	local result = '0000000000'

	result = string.match( s, pattern )
	if (result == nil) then
		result = '0000000000'
	end

	return result
end

function p.extract_data_page_from_title( frame )
	local id = p.extract_log_id_from_title( frame )
	local dataPage = "Module:විකිමාධ්‍ය දින පොත/දත්ත/"..id
	return dataPage
end

function p.check_data_corrected( frame )
	local v = {}
	local s = frame.args['s']
	local pattern = '(%d+)$'
	local id = '0000000000'
	local result = ''

	id = string.match( s, pattern )
	if (id == nil) then
		id = loop_log_id_list( 'වත්මන්' )
	end

	local dataPage = "Module:විකිමාධ්‍ය දින පොත/දත්ත/"..id
	
	local data
	local data_loaded = "False"
	if pcall(function () data = mw.loadData(dataPage) end ) then data_loaded = "True" end
	-- local data = mw.loadData(dataPage)
	
	if (data_loaded == "True") then
		v["දත්ත පිටුවට"] = frame:expandTemplate{ title = 'ක්‍රියා කරවිය හැකි පැනලය', args = { dataPage, 'දත්ත පිටුව' } }

		if data["නිවැරදිය"] == "එසේය" then
			-- result = frame:expandTemplate{ title = 'දැන්වීම', args = { 'මෙහි දැක්වෙන දත්ත නිවැරදි කර ඇත: '..v["දත්ත පිටුවට"] } }
			result = v["දත්ත පිටුවට"]
		else
			result = frame:expandTemplate{ title = 'දැන්වීම', args = { 'මෙහි දැක්වෙන දත්ත නිවැරදි කර නොමැත: '..v["දත්ත පිටුවට"] } }
		end

	-- result = result..frame:expandTemplate{ title = 'ක්‍රියා කරවිය හැකි පැනලය', args = { dataPage, 'දත්ත පිටුව' } }
	end

	return result
end

return p
"https://si.wikibooks.org/w/index.php?title=Module:විකිමාධ්‍ය_දින_පොත&oldid=25175" වෙතින් සම්ප්‍රවේශනය කෙරිණි