Module:Sandbox/Galobtter/Related changes

From blackwiki
Jump to navigation Jump to search

The module “Sandbox/Galobtter” is a Lua counterpart to Special:Related changes. Unlike that feature, the module only has access to the last revision date and user. However, it can be run through filters so that only the articles that have gone longest without being edited are listed, only IP users are listed, bots are omitted, etc. ( That is, it can be run through those filters once that part is written! Present status is "proof of concept" only.)

Usage

To find related changes for Velociraptor: {{#invoke:Sandbox/Galobtter|main|Velociraptor}}

Module:Sandbox/Galobtter/Related changes

Parameters

The text below assumes that functions are declared as

function p.function_name( frame )
  • Positional parameters – the value of frame.args[1] is passed after function name and vertical bar, the value of frame.args[2] is passed after the value of frame.args[1] and vertical bar, and so on.
  • Named parameters (specify them) – the same name=value syntax as in MediaWiki templates, are accessible inside the function as frame.args["name"].

Examples

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 '<span style="color:red">[[Module:Related changes]] error: failed to access page: [[' + tostring(page) + ']]</span>',false
        end
    else
        indexpage = mw.title.getCurrentTitle()
        if (not(indexpage)) then
            return '<span style="color:red">[[Module:Related changes]] bug: failed to access getCurrentTitle!</span>',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('<nowiki>'..display(linklist)..'</nowiki>')
    else
    	return display(linklist,param.options)
    end
end

return p