Module:Redirect

From MINR.ORG WIKI
Revision as of 16:01, 18 March 2013 by Wnt (talk) (Should be the whole thing. Plus a few crawly passengers...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Redirect/doc

 ---- This module accepts the name of a single page.  It determines if the page is a redirect; if so it looks up the page, extracts the target, and returns the target name as text.  Its usage is {{#invoke:redirect|main|redirect-page-name}}.  The template neither takes nor gives square brackets, so to show the result as a link use [[{{#invoke:redirect|main|redirect-page-name}}]].  An error is produced if the redirect does not exist, but for versatility no error is given if the file is not a redirect (returns the original text) or if the file name is blank (returns blank).

p={}

function p.main(frame)
   args=frame.args
   parent=frame.getParent(frame)
   if parent then pargs = parent.args else pargs={} end
   rname=args[1] or pargs[1] or ""
   if redirect=="" then return "" end
   rpage=mw.title.new(frame,rname)
   local err=""
   if rpage then -- avoid possibly expensive FileExists if we don't have anything
      if rpage.id ~= 0 then
         if rpage.fileExists then
            if rpage.isRedirect then
               return (mw.ustring.find(rpage.getContent,"REDIRECT(.-)%[%[(.-)%]%]") or [=[<span style="text-color:red;">[[Module:redirect]] error: failed to understand - [[]=] .. rname .. [=[]]</span]=])
            else return rname -- not a redirect so leave the original text (for some general-purpose template use)
            end
         else err="File does not exist: "
         end
      else err="File not found (id=0): "
      end
   else err="File not found (mw.title.new failed)"
   end
   return [=[<span style="text-color:red;">[[Module:redirect]] error: ]=] .. err .. [=[ - [[]=] .. rname .. [=[]]</span]=]
end

return p