Module:Lua banner

From Viki
Jump to navigation Jump to search

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

  1 -- This module implements the {{lua}} template.
  2 local yesno = require('Module:Yesno')
  3 local mList = require('Module:List')
  4 local mTableTools = require('Module:TableTools')
  5 local mMessageBox = require('Module:Message box')
  6 
  7 local p = {}
  8 
  9 function p.main(frame)
 10 	local origArgs = frame:getParent().args
 11 	local args = {}
 12 	for k, v in pairs(origArgs) do
 13 		v = v:match('^%s*(.-)%s*$')
 14 		if v ~= '' then
 15 			args[k] = v
 16 		end
 17 	end
 18 	return p._main(args)
 19 end
 20 
 21 function p._main(args)
 22 	local modules = mTableTools.compressSparseArray(args)
 23 	local box = p.renderBox(modules)
 24 	local trackingCategories = p.renderTrackingCategories(args, modules)
 25 	return box .. trackingCategories
 26 end
 27 
 28 function p.renderBox(modules)
 29 	local boxArgs = {}
 30 	if #modules < 1 then
 31 		boxArgs.text = '<strong class="error">Error: no modules specified</strong>'
 32 	else
 33 		local moduleLinks = {}
 34 		for i, module in ipairs(modules) do
 35 			moduleLinks[i] = string.format('[[:%s]]', module)
 36 			local maybeSandbox = mw.title.new(module .. '/sandbox')
 37 			if maybeSandbox.exists then
 38 				moduleLinks[i] = moduleLinks[i] .. string.format(' ([[:%s|sandbox]])', maybeSandbox.fullText)
 39 			end
 40 		end
 41 		local moduleList = mList.makeList('bulleted', moduleLinks)
 42 		local title = mw.title.getCurrentTitle()
 43 		if title.subpageText == "doc" then
 44 			title = title.basePageTitle
 45 		end
 46 		if title.contentModel == "Scribunto" then
 47 			boxArgs.text = 'This module depends on the following other modules:' .. moduleList
 48 		else
 49 			boxArgs.text = 'This template  uses [[Wikipedia:Lua|Lua]]:\n' .. moduleList
 50 		end
 51 	end
 52 	boxArgs.type = 'notice'
 53 	boxArgs.small = true
 54 	boxArgs.image = '[[File:Lua-Logo.svg|30px|alt=|link=]]'
 55 	return mMessageBox.main('mbox', boxArgs)
 56 end
 57 
 58 function p.renderTrackingCategories(args, modules, titleObj)
 59 	if yesno(args.nocat) then
 60 		return ''
 61 	end
 62 
 63 	local cats = {}
 64 
 65 	-- Error category
 66 	if #modules < 1 then
 67 		cats[#cats + 1] = 'Lua templates with errors'
 68 	end
 69 
 70 	-- Lua templates category
 71 	titleObj = titleObj or mw.title.getCurrentTitle()
 72 	local subpageBlacklist = {
 73 		doc = true,
 74 		sandbox = true,
 75 		sandbox2 = true,
 76 		testcases = true
 77 	}
 78 	if not subpageBlacklist[titleObj.subpageText] then
 79 		local protCatName
 80 		if titleObj.namespace == 10 then
 81 			local category = args.category
 82 			if not category then
 83 				local categories = {
 84 					['Module:String'] = 'Templates based on the String Lua module',
 85 					['Module:Math'] = 'Templates based on the Math Lua module',
 86 					['Module:BaseConvert'] = 'Templates based on the BaseConvert Lua module',
 87 					['Module:Citation'] = 'Templates based on the Citation/CS1 Lua module'
 88 				}
 89 				categories['Module:Citation/CS1'] = categories['Module:Citation']
 90 				category = modules[1] and categories[modules[1]]
 91 				category = category or 'Lua-based templates'
 92 			end	
 93 			cats[#cats + 1] = category
 94 			protCatName = "Templates using under-protected Lua modules"
 95 		elseif titleObj.namespace == 828 then
 96 			protCatName = "Modules depending on under-protected modules"
 97 		end
 98 		if not args.noprotcat and protCatName then
 99 			local protLevels = {
100 				autoconfirmed = 1,
101 				extendedconfirmed = 2,
102 				templateeditor = 3,
103 				sysop = 4
104 			}
105 			local currentProt
106 			if titleObj.id ~= 0 then
107 				-- id is 0 (page does not exist) if am previewing before creating a template.
108 				currentProt = titleObj.protectionLevels["edit"][1]
109 			end
110 			if currentProt == nil then currentProt = 0 else currentProt = protLevels[currentProt] end
111 			for i, module in ipairs(modules) do
112 				if module ~= "WP:libraryUtil" then
113 					local moduleProt = mw.title.new(module).protectionLevels["edit"][1]
114 					if moduleProt == nil then moduleProt = 0 else moduleProt = protLevels[moduleProt] end
115 					if moduleProt < currentProt then
116 						cats[#cats + 1] = protCatName
117 						break
118 					end
119 				end
120 			end
121 		end
122 	end
123 	for i, cat in ipairs(cats) do
124 		cats[i] = string.format('[[Category:%s]]', cat)
125 	end
126 	return table.concat(cats)
127 end
128 
129 return p