Documentation for this module may be created at Module:Sandbox/Paweł Ziemian/test/doc

local m = {}

function m.D1(frame)
	mw.logObject(frame.args[1], "frame.args[1]")
	return frame.args[1]
end

m.NumberOfArticlesBetween = function(frame)
	local pf = frame:getParent()
	local start = frame.args[1] or pf.args[1]
	if not start then
		return
	end
	
	local stop =  frame.args[2] or pf.args[2]
	if not stop then
		return
	end
	
	local content = mw.title.getCurrentTitle():getContent()
	if not content then
		return
	end
	
	local startPattern = "<!--"..start.."-->"
	local startPosition = string.find(content, startPattern, 1, true)
	if not startPosition then
		return
	end
	
	startPosition = startPosition + #startPattern
	
	local stopPattern = "<!--"..stop.."-->"
	local stopPosition = string.find(content, stopPattern, startPosition, true)
	if not stopPosition then
		return
	end
	
	stopPosition = stopPosition - 1
	if startPosition >= stopPosition then
		return
	end
	
	local text = string.sub(content, startPosition, stopPosition)
	local _, count = mw.ustring.gsub(text, "(%[%[[^%[%]]-%]%])", "%1")
	local _, files = mw.ustring.gsub(text, "(%[%[Plik:[^%[%]]-%]%])", "%1")
	count = count - files
	
	local threshold = tonumber(frame.args.threshold or pf.args.threshold) or 1
	if count < threshold then
		return
	end
	
	local lang = mw.getContentLanguage()
	local number = lang:formatNum(count)
	local articles = lang:convertPlural(count, { frame.args[4] or pf.args[4] or "article", frame.args[5] or pf.args[6] or "articles" })

	local result, _ = string.gsub(frame.args[3] or pf.args[3] or "<small>($1)</small>", "$1", number.."&nbsp;"..articles)
	return result
end

m.countLinks = function(frame)
	local text = frame.args[1]
	if text then
		text = mw.text.trim(text)
		local _, count = mw.ustring.gsub(text, "(%[%[[^%[%]]-%]%])", "%1")
		if count >= 2 then
			local lang = mw.getContentLanguage()
			local number = lang:formatNum(count)
			return text .. "<small> ("..number.."&nbsp;articles)</small>"
		else
			return text
		end
	end
end

m.goodCounter = function(frame)
	local fullText = frame.args[1]
	if not fullText then
		return nil
	end
	
	function testlink(text)
		local b1 = string.byte(text, 1)
		if (b1 ~= 39) and (b1 ~= 91) then
			return false
		end
		
		local prefix = string.sub(text, 1, 7)
		if prefix == "[[File:" then
			return false
		end
		
		return (string.sub(prefix, 1, 2) == "[[") or (string.sub(prefix, 1, 4) == "''[[")
	end
	
	local lang = mw.getContentLanguage()
	
	function showcounter(counter)
		local number = lang:formatNum(counter)
		return "<small>("..number.." articles)</small>\n"
	end
	
	local lines = mw.text.split(fullText, "\n", true)
	local counter = 0
	local result = {}
	for i, line in ipairs(lines) do
		if os.clock() > 30 then
			break
		end
		
		if testlink(line) then
			table.insert(result, line)
			counter = counter + 1
		else
			if counter > 1 then
				table.insert(result, showcounter(counter))
			end
			counter = 0
			table.insert(result, line)
		end
		table.insert(result, "\n")
	end
	
	if counter > 1 then
		table.insert(result, showcounter(counter))
	end

	return table.concat(result, "")
end

local easter = require("Module:Easter")

local function prepareDateFormatter(format)
    if not format then
        local convertToRoman = { ["03"] = "III", ["04"] = "IV", ["05"] = "V", }
        return function(date)
            local month = convertToRoman[string.sub(date,6,7)]
            local day = tonumber(string.sub(date,9))
            return day .. " " .. month
        end
    elseif format == "none" then
        return function(date)
            return date
        end
    else
        local lang = mw.language.getContentLanguage()
        return function(date)
            return lang:formatDate(format, date)
        end
    end
end

function m.Table2(frame)
    local firstYear = tonumber(frame.args[1])
    local lastYear = tonumber(frame.args[2])
    local formatDate = prepareDateFormatter(frame.args.format)
    local result = {}
 
    table.insert(result, "<table class=\"wikitable\" style=\"text-align:center\">")
    --table.insert(result, "<thead>")
    table.insert(result, "<tr>")
    table.insert(result, "<th>Year</th><th colspan=\"2\">...0</th><th colspan=\"2\">...1</th><th colspan=\"2\">...2</th><th colspan=\"2\">...3</th><th colspan=\"2\">...4</th><th colspan=\"2\">...5</th><th colspan=\"2\">...6</th><th colspan=\"2\">...7</th><th colspan=\"2\">...8</th><th colspan=\"2\">...9</th>")
    table.insert(result, "</tr>")
    table.insert(result, "<tr>")
    table.insert(result, "<th><small>decade</small></th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th><th>W</th><th>E</th>")
    table.insert(result, "</tr>")
    --table.insert(result, "</thead>")
    --table.insert(result, "<tbody>")
    local firstDecade = math.floor(firstYear / 10)
    local lastDecade = math.floor(lastYear / 10)
    while firstDecade <= lastDecade do
        table.insert(result,"<tr>")
        table.insert(result,"<th>" .. firstDecade .. "...</td>")
        for i = 0, 9 do
            local y = (firstDecade * 10) + i
            if (y >= firstYear) and (y <= lastYear) then
                local v1 = easter.Calculate({ args = { [1] = y, method = "Western", format = "none", } })
                local v2 = easter.Calculate({ args = { [1] = y, method = "Eastern", format = "none", } })
                if v1 ~= v2 then
                    table.insert(result, "<td>" .. formatDate(v1) .. "</td><td>" .. formatDate(v2) .. "</td>")
                else
                    table.insert(result, "<td colspan=\"2\">" .. formatDate(v1) .. "</td>")
                end
            else
                table.insert(result, "<td colspan=\"2\">&nbsp;</td>")
            end
        end
        table.insert(result,"</tr>")
        firstDecade = firstDecade + 1
    end
 
    --table.insert(result, "</tbody>")
    table.insert(result, "</table>")
    return table.concat(result,"")
end

-- look into entity object
function m.ViewSomething(frame)
	local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
	local data = mw.wikibase.getEntityObject(f.args.id)
	if not data then
		return nil
	end

	local i = 1
	while true do
		local index = f.args[i]
		if not index then
			if type(data) == "table" then
				return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
			else
				return tostring(data)
			end
		end
		
		data = data[index] or data[tonumber(index)]
		if not data then
			return
		end
		
		i = i + 1
	end
end

local i18n = {
	["warnDump"] = "[[Category:Called function 'Dump' from module Wikidata]]"
}

function m.Dump(frame)
	local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
	local data = mw.wikibase.getEntityObject(f.args.id)
	if not data then
		return i18n.warnDump
	end

	local i = 1
	while true do
		local index = f.args[i]
		if not index then
			return "<pre>"..mw.dumpObject(data).."</pre>".. i18n.warnDump
		end

		data = data[index] or data[tonumber(index)]
		if not data then
			return i18n.warnDump
		end

		i = i + 1
	end
end


return m