Jump to content

Module:Motd

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 18:53, 10 May 2016 (ok, back to basics to start...). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--This module aims to a) extract motds from various sources and return a data file to be copied
                   -- b) use that data file to return a motd chosen from within it
--MOTD is [[WP:Motto of the day]], not message!
--Note: it's been a while since I last touched Lua, so I may have a slow start

local p = {}

function p.main(frame)
	mottos = mw.loadData("Module:Motd/data/2012")
	yday = os.date("!*t", nil).yday -- should be day of year, we'll see...
	return frame:preprocess(mottos[yday])
end

function p.read(frame)
    local parent=frame.getParent(frame) or {}
    local currentpage
    ---- args in the #invoke itself trump args in the parent frame
    currentpage = (parent and parent.args.page) or frame.args.page
    -- I'm not sure getting the current page makes sense but I had the code handy so I'll leave it.
    local pagepointer
    if not(currentpage) then
        pagepointer=mw.title.getCurrentTitle()
        assert(pagepointer,"failed to access getCurrentTitle")
        currentpage=pagepointer.fullText
    else pagepointer=mw.title.new(currentpage)
        assert(pagepointer,"failed to access mw.title.new("..tostring(currentpage)..")")
    end
    ---- get the text of the currentpage
    local text=pagepointer.getContent(pagepointer)
    assert (text,"error: failed to get text from ".. currentpage)
    local linkmatch = "%[%[(.-)%]%]"
    local prowl=mw.ustring.gmatch(text,linkmatch)
    local archive={}
    local link=prowl()
    while link do
    	link = mw.ustring.gmatch(link,"(.-)%|")() or link
    	contents = frame.expandTemplate(frame, {title = link, args = nil})
        -- table.insert(archive,'[==[' .. link .. ']==],</nowiki><br><nowiki>')
        -- I don't think I actually need to include the link for this use
        table.insert(archive,'[==[' .. contents .. ']==],</nowiki><br><nowiki>')
        link=prowl()
    end
    
    local output=""
    for i = 1, table.maxn(archive) do
        output=output..(archive[i] or "")
    end
    output = mw.ustring.gsub(output,",</nowiki><br><nowiki>$","</nowiki><br><nowiki>")
    output = "<nowiki>return {</nowiki><br><nowiki>"..output.."}</nowiki>"
    return frame.preprocess(frame,output)
end

return p