Module:Page: Difference between revisions

Content deleted Content added
Darklama (talk | contribs)
simplify and fix pageid passing
Add support for tables
 
(20 intermediate revisions by 7 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
function main(frame, field)
if not id and not mw.ustring.match( page, '%S' ) then page = nil end
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
 
local makeTitle=args.makeTitle or pargs.makeTitle or "";
if id then
local namespace=args.namespace or pargs.namespace or "";
title = callAssert(mw.title.new, 'mw.title.new', id)
local fragment=args.fragment or pargs.fragment or "";
elseif not page then
local interwiki=args.interwiki or pargs.interwiki or "";
title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle')
local nowiki=args.nowiki or pargs.nowiki or false;
elseif makeTitle then
local page=args.page or args[1] or pargs.page or pargs[1];
title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki)
local id= tonumber( args.id or pargs.id );
else
local pn = { "", "", "", "", "", "", "", "", "" };
title = callAssert(mw.title.new, 'mw.title.new', page, namespace)
local title -- holds the result of the mw.title.xxx call
end
 
for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] or ""; end
local result = title[field]
if not id and not mw.ustring.match( page, '%S' ) then page = nil; end
if type(result) == "function" then
result = result(title, unpack(pn))
if id then
elseif type(result) == "table" then
title = mw.title.new(id);
result = mw.text.listToText(result)
if not title then return "error: failed to mw.title.new(" .. id .. ")"; end
end
elseif not page then
 
title = mw.title.getCurrentTitle();
return tostring(result or "")
if not title then return "error: failed to getCurrentTitle()"; end
elseif makeTitle then
title = mw.title.makeTitle(namespace, page, fragment, interwiki);
if not title then
return mw.ustring.format("error: failed to makeTitle(%s,%s,%s,%s)", namespace, page, fragment, interwiki);
end
else
title=mw.title.new(page, namespace);
if not title then return "error: failed to mw.title.new(" .. page .. "," .. namespace .. ")"; end
end
local result = title[field];
if type(result) == "function" then
result = result( title, unpack(pn) );
if nowiki and result ~= nil then
return mw.text.nowiki( tostring( result ) );
elseif result == nil or result == false then
return "";
else
return tostring(result);
end
else
return tostring(result or "");
end
end
 
-- handle all errors in main
local p = {};
main = require('Module:Protect')(main)
 
local p = {}
 
-- main function does all the work
local meta = {}
function p.id(frame) return main(frame, "id"); end
function pmeta.interwiki__index(frame) return main(frameself, "interwiki"key); end
function p.namespace(frame) return mainfunction(frame, "namespace"); end
function p.fragment(frame) return main(frame, "fragment"key); end
end
function p.nsText(frame) return main(frame, "nsText") end
end
function p.subjectNsText(frame) return main(frame, "subjectNsText"); end
setmetatable(p, meta)
function p.text(frame) return main(frame, "text"); end
 
function p.prefixedText(frame) return main(frame, "prefixedText"); end
function p.fullTextgetContent(frame) return main(frame, "fullText"); end
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
function p.rootText(frame) return main(frame, "rootText"); end
local fmt = args.as or pargs.as or "pre"
function p.baseText(frame) return main(frame, "baseText"); end
function local p.subpageText(frame)text return= main(frame, "subpageTextgetContent"); end
 
function p.canTalk(frame) return main(frame, "canTalk"); end
fmt = mw.text.split( fmt, ", ?" )
function p.exists(frame) return main(frame, "exists"); end
 
function p.fileExists(frame) return main(frame, "fileExists"); end
for _, how in ipairs( fmt ) do
function p.isContentPage(frame) return main(frame, "isContentPage"); end
if how == "pre" then
function p.isExternal(frame) return main(frame, "isExternal"); end
text = table.concat{ "<pre>", text, "</pre>" }
function p.isLocal(frame) return main(frame, "isLocal"); end
elseif how == "expand" then
function p.isRedirect(frame) return main(frame, "isRedirect"); end
text = frame:preprocess(text)
function p.isSpecialPage(frame) return main(frame, "isSpecialPage"); end
elseif how == "nowiki" then
function p.isSubpage(frame) return main(frame, "isSubpage"); end
text = mw.text.nowiki(text)
function p.isTalkPage(frame) return main(frame, "isTalkPage"); end
end
function p.isSubpageOf(frame) return main(frame, "isSubpageOf"); end
end
function p.inNamespace(frame) return main(frame, "inNamespace"); end
 
function p.inNamespaces(frame) return main(frame, "inNamespaces"); end
return text
function p.hasSubjectNamespace(frame) return main(frame, "hasSubjectNamespace"); end
end
function p.contentModel(frame) return main(frame, "contentModel"); end
function p.basePageTitle(frame) return main(frame, "basePageTitle"); end
function p.rootPageTitle(frame) return main(frame, "rootPageTitle"); end
function p.talkPageTitle(frame) return main(frame, "talkPageTitle"); end
function p.subjectPageTitle(frame) return main(frame, "subjectPageTitle"); end
function p.subPageTitle(frame) return main(frame, "subPageTitle"); end
function p.partialUrl(frame) return main(frame, "partialUrl"); end
function p.fullUrl(frame) return main(frame, "fullUrl"); end
function p.localUrl(frame) return main(frame, "localUrl"); end
function p.canonicalUrl(frame) return main(frame, "canonicalUrl"); end
function p.getContent(frame) return main(frame, "getContent"); end
 
return p