require('strict')
localp={}
localgetArgs=require('Module:Arguments').getArgs
local_fetch=require('Module:Transclusion_count')._fetch-- _fetch looks at the 'demo' argument
localyesno=require('Module:Yesno')
locallang_obj=mw.getContentLanguage()-- this here because the language object is used multiple places in the module
locallarge_count_cutoff=100000
localapprox_num_total_pages=63000000
localuser_subpage_info_page='Wikipedia:User pages#SUB'
localsandbox_module_page='Module:Sandbox'
localsystem_messages_cat='Pages used in system messages needing protection'
localsandbox_word='sandbox'
localtestcases_word='testcases'
localdoc_word='doc'
localfunctioncount_from_args(args)
iftonumber(args.count)then-- check if function has already been used
returntonumber(args.count)-- early exit if so
end
localcount
ifyesno(args['fetch'])~=falsethen
count=_fetch(args)-- fetch transclusion count
end
-- use explicitly-provided count when fetch fails
ifcount==nilandargs[1]~=nilandargs[1]~=''then
-- convert local language number string to a number understandable by Lua
count=mw.ustring.gsub(args[1],'+$','')
count=lang_obj:parseFormattedNumber(args[1])
end
-- in case someone writes a non-positive number
ifcountandcount>0then
returncount
end
returnnil
end
-- Actions if there is a large (greater than or equal to 100,000) transclusion count
localfunctionrisk_boolean(args)
ifargs.risk==trueorargs.risk==falsethen
returnargs.risk
elseifargs[1]=='risk'then
returntrue
else
localcount=count_from_args(args)
ifcountandcount>=large_count_cutoffthen
returntrue
end
end
returnfalse
end
-- function retained for backwards compatibility
functionp._risk(args)
returnrisk_boolean(args)and'risk'or''
end
-- function retained for backwards compatibility
functionp.risk(frame)
returnp._risk(getArgs(frame))
end
-- count and no_percent arguments retained for backwards compatibility
functionp._num(args,count,no_percent)
ifcount==nilthen
count=count_from_args(args)
end
args.count=count
args.risk=risk_boolean(args)
-- Build output string
localreturn_value=''
ifargs.count==nilandargs.riskthen
return'a very large number of'
elseifargs.count==nilthen
return'many'
else
-- Use 2 significant figures for smaller numbers and 3 for larger ones
localsigfig=2
ifargs.count>=large_count_cutoffthen
sigfig=3
end
-- Prepare to round to appropriate number of sigfigs
localf=math.floor(math.log10(args.count))-sigfig+1
-- Round and insert 'approximately' or '+' when appropriate
ifyesno(args[2])==trueor(type(args[1])=='string'and(mw.ustring.sub(args[1],-1)=='+'))then
-- Round down
return_value=string.format('%s+',lang_obj:formatNum(math.floor((args.count/10^(f)))*(10^(f))))
else
-- Round to nearest
return_value=string.format('approximately %s',lang_obj:formatNum(math.floor((args.count/10^(f))+0.5)*(10^(f))))
end
-- Insert percentage of pages if that is likely to be >= 1% and when |no-percent= not set to yes
no_percent=yesno(no_percentorargs['no-percent'])
ifargs.countandargs.count>=approx_num_total_pages/100andnotno_percentthen
localnum_total_pages=mw.getCurrentFrame():callParserFunction('NUMBEROFPAGES','R')
localtotal_percent=math.floor(((args.count/num_total_pages)*100)+0.5)
iftotal_percent>=1then
return_value=string.format('%s pages, or roughly %s%% of all',return_value,total_percent)
end
end
end
returnreturn_value
end
-- used by [[Template:Stub documentation]] and other pages
-- count argument retained for backwards compatibility
functionp.num(frame,count)
returnp._num(getArgs(frame),count)
end
-- count argument retained for backwards compatibility
functionp._text(args,count)
--[=[
Only show the information about how this template gets updated
if someone is actually editing the page and maybe trying to update the count.
]=]
localbot_text=(mw.getCurrentFrame():preprocess('{{REVISIONID}}')=='')and("\n\n----\n'''Preview message''':"..' Transclusion count updated automatically ([[Template:High-use/doc#Technical details|see documentation]]).')or''
ifcount==nilthen
count=count_from_args(args)
end
args.count=count
args.risk=risk_boolean(args)
-- trim /doc, /sandbox and /testcases
localtitle=args.titleor(args.demoandargs.demo~=''andmw.title.new(args.demo,'Template'))ormw.title.getCurrentTitle()
iftitle.subpageText==doc_wordortitle.subpageText==sandbox_wordortitle.subpageText==testcases_wordthen
title=title.basePageTitle
end
-- use /testcases of base template
localtestcases_page=mw.title.new(title.prefixedText..'/'..testcases_word)
-- exists is expensive
whiletestcases_page.basePageTitle.isSubpageandnottestcases_page.existsdo
testcases_page=mw.title.new(testcases_page.basePageTitle.basePageTitle.prefixedText..'/'..testcases_word)
end
localsystemMessages=(args['system']or'')~=''
-- This retrieves the project URL automatically to simplify localization.
localtemplateCount=('on [https://linkcount.toolforge.org/?project=%s&page=%s#transclusions %s pages]'):format(
title:fullUrl():gsub('//(.-)/.*','%1'),
mw.uri.encode(title.fullText),p._num(args))
localused_on_text="'''This "..(title:inNamespace('Module')and'Lua module'or'template')..' is used '
ifsystemMessagesthen
used_on_text=used_on_text..args['system']..
((args.countandargs.count>2000)and("''', and "..templateCount)or("'''"))
else
used_on_text=used_on_text..templateCount.."'''"
end
localsandbox_text=('%s\'s [[%s/sandbox|/sandbox]] or [[%s|/testcases]] subpages, or in your own [[%s]]. '):format(
title:inNamespace('Module')and'module'or'template',
title.fullText,
testcases_page.fullText,
title:inNamespace('Module')and(sandbox_module_page..'|module sandbox')or(user_subpage_info_page..'|user subpage')
)
localinfoArg=args['info']~=''andargs['info']
if(systemMessagesorargs.risk)then
localinfo='.'
ifsystemMessagesthen
info=info..'<br />Changes to it can cause immediate changes to the '..mw.site.namespaces.Project.name..' user interface.'
end
ifinfoArgthen
info=info..'<br />'..infoArg
end
sandbox_text=info..'<br /> To avoid major disruption'..
(args.countandargs.count>=large_count_cutoffand' and server load'or'')..-- should this use args.risk?
', any changes should be tested in the '..sandbox_text..
'The tested changes can be added to this page in a single edit. '
else
sandbox_text=(infoArgand('.<br />'..infoArg..' C')or' and c')..
'hanges may be widely noticed. Test changes in the '..sandbox_text
end
localdiscussion_text=systemMessagesand'Please discuss changes 'or'Consider discussing changes '
ifargs[2]~=nilandargs[2]~=''andyesno(args[2])==nilthen
discussion_text=string.format('%sat [[%s]]',discussion_text,args[2])
else
discussion_text=string.format('%son the [[%s|talk page]]',discussion_text,title.talkPageTitle.fullText)
end
returnused_on_text..sandbox_text..discussion_text..' before implementing them.'..bot_text
end
-- used by [[Template:R from high-use template]]
-- count argument retained for backwards compatibility
functionp.text(frame,count)
returnp._text(getArgs(frame),count)
end
-- nocat argument retained for backwards compatibility
functionp._main(args,nocat)
args.count=count_from_args(args)
args.risk=risk_boolean(args)
args.title=(args.demoandargs.demo~=''andmw.title.new(args.demo,'Template'))ormw.title.getCurrentTitle()
localimage='Ambox warning yellow.svg'
localtype_param='style'
localepilogue=''
ifargs['system']andargs['system']~=''then
image='Ambox important.svg'
type_param='content'
ifyesno(nocatorargs['nocat'])~=trueandnotargs.title.isRedirectthen
localprotection_action=(args.title:inNamespace('File')and'upload')or'edit'
localprotection_level=require('Module:Effective protection level')._main(protection_action,args.title.fullText)
ifprotection_level~='sysop'andprotection_level~='templateeditor'andprotection_level~='interfaceadmin'then
epilogue=mw.getCurrentFrame():expandTemplate{
title='sandbox other',
args={
[2]='[[Category:'..system_messages_cat..']]'
}
}
end
end
elseifargs.riskthen
image='Ambox warning orange.svg'
type_param='content'
end
image='[[File:'..image..'|40px|alt=Warning|link=]]'
ifargs['form']=='editnotice'then
returnmw.getCurrentFrame():expandTemplate{
title='editnotice',
args={
['image']=image,
['text']=p._text(args),
['expiry']=(args['expiry']or'')
}
}..epilogue
else
returnrequire('Module:Message box').main('ombox',{
type=type_param,
image=image,
text=p._text(args),
expiry=(args['expiry']or'')
})..epilogue
end
end
functionp.main(frame)
returnp._main(getArgs(frame))
end
returnp