Jump to content

Module:User scripts table: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
test not using titleObj.exists and using titleObj.redirectTarget (which is surprisingly not expensive) instead of titleObj.isRedirect
Line 2: Line 2:
local getExcerpt = require('Module:Transcluder').get
local getExcerpt = require('Module:Transcluder').get
-- local plainText = require('Module:Plain text')._main
-- local plainText = require('Module:Plain text')._main
local mRedirect = require('Module:Redirect')


function p.main(frame)
function p.main(frame)
Line 14: Line 13:
header :tag('th'):wikitext('Script')
header :tag('th'):wikitext('Script')
:tag('th'):wikitext('Author')
:tag('th'):wikitext('Author')
:tag('th'):wikitext('Decription')
:tag('th'):wikitext('Description')
:tag('th'):wikitext('Active')
:tag('th'):wikitext('Active')
:tag('th'):wikitext('Total')
:tag('th'):wikitext('Total')
Line 20: Line 19:
for script, total, active in string.gmatch(source, '\n%| %[%[([^%]]+)%]%] -\n%| (%d+) -\n%| (%d+)') do
for script, total, active in string.gmatch(source, '\n%| %[%[([^%]]+)%]%] -\n%| (%d+) -\n%| (%d+)') do
count = count + 1
count = count + 1
local redirectTarget = mw.title.new(script).redirectTarget
if mRedirect.luaIsRedirect(script) then script = mRedirect.getTarget(script) end
if redirectTarget then script = redirectTarget.prefixedText end
data[script] = { total = tonumber(total), active = tonumber(active) }
data[script] = { total = tonumber(total), active = tonumber(active) }
local excerpt, name
local excerpt, name
local desc = script:match('(.-)%.[CJcj][Ss][Ss]?$')
local desc = script:match('(.-)%.[CJcj][Ss][Ss]?$')
local redirectTarget = mw.title.new(desc).redirectTarget
if mRedirect.luaIsRedirect(desc) then desc = mRedirect.getTarget(desc) end
if redirectTarget then desc = redirectTarget.prefixedText end
name = desc:gsub('%.[CJcj][Ss][Ss]?$', ''):match('([^/:]-)$')
name = desc:gsub('%.[CJcj][Ss][Ss]?$', ''):match('([^/:]-)$')
local ok
if mw.title.new(desc).exists then
excerpt = getExcerpt(desc, {only = 'paragraphs', paragraphs = '1-3', templates = 0, tables = 0, files = 0})
ok, excerpt = pcall(getExcerpt, desc, {only = 'paragraphs', paragraphs = '1-3', templates = 0, tables = 0, files = 0})
if ok then
excerpt = excerpt
:gsub(' ', ' ') --replace nbsp spaces with regular spaces
:gsub(' ', ' ') --replace nbsp spaces with regular spaces
:gsub('<br ?/?>', ', ') --replace br with commas
:gsub('<br ?/?>', ', ') --replace br with commas
Line 64: Line 67:
:tag('td'):wikitext(data[script].active):attr('style','text-align:right')
:tag('td'):wikitext(data[script].active):attr('style','text-align:right')
:tag('td'):wikitext(data[script].total):attr('style','text-align:right')
:tag('td'):wikitext(data[script].total):attr('style','text-align:right')
if count > 216 then return mytable end
if count >= 260 then return mytable end
end
end
return mytable
return mytable

Revision as of 19:45, 26 May 2021

local p = {}
local getExcerpt = require('Module:Transcluder').get
-- local plainText = require('Module:Plain text')._main

function p.main(frame)
    local source = mw.title.new('Wikipedia:User scripts/Most imported scripts'):getContent()
    local data = {}
    local mytable = mw.html.create('table')
    	:addClass('wikitable')
    	:addClass('sortable')
    	:addClass('static-row-numbers')
    local header=mytable:tag('tr'):addClass('static-row-header')
    header  :tag('th'):wikitext('Script')
    		:tag('th'):wikitext('Author')
    		:tag('th'):wikitext('Description')
    		:tag('th'):wikitext('Active')
    		:tag('th'):wikitext('Total')
    local count = 0
    for script, total, active in string.gmatch(source, '\n%| %[%[([^%]]+)%]%] -\n%| (%d+) -\n%| (%d+)') do
		count = count + 1
		local redirectTarget = mw.title.new(script).redirectTarget
		if redirectTarget then script = redirectTarget.prefixedText end
		data[script] = { total = tonumber(total), active = tonumber(active) }
    	local excerpt, name
		local desc = script:match('(.-)%.[CJcj][Ss][Ss]?$')
		local redirectTarget = mw.title.new(desc).redirectTarget
		if redirectTarget then desc = redirectTarget.prefixedText end
		name = desc:gsub('%.[CJcj][Ss][Ss]?$', ''):match('([^/:]-)$')
		local ok
		ok, excerpt = pcall(getExcerpt, desc, {only = 'paragraphs', paragraphs = '1-3', templates = 0, tables = 0, files = 0})
		if ok then
			excerpt = excerpt
				:gsub('&nbsp;', ' ') --replace nbsp spaces with regular spaces
				:gsub('<br ?/?>', ', ') --replace br with commas
				:gsub('<span.->(.-)</span>', '%1') --remove spans while keeping text inside
--				:gsub('<i.->(.-)</i>', '%1') --remove italics while keeping text inside
				:gsub('<.->.-<.->', '') --strip out remaining tags and the text inside
				:gsub('<.->', '') --remove any other tag markup
				:gsub('=+.-=+', '') --remove section titles
				:gsub('__.-__', '') --remove __ markups
				:gsub('%[%[%s*[Ff]ile%s*:.-%]%]', '') --strip out files
				:gsub('%[%[%s*[Ii]mage%s*:.-%]%]', '') --strip out use of image:
				:gsub('%[%[%s*[Cc]ategory%s*:.-%]%]', '') --strip out categories
--				:gsub('%[%[[^%]]-|', '') --strip out piped link text
--				:gsub('[%[%]]', '') --then strip out remaining [ and ]
				:gsub("'''''", "") --strip out bold italic markup
				:gsub("'''?", "") --not stripping out '''' gives correct output for bolded text in quotes
--				:gsub("('''''.-)$", "%1'''''") --close bold+italic
--				:gsub("('''?)(.-)$", "%1%2%1") --close bold/italic
				:gsub('----', '') --remove ---- lines
				:gsub("^[:;%s]+", "") --strip indents, leading 
				:gsub("%s+$", "") --and trailing spaces
				:gsub("%s+", " ") --strip redundant spaces
--				:gsub("([%[%{]-.-)"..name.."(.-[%]%}]-)", "'''%1"..name.."%2'''")
--				:gsub("^%*", "\n*") --recover bullets and numbering
				..' '..'[['..desc..'|→]]'
		else
			excerpt = ''
		end
		if not excerpt then excerpt = '' end
		local author = script:match('User:(.-)%/')
	    local row = mytable: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))
			:tag('td'):wikitext(string.format('[[User:%s|%s]]', author, author))
			:tag('td'):wikitext(excerpt)
			:tag('td'):wikitext(data[script].active):attr('style','text-align:right')
			:tag('td'):wikitext(data[script].total):attr('style','text-align:right')
	    if count >= 260 then return mytable end
    end
    return mytable
end

return p