Jump to content

Module:User scripts table/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Guarapiranga (talk | contribs) at 14:33, 15 June 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local tmpv = require('Module:Template parameter value').main

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 doc = script:match('(.-)%.[CJcj][Ss][Ss]?$')
				local redirectTarget = mw.title.new(doc).redirectTarget
				if redirectTarget then doc = redirectTarget.prefixedText end
				local name = doc:match('([^/:]-)$')
				local author = script:match('User:([^%/]+)')
				local desc = ''
				local status = ''
				local browsers = ''
				local skins = ''
				local doctext = mw.title.new(doc):getContent() or ''
				if doctext ~= '' then
					name = mw.text.trim(doctext:match('|%s*[Nn][Aa][Mm][Ee]%s*=%s*([^|]+)') or doc:match('([^/:]-)$'))
					author = mw.text.trim(doctext:match('|%s*[Aa][Uu][Tt][Hh][Oo][Rr]%s*=%s*([^|]+)') or script:match('User:([^%/]+)')):gsub('%[%[User%s*:%s*([^|%]]+)%]%]', '%1'):gsub('{{.-|([^|}]+)}}', '%1')
					status = mw.text.trim(doctext:match('|%s*[Ss][Tt][Aa][Tt][Uu][Ss]%s*=%s*([^|]+)') or '')
					browsers = mw.text.trim(doctext:match('|%s*[Bb][Rr][Oo][Ww][Ss][Ee][Rr][Ss]%s*=%s*([^|]+)') or '')
					skins = mw.text.trim(doctext:match('|%s*[Ss][Kk][Ii][Nn][Ss]%s*=%s*([^|]+)') or '')
					desc = doctext:match('|%s*[Dd][Ee][Ss][Cc][^=]+=%s*([^|]+)') or mw.text.truncate(doctext
						--keep descriptions from template params						
						:gsub('^{{[Uu]|([^}]+)', '[[User:%1|%1]]') --expand {{u}} at top level
						:gsub("%b{}", '') --remove other templates
						--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('^(.+%.).+$', '%1') --truncate at end of last sentence before 600 chars
						.. '<!-- -->'
					desc = mw.text.trim(desc):gsub('^(.+)%.$', '%1')..'.'  --trim and add last period, if missing
						.. ' [[' .. doc .. '|→]]' --add arrow link to full doc
				end
				local row = rows:tag('tr'):attr('id', script):attr('style','vertical-align:top')
				if doctext == '' then link = script else link = doc end
				row:tag('td'):wikitext(string.format('\'\'\'[[%s|%s]]\'\'\'<span id="%s" class=scriptInstallerLink></span>\n<p>%s</p>', link, name, script, desc))
				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(status)
				row:tag('td'):wikitext(skins)
				row:tag('td'):wikitext(browsers)
				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