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 18:12, 26 March 2013 (adding escape characters will foul it up badly anyway on the actual output. And hunting the bug was troublesome before that. Giving up on supporting stray nowiki stuff in the file until someone throws me a clue.). 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
        local pp=mw.title.getCurrentTitle()
        assert(pp,"failed to access getCurrentTitle")
        currentpage=pp.fullText
    end
    local index={}
    if mw.ustring.sub(indexmodule,1,6)=="Module" then
        ---- get a table of the pages in order from indexmodule
        index=mw.loadData(indexmodule)
    else pp=mw.title.new(indexmodule)
        assert(pp,"failed to access mw.title.new("..indexmodule..") to load the index")
        local textindex=pp.getContent(pp)
        prowl=mw.ustring.gmatch(textindex,"%[%[(.-)[%]|]") -- first half of any wikilink
        index={}
        repeat
            link=prowl()
            if not(link) then break end
            if link~="" then table.insert(index,link) end
        until false
    end
    displacement=displacement or 0 -- assume a null parameter is just display the same
    ---- 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)
    return p.main(frame,1)
end

function p.last_page(frame)
    return p.main(frame,-1)
end

return p