Module:User Files

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage

[edit]

{{#invoke:User Files|function_name}}


Code

require('strict')

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local userbox = require('Module:Userbox').userbox

local p = {}

local function color_scheme(colors, part)
	local schemes = {
		['golden'] = {
			['id-c'] = '#f0e68c',
			['info-c'] = '#fafad2',
			['border-c'] = '#f0e68c'
		},
		['light-blue'] = {
			['id-c'] = '#ffffff',
			['info-c'] = '#ffffff',
			['border-c'] = '#0581b5'
		},
		['light-grey'] = {
			['id-c'] = '#f6f6f6',
			['info-c'] = '#f6f6f6',
			['border-c'] = '#bbbbbb'
		},
		['standard'] = {
			['id-c'] = '#DDD',
			['info-c'] = '#EEE',
			['border-c'] = '#999'
		}
	}
	local scheme = schemes[colors] or schemes['light-grey']
	return scheme[part] or scheme['id-c']
end

local function bdi_open(lang)
	if lang then
		local dir = require('Module:Dir').select(lang, 'rtl', 'ltr')
		return '<bdi lang="' .. lang .. '" dir="' .. dir .. '">'
	else
		return '<bdi>'
	end
end

local function user_files_text(args)
	-- language info
	local lang = args.lang
	if not lang then
		return nil
	end
	local lang_obj = mw.language.new(lang)
	
	-- user info
	local username = args.username
	local gender = args.gender or username or mw.title.getCurrentTitle().rootText
	
	local username_defaults = {
		['de'] = {'Dieser Benutzer', 'Diese Benutzerin'},
		['en'] = {'This user'},
		['eo'] = {'Ĉi tiu uzulo'},
		['es'] = {'Este usuario', 'Esta usuaria', 'Este usuarie'},
		['et'] = {'See kasutaja'},
		['fi'] = {'Tämä käyttäjä'},
		['fr'] = {'Cet utilisateur', 'Cette utilisatrice'},
		['gl'] = {'Este usuario', 'Esta usuaria'},
		['hu'] = {'Ez a szerkesztő'},
		['it'] = {'Questo utente', 'Questa utente', "Quest'utente"},
		['ml'] = {'ഈ ഉപയോക്താവ്'},
		['no'] = {'Denne brukeren'},
		['pl'] = {'Ten użytkownik', 'Ta użytkowniczka'},
		['ro'] = {'Acest utilizator', 'Această utilizatoare'},
		['ru'] = {'Этот участник', 'Эта участница'},
		['sv'] = {'Denna användare'},
		['uk'] = {'Цей користувач', 'Ця користувачка'}
	}
	if not username and not username_defaults[lang] then
		return nil
	end
	local username_text = username or lang_obj:gender(gender, username_defaults[lang])
	
	-- file info
	
	-- category info
	local category = args.category or args.cat
	local number
	if category then
		number = mw.site.stats.pagesInCategory(category, 'files')
	else
		number = tonumber(args.number) or tonumber(args[1]) or 0
	end
	local subtract = tonumber(args.subtract) or tonumber(args.sub) or 0
	number = number - subtract
	local formatted_number = require('Module:Formatnum').formatNum(number, lang, '', false)
	local cat_text
	if category and number > 0 then
		cat_text = '[[Category:' .. category .. '|' .. formatted_number .. ']]'
	elseif number > 0 then
		cat_text = formatted_number
	else
		cat_text = ''
	end
	
	local link = args.link
	
	-- file forms: form1, form2, etc.
	local default_file_forms = {
		['de'] = {'Datei', 'Dateien'},
		['en'] = {'file', 'files'},
		['eo'] = {'dosieron', 'dosierojn'},
		['es'] = {'archivo', 'archivos'},
		['fi'] = {'tiedoston', 'tiedostoa'},
		['fr'] = {'fichier', 'fichiers'},
		['hu'] = {'fájlt', 'fájlt'},
		['it'] = {'file', 'file'},
		['ml'] = {'പ്രമാണം', 'പ്രമാണങ്ങൾ'},
		['no'] = {'fil', 'filler'},
		['pl'] = {'plik', 'plików'},
		['ro'] = {'fișier', 'fișiere'},
		['ru'] = {'файл', 'файлов'},
		['uk'] = {'файл', 'файлів'}
	}
	
	local file_forms = args.file_forms or {}
	if #file_forms == 0 then
		file_forms = default_file_forms[lang]
	end
	if not file_forms[lang] then
		return nil
	end
	
	local link_text = lang_obj:plural(number, file_forms)
	if link then
		link_text = '[[' .. link .. '|' .. link_text .. ']]'
	end
	
	-- is the word order different in some languages?
	local file_text = "'''" .. mw.text.trim(cat_text .. ' ' .. link_text) .. "'''"
	
	local text_args = {
		['de'] = username_text .. ' hat ' .. file_text .. ' auf [[Main Page|Wikimedia Commons]] hochgeladen.',
		['en'] = username_text .. ' has uploaded ' .. file_text .. ' to [[Main Page|Wikimedia Commons]].'
	}
	return bdi_open(lang) .. text_args[lang] .. '</bdi>'
end

function p._user_files(args)
	-- language
	local lang = args.lang
	if not lang then
		return nil
	end
	
	-- icon
	local icon = args.icon or 'Gnome-emblem-documents.svg'
	local id
	local link
	if icon and icon ~= '' then
		id = '[[File:' .. icon .. '|40x40px|link=' .. (link or '') .. ']]'
	else
		id = '&nbsp;'
	end
	
	-- colors
	local colors = args.colors
	local id_c = color_scheme(colors, 'id-c')
	local info_c = color_scheme(colors, 'info-c')
	local border_c = color_scheme(colors, 'border-c')
	
	-- text
	local file_forms = {}
	for k, v in pairs(args) do
		if string.len(k) > 4 and string.sub(k, 1, 4) == 'form' and tonumber(string.sub(k, 5)) then
			file_forms[#file_forms + 1] = v
		end
	end
	args.file_forms = file_forms
	
	local info = user_files_text(args)
	
	return userbox({
		['id'] = id,
		['id-c'] = id_c,
		['info-c'] = info_c,
		['border-c'] = border_c,
		['info'] = info
	})
end

function p.user_files(frame)
	return p._user_files(getArgs(frame))
end

return p