Модуль:Не перакладзена

Каб пазбегнуць паломак старонак, дзе ўжыты гэты модуль, пажадана эксперыментаваць у пясочніцы для модуляў.


local p = {}

local categories = {
	['error'] = 'Вікіпедыя:Артыкулы з некарэктна запоўненым шаблонам Не перакладзена',
	['outdated'] = 'Вікіпедыя:Артыкулы з неактуальным шаблонам Не перакладзена',
	['redirect'] = 'Вікіпедыя:Запыты на замену перанакіраванняў перакладамі',
	['unknown'] = 'Вікіпедыя:Артыкулы з невядомымі параметрамі шаблонаў серыі Не перакладзена',
	['semiold'] = 'Вікіпедыя:Артыкулы з паўстарым сінтаксісам у шаблонах серыі Не перакладзена',
	['probably_wrong'] = 'Вікіпедыя:Артыкулы з меркавана памылковымі параметрамі ў шаблонах серыі Не перакладзена',
	['lang-not-exists'] = 'Вікіпедыя:Артыкулы з шаблонам Не перакладзена 2, што змяшчаюць неіснуючы шаблон lang-XX',
}

local function is_empty(param)
	return param == nil or param == ''
end

local function is_wikidata(lang)
	return lang == 'd'
end

local function getTitle(page, iw_title, is_wikidata_link)
	local success, result = pcall(function()
		if is_wikidata_link then
			local entity = mw.wikibase.getSitelink(iw_title)
			if entity ~= nil then
				return {
					title = entity,
					exists = true,
					-- дапушчэнне, што ў Вікіданых не перанакіраванне
					isRedirect = false,
				}
			end
			
			return nil
		else
			local title = mw.title.new(page)
			return {
				title = page,
				exists = title.exists,
				isRedirect = title.isRedirect,
			}
		end
	end)
	
	if success then
		return result
	end
	
	return nil
end

local function wikilink(title, text, tooltip, lang, style, is_redirect, class)
	if is_empty(text) then
		text = title
	end
	if not is_empty(lang) and lang ~= 'be' then
		title = string.format(':%s:%s', lang, title)
	end
	if is_wikidata(lang) then
		title = title .. '#sitelinks-wikipedia'
	end
	if not is_empty(tooltip) then
		text = string.format('<span title="%s">%s</span>', tooltip, text)
	end
	if not is_empty(style) then
		text = string.format('<span style="%s">%s</span>', style, text)
	end
	if not is_empty(class) then
		text = string.format('<span class="%s">%s</span>', class, text)
	end
	if is_redirect then
		return string.format(
			'<span class="plainlinks">[%s %s]</span>',
			tostring(mw.uri.fullUrl(title,'redirect=no')),
			text
		)
	elseif text ~= title then
		return string.format('[[%s|%s]]', title, text)
	else
		return string.format('[[%s]]', title)
	end
end

function addMetadata(text, lang, article)
	return string.format(
		'<span data-interwiki-lang="%s" data-interwiki-article="%s">%s</span>',
		lang, article, text
		)
end

local function throwError(text, category_code, allow_cat)
	local error = require('Module:Error').error
	if category_code and allow_cat then
		return error{text} .. '[[Category:' .. categories[category_code] .. ']]'
	else
		return error{text}
	end
end

function p.main(frame)
	local yesno = require('Module:Yesno')
	local getArgs = require('Module:Arguments').getArgs
	local languages = mw.loadData('Module:Languages/data')
	local prepositional = require('Module:Languages')._transform_lang

	local args = getArgs(frame)

	local categories_list = {}
	local nocat = yesno(args['nocat'])
	local allow_cat = (mw.title.getCurrentTitle().namespace == 0
		or mw.title.getCurrentTitle().namespace == 10) and not nocat
	
	-- вызначэнне які з шаблонаў "не перакладзена N"
	local mode = tonumber(args['mode'])
	local template_name = 'Не перакладзена'
	if mode ~= 1 then
		template_name = template_name .. ' ' .. tostring(mode)
	end
	
	-- праверка на невядомыя параметры
	local redundant_params = {}
	local good_params = {'mode', 1, 2, 3, 4, 'l', 'leave', 'r', 'q', 'nocat'}
	if mode == 2 then
		table.insert(good_params, 5)
		table.insert(good_params, 'text')
		table.insert(good_params, 'тэкст')
	end
	for k, _ in pairs(args) do
		local is_good_param = false
		for _, v in pairs(good_params) do
		  	if k == v then
		  		is_good_param = true
		  	end
		end
		if not is_good_param then
			table.insert(redundant_params, k)
		end
	end
	if #redundant_params > 0 then
		table.insert(categories_list, {'unknown', table.concat(redundant_params)})
	end

	-- атрыманне параметраў (акрамя nocat)
	local title = args[1]
	local text = args[2]
	local lang = args[3]
	local iw_title = args[4]
	local iw_text = args[5]
	local quotes = yesno(args['q'])
	local addition = args['text'] or args['тэкст']
	local leave_always = yesno(args['l']) or yesno(args['leave'])
	local leave_if_redirect = yesno(args['r'])
	
	-- праверка правільнасці сырых параметраў
	if not mw.isSubsting() then
		if is_empty(title) then
			return throwError('не пазначана назва артыкула', 'error', allow_cat)
		end
		if title:match('^:[a-z-]+:') then
			return throwError('шаблон не падтрымлівае такі сінтаксіс', 'semiold', allow_cat)
		end
		if is_empty(lang) and not is_empty(iw_title) and iw_title:match('^[a-z][a-z]$') then
			table.insert(categories_list, {'probably_wrong', nil})
		end
	end
	
	-- прывядзенне параметраў да патрэбнага выгляду
	if is_empty(text) then
		text = title
	end
	if is_empty(lang) then
		lang = 'en'
	else
		lang = mw.ustring.lower(lang)
	end
	if is_empty(iw_title) then
		iw_title = title
	end
	if is_empty(iw_text) then
		iw_text = iw_title
	end
	
	-- калі ўжываецца з падстаноўкай, выкінуць амаль без апрацоўкі
	if mw.isSubsting() then
		local be_link = wikilink(title, text)
		if mode == 2 then
			local iw_link = '{{lang-' .. lang .. '|' .. iw_text .. '}}'
			if is_empty(addition) then
				return string.format('%s (%s)', be_link, iw_link)
			else
				return string.format('%s (%s; %s)', be_link, iw_link,  addition)
			end
		else
			return be_link
		end
	end
	
	-- атрыманне старонкі, каб ведаць, ці існуе яна і ці з’яўляецца яна перанакіраваннем
	local isWikidataLink = is_wikidata(lang)
	local considerAsExists
	local isRedirect
	local considerAsRedirect
	if leave_always then -- калі зададзена |l=1, то нічога не правяраць, бо дорага
		considerAsExists = false
		isRedirect = false
		considerAsRedirect = false
	else
		-- дарагая функцыя, пасля 500-га выкліку падае
		local titleObject = getTitle(title, iw_title, isWikidataLink)
		if titleObject then
			title = titleObject.title
			considerAsExists = titleObject.exists
			isRedirect = titleObject.isRedirect
			considerAsRedirect = isRedirect and not leave_if_redirect
		else -- пасля 500-га выкліку або калі ў назве ёсць некарэктныя сімвалы
			considerAsExists = false
			isRedirect = false
			considerAsRedirect = false
		end
	end
	
	-- праверка правільнасці апрацаваных параметраў
	if lang ~= 'd' and languages[lang] == nil then
		return throwError('некарэктны ISO-код «' .. lang .. '»', 'error', allow_cat)
	end
	if mode == 2 and isWikidataLink then
		return throwError('шаблон Не перакладзена 2 не падтрымлівае Вікіданыя замест мовы')
	end
	if mode == 2 and not mw.title.new('Template:Lang-' .. lang).exists then -- дарагая функцыя
		return throwError('не знойдзены шаблон ' .. frame:expandTemplate{ title = 'tl', args = { 'lang-' .. lang } },
			'lang-not-exists', allow_cat)
	end
	
	if mode == 5 and considerAsRedirect then
		table.insert(categories_list, {'redirect', nil})
	elseif considerAsExists then
		table.insert(categories_list, {'outdated', nil})
	end
	
	-- усплываючая падказка да спасылкі на іншамоўны артыкул
	local iw_tooltip
	local ucfirst_title = mw.getContentLanguage():ucfirst(title)
	if isWikidataLink then
		iw_tooltip = string.format('Элемент артыкула «%s» на Вікіданых', ucfirst_title)
	else
		iw_tooltip = string.format('%s — версія артыкула «%s» на %s', iw_title, ucfirst_title, prepositional(lang))
	end

	-- фарміраванне першай часткі - асноўнай спасылкі
	local main_text
	if considerAsExists or mode == 1 or mode == 2 or mode == 5 then
		local class = (considerAsExists and (isRedirect and ' iw--redirect' or ' iw--exists') or ' iw-nonexists')
		main_text = wikilink(title, text, nil, nil, nil, isRedirect, class)
		if not considerAsExists then
			main_text = addMetadata(main_text, lang, iw_title)
		end
	else
		main_text = wikilink(iw_title, text, iw_tooltip, lang)
	end
	if mode == 4 and quotes then -- TODO: усе шаблоны або адключыць
		main_text = '«' .. main_text .. '»'
	end
	
	-- фарміраванне другой часткі - моўнай меткі
	local post_text = ''
	if not (considerAsExists and not (isRedirect and leave_if_redirect)) or mode == 2 or (mode == 5 and considerAsRedirect) then
		-- для шаблона "не перакладзена"
		if mode == 1 then
			local lang_text
			if isWikidataLink then
				lang_text = 'ВД'
			else
				lang_text = languages[lang][1]
			end
			
			local iw_link = wikilink(iw_title, lang_text, iw_tooltip, lang)
			post_text = string.format(
				'<span class="noprint" style="white-space: nowrap; font-size: 85%%;"> (%s)</span>',
				iw_link
			)
		
		-- для шаблона "не перакладзена 2"
		elseif mode == 2 then
			local iw_link = ''
			if considerAsExists then
				iw_link = frame:expandTemplate{ title = 'lang-' .. lang, args = { iw_text } }
			else
				iw_link = frame:expandTemplate{ title = 'lang-' .. lang, args = { wikilink(iw_title, iw_text, iw_tooltip, lang) } }
			end
			
			if is_empty(addition) then
				post_text = string.format(' (%s)', iw_link)
			else
				post_text = string.format(' (%s; %s)', iw_link,  addition)
			end
		
		-- для шаблона "не перакладзена 3"
		-- TODO: перанесці ўсё ў TemplateStyles
		elseif mode == 3 then
			local ref = string.format(
				-- непарыўны прабел на гэтай пазіцыі прадухіляе перанос перад элементам з display:inline-block у Google Chrome
				'<span style="font-size:117.6%%; margin-left:-0.23em; position:relative; top:0.28em;">%s</span>',
				frame:expandTemplate{
					title = 'ref-' .. (isWikidataLink and 'info' or lang),
					args = {
						isWikidataLink and 'ВД' or ''
					}
				}
			)
			local be_link = wikilink(title, 'бел.')
			if not considerAsExists then
				be_link = addMetadata(be_link, lang, iw_title)
			end
			be_link = string.format(
				-- position:absolute; дазваляе дакладней пазіцыянаваць пазнаку; інакш яна «скача», па меншай меры ў Google Chrome
				'<span class="link-be metadata" style="margin-left:-0.43em; position:absolute; left:0; z-index:1; margin-top:-0.35em;"><span style="visibility:hidden; margin-right:0.099em;">&nbsp;(</span>%s</span>',
				be_link
			)
			post_text = string.format(
				-- памер шрыфту і ўласцівасць display:inline-block пазначаецца тут з-за праблем з памерамі шрыфту ў Google Chrome на Android
				'<span style="white-space:nowrap;">&nbsp;<span style="display:inline-block; font-size:80%%; position:relative;">%s%s</span></span>',
				ref, be_link
			)
		
		-- для шаблона "не перакладзена 4"
		elseif mode == 4 then
			local styles = ''
			if lang == 'fr' then
				styles = 'margin-right:0.3em;'
			elseif lang == 'fi' then
				styles = 'margin-right:0.35em;'
			elseif lang == 'it' then
				styles = 'margin-right:0.5em;'
			end
			
			local be_link = wikilink(title, 'be', 'Артыкул «' .. title .. '» у беларускім раздзеле адсутнічае')
			if not considerAsExists then
				be_link = addMetadata(be_link, lang, iw_title)
			end
			be_link = string.format(
				'<sup style="margin-left:2px; position:relative; top:-1px;">%s</sup>',
				be_link
			)
			local ref = string.format(
				'<sub style="margin-left:-1.06em; %s" title="Па спасылцы даступны артыкул на %s">%s</sub>',
				styles, prepositional(lang), lang
			)
			post_text = string.format('%s%s', be_link, ref)
		
		-- для шаблона "не перакладзена 5"
		else
			post_text = string.format(
				'<sup>%s</sup>',
				wikilink(iw_title, '[' .. lang .. ']', iw_tooltip, lang)
			)
		end
	end
	
	if mode == 3 or mode == 4 or mode == 5 then -- TODO: або для ўсіх?
		post_text = string.format('<span class="noprint" style="font-style:normal; font-weight:normal;">%s</span>', post_text)
	end
	
	-- фарміраванне трэцяй часткі - паведамлення пра існаванне старонкі
	local exist_message = ''
	if considerAsExists and not (isRedirect and leave_if_redirect) then
		local exist_message_link
		if mode == 5 then
			if considerAsRedirect then
				exist_message_link = 'Шаблон:' .. template_name .. '#Калі існуе перанакіраванне'
			else
				exist_message_link = 'Шаблон:' .. template_name .. '#Калі існуе артыкул'
			end
		else
			exist_message_link = 'Шаблон:' .. template_name .. '#Дзеянні пасля з’яўлення старонкі'
		end
		
		if mode == 5 and considerAsRedirect then
			exist_message = string.format(
				'<sup class="noprint" style="margin:0 0 0 1px;">%s</sup>',
				wikilink(exist_message_link, '*', 'Замяніце назву перанакіравання на назву артыкула або прыбярыце шаблон «' .. template_name .. '»', nil, 'color:red;')
			)
		else
			exist_message = string.format(
				'<sup class="noprint">%s</sup>',
				wikilink(exist_message_link, '?!', 'Прыбярыце шаблон «' .. template_name .. '» з артыкула і замяніце яго простай вікі-спасылкай', nil, 'color:red;')
			)
		end
		
		exist_message = string.format('<span style="font-style:normal; font-weight:bold;">%s</span>', exist_message)
	end
	
	local categories_text = ''
	if allow_cat then
		for _, category_data in pairs(categories_list) do
			local category_name = categories[category_data[1]]
			if category_data[2] ~= nil then
				category_name = category_name .. '|' .. category_data[2]
			end
			categories_text = categories_text .. '[[Category:' .. category_name .. ']]'
		end
	end
	
	local result
	if mode == 2 then
		result = main_text .. exist_message .. post_text .. categories_text
	else
		result = main_text .. post_text .. exist_message .. categories_text
	end
	
	return result
end

return p