Module:User scripts table

MyWikiBiz, Author Your Legacy — Thursday April 25, 2024
Jump to navigationJump to search

Template:Module rating This module reads the table in WP:User scripts/Most imported scripts and produces the one in WP:User scripts/Ranking.

Usage

The module can be called either directly through User scripts table#mainhttps://mywikibiz.com/User_scripts_table#main or via its associated template {{User scripts table}}.

Template:Sandbox other


local p = {}

local function allcases(s)
	return s:gsub('([%^%$%(%)%%%.%[%]%*%+%-])', '%%%1')
		:gsub('%a', function(letter) return '['..letter:upper()..letter:lower()..']' end)
end

function p.main(frame)
    local rowsToGet = tonumber(frame.args[1]) or 200  -- use this to determine number of rows to get (default 200)
    local rowOffset = tonumber(frame.args[2]) or 0   -- use this offset to allow multiple calls to build larger table   
    local source = mw.title.new('Wikipedia:User scripts/Most imported scripts'):getContent()
    local data = {}
	local rows = mw.html.create()
    local count = 0
    for script, total, active in source:gmatch('\n| %[%[([^%]]+)%]%] -\n| (%d+) -\n| (%d+)') do
		count = count + 1
		if count > rowOffset then
			local redirectTarget = mw.title.new(script).redirectTarget
			if redirectTarget then script = redirectTarget.prefixedText end
			local jsContent = mw.title.new(script):getContent()
			-- don't include scripts that have been blanked or redirected as non-functional
			if not jsContent:find("mw.log.warn( 'You installed the userscript", 1, true) then
				data[script] = { total = total, active = active }
				local desc = script:match('(.-)%.[CJcj][Ss][Ss]?$')
				local redirectTarget = mw.title.new(desc).redirectTarget
				if redirectTarget then desc = redirectTarget.prefixedText end
				local name = desc:match('([^/:]-)$')
				local author = script:match('User:([^%/]+)')
				local excerpt = mw.title.new(desc):getContent() or ''
				if excerpt ~= '' then
					excerpt = mw.text.truncate(excerpt
						--keep descriptions from template params
						--expand {{u}} at top level
						--remove other templates
						:gsub("%b{}", function(bracketed)
							local desc_first, desc_rest = bracketed:match('|%s*[Dd][Ee][Ss][Cc][^=]+=%s*([^|])([^|]+)|')
							if desc_first then
								return desc_first:upper() .. desc_rest
							end
							local user = bracketed:find "^{{[Uu]|([^}]+)}}"
							if user then
								return '[[User:' .. user .. '|' .. user .. ']]'
							end
							if bracketed:find('^{{') and bracketed:find('}}$') then
								return ''
							end
						end)
						--strip out images, files, media, categories
						:gsub('%b[]',
							function(bracketed)
								return bracketed:gsub('^%[%[%s*(%a+):.-%]%]$',
									function(link_prefix)
										link_prefix = link_prefix:lower()
										if link_prefix == "image" or link_prefix == "file"
										or link_prefix == "media" or link_prefix == "category" then
											return ""
										end -- otherwise leave it alone
									end)
							end)
						--remove spans while keeping text inside
						--strip out remaining tags and the text inside
						:gsub('<(%a+)[^>]+>(.-)</%1>', function(tag, contents)
							if tag:lower() == "span" then
								return contents
							else
								return ""
							end
						end)
						:gsub('%b<>', '') --remove any other tag markup
						:gsub('__[^_]+__', '') --remove __ markups
						:gsub('^=+[^=]+=+', ''):gsub('\n=+[^=]+=+', '') --remove section titles
						:gsub("''+", "") --strip out bold and italic markup
						:gsub('&nbsp;', ' ') --replace nbsp spaces with regular spaces
						:gsub('^[:;%s]+', ''):gsub('\n[:;%s]+', '\n') --strip indents, leading
						:gsub('{|.-\n|}', '') --remove tables
						:gsub('\n|[^\n]*\n', '') --remove table fragments
						:gsub('%s+\n', '\n') --and trailing spaces
						:gsub('(%s)%s+', '%1') --strip redundant spaces
						:gsub(allcases(name)..'(%s)', "'''"..name.."'''%1")
						, 600, ''):gsub('(.+)\n', '%1') -- truncate at end of last paragraph before 600 chars
						..' '..'[['..desc..'|→]]'
				end
				if not excerpt then excerpt = '' end
				local row = rows:tag('tr'):attr('id', script):attr('style','vertical-align:top')
				row:tag('td'):wikitext(string.format('\'\'\'[[%s|%s]]\'\'\'<span id="%s" class=scriptInstallerLink></span>', script, name, script))
				row:tag('td'):wikitext(string.format('[[User:%s|%s]]', author, author))
				row:tag('td'):wikitext(frame:callParserFunction('#time', 'j M Y', frame:callParserFunction('REVISIONTIMESTAMP', script))):addClass('nowrap'):attr('style','text-align:right')
				row:tag('td'):wikitext(excerpt .. '<!-- -->') -- add an empty HTML comment so that if the excerpt ends with an unclosed comment 
				-- (such as [[User:Steel359/Protection js]]), it doesn't break things
				row:tag('td'):wikitext(data[script].active):attr('style','text-align:right')
				row:tag('td'):wikitext(data[script].total):attr('style','text-align:right')
				rows:wikitext('\n')
			end
		end
	    if count >= rowsToGet + rowOffset then break end
    end
    return rows
end

return p