|
|
| 第1行: |
第1行: |
| ---- This module is meant to allow the goodies listed in
| | local callAssert = require('Module:CallAssert') |
| ---- http://www.mediawiki.wikimirror.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 ...
| |
|
| |
|
| ---- parameters are:
| | local function main(frame, field) |
| ---- page = (name of page to load; leave blank to call mw.title.getCurrentTitle()
| | local args, pargs = frame.args, ( frame:getParent() or {} ).args or {} |
| ---- this is "text" passed to mw.title.new or "title" passed to mw.title.makeTitle
| | local makeTitle=args.makeTitle or pargs.makeTitle |
| ---- makeTitle = nonblank to call mw.title.makeTitle otherwise mw.title.new is called
| | local namespace=args.namespace or pargs.namespace or "" |
| ---- namespace = (parameter passed to new/makeTitle)
| | local fragment=args.fragment or pargs.fragment or "" |
| ---- fragment = (parameter passed to makeTitle)
| | local interwiki=args.interwiki or pargs.interwiki or "" |
| ---- interwiki = (parameter passed to makeTitle) | | local page=args.page or args[1] or pargs.page or pargs[1] or "" |
| ---- p1 = first parameter passed to functions within the title object
| | local id= tonumber( args.id or pargs.id ) |
| ---- p2 = second parameter " " " "
| | local pn = {} |
| ---- p3 etc. (for inNamespaces)
| | local title -- holds the result of the mw.title.xxx call |
|
| |
|
| function main(frame, field)
| | for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end |
| local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
| | if not id and not mw.ustring.match( page, '%S' ) then page = nil end |
| local makeTitle=args.makeTitle or pargs.makeTitle or "";
| | |
| local namespace=args.namespace or pargs.namespace or "";
| | if id then |
| local fragment=args.fragment or pargs.fragment or "";
| | title = callAssert(mw.title.new, 'mw.title.new', id) |
| local interwiki=args.interwiki or pargs.interwiki or "";
| | elseif not page then |
| local nowiki=args.nowiki or pargs.nowiki or false;
| | title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle') |
| local page=args.page or args[1] or pargs.page or pargs[1] or "";
| | elseif makeTitle then |
| local id= tonumber( args.id or pargs.id );
| | title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki) |
| local pn = {};
| | else |
| local title -- holds the result of the mw.title.xxx call
| | title = callAssert(mw.title.new, 'mw.title.new', page, namespace) |
|
| | end |
| 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
| | local result = title[field] |
|
| | if type(result) == "function" then |
| if id then
| | result = result(title, unpack(pn)) |
| title = mw.title.new(id);
| | elseif type(result) == "table" then |
| if not title then return "error: failed to mw.title.new(" .. id .. ")"; end
| | result = mw.text.listToText(result) |
| elseif not page then
| | end |
| title = mw.title.getCurrentTitle();
| | |
| if not title then return "error: failed to getCurrentTitle()"; end
| | return tostring(result or "") |
| 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, success = title[field];
| |
| if type(result) == "function" then
| |
| success, result = pcall( result, title, unpack(pn) );
| |
| if not success then
| |
| return mw.ustring.format("error: failed to title:%s(%s)", field, table.concat(pn, ',' ));
| |
| end
| |
| end
| |
| if nowiki and result ~= nil then
| |
| return mw.text.nowiki( tostring( result ) );
| |
| else
| |
| return tostring(result or "");
| |
| end
| |
| end | | end |
|
| |
|
| local p = {}; | | -- handle all errors in main |
| | main = require('Module:Protect')(main) |
| | |
| | local p = {} |
|
| |
|
| -- main function does all the work | | -- main function does all the work |
| function p.id(frame) return main(frame, "id"); end | | local meta = {} |
| function p.interwiki(frame) return main(frame, "interwiki"); end | | function meta.__index(self, key) |
| function p.namespace(frame) return main(frame, "namespace"); end
| | return function(frame) |
| function p.fragment(frame) return main(frame, "fragment"); end
| | return main(frame, key) |
| function p.nsText(frame) return main(frame, "nsText") end
| | end |
| function p.subjectNsText(frame) return main(frame, "subjectNsText"); end
| | end |
| function p.text(frame) return main(frame, "text"); end | | setmetatable(p, meta) |
| function p.prefixedText(frame) return main(frame, "prefixedText"); end
| | |
| function p.fullText(frame) return main(frame, "fullText"); end
| | function p.getContent(frame) |
| function p.rootText(frame) return main(frame, "rootText"); end
| | local args, pargs = frame.args, ( frame:getParent() or {} ).args or {} |
| function p.baseText(frame) return main(frame, "baseText"); end
| | local fmt = args.as or pargs.as or "pre" |
| function p.subpageText(frame) return main(frame, "subpageText"); end
| | local text = main(frame, "getContent") |
| function p.canTalk(frame) return main(frame, "canTalk"); end
| | |
| function p.exists(frame) return main(frame, "exists"); end
| | fmt = mw.text.split( fmt, ", ?" ) |
| function p.fileExists(frame) return main(frame, "fileExists"); end
| | |
| function p.isContentPage(frame) return main(frame, "isContentPage"); end
| | for _, how in ipairs( fmt ) do |
| function p.isExternal(frame) return main(frame, "isExternal"); end
| | if how == "pre" then |
| function p.isLocal(frame) return main(frame, "isLocal"); end
| | text = table.concat{ "<pre>", text, "</pre>" } |
| function p.isRedirect(frame) return main(frame, "isRedirect"); end
| | elseif how == "expand" then |
| function p.isSpecialPage(frame) return main(frame, "isSpecialPage"); end
| | text = frame:preprocess(text) |
| function p.isSubpage(frame) return main(frame, "isSubpage"); end
| | elseif how == "nowiki" then |
| function p.isTalkPage(frame) return main(frame, "isTalkPage"); end
| | text = mw.text.nowiki(text) |
| function p.isSubpageOf(frame) return main(frame, "isSubpageOf"); end
| | end |
| function p.inNamespace(frame) return main(frame, "inNamespace"); end
| | end |
| function p.inNamespaces(frame) return main(frame, "inNamespaces"); end
| | |
| function p.hasSubjectNamespace(frame) return main(frame, "hasSubjectNamespace"); end
| | return text |
| function p.contentModel(frame) return main(frame, "contentModel"); end
| | 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 | | return p |
此模块已评为通行版,其已臻成熟,可正常使用于各处而不会出现错误。已可在帮助页面和其他求闻百科资源中提及,以帮助新用户学习。为降低服务器负载和错误输出,改善本模块前应进行沙盒测试,而不是重复的试错性编辑。
local callAssert = require('Module:CallAssert')
local function main(frame, field)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local makeTitle=args.makeTitle or pargs.makeTitle
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 page=args.page or args[1] or pargs.page or pargs[1] 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
result = result(title, unpack(pn))
elseif type(result) == "table" then
result = mw.text.listToText(result)
end
return tostring(result or "")
end
-- handle all errors in main
main = require('Module:Protect')(main)
local p = {}
-- main function does all the work
local meta = {}
function meta.__index(self, key)
return function(frame)
return main(frame, key)
end
end
setmetatable(p, meta)
function p.getContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local fmt = args.as or pargs.as or "pre"
local text = main(frame, "getContent")
fmt = mw.text.split( fmt, ", ?" )
for _, how in ipairs( fmt ) do
if how == "pre" then
text = table.concat{ "<pre>", text, "</pre>" }
elseif how == "expand" then
text = frame:preprocess(text)
elseif how == "nowiki" then
text = mw.text.nowiki(text)
end
end
return text
end
return p