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:21, 26 March 2013 (trying uri.decode in the hope of losing whatever mysterious character makes the difference). 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.next_page(frame)
    local parent=frame.getParent(frame)
    local currentpage,indexmodule,defaultpage
    if parent then
        currentpage=parent.args.page
        indexmodule=parent.args.index -- index is a module return{'page1','page2', ...}
        defaultpage=parent.args.defaultpage
        end
    currentpage = frame.args.page or currentpage
    defaultpage = frame.args.defaultpage or "" -- don't know where to send people by default
    indexmodule = frame.args.index or indexmodule or "Module:TrainingPages/default index" -- example given in Lua:Requests
    if not(currentpage) then -- defaults to title of the current page
        pagepointer=mw.title.getCurrentTitle()
        assert(pagepointer,"failed to access getCurrentTitle")
        currentpage=pagepointer.fullText
    end
    local index=mw.loadData(indexmodule)
    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
    if (lookup[mw.uri.decode(currentpage)]) then
        local nextpage=index[lookup[currentpage]+1] or defaultpage
    else local nextpage=defaultpage
    end
    return tostring(nextpage)..currentpage.."---"..index[2]..next(lookup)
end


return p