پرش به محتوا

پودمان:Archives

از ویکی‌گفتاورد
نماد توضیحات توضیحات پودمان[ایجاد]
local p = {};
local helper = {};
helper.rec = function(min, max, prefix, namespace)
		if (min + 1) == max then
			return min
		elseif max == nil then
			local doublemin = min * 2
			local title = mw.title.new(prefix .. " " .. doublemin, namespace)
			if title.exists then
				return helper.rec(doublemin, max, prefix, namespace)
			else
				return helper.rec(min, doublemin, prefix, namespace)
			end
		else
			local dif = max - min
			local test = min + math.ceil(dif/2)
			local title = mw.title.new(prefix .. " " .. test, namespace)
			if title.exists then
				return helper.rec(test, max, prefix, namespace)
			else
				return helper.rec(min, test, prefix, namespace)
			end
	end
end
 
p.archivenumber = function (namespace, prefix)
	return helper.rec(1, nil, prefix, namespace)
end
 
helper.buildtable = function(agg, cur, last, namespace, page, archiveprefix)
	if cur > last then return table.concat(agg, "")
	else
		local next = agg
		if ((cur - 1) % 3) == 0 then table.insert(next,  "\n|-") end
		table.insert(next, "\n|[[")
		table.insert(next, namespace)
		table.insert(next, ":")
		table.insert(next, page)
		table.insert(next, "/")
		table.insert(next, archiveprefix)
		table.insert(next, " ")
		table.insert(next, cur .. "|")
		table.insert(next, archiveprefix)
		table.insert(next, " ")
		table.insert(next, cur .. "]]")
	    return helper.buildtable(next, cur + 1, last, namespace, page, archiveprefix)
    end
end
 
p.buildtable = function(namespace, page, archiveprefix)
	local init = {}
	table.insert(init, "{| style=\"width: 100%; padding: 0px; text-align: center; background-color: transparent;\"")
	local last = p.archivenumber(namespace, page .. "/" .. archiveprefix)
	local wikitext = helper.buildtable(init, 1, last, namespace, page, archiveprefix) .. "\n|}"
	return wikitext
end
 
p.run = function(frame)
	local namespace
	local page
	local prefix
	local archive
	if (frame.args.root and frame.args.root ~= "") then
		local root = frame.args.root
		mw.log("root")
		mw.log(root)
		local pattern = "([^:]+):(.+)"
		local endindex
		_, endindex, namespace, page, _, prefix = mw.ustring.find(root, pattern)
		local rest = mw.ustring.sub(root, endindex)
		local subpage
		_, _, subpage = mw.ustring.find(rest, "/(.*)")
		mw.log("namespace, page, prefix")
		mw.log(namespace)
		mw.log(page)
		mw.log(prefix)
		prefix = prefix or "Archive"
		archive = "Archive"
		namespace = mw.ustring.gsub(namespace, " ", "_")
	else
		local current = mw.title.getCurrentTitle()
		namespace = mw.ustring.gsub(current.nsText, " ", "_")
		page = current.text
		archive = frame.args.archivename or "Archive"
	end
	return p.buildtable(namespace, page, archive)
end
 
return p