มอดูล:Update list overview

จากวิกิพีเดีย สารานุกรมเสรี
Documentation icon คู่มือการใช้งานมอดูล[สร้าง]
local p={}
local debuglog = ""
local listContent = ""

-- from http://lua-users.org/wiki/StringRecipes
function endswith(s, e)
	return e == '' or string.sub(s, -string.len(e)) == e
end

function cleanup(str)
	str = mw.ustring.match(str, "^%s*(%S.-%S)%s*$") or str -- (pass through "" and one character stuff)
	str = mw.ustring.gsub(str,"Module:","") -- remove all "Module:" from these, so they match up
	return str
end

function inList(name)
	name = '[' .. name:sub(1, 1):lower() .. name:sub(1, 1):upper() .. ']' .. name:sub(2)
	name = name:gsub(' ', '[ _]')
			   :gsub('%-', '%%%-')
	if listContent == "" then
		listContent = mw.title.new('User:Nullzerobot/ปรับปรุงหน้าอัตโนมัติ/ปกติ'):getContent()
	end
	if string.match(listContent, "page = u?'[Mm]odule:" .. name .. "'") then
		return 'yes'
	else
		return 'no'
	end
end

function p.getTable(frame)
    local alltable = mw.text.split(p.getAll(frame), "\n")
    for i = 1, #alltable do
        alltable[i] = "|-\n|[[Module:" .. alltable[i] .. "|" .. alltable[i] .. "]]\n|" .. inList(alltable[i])
    end
    return '{| class = "wikitable"\n' .. table.concat(alltable,'\n') .. '\n|}\n'
end

function p.getAll(frame)
	local modules = ""
    local index = mw.text.unstrip(frame:preprocess("{{Special:AllPages|namespace=828}}"))
    local nextlink = mw.ustring.gmatch(index, "<a.->.-:(.-)</a>")
    local pagelist = {}
    from, to = nextlink(), nextlink()
    while from and to do
    	local tryget = "{{Special:AllPages|from="..from.."|to="..to.."|namespace=828}}" -- this is incredibly finicky, any variation yields list from A
    	modules = mw.text.unstrip(frame:preprocess(tryget))
        local nextmodule = mw.ustring.gmatch(modules, '<a.->(.-)</a>')
        modulelink = nextmodule()
        while modulelink do
        	modulelink = mw.ustring.gsub(modulelink, "Module:", "")
        	name = mw.text.decode(modulelink)
        	if (not endswith(name, '/sandbox')) and (not endswith(name, '/doc')) and (not endswith(name, '/testcases')) then
    	    	table.insert(pagelist, name)
    	    end
    	    modulelink = nextmodule()
        end
        from, to = nextlink(),nextlink()
    end
    return table.concat(pagelist, "\n") .. debuglog
end

--[=[

function dictinsert(dict, data, heading) -- passing in a reference to the dictionary, not a copy of it, so whichever is specified gets altered
	local getentry = mw.ustring.gmatch(data, "([^\n\t]+)")
	local entry = " "
	while entry do
		entry = cleanup(entry)
		dict[entry] = heading
	    entry = getentry()
	end
end

function p.main(frame)
	local args = frame.args -- not bothering reading parent frame, this is to be invoked only
    local title = args[1]
    return p._main(title, frame)
end

function p._main(title, frame)
    local titleObject, titleContent
    if title then
    	titleObject = mw.title.new(title)
    	if titleObject then
    		datapage = titleObject:getContent()
	    end
	end
	if not datapage then
		return "Error: data page not found"
    end
    -- OK, we're now looking at a big juicy file with a lot of sections in top level headings surrounded by == ==
    -- Many of these then are subject to commands in [ ] that follow.  The heading name should be remembered to each of these.
    -- Comments in <!-- --> are to be ignored
    -- All other terms are page names.
    datapage = mw.ustring.gsub(datapage, "<!%-%-.-%-%-%>", "") -- remove all comments
    datapage = datapage .. "\n== ==\n" -- add a final blank heading so it is easier to excise all headinged sections
    datapage = mw.ustring.gsub(datapage, "==([^\n]-)==", "[end content token][begin heading token]%1[end heading token]")
    getsection = mw.ustring.gmatch(datapage, "%[begin heading token%](.-)%[end heading token%](.-)%[end content token%]")
    local outtable = {} -- sequence
    local entries = {} -- sequence
    local rating = {} -- dictionary
    local category = {} -- dictionary
    section = ""
    while section do
    	-- within each section, there are commands in [ ] which affect the content that follows, sometimes using the current heading.
    	-- ONLY specific commands in [ ] listed below are parsed; the rest don't count and will end up being interpreted as pages.
        section = mw.ustring.gsub(section, "%[%s*autoindex%s*%]", "[end data token][begin command token]autoindex[end command token]")
    	section = mw.ustring.gsub(section, "%[%s*index%s*%]", "[end data token][begin command token]index[end command token]")
    	section = mw.ustring.gsub(section, "%[%s*rating%s*%]", "[end data token][begin command token]rating[end command token]")
    	section = mw.ustring.gsub(section, "%[%s*keyword %-%s*%]", "[end data token][begin command token]keyword -[end command token]")
    	section = mw.ustring.gsub(section, "%[%s*category%s*%]", "[end data token][begin command token]category[end command token]")	
    	section = section .. "\n[end data token]" -- add a final blank command to make it easier to excise all commands
    	getcommand = mw.ustring.gmatch(section, "%[begin command token%](.-)%[end command token%](.-)%[end data token%]")
    	command = " "
    	while command do
    		command, data = getcommand()
    		data = cleanup(data or "")
    		if "autoindex" == command then
    			command = "index"
    			data = p.getAll(frame)
    		end
    		if "index" == command then
    		   	local getentry = mw.ustring.gmatch(data, "([^\n\t]+)")
    		    local entry = true
    		    while entry do
    		    	entry = getentry()
    		    	table.insert(entries, cleanup(entry or ""))
    		    end
    		elseif "keyword -" == command then
                        data = mw.text.trim(data)
    			if data ~= "" then				
    			    local i
    			    for i = #entries,1,-1 do -- reverse order so the numbers stay accurate!
    			        if mw.ustring.match(entries[i],data) then
    			        	table.remove(entries,i)
    			        end
    			    end
    			end
    		elseif "category" == command then
    			mw.log('c' .. heading)
    		    dictinsert(category, data, heading)
			elseif "rating" == command then
				mw.log('r' .. heading)
				dictinsert(rating, data, heading)
			end
        end
        heading, section = getsection()
        heading = cleanup(heading or "")
    end
    table.insert(outtable, '\n{| class="wikitable sortable"')
    table.insert(outtable, '\n!Module name')
    table.insert(outtable, '\n!Category')
    table.insert(outtable, '\n!Rating')
    for i = 1, #entries do
    	table.insert(outtable, "\n|-")
    	local entry = entries[i] or ""  -- better not be nil...
    	table.insert(outtable, "\n|")
    	table.insert(outtable, "[[Module:" .. entry .. "|" .. entry .. "]] ([[Module talk:" .. entry .."|talk]])")
    	table.insert(outtable, "\n|")
    	table.insert(outtable, category[entry] or "")
    	table.insert(outtable, "\n|")
    	table.insert(outtable, rating[entry] or "")
    end
    table.insert(outtable, "\n|}") -- end wikitable
    local outdata = table.concat(outtable)
	return outdata..debuglog
end
]=]

return p