Modul:UserLinks/shared

Vikipediya, ochiq ensiklopediya

Bu modul uchun Modul:UserLinks/shared/doc nomli hujjat sahifasini yaratishingiz mumkin

local cfg = mw.loadData('Module:UserLinks/config')
local namespaces = mw.site.namespaces


local mCategoryHandler


local colonNamespaces = {
	[6] = true, -- Fayl
	[14] = true, -- Turkum
}

local p = {}

function p.maybeLoadModule(s)
	
	local success, mdl = pcall(require, s)
	if success then
		return mdl
	else
		return false
	end
end

function p.raiseError(message, section, level)
	-- 
	if section then
		message = message .. '|' .. section
	end
	if not level or level == 0 then
		level = 0
	else
		level = level + 1
	end
	error(message, level)
end

local localBlacklist = {
	'/[sS]andbox$', -- 
	'/[tT]est ?cases$', --
}

local function currentTitleMatchesLocalBlacklist()
	-- 
	local title = mw.title.getCurrentTitle().prefixedText
	for i, pattern in ipairs(localBlacklist) do
		if title:find(pattern) then
			return true
		end
	end
	return false
end

function p.makeWikitextError(encodedMessage, demo)
	local errorMessage, section = mw.ustring.match(encodedMessage, '^(.-)|(.*)$')
	errorMessage = errorMessage or encodedMessage
	
	-- 
	local category
	if not demo then
		category = string.format(
			'[[%s:%s]]',
			namespaces[14].name,
			p.message('error-config-category')
		)
		mCategoryHandler = p.maybeLoadModule('Module:Category handler')
		if mCategoryHandler then
			-- 
			category = mCategoryHandler.main{all = category}
		end
		if category and currentTitleMatchesLocalBlacklist() then
			category = nil
		end
	end
	category = category or ''
	
	-- 
	local formattedError
	if section then
		formattedError = p.message(
			'error-config-message-help',
			errorMessage,
			section
		)
	else
		formattedError = p.message(
			'error-config-message-nohelp',
			errorMessage
		)
	end

	-- 
	return string.format(
		'<strong class="error">%s</strong>%s',
		formattedError,
		category
	)
end

local function formatPage(interwiki, namespace, page)
	-- 
	local ret = {}
	interwiki = interwiki or ''
	if interwiki ~= '' or colonNamespaces[namespace] then
		ret[#ret + 1] = ':'
	end
	ret[#ret + 1] = interwiki
	if interwiki ~= '' then
		ret[#ret + 1] = ':'
	end
	if namespace then
		local nsTable = namespaces[namespace]
		if not nsTable then
			error('"' .. tostring(namespace) .. '" is not a valid namespace key', 2)
		end
		ret[#ret + 1] = nsTable.name
		if namespace ~= 0 then
			ret[#ret + 1] = ':'
		end
	end
	ret[#ret + 1] = page
	return table.concat(ret)
end

local function formatDisplay(s)
	-- 
	if mw.isSubsting() then
		return s
	else
		return s:gsub(' ', '&nbsp;')
	end
end

function p.makeWikilink(interwiki, namespace, page, display)
	-- 
	local formattedPage = formatPage(interwiki, namespace, page)
	if display then
		display = formatDisplay(display)
		return string.format('[[%s|%s]]', formattedPage, display)
	else
		return string.format('[[%s]]', formattedPage)
	end
end

local function formatUrlLink(url, display)
	-- 
	if display then
		display = formatDisplay(display)
		return string.format('[%s %s]', url, display)
	else
		return string.format('[%s]', url)
	end
end

function p.makeUrlLink(s, display)
	-- 
	local url = mw.uri.new(s)
	url = tostring(url)
	return formatUrlLink(url, display)
end

function p.makeFullUrlLink(interwiki, namespace, page, query, display)
	-- 
	local formattedPage = formatPage(interwiki, namespace, page)
	local url = mw.uri.fullUrl(formattedPage, query)
	url = tostring(url)
	return formatUrlLink(url, display)
end

function p.message(key, ...)
	-- 
	local msg = cfg[key]
	if not msg then
		p.raiseError(
			'No message found with key "' .. tostring(key) .. '"',
			'No message found',
			2
		)
	end
	local noArgs = select('#', ...)
	if noArgs < 1 then
		return msg
	else
		local msg = mw.message.newRawMessage(msg, ...)
		return msg:plain()
	end
end

return p