Module:Sandbox/Galobtter/Related changes

local p={}

function getParam(frame) -- This is a longwinded way to return all the args (first choice) or parent.args (second choice) in an array. local parent, pargs, args local param={} parent = frame['parent'] if (parent) then pargs = parent['args'] end if (pargs) then for k,v in pairs(args) do           param[k] = v        end end args = frame['args'] if (args) then for k,v in pairs(args) do           param[k] = v        end end return param end

function getIndexpage(page) -- get the contents of either the current page (inadvisable...) or a specified page -- returns either a title object or an error string, and a boolean true for successful completion local indexpage if (page) then indexpage = mw.title.new(page) if (not(indexpage)) then return ' Module:Related changes error: failed to access page: ' + tostring(page) + ' ',false end else indexpage = mw.title.getCurrentTitle if (not(indexpage)) then return ' Module:Related changes bug: failed to access getCurrentTitle! ',false end end return indexpage, true end

function getLinks(frame,indexpage) local index = indexpage:getContent or "" -- indexpage should exist, so no further checking for errors, just return blank local nextLink = mw.ustring.gmatch(index,"%[%[([^%]|]+)[^%]]-%]%]") local linklist = {} for link in nextLink do		local linkval = frame:callParserFunction('REVISIONTIMESTAMP', link) if (mw.ustring.len(linkval) > 8) then -- don't even index pages that don't get meaningful results linklist[tostring(link)] = linkval .. '|' .. frame:callParserFunction('REVISIONUSER', link) end end return linklist end

function display(linklist, options) if (not(options)) then options = 'd-' end local sorttype = mw.ustring.match(options,'([%l%u])') local sortdir = mw.ustring.match(options,'([%+%-])') if (sorttype == 'n') then sorttype = 'name' if (sortdir == '-') then sortdir = 'descending' else sortdir = 'ascending' end else sorttype = 'date' if (sortdir == '+') then sortdir = 'ascending' else sortdir = 'descending' end end local outsort, outarray = {}, {'{| class="wikitable sortable"\n!Name\n!Last edited\n!Last editor'} for k,v in pairs(linklist) do		if sorttype == 'name' then table.insert(outsort, k..'|'..v)   	else table.insert(outsort, v..'|'..k)       end end table.sort(outsort) for i = 1,#outsort do		local n, d, u, split split = mw.text.split(outsort[i],'|',true) if sorttype == 'name' then n, d, u= split[3], split[1], split[2] else d, u, n = split[1], split[2], split[3] end if ((mw.ustring.sub(n, 1, 5) == 'File:') or (mw.ustring.sub(n, 1, 6) == 'Image:')) then n = ':' .. n   	end table.insert(outarray,'\n|-\n|'..(n or '[[Module:Related changes bug: missing name') .. ']]\n|' .. (d or 'Module:Related changes bug: missing date') .. '\n|User:' .. u .. '') end table.insert(outarray,'\n|}') return table.concat(outarray) end

function filter(linklist, action) -- pass end

function p.main(frame)

local param = getParam(frame) -- get all parameters in param tabel; args override parent.args local indexpage = getIndexpage(param.page) local linklist = getLinks(frame,indexpage) for operation = 1, #param do   	filter(linklist, param[operation]) end if param.nowiki then return frame:preprocess(' '..display(linklist)..' ') else return display(linklist,param.options) end end

return p