Jump to content

Module:TrainingPages

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 03:36, 26 March 2013 (Generalizing and twiddling). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--The purpose of this module is to take a list of page names and page numbers that define a module of one of the [[Wikipedia:Training]] series, and use it to determine the next and previous page to link to 

local p = {}

-- Return the next page, used like: {{#invoke:TrainingPages| next_page | currentpage=Wikipedia:Training/Foo/Bar | {{listofpages}} }}
function p.main(frame,displacement)
    local parent=frame.getParent(frame)
    local currentpage,indexmodule,defaultpage
    ---- args in the #invoke itself trump args in the parent frame
    currentpage = frame.args.page
    defaultpage = frame.args.defaultpage
    indexmodule = frame.args.index
    displacement = displacement or frame.args.displacement -- can get from function name
    ---- args in the parent frame come next
    if parent then
        currentpage=currentpage or parent.args.page
        indexmodule=indexmodule or parent.args.index -- index is a module return{'page1','page2', ...}
        defaultpage=defaultpage or parent.args.defaultpage
        end
    ---- default values if parameters aren't provided
    defaultpage=defaultpage or "" -- don't know where to send people by default
    indexmodule=indexmodule or "Module:TrainingPages/default index" -- example given in Lua:Requests
    if not(currentpage) then
        pagepointer=mw.title.getCurrentTitle()
        assert(pagepointer,"failed to access getCurrentTitle")
        currentpage=pagepointer.fullText
    end
    displacement=displacement or 0 -- assume a null parameter is just display the same
    ---- get a table of the pages in order from indexmodule
    local index=mw.loadData(indexmodule)
    ---- set up the reverse lookup in lookup.
    ---- it would be faster to set this up in the indexmodule
    ---- but we don't want inconsistencies from user input!
    local lookup={}
    local i=0
    repeat
        i=i+1
        local j=index[i]
        if j then lookup[mw.uri.decode(j)]=i else break end -- lookup["page name"] => page number
    until false
    --- get the page to return
    local returnpage
    if (lookup[mw.uri.decode(currentpage)]) then
        returnpage=index[lookup[currentpage]+displacement] or defaultpage
    else returnpage=defaultpage
    end
    return tostring(returnpage)
end

function p.next_page(frame)
    local displacement=1
    return p.main(frame)
end

return p