Module:MyUploads

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code

local p = {};


function p.ImgNameFromSrc(src)
	
end

function p.listUploads(user, limit, offset)
	local m_parser = require('Module:HTMLParser')
	local m_filepath = require('Module:FilePath')
	
	
	limit = limit or 50
	user = user or ''
	offset = offset or ''
	local frame = mw.getCurrentFrame()
	local wikitext = '{{Special:ListFiles/' .. user .. '|limit=' .. limit .. '|offset=' .. offset .. '|ilshowall=1}}'
	local html = mw.text.unstrip(frame:preprocess(wikitext))
	
	-- Sanitize HTML as the parser is not as good as it should be
	html = html:gsub('<div id="mwe_player[^\n]+</div>', 
		function(player)
			local ret = ''
			for imgSrc in player:gmatch('<img [^>\n]*src="([^>\n]+)"') do
				ret = '<img src="' .. imgSrc .. '" />'
			end
			return ''
		end
	)
	local root = m_parser.parse(html)
	local rows = root:select('table.listfiles tr')
	local uplds = {}
	
	for r in pairs(rows) do
		local ul = {}
		
		for x in pairs(r:select('img')) do
			ul['file'] = m_filepath.titleFromSrc( x.attributes['src'] );
		end
		
		if ul.file then
			for x in pairs(r:select('.TablePager_col_img_timestamp')) do
				ul['time'] = x:getcontent()
			end
			for x in pairs(r:select('.TablePager_col_img_size')) do
				ul['size'] = x:getcontent()
			end
			table.insert(uplds, ul)
		end
	end
	
	return uplds
end
--[[
	@param list table of Uploads obtained through p.listUploads
	@param outer string outer format, e.g. opening and closing gallery tags, see example
	@param each string inner formatting string that will be applied for each image
	@param sepa string separator used between each image
	@example
		p.formatUploadList(p.listUploads('Rillke', 5),
						'<gallery mode="packed" widths="400">%content%\n</gallery>',
						'\n%file%|File was uploaded on %time% and has a size of %size%'
					)
--]]
function p.formatUploadList(list, outer, each, sepa)
	local listItems = {}
	sepa = sepa or ''
	
	for i, r in ipairs(list) do
		local listItem = each
		for k, v in pairs(r) do
			listItem = listItem:gsub('%%' .. k .. '%%', v)
		end
		table.insert(listItems, listItem)
	end
	
	return outer:gsub('%%content%%', table.concat(listItems, sepa))
end

-- This function may be invoked by a template but please don't call it directly
function p.fancyUploads(frame)
	local args = frame:getParent().args or {}
	local list = p.listUploads(args.user or args.name or 'Rillke', args.count or args.limit or 5, args.offset)
	local unstripW = args.unstripPatternWhole
	local unstripE = args.unstripPatternEach
	local ph = args.patternWhole
	local pe = args.patternEach
	if ph and unstripW then ph = mw.text.unstrip(ph) end
	if pe and unstripE then pe = mw.text.unstrip(pe) end
	if ph and args.isGallery then ph = '<gallery ' .. ph .. '>\n%content%\n</gallery>' end
	local wikimarkup = p.formatUploadList(
						list,
						ph or '<gallery mode="packed" widths="400">%content%\n</gallery>',
						pe or '\n%file%|File was uploaded on %time% and has a size of %size%'
					)

	return frame:preprocess(wikimarkup)
end

return p;