Module:High-use

From Viki
Jump to navigation Jump to search

Documentation for this module may be created at Module:High-use/doc

  1 local p = {}
  2 
  3 -- _fetch looks at the "demo" argument.
  4 local _fetch = require('Module:Transclusion_count').fetch
  5 local yesno = require('Module:Yesno')
  6 
  7 function p.num(frame, count)
  8 	if count == nil then
  9 		if yesno(frame.args['fetch']) == false then
 10 			if (frame.args[1] or '') ~= '' then count = tonumber(frame.args[1]) end
 11 		else
 12 			count = _fetch(frame)
 13 		end
 14 	end
 15 	
 16 	-- Build output string
 17 	local return_value = ""
 18 	if count == nil then
 19 		if frame.args[1] == "risk" then
 20 			return_value = "a very large number of"
 21 		else
 22 			return_value = "many"
 23 		end
 24 	else
 25 		-- Use 2 significant figures for smaller numbers and 3 for larger ones
 26 		local sigfig = 2
 27 		if count >= 100000 then
 28 			sigfig = 3
 29 		end
 30 		
 31 		-- Prepare to round to appropriate number of sigfigs
 32 		local f = math.floor(math.log10(count)) - sigfig + 1
 33 		
 34 		-- Round and insert "approximately" or "+" when appropriate
 35 		if (frame.args[2] == "yes") or (mw.ustring.sub(frame.args[1],-1) == "+") then
 36 			-- Round down
 37 			return_value = string.format("%s+", mw.getContentLanguage():formatNum(math.floor( (count / 10^(f)) ) * (10^(f))) )
 38 		else
 39 			-- Round to nearest
 40 			return_value = string.format("approximately %s", mw.getContentLanguage():formatNum(math.floor( (count / 10^(f)) + 0.5) * (10^(f))) )
 41 		end
 42 
 43 		-- Insert percentage of pages if that is likely to be >= 1% and when |no-percent= not set to yes
 44 		if count and count > 250000 and not yesno (frame:getParent().args['no-percent']) then
 45 			local percent = math.floor( ( (count/frame:callParserFunction('NUMBEROFPAGES', 'R') ) * 100) + 0.5)
 46 			if percent >= 1 then
 47 				return_value = string.format("%s pages, or roughly %s%% of all", return_value, percent)
 48 			end
 49 		end	
 50 	end
 51 	
 52 	return return_value
 53 end
 54 -- Actions if there is a large (greater than or equal to 100,000) transclusion count
 55 function p.risk(frame)
 56 	local return_value = ""
 57 	if frame.args[1] == "risk" then
 58 		return_value = "risk"
 59 	else
 60 		local count = _fetch(frame)
 61 		if count and count >= 100000 then return_value = "risk" end
 62 	end
 63 	return return_value
 64 end
 65 
 66 function p.text(frame, count)
 67 	-- Only show the information about how this template gets updated if someone
 68 	-- is actually editing the page and maybe trying to update the count.
 69 	local bot_text = (frame:preprocess("{{REVISIONID}}") == "") and "\n\n----\n'''Preview message''': Transclusion count updated automatically ([[Template:High-use/doc#Technical details|see documentation]])." or ''
 70 	
 71 	if count == nil then
 72 		if yesno(frame.args['fetch']) == false then
 73 			if (frame.args[1] or '') ~= '' then count = tonumber(frame.args[1]) end
 74 		else
 75 			count = _fetch(frame)
 76 		end
 77 	end
 78 	local title = mw.title.getCurrentTitle()
 79 	if title.subpageText == "doc" or title.subpageText == "sandbox" then
 80 		title = title.basePageTitle
 81 	end
 82 	
 83 	local systemMessages = frame.args['system']
 84 	if frame.args['system'] == '' then
 85 		systemMessages = nil
 86 	end
 87 	
 88 	-- This retrieves the project URL automatically to simplify localiation.
 89 	local templateCount = ('on [https://linkcount.toolforge.org/index.php?project=%s&page=%s %s pages]'):format(
 90 		mw.title.getCurrentTitle():fullUrl():gsub('//(.-)/.*', '%1'),
 91 		mw.uri.encode(title.fullText), p.num(frame, count))
 92 	local used_on_text = "'''This " .. (mw.title.getCurrentTitle().namespace == 828 and "Lua module" or "template") .. ' is used ';
 93 	if systemMessages then
 94 		used_on_text = used_on_text .. systemMessages ..
 95 			((count and count > 2000) and (",''' and " .. templateCount) or ("'''"))
 96 	else
 97 		used_on_text = used_on_text .. templateCount .. "'''"
 98 	end
 99 	
100 	
101 	local sandbox_text =  ("%s's [[%s/sandbox|/sandbox]] or [[%s/testcases|/testcases]] subpages, or in your own [[%s]]. "):format(
102 		(mw.title.getCurrentTitle().namespace == 828 and "module" or "template"),
103 		title.fullText, title.fullText,
104 		mw.title.getCurrentTitle().namespace == 828 and "Module:Sandbox|module sandbox" or "Wikipedia:User pages#SUB|user subpage"
105 	)
106 	
107 	local infoArg = frame.args["info"] ~= "" and frame.args["info"]
108 	if (systemMessages or frame.args[1] == "risk" or (count and count >= 100000) ) then
109 		local info = systemMessages and '.<br/>Changes to it can cause immediate changes to the Wikipedia user interface.' or '.'
110 		if infoArg then
111 			info = info .. "<br />" .. infoArg
112 		end
113 		sandbox_text = info .. '<br /> To avoid major disruption' ..
114 			(count and count >= 100000 and ' and server load' or '') ..
115 			', any changes should be tested in the ' .. sandbox_text ..
116 			'The tested changes can be added to this page in a single edit. '
117 	else
118 		sandbox_text = (infoArg and ('.<br />' .. infoArg .. ' C') or ' and c') ..
119 			'hanges may be widely noticed. Test changes in the ' .. sandbox_text
120 	end
121 
122 	
123 	local discussion_text = systemMessages and 'Please discuss changes ' or 'Consider discussing changes '
124 	if frame.args["2"] and frame.args["2"] ~= "" and frame.args["2"] ~= "yes" then
125 		discussion_text = string.format("%sat [[%s]]", discussion_text, frame.args["2"])
126 	else
127 		discussion_text = string.format("%son the [[%s|talk page]]", discussion_text, title.talkPageTitle.fullText )
128 	end
129 	
130 	return used_on_text .. sandbox_text .. discussion_text .. " before implementing them." .. bot_text
131 end
132 
133 function p.main(frame)
134 	local count = nil
135 	if yesno(frame.args['fetch']) == false then
136 		if (frame.args[1] or '') ~= '' then count = tonumber(frame.args[1]) end
137 	else
138 		count = _fetch(frame)
139 	end
140 	local image = "[[File:Ambox warning yellow.svg|40px|alt=Warning|link=]]"
141 	local type_param = "style"
142 	local epilogue = ''
143 	if frame.args['system'] and frame.args['system'] ~= '' then
144 		image = "[[File:Ambox important.svg|40px|alt=Warning|link=]]"
145 		type_param = "content"
146 		local nocat = frame:getParent().args['nocat'] or frame.args['nocat']
147 		local categorise = (nocat == '' or not yesno(nocat))
148 		if categorise then
149 			epilogue = frame:preprocess('{{Sandbox other||{{#switch:{{#invoke:Effective protection level|{{#switch:{{NAMESPACE}}|File=upload|#default=edit}}|{{FULLPAGENAME}}}}|sysop|templateeditor|interfaceadmin=|#default=[[Category:Pages used in system messages needing protection]]}}}}')
150 		end
151 	elseif (frame.args[1] == "risk" or (count and count >= 100000)) then
152 		image = "[[File:Ambox warning orange.svg|40px|alt=Warning|link=]]"
153 		type_param = "content"
154 	end
155 	
156 	if frame.args["form"] == "editnotice" then
157 		return frame:expandTemplate{
158 				title = 'editnotice',
159 				args = {
160 						["image"] = image,
161 						["text"] = p.text(frame, count),
162 						["expiry"] = (frame.args["expiry"] or "")
163 				}
164 		} .. epilogue
165 	else
166 		return require('Module:Message box').main('ombox', {
167 			type = type_param,
168 			image = image,
169 			text = p.text(frame, count),
170 			expiry = (frame.args["expiry"] or "")
171 		}) .. epilogue
172 	end
173 end
174 
175 return p