Module:Page: Difference between revisions

Content deleted Content added
Hmmm, I thought frame would pass automatically, but it came up as a nil error
Add support for tables
 
(33 intermediate revisions by 8 users not shown)
Line 1:
local callAssert = require('Module:CallAssert')
---- This module is meant to allow the goodies listed in
---- http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Title_objects
---- to be accessed by people who don't want to program a Lua module.
---- Usage is: {{#invoke:Page|(function)|parameters}}
---- (function) is one of the function names from the table above:
---- id, interwiki, namespace, fragment, nsText, subjectNsText, text, prefixedText, fullText ...
 
local function main(frame, field)
---- parameters are:
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
---- page = (name of page to load; leave blank to call mw.title.getCurrentTitle()
local makeTitle=args.makeTitle or pargs.makeTitle
---- this is "text" passed to mw.title.new or "title" passed to mw.title.makeTitle
local namespace=args.namespace or pargs.namespace or ""
---- makeTitle = nonblank to call mw.title.makeTitle otherwise mw.title.new is called
local fragment=args.fragment or pargs.fragment or ""
---- namespace = (parameter passed to new/makeTitle)
local interwiki=args.interwiki or pargs.interwiki or ""
---- fragment = (parameter passed to makeTitle)
local page=args.page or args[1] or pargs.page or pargs[1] or ""
---- interwiki = (parameter passed to makeTitle)
local id= tonumber( args.id or pargs.id )
---- p1 = first parameter passed to functions within the title object
local pn = {}
---- p2 = second parameter " " " "
local title -- holds the result of the mw.title.xxx call
---- p3 etc. (for inNamespaces)
 
for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end
if not id and not mw.ustring.match( page, '%S' ) then page = nil end
 
if id then
title = callAssert(mw.title.new, 'mw.title.new', id)
elseif not page then
title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle')
elseif makeTitle then
title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki)
else
title = callAssert(mw.title.new, 'mw.title.new', page, namespace)
end
 
local result = title[field]
---- Sorry, getContents doesn't work for many applications because all templates are destroyed. It does work on plain text though.
if type(result) == "function" then
 
result = result(title, unpack(pn))
local p = {}
elseif type(result) == "table" then
 
result = mw.text.listToText(result)
function p.main(frame)
end
local args=frame.args
local parent=frame.getParent(frame)
local pargs={}
if parent then pargs=parent.args end
local makeTitle=args.makeTitle or pargs.makeTitle or ""
local namespace=args.namespace or pargs.namespace or ""
local fragment=args.fragment or pargs.fragment or ""
local interwiki=args.interwiki or pargs.interwiki or ""
local p1 = args.p1 or pargs.p1 or ""
local p2 = args.p2 or pargs.p2 or ""
local p3 = args.p3 or pargs.p3 or ""
local p4 = args.p4 or pargs.p4 or ""
local p5 = args.p5 or pargs.p5 or ""
local p6 = args.p6 or pargs.p6 or ""
local p7 = args.p7 or pargs.p7 or ""
local p8 = args.p8 or pargs.p8 or ""
local p9 = args.p9 or pargs.p9 or ""
local page=args.page or pargs.page
local title -- holds the result of the mw.title.xxx call
if not(page) then
title=mw.title.getCurrentTitle()
if not(page) then return "error: failed to getCurrentTitle()" end
else if makeTitle then
title=mw.title.makeTitle(namespace,page,fragment,interwiki)
if not (page) then return "error: failed to makeTitle(" .. namespace .. "," .. page .. "," .. fragment .. "," .. interwiki .. ")" end
else if id then
title=mw.title.new(id)
if not (page) then return "error: failed to mw.title.new(" .. id .. ")" end
else
title=mw.title.new(page,namespace)
if not (page) then return "error: failed to mw.title.new(" .. page .. "," .. namespace .. ")" end
end -- if id
end -- if makeTitle
end -- if not(page)
local result=page[field]
if type(result)=="function" then
return frame.preprocess(frame, "<pre><nowiki>" .. result(page,p1,p2,p3,p4,p5,p6,p7,p8,p9) [[</nowiki></pre><span style="display:none;">]]) -- I *think* these are all page:x() calls
else return tostring(result) -- note that nil values will be returned as "nil", not ""
end
 
return tostring(result or "")
end
 
-- handle all errors in main
main = require('Module:Protect')(main)
 
local p = {}
function p.id(frame)
field="id"
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
end
 
-- main function p.interwiki(frame)does all the work
local meta = {}
field="interwiki"
function meta.__index(self, key)
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
return function(frame)
return main(frame, key)
end
end
setmetatable(p, meta)
 
function p.namespacegetContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
field="namespace"
local fmt = args.as or pargs.as or "pre"
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
local text = main(frame, "getContent")
end
 
fmt = mw.text.split( fmt, ", ?" )
function p.fragment(frame)
field="fragment"
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
end
 
for _, how in ipairs( fmt ) do
function p.nsText(frame)
if how == "pre" then
field="nsText"
text = table.concat{ "<pre>", text, "</pre>" }
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
elseif how == "expand" then
end
text = frame:preprocess(text)
 
elseif how == "nowiki" then
function p.subjectNsText(frame)
text = mw.text.nowiki(text)
field="subjectNsText"
end
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
end
 
function p.text(frame)
field="text"
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
end
 
return text
function p.prefixedText(frame)
field="prefixedText"
return p.main(frame) -- I ''think'' that frame, field automatically is available to p.main
end