Module:Assessments

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Experimental Lua rewrite of {{Assessments}}, {{Assessments/images/left}} and {{Assessments/images/right}}.

Features:

  • single list of Wikipedias with supported assessments;
  • user-friendly error messages for obsolete parameters.

Todo:

  • add VI-specific arguments;
  • improve internationalization system.

Code

-- default icon size
local defaultSize = '50px'

-- DB names accepted as template arguments
local sites = {
	'arwiki',
	'azwiki',
	'cawiki',
	'cswiki',
	'dewiki',
	'enwiki',
	'eswiki',
	'fawiki',
	'frwiki',
	'gawiki',
	'hewiki',
	'hrwiki',
	'huwiki',
	'idwiki',
	'jawiki',
	'kawiki',
	'kkwiki',
	'ltwiki',
	'mlwiki',
	'mswiki',
	'orwiki',
	'plwiki',
	'ptwiki',
	'rowiki',
	'slwiki',
	'svwiki',
	'tawiki',
	'thwiki',
	'trwiki',
	'viwiki',
	'zhwiki',
	'zh-yuewiki'
}

local p = {}
local title = mw.title.getCurrentTitle()
local contentLang = mw.language.getContentLanguage()

-- replacement for Template:Assessments/images/left
function imagesLeft(args,rowspan,size)
	if size == nil then
		size = defaultSize
	end

	local res = '|style="width:'..size..';text-align:center;" rowspan="'..(rowspan or '2')..'"|'

	-- Picture Of The Year barnstars and ribbons
	local POTYicons = {
		'PODY barnstar.svg',
		'PODY 2nd barnstar.svg',
		'PODY 3rd barnstar.svg',
		'PODY ribbon.svg',
		'PODY candidate.svg'
	}
	POTYicons.f = POTYicons[4]
	POTYicons.c = POTYicons[5]
	if POTYicons[args.POTY] ~= nil then
		res = res..string.format('[[File:%s|link=Commons:Picture of the Year/%s|%s|center|POTY]]', POTYicons[args.POTY], args.POTYyear, size)
	end

	-- Picture Of The Day logo
	if args.POTD == '1' then
		res = res..string.format('[[File:Potd-logo.svg|link=Template:Potd/%s-%s#%s|%s|center|POTD]]', args.POTDyear, args.POTDmonth, args.POTDday, size)
	end

	-- Featured Picture icons (current / former)
	if args.featured == '1' or args.featured == '3' then
		res = res..'[[File:Cscr-featured.svg|'..size..'|link=]]'
	elseif args.featured == '2' or args.featured == '4' then
		res = res..'[[File:Cscr-former.svg|'..size..'|link=]]'
	end

	-- Quality Image icons (current / former)
	if args.quality == '1' then
		res = res..'[[File:Quality images logo.svg|link=|'..size..'|center|Quality picture]]'
	elseif args.quality == '2' then
		res = res..'Former quality image logo goes here'
	end

	-- Valued Image icons (current / former / candidate)
	if args.valued == '1' then
		res = res..'[[File:Valued image seal.svg|link=|'..size..'|center|Valued image seal]]'
	elseif args.valued == '2' then
		res = res..'Former Valued image logo goes here'
	elseif args.valued == '3' then
		res = res..'Candidate Valued image logo goes here'
	end

	return res
end

-- replacement for Template:Assessments/images/right
function imagesRight(commons,wikimedia,wikipedia,size,rowspan)
	if size == nil then
		size = defaultSize
	end
	local res = '\n|style="width:'..size..';text-align:center;" rowspan="'..(rowspan or '2')..'"|'
	if commons and commons ~= '' then
		res = res..'[[File:Commons-logo.svg|'..size..'|link=|Wikimedia Commons]]'
	end
	if wikimedia and wikimedia ~= '' then
		res = res..'[[File:Wikimedia-logo.svg|'..size..'|link=|Wikimedia]]'
	end
	if wikipedia and wikipedia ~= '' and wikipedia ~= '0' then
		res = res..'[[File:Wikipedia-logo-v2.svg|'..size..'|link=|Wikipedia]]'
	end
	return res
end

function assessmentsCommons(div, aarg)
	if aarg.lang == nil then
		aarg.lang = lang
	end
	div:wikitext(mw.getCurrentFrame():expandTemplate{ title = 'Assessments/commons', args = aarg })
end

-- replacement for Template:Assessments
function p.box(frame)
	local args = frame:getParent().args
	-- remove empty arguments
	for key, value in pairs(args) do
		if mw.text.trim(value) == '' then
			args[key] = nil
		end
	end

	local errors = {}

	-- obsolete arguments
	local obsolete = {'comy', 'com', 'com2', 'com3', 'com4', 'com5', 'subpage'}
	local obsoleteUsed = {}
	for i, arg in pairs(obsolete) do
		if args[arg] ~= nil then
			table.insert(obsoleteUsed, '\''..arg..'\'')
		end
	end
	if #obsoleteUsed > 0 then
		table.insert(errors, 'The '..mw.text.listToText(obsoleteUsed)..' parameter'..contentLang:plural(#obsoleteUsed, ' is', 's are')..' obsolete! Please refer to [[Template:Assessments/doc]]!\n[[Category:Assessment tagged pages that use obsolete parameters]]')
	end

	lang = args.lang or frame:preprocess('{{int:Lang}}')
	local dir = frame:expandTemplate{ title = 'Dir', args = { lang } }

	-- initialize
	local assessments = {
		featured = false,
		quality = false,
		valued = false,
	}
	for key, value in pairs(assessments) do
		if args[key] ~= nil then
			assessments[key] = args[key]
		end
	end

	assessments.sites = {}
	local featSites = 0
	for i, dbname in pairs(sites) do
		if args[dbname] ~= nil then
			assessments.sites[dbname] = args[dbname]
			featSites = featSites + 1
		end
	end

	--[[ experimental: allow unnamed parameters
	disabled for backwards-compatibility
	for key, value in pairs(args) do
		if type(key) == 'number' and assessments[value] == false then
			assessments[value] = '1'
		end
	end]]

	-- Pages that do not link to nomination
	if assessments.featured and title:inNamespace('File') then
		local ft = nil
		if assessments.featured == '1' then
			ft = 'FPC/'
		elseif assessments.featured == '2' then
			ft = 'FPCR/'
		elseif assessments.featured == '3' then
			ft = 'FSC/'
		end
		if mw.title.new(frame:expandTemplate{ title = ft, args = { (args['com-nom'] or (args.renamed or (title.text or ''))) } }).exists() == false then
			table.insert(errors, 'This file does not link to the relevant nomination page! Please refer to [[Template:Assessments/doc]]! [[Category:Assessment tagged pages that do not link to nomination]]')
		end
	end

	-- HTML container
	local div = mw.html.create('div')
	:attr({
		id = 'assessments',
		dir = dir,
		lang = lang
	})
	:wikitext('\n{|cellspacing="8" cellpadding="0" style="background:transparent;margin:0.5em auto"\n|\n')

	if assessments.valued then
		div:wikitext('<table '..frame:expandTemplate{ title = 'Layout', args = { Layout = 'Valued', margin = '0', lang = lang } }..'><tr><td>')
	end

	if assessments.quality then
		div:wikitext('<table '..frame:expandTemplate{ title = 'Layout', args = { Layout = 'Quality', margin = '0', lang = lang } }..'><tr><td>')
	end

	if assessments.featured or featSites > 0 then
		div:wikitext('<table '..frame:expandTemplate{ title = 'Layout', args = { Layout = 'Featured', margin = '0', lang = lang } }..'><tr><td>')
	elseif (not assessments.valued) and (not assessments.quality) then
		div:wikitext('<table '..frame:expandTemplate{ title = 'Layout', args = { Layout = 'Infobar', lang = lang } }..'><tr><td>')
	end

	div:wikitext('\n{|style="background:transparent;"\n|-\n')

	div:wikitext(imagesLeft({
		POTY = (args.POTY or ''),
		POTYyear = (args.POTYyear or '2006'),
		POTD = (args.POTD or ''), POTDyear = (args.POTDyear or '2004'), POTDmonth = args.POTDmonth, POTDday = args.POTDday,
		featured = (assessments.featured or ''),
		quality = (assessments.quality or ''),
		valued = (assessments.valued or ''),
		wallpaper = (args.wallpaper or '')
	}, 2, '40px')..'\n|')

	if #errors > 0 then
		div
		:tag('strong')
		:addClass('error')
		:wikitext(table.concat(errors, '\n\n'))
	end

	div:wikitext(imagesRight((assessments.featured or assessments.quality or assessments.valued or args.wallpaper or args.POTD or args.POTY), (args['blog-wmf'] or ''), tostring(featSites), '30px', 2))

	-- Non-file namespace uses
	if title:inNamespace('Template') == false and title.inNamespace('File') == false then
		div:wikitext('[[Category:Assessment tagged pages that are not in the file namespace]]')
	end

	div:wikitext('\n|-\n|style="font-size:95%;"|')

	-- Commons assesments

	-- Picture Of The Year
	assessmentsCommons(div, { wiki = 'POTY', num = (args.POTY or ''), POTYyear = (args.POTYyear or '2006') })

	-- Picture Of The Day
	assessmentsCommons(div, { wiki = 'POTD', num = (args.POTD or ''), POTDyear = (args.POTDyear or ''), POTDmonth = (args.POTDmonth or ''), POTDday = (args.POTDday or '') })

	-- Featured picture
	assessmentsCommons(div, { wiki = 'featured', num = (args.featured or ''), nom = (args['com-nom'] or (args.renamed or '')) })

	-- Quality image
	assessmentsCommons(div, { wiki = 'quality', num = (args.quality or '') })

	-- Valued image
	assessmentsCommons(div, { wiki = 'valued', num = (args.valued or '') })

	-- Wallpaper image
	assessmentsCommons(div, { wiki = 'wallpaper', num = (args.wallpaper or '') })

	-- Non-commons assessments

	-- Wikimedia Foundation blog
	if (args['blog-wmf'] or args['blog-wmf-post']) then
		if assessments.featured or assessments.quality or assessments.valued or args.wallpaper or args.POTD or args.POTY then
			div:wikitext('<hr>')
		end
		div:wikitext(frame:expandTemplate{ title = 'Assessments/wmf', args = { lang = lang, type = 'blog', wmf = (args['blog-wmf'] or ''), ['wmf-post'] = (args['blog-wmf-post'] or ''), '', year = args['blog-wmf-year'], month = args['blog-wmf-month'], day = args['blog-wmf-day'] } })
	end

	-- Start of Wikipedia assessments
	if (assessments.featured or assessments.quality or assessments.valued or args.wallpaper or args.POTD or args.POTY or args['blog-wmf']) and featSites > 0 then
		div:wikitext('<hr>')
	end

	-- collapse when more than 2 entries are shown
	local tname = 'Assessments/Blank'
	if featSites > 2 then
		tname = 'collapse'
	end

	local content = ''
	for i, dbname in pairs(sites) do
		content = content..frame:expandTemplate{ title = 'Assessments/wikipedia', args = { lang = lang, wiki = dbname, num = (args[dbname] or '0'), nom = (args[dbname..'-nom'] or args.renamed) } }
	end

	div:wikitext(frame:expandTemplate{
		title = tname,
		args = {
			['header-c'] = '#E5D4A1',
			['border-c'] = '#FAF5E6',
			['background-c'] = '#FAF5E6',
			['font-size'] = '100%',
			align = frame:expandTemplate{ title = 'Dir', args = { lang, 'right', 'left' } },
			title = frame:expandTemplate{ title = 'Assessments/translate/Wikipedia', args = { lang = lang } },
			['1'] = content
		}
	})

	div:wikitext('<hr>')

	-- Footer
	div:wikitext(frame:expandTemplate{ title = 'Assessments/commons', args = { lang = lang, wiki = 'footer', num = (assessments.featured or '0') } })
	div:wikitext('\n----\n')
	div:wikitext(frame:expandTemplate{ title = 'Assessments/translate/lang' })
	div:wikitext('\n|}')

	if assessments.featured or featSites > 0 or (assessments.valued == false and assessments.quality == false) then
		div:wikitext('</td></tr></table>')
	end
	if assessments.quality then
		div:wikitext('</td></tr></table>')
	end
	if assessments.valued then
		div:wikitext('</td></tr></table>')
	end

	div:wikitext('\n|}')

	return tostring(div)
end

return p