Jump to content

Module:Page

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 18:45, 20 March 2013 (I'm not sure I'm doing this right.... idea is to avoid a wasted "parameter", among other things). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

 ---- 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 ...

 ---- parameters are:
 ---- page = (name of page to load; leave blank to call mw.title.getCurrentTitle()
 ----    this is "text" passed to mw.title.new or "title" passed to mw.title.makeTitle
 ---- makeTitle = nonblank to call mw.title.makeTitle otherwise mw.title.new is called
 ---- namespace = (parameter passed to new/makeTitle)
 ---- fragment = (parameter passed to makeTitle)
 ---- interwiki = (parameter passed to makeTitle)
 ---- p1 = first parameter passed to functions within the title object
 ---- p2 = second parameter " " " "
 ---- p3 etc. (for inNamespaces)



 ---- Sorry, getContents doesn't work for many applications because all templates are destroyed.  It does work on plain text though.

local p = {}

function p.main(frame)
   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 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

end


function p.id(frame)
   field="id"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.interwiki(frame)
   field="interwiki"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.namespace(frame)
   field="namespace"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.fragment(frame)
   field="fragment"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.nsText(frame)
   field="nsText"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.subjectNsText(frame)
   field="subjectNsText"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.text(frame)
   field="text"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

function p.prefixedText(frame)
   field="prefixedText"
   return p.main() -- I ''think'' that frame, field automatically is available to p.main
end

return p