Jump to content

Module:UserLinks/shared: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Undid revision 602599208 by Mr. Stradivarius (talk) didn't help
fix the wikilink function for links without namespaces
Line 51: Line 51:


local function formatPage(interwiki, namespace, page)
local function formatPage(interwiki, namespace, page)
namespace = namespace or error('no namespace defined', 2)
local ret = {}
local ret = {}
interwiki = interwiki or ''
interwiki = interwiki or ''
Line 61: Line 60:
ret[#ret + 1] = ':'
ret[#ret + 1] = ':'
end
end
if namespace then
local nsTable = namespaces[namespace]
if not nsTable then
local nsTable = namespaces[namespace]
error('"' .. tostring(namespace) .. '" is not a valid namespace key', 2)
if not nsTable then
error('"' .. tostring(namespace) .. '" is not a valid namespace key', 2)
end
end
ret[#ret + 1] = nsTable.name
ret[#ret + 1] = nsTable.name
if namespace ~= 0 then
if namespace ~= 0 then
ret[#ret + 1] = ':'
ret[#ret + 1] = ':'
end
end
end
ret[#ret + 1] = page
ret[#ret + 1] = page

Revision as of 16:37, 3 April 2014

-- This module stores functions that are shared between [[Module:UserLinks]]
-- and [[Module:UserLinks/extra]].

-- Load data and define often-used variables
local cfg = mw.loadData('Module:UserLinks/config')
local namespaces = mw.site.namespaces

-- Define namespaces for which links need to be escaped with the colon trick.
-- See [[w:en:Help:Colon trick]].
local colonNamespaces = {
	[6] = true, -- File
	[14] = true, -- Category
}

local p = {}

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

function p.makeWikitextError(encodedMessage, demo)
	local message, section = mw.ustring.match(encodedMessage, '^(.-)|(.*)$')
	message = message or encodedMessage
	if section then
		section = ' ([[Template:User-multi#' .. section .. '|help]])'
	else
		section = ''
	end
	local category
	if not demo then
		category = '[[Category:UserLinks transclusions with errors]]'
		mCategoryHandler = require('Module:Category handler')
		category = mCategoryHandler.main{
			all = category -- Categorise all namespaces, but not blacklisted pages.
		}
	end
	category = category or ''
	return string.format(
		'<strong class="error">[[Template:User-multi|User-multi]] error: %s%s.</strong>%s',
		message, section, 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

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

function p.makeUrlLink(interwiki, namespace, page, query, display)
	local formattedPage = formatPage(interwiki, namespace, page)
	query = query or {}
	local url = mw.uri.fullUrl(formattedPage, query)
	url = tostring(url)
	if display then
		display = display:gsub(' ', '&nbsp;')
		return string.format('[%s %s]', url, display)
	else
		return string.format('[%s]', url)
	end
end

function p.message(key)
	local msg = cfg[key]
	return msg or p.raiseError(
		'No message found with key "' .. tostring(key) .. '"',
		'No message found',
		2
	)
end

return p