Jump to content

Module:Interwiki extra/data: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
raise an error if there is no url object or no host, and use "url" for the string and "urlObj" for the object
we can't put URI objects in the data, as they have metatables
Line 18: Line 18:
prefixData[k] = v
prefixData[k] = v
end
end
local url = prefixData.url
local urlObj = mw.uri.new(url)
if not urlObj then
error(string.format(
"could not make URI object for prefix '%s'; " ..
"please check your wiki's interwiki data",
tostring(prefix)
))
end
prefixData.url = url
prefixData.urlObj = urlObj
prefixes[prefix] = prefixData
prefixes[prefix] = prefixData


Line 38: Line 27:
prefixData
prefixData
)
)
urlData.url = url
urlData.url = prefixData.url
urlData.urlObj = urlObj
urls[url] = urlData
urls[url] = urlData
end
end


for url, urlData in pairs(urls) do
for url, urlData in pairs(urls) do
local urlObj = mw.uri.new(url) -- We can't output URI objects as they have metatables
local host = urlData.urlObj.host
if not host then
if not urlObj or not urlObj.host then
error(string.format(
error(string.format(
"could not find a host in URL '%s'; " ..
"could not find a host in URL '%s'; " ..
Line 59: Line 47:
urlData
urlData
)
)
hostData.host = host
hostData.host = urlObj.host
hosts[host] = hostData
hosts[host] = hostData
end
end

Revision as of 01:43, 21 January 2015

-- This module processes data from mw.site.interwikiMap for use with
-- [[Module:Interwiki]]. By loading the data with mw.loadData we ensure that
-- this is only done once per page parse.

local prefixes, urls, hosts = {}, {}, {}

local function makeCountedData(t, storeKey, counterKey, itemKey, item)
	t = t or {}
	t[storeKey] = t[storeKey] or {}
	t[storeKey][itemKey] = item
	t[counterKey] = t[counterKey] and t[counterKey] + 1 or 1
	return t
end

for prefix, data in pairs(mw.site.interwikiMap()) do
	local prefixData = {}
	for k, v in pairs(data) do
		prefixData[k] = v
	end
	prefixes[prefix] = prefixData

	local urlData = makeCountedData(
		urls[urlText],
		'prefixes',
		'nPrefixes',
		prefix,
		prefixData
	)
	urlData.url = prefixData.url
	urls[url] = urlData
end

for url, urlData in pairs(urls) do
	local urlObj = mw.uri.new(url) -- We can't output URI objects as they have metatables
	if not urlObj or not urlObj.host then
		error(string.format(
			"could not find a host in URL '%s'; " ..
			"please check your wiki's interwiki data",
			tostring(url)
		))
	end
	local hostData = makeCountedData(
		hosts[host],
		'urls',
		'nUrls',
		url,
		urlData
	)
	hostData.host = urlObj.host
	hosts[host] = hostData
end

return {
	prefixes = prefixes,
	urls = urls,
	hosts = hosts
}