Module:User scripts table/sandbox
Appearance
![]() | This is the module sandbox page for Module:User scripts table (diff). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
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 {{#invoke:User scripts table|main}} or via its associated template {{User scripts table}}.
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('[Ii]nfobox user script.-|%s*' .. allcases('name') .. '%s*=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('name') .. '%s*=%s*([^|]+)') or doc:match('([^/:]-)$'))
author = mw.text.trim(doctext:match('[Ii]nfobox user script.-|%s*' .. allcases('author') .. '%s*=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('author') .. '%s*=%s*([^|]+)') or script:match('User:([^%/]+)')):gsub('%[%[User%s*:%s*([^|%]]+).*%]%]', '%1'):gsub('{{[^|]+|%s*([^|}]+)}}', '%1')
status = mw.text.trim(doctext:match('[Ii]nfobox user script.-|%s*' .. allcases('status') .. '%s*=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('status') .. '%s*=%s*([^|]+)') or '')
browsers = mw.text.trim(doctext:match('[Ii]nfobox user script.-|%s*' .. allcases('browsers') .. '%s*=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('browsers') .. '%s*=%s*([^|]+)') or ''):gsub('{{[^|]+|%s*([^|}]+)}}', '%1')
skins = mw.text.trim(doctext:match('[Ii]nfobox user script.-|%s*' .. allcases('skins') .. '%s*=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('skins') .. '%s*=%s*([^|]+)') or ''):gsub('{{[^|]+|%s*([^|}]+)}}', '%1')
desc = doctext:match('[Ii]nfobox user script.-|%s*' .. allcases('desc') .. '[^=]+=%s*([^|]+)') or doctext:match('[Ii]nfobox Wikipedia user script.-|%s*' .. allcases('desc') .. '[^=]+=%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(' ', ' ') --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