Module:TrainingPages

--The purpose of this module is to take a list of linked page, and use it to determine the next and previous page in the list as well as the total number of pages.

local p = {}

function p.main(frame,displacement,varstoreturn) 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,currentpagenumber if (lookup[mw.uri.decode(currentpage)]) then currentpagenumber=lookup[currentpage] returnpage=index[currentpagenumber+displacement] or defaultpage else returnpage=defaultpage end if not(varstoreturn) then return tostring(returnpage) else return tostring(returnpage),currentpagenumber,#index end end

-- Return the next page in the index -- Used like if on a page that is part of the index: -- -- Used like this to find the next page after a specified page: -- function p.next_page(frame) local returnpage,pagenumber,totalpages=p.main(frame,1,true) return returnpage end

-- Same as above, but returns the previous page function p.last_page(frame) local returnpage,pagenumber,totalpages=p.main(frame,-1,true) return returnpage end

function p.page_number(frame) local returnpage,pagenumber,totalpages=p.main(frame,0,true) return pagenumber end

function p.total_pages(frame) local returnpage,pagenumber,totalpages=p.main(frame,0,true) return totalpages end

return p