VOOZH about

URL: https://en.wikipedia.org/wiki/Module:Link_if_exists

⇱ Module:Link if exists - Wikipedia


Jump to content
From Wikipedia, the free encyclopedia
👁 Image
Module documentation
[view] [edit] [history] [purge]
👁 Image
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
👁 Page template-protected
This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
👁 Warning
This Lua module is used on approximately 300,000 pages.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
👁 Image
This module depends on the following other modules:

Implements {{Link if exists}}.

Usage

{{#invoke:Link if exists|main}}

Arguments

1
title of page to link to
2
text to display
prefix
prepend this string to title
nsp
namespace for title, does not display
color
Color of text if no link is found
hide_display
If page does not exist, do not display anything
require('strict')
localp={}
localfunctiontitleExistsUnprotected(titleObject)
returntitleObject.exists
end
-- test for title existing
-- if we get an error accessing titleObject.exists, assume it doesn't exist
localfunctiontitleExists(titleObject)
localsuccess,exists=pcall(titleExistsUnprotected,titleObject)
returnsuccessandexists
end
functionp._main(args)
localtitle=args[1]
ifnottitlethen
returntitle
end
localdisplay=args[2]ortitle
title=args.prefixandargs.prefix..':'..titleortitle
localtitleObject=mw.title.new(title,args.nsp)
localresult=''
iftitleObjectandtitleExists(titleObject)then
-- use prefix only if args[2] is empty/false
display=args[2]ortitle
result=result..'[['
result=result..(titleObject.namespace~=0and':'or'')
result=result..(titleObject.fullText~=displayandtitleObject.fullText..'|'or'')
result=result..display..']]'
elseifnotargs.hide_displaythen
result=result..(args.colorand'<span style="color:'..args.color..'">'or'')
result=result..display
result=result..(args.colorand'</span>'or'')
end
returnresult
end
functionp.main(frame)
localgetArgs=require('Module:Arguments').getArgs
localargs=getArgs(frame)
returnp._main(args)or""
end
returnp