Editing Module:Redirect
From MINR.ORG WIKI
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | -- This module | + | ---- 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) | function p.main(frame) | ||
− | + | args=frame.args | |
− | + | parent=frame.getParent(frame) | |
− | + | if parent then pargs = parent.args else pargs={} end | |
− | + | local rname=args[1] or pargs[1] or "" | |
− | -- | + | local bracket=args.bracket or pargs.bracket |
− | + | local bb = "";local be= "" -- store brackets if restoring to an output if bracket option is set | |
− | + | if rname then | |
− | + | if mw.ustring.gsub(rname,"%s","") == "" then return "" end | |
− | + | if bracket then | |
− | + | local strip=mw.ustring.gsub(rname,"%[%[(.+)%]%]") | |
− | + | if strip then rname=strip;bb="[[";be="]]" end | |
− | + | end | |
− | + | else return "" | |
− | + | end | |
− | + | local rpage=mw.title.new(rname) | |
− | + | local err="" | |
− | + | if rpage then -- avoid possibly expensive FileExists if we don't have anything | |
− | -- | + | if rpage.id ~= 0 then |
− | -- if | + | -- if rpage.fileExists then |
− | + | if rpage.isRedirect then | |
− | + | return (bb.. tostring(mw.ustring.match(tostring(rpage.getContent(rpage)),"REDIRECT.-%[%[(.-)%]%]")) or [=[<span style="text-color:red;">[[Module:redirect]] error: failed to understand - [[]=] .. rname .. [=[]]</span]=] .. be) | |
− | + | 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 | end | ||
return p | return p |