Module:Submission

From Wikimania

Documentation for this module may be created at Module:Submission/doc

me = {}

local function checkLanguage(subpage)
    --[[Check first if there's an any invalid character that would cause the
        mw.language.isKnownLanguageTag function() to throw an exception:
        - all ASCII controls in [\000-\031\127],
        - double quote ("), sharp sign (#), ampersand (&), apostrophe ('),
        - slash (/), colon (:), semicolon (;), lower than (<), greater than (>),
        - brackets and braces ([, ], {, }), pipe (|), backslash (\\)
        All other characters are accepted, including space and all non-ASCII
        characters (including \192, which is invalid in UTF-8).
    --]]
    if mw.language.isValidCode(subpage) and mw.language.isKnownLanguageTag(subpage)
    --[[However "SupportedLanguages" are too restrictive, as they discard many
        valid BCP47 script variants (only because MediaWiki still does not
        define automatic transliterators for them, e.g. "en-dsrt" or
        "fr-brai" for French transliteration in Braille), and country variants,
        (useful in localized data, even if they are no longer used for
        translations, such as zh-cn, also useful for legacy codes).
        We want to avoid matching subpagenames containing any uppercase letter,
        (even if they are considered valid in BCP 47, in which they are
        case-insensitive; they are not "SupportedLanguages" for MediaWiki, so
        they are not "KnownLanguageTags" for MediaWiki).
        To be more restrictive, we exclude any character
        * that is not ASCII and not a lowercase letter, minus-hyphen, or digit,
          or does not start by a letter or does not finish by a letter or digit;
        * or that has more than 8 characters between hyphens;
        * or that has two hyphens;
        * or with specific uses in template subpages and unusable as languages.
    --]]
    or  string.find(subpage, "^[%l][%-%d%l]*[%d%l]$") ~= nil
    and string.find(subpage, "[%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l]") == nil
    and string.find(subpage, "%-%-") == nil
    and subpage ~= "doc"
    and subpage ~= "layout"
    and subpage ~= "sandbox"
    and subpage ~= "testcases"
    then
        return subpage
    end
    -- Otherwise there's currently no known language subpage
    return nil
end

--[[Get the last subpage of the current page if it is a translation.
    ]]
local function getLanguageSubpage()
	--[[This code does not work in all namespaces where the Translate tool works.
	--  It works in the main namespace on Meta because it allows subpages there
	--  It would not work in the main namespace of English Wikipedia (but the
	--  articles are monolignual on that wiki).
	--  On Meta-Wiki the main space uses subpages and its pages are translated.
	--  The Translate tool allows translatng pages in all namespaces, even if
	--  the namespace officially does not have subpages.
	--  On Meta-Wiki the Category namespace still does not have subpages enabled,
	--  even if they would be very useful for categorizing templates, that DO have
	--  subpages (for documentatio and tstboxes pages). This is a misconfiguration
	--  bug of Meta-Wiki. The work-around is to split the full title and then
	--  get the last titlepart.
	local subpage = mw.title.getCurrentTitle().subpageText
	--]]
    local titleparts = mw.text.split(mw.title.getCurrentTitle().fullText, '/')
    local subpage = titleparts[#titleparts]
    return checkLanguage(subpage, '')
end

local function languages( args, prefix )
	local langs = {}
	for k,v in pairs(args) do
		match = mw.ustring.match(k, '^' .. prefix .. '_(%a%a)$')
		if match then
			langs[match] = true
		end
	end
	return langs
end

local function author(args, num, fancy)
	local out
	local name = args['author'..num]
	local user = args['user'..num]
	local email = args['email'..num]
	
	if not fancy then
		return name
	end
	if user and user~='' then
		out = '[[' .. user .. '|' .. name .. ']]'
	else
		out = name
	end
	if email and email~='' then
		out = out .. ' <sup>([mailto:' .. email .. ' email])</sup>'
	end
	return out
end

local function languagetable( args, list, prefix, heading )
	local out
	local first = true
	local num = 0
	
	out = ''
	for k,_ in pairs(list) do
		num = num + 1
	end
	for k,_ in pairs(list) do
		if first then
			out = out .. '! rowspan="' .. num .. '"|' .. heading .. '\n'
			first = false
		else
			out = out .. "|-\n"
		end
		out = out .. '| (' .. mw.language.fetchLanguageName(k) .. ')\n| ' .. args[prefix..k]  .. '\n'
	end
	return out
end

local types = {
	p = 'presentation', l = 'lightning', w = 'workshop', k = 'keynote', b = 'bof',
};

local statuses = {
	n = 'new', r = 'review', x = 'rejected', a = 'accepted', s = 'standby', i = 'invalid', u = 'unconditionally',
};


me.display = function ( frame )
	
	fail = {}

	sub = frame:getParent()
	display = frame.args[1] or sub.args['display'] or ''
	if (display == '') then display = 'full' end
	lang = sub.args['language'] or ''
	titles = languages(sub.args, 'title')
	abstracts = languages(sub.args, 'abstract')
	
	if lang == '' then
		fail['lang'] = 'no-language'
		lang = 'en'
	else
		if not titles[lang] then
			fail['titles'] = 'title-not-language'
		end
		for k,_ in pairs(titles) do
			if not abstracts[k] then
				fail['abstracts'] = 'title-no-abstract'
			end
		end
	end

	status = sub.args['status'] and statuses[sub.args['status']]
	if not status then
		fail['status'] = 'status-invalid'
	end
	type = sub.args['type'] and types[sub.args['type']]
	if not type then
		fail['type'] = 'type-invalid'
		type = 'invalid'
	end
	
	if not sub.args['author1'] then
		fail['authors'] = 'no-author'
		authors = ''
		alist = ''
	else
		if sub.args['author2'] and sub.args['author2']~='' then
			authors = author(sub.args, 1, true) .. ', ' .. author(sub.args, 2, true)
			alist = author(sub.args, 1) .. ', ' .. author(sub.args, 2)
			if (sub.args['more_authors'] or 'no') == 'yes' then
				authors = authors .. ', et. al.'
				alist = alist .. ', et. al.'
			end
		else
			authors = author(sub.args, 1, true)
			alist = author(sub.args, 1)
		end
	end

	local numfails
	numfails = 0
	
	for k,v in pairs(fail) do
		numfails = numfails+1
	end
	if numfails > 0 then
		status = 'invalid'
	end
	ul = getLanguageSubpage() or lang
	if not titles[ul] then ul = lang end

	if     display=='titles' then
		return languagetable(sub.args, titles, 'title_', frame:expandTemplate{ title = 'TNT', args = { 'heading-title' } } .. ':' )
	elseif display=='abstracts' then
		return languagetable(sub.args, abstracts, 'abstract_', frame:expandTemplate{ title = 'TNT', args = { 'heading-abstract' } } .. ':' )
	elseif display=='authors' then
		return authors
	elseif display=='alist' then
		return alist
	elseif display=='status' then
		return status
	elseif display=='type' then
		return type
	elseif display=='errors' then
		return numfails
	elseif display=='title' then
		return sub.args['title_'..ul]
	elseif display=='errorlist' then
		local out
		out = ''
		for k,v in pairs(fail) do
			out = out .. frame:expandTemplate{ title = 'TNT', args = { 'submission-error-'..v } }
		end
		return out
	else
		return frame:expandTemplate{ title = 'Submission display ' .. display, args = sub.args }
	end
end

return me