Module:Motd
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
Usage
{{#invoke:Motd|read|archive}}
archives are pages like Wikipedia:Motto_of_the_day/Schedule/Archive_2012. (2012 to 2015)
{{#invoke:Motd|read11|archive}}
archives are pages like Wikipedia:Motto_of_the_day/Schedule/Archive_2009. (up to 2011)
The read routine produces an output that has been manually cut-and-pasted into Module:Motd/data/2012 etc., and in some cases further modified. Ideally, each year should have one motto per day, possibly appropriate to that day. The read function produces the data to paste and the main function calls on this data for its output.
{{#invoke:Motd|main|day = day|year = year}}
Picks a motto. Defaults to 2012 for year, current day for day. Years 2006 to 2015 exist, but 2006, 2014, 2015 themselves don't have a full set of days. Haven't checked the others to be sure either.
--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 = {}
-- Return the reordered page, used like this: {{#invoke:NewestAtTop|main|page=Module talk:NewestAtTop/Test|top=yes}}
function p.main(frame,header)
local parent=frame.getParent(frame) or {}
local currentpage,top
---- args in the #invoke itself trump args in the parent frame
currentpage = frame.args.page
header = header or frame.args.header or parent.args.header -- can get from function name
top = frame.args.top or parent.args.top
---- args in the parent frame come next
if parent then
currentpage=currentpage or parent.args.page
header=header or parent.args.header
top = top or parent.args.top
end
---- default values if parameters aren't provided
header=header or 2 -- Reorder even = = sections. Note this isn't tested and may not work...
local pagepointer
if top then top=1 else top=0 end
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 headerdef=mw.ustring.rep("=",header)
local headermatch="%s*\n%s*"..headerdef.."(=*)(.-)(=*)"..headerdef.."%s*\n%s*"
local prowl=mw.ustring.gmatch(text,"(.-)"..headermatch)
local archive={}
local chunk,h1,title,h2=prowl()
local level=math.min(mw.ustring.len(h1),mw.ustring.len(h2)) -- if unmatched, higher level trumps
local lastchunk="";local lasth1="==";local lasth2="==" -- keeps track of the title from the previous iteration to recover at end
local tinsertpoint={};local insertpoint=1;local lastlevel=level
table.insert(archive,insertpoint,chunk or "") --- top chunk is the first thing in. Top means display it at bottom
table.insert(archive,insertpoint+top,"\n"..headerdef..title..headerdef.."\n")
repeat
chunk, h1,title,h2=prowl()
h1=h1 or lasth1;h2=h2 or lasth2 -- default to normal header and to avoid script error
if not(chunk) then break end
lastchunk=chunk; -- to find next section. You can't find a title in the string it came from.
table.insert(archive,1+insertpoint+top,chunk) --- insert the OLDER chunk under LAST iteration's title and level
local lastlevel=level
level=math.min(mw.ustring.len(h1),mw.ustring.len(h2)) -- if unmatched, higher level trumps
if level>lastlevel then
tinsertpoint[lastlevel]=insertpoint;insertpoint=insertpoint+2
else if level < lastlevel then
insertpoint=tinsertpoint[level]
end
end
if title then
lasth1=h1;lasth2=h2
table.insert(archive,insertpoint+top,"\n"..string.rep("=",header+level)..title..string.rep("=",header+level).."\n")
end --- insert the new title at the beginning
until false
h1,title,h2,chunk=mw.ustring.match(text,lastchunk..headermatch.."(.*)$")-- everything from the last section header to the end of string. Fails if two identical section headers.
table.insert(archive,insertpoint+1+top,chunk)
local output=""
for i = 1, table.maxn(archive) do
output=output..(archive[i] or "")
end
return frame.preprocess(frame,output)
end
return p