Module:Page: Difference between revisions

Content deleted Content added
Get rid of repeated code, use Lua errors without ___location information for error handling
Add support for tables
 
(9 intermediate revisions by 4 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 or "";
---- 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 ppn = {};
---- 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
local function callAssert(func, funcName, ...)
if not id and not mw.ustring.match( page, '%S' ) then page = nil; end
local result = { func(...) }
 
if not result[1] then
if id then
error(mw.ustring.format('%s(%s) failed', funcName, join(', ', ...)), 0)
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
return unpack(result)
end
 
local result = { func(...) }title[field]
function main(frame, field)
if not type(result[1]) == "function" then
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
success, result = pcallresult( result, title, unpack(pn) );
local makeTitle=args.makeTitle or pargs.makeTitle or "";
if elseif type(result) == "functiontable" then
local namespace=args.namespace or pargs.namespace or "";
result = mw.text.listToText(result)
local fragment=args.fragment or pargs.fragment or "";
end
local interwiki=args.interwiki or pargs.interwiki or "";
 
local page=args.page or args[1] or pargs.page or pargs[1] or "";
return unpacktostring(result or "")
local id= tonumber( args.id or pargs.id );
local pn = {};
local title -- holds the result of the mw.title.xxx call
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];
if type(result) == "function" then
local success;
success, result = pcall( result, title, unpack(pn) );
if not success then
error(result, 0);
end
end
return tostring(result or "");
end
 
-- handle all errors in main
local p = {};
main = require('Module:Protect')(main)
 
local pnp = {};
 
-- main function does all the work
local meta = {};
function meta.__index(tableself, key)
return function(frame)
return main(frame, key)
Line 73 ⟶ 50:
 
function p.getContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
local fmt = args.as or pargs.as or false"pre"
local text = main(frame, "getContent")
 
if not fmt then
return frame:preprocess( "<pre>" .. text .. "</pre>" )
end
fmt = mw.text.split( fmt, ", ?" )
 
for _, how in ipairs( fmt ) do
if how == "pre" then