Content deleted Content added
Improve error handling |
If the random page is unavailable, bring on a substitute |
||
Line 7:
function p._lead(pagenames, options)
errors = options.errors
if not pagenames or #pagenames < 1 then return p.err("No page names given") end
local pagename = pagenames[1]▼
math.randomseed(os.time())▼
pagename = pagenames[math.random(#pagenames)] --pick a random title▼
end▼
pagename = mw.ustring.match(pagename, "%[%[%s*(.-)[]|#]") or pagename -- "[[Foo|Bar]]" → "Foo"▼
pagename = mw.ustring.match(pagename, "%S.*%S") -- strip leading and trailing white space▼
if not pagename then return p.err("Nil page name") end▼
if pagename == "" then return p.err("Blank page name") end▼
local pagename
local title = mw.title.new(pagename) -- Find the lead section of the named page▼
local text
local
local firstpage = pagenames[1] or "(nil)" -- save for error message, as it the name will be deleted
if redir then title = mw.title.new(redir) end▼
-- read the page, or a random one if multiple pages were provided
while not text and pagecount > 0 do
local pagenum = 1
▲
if pagename and pagename ~= "" then
if pagename and pagename ~= "" then
local redir = mRedirect.getTarget(title)
end
end
if not text then table.remove(pagenames, pagenum) end -- this one didn't work; try another
pagecount = pagecount - 1 -- ensure that we exit the loop eventually
▲ end
▲ if
▲ local text = title:getContent()
text = mw.ustring.gsub(text, "%c%s*==.*","") -- remove first heading and everything after it
text = mw.ustring.gsub(text, "<noinclude>.-</noinclude>", "") -- remove noinclude bits
|