Pumunta sa nilalaman

Module:debug/track

Mula Wiksiyonaryo

Ang module na ito ay nangangailangan ng dokumentasyon.
Mangyaring idokumento ang module na ito sa pamamagitan ng paglalarawan ng layunin at paggamit nito sa the pahina ng dokumentasyon.

-- TODO 1: normalize keys with leading spaces, which don't get removed when
-- "Tracking/" is prefixed.

-- TODO 2: avoid weird inputs like "", which don't register as invalid since
-- "Tracking/" is a valid page title.

-- TODO 3: use varargs instead of a table input, then recurse with track().

local title_make_title_module = "Module:title/makeTitle"

-- Transclusion-based tracking as subpages of [[Wiktionary:Tracking]].
-- Tracked pages can be found at [[Special:WhatLinksHere/Wiktionary:Tracking/KEY]].
local error = error
local find = string.find
local log = mw.log
local type = type

local invalid_tracking_key_key = "debug/track/invalid key"

local function make_title(...)
	make_title = require(title_make_title_module)
	return make_title(...)
end

local memo

local function track(key)
	if not memo then
		memo = {}
	-- Return if memoized.
	elseif memo[key] then
		return
	end
	-- Throw an error if `key` isn't a string.
	local key_type = type(key)
	if key_type ~= "string" then
		error("Tracking keys supplied to [[Module:debug/track]] must be strings; received " .. key_type .. ".", 3)
	end
	-- make_title returns nil for invalid titles, but "#" is treated as a
	-- fragment separator (e.g. "foo#bar" generates the title "foo"), so it
	-- needs to be manually excluded.
	local title = not find(key, "#", nil, true) and make_title(4, "Tracking/" .. key)
	if not title then
		-- Track uses of invalid keys. Instead of recursing, simply memoize the
		-- invalid key and replace it with the 'invalid tracking key' key.
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/debug/track/invalid key]]
		log("Invalid tracking key: " .. key)
		memo[key] = true
		key = invalid_tracking_key_key
		if memo[key] then
			return
		end
		title = make_title(4, "Tracking/" .. key)
	end
	-- Normalize the key by treating it as a subpage of "Tracking", which gives
	-- the normalized title without the initial "Tracking/". If the normalized
	-- form has been memoized, don't transclude the page again. Otherwise,
	-- transclude the page using the `content` key on the title, which is the
	-- cheapest way to trigger transclusion, as it avoids any parser expansion
	-- of the target page.
	local normalized = title.subpageText
	if not memo[normalized] then
		title = title.content
		memo[normalized] = true
	end
	-- Memoize the original key.
	memo[key] = true
end

return function(input)
	if input == nil then
		error("No tracking key supplied to [[Module:debug/track]].", 2)
	elseif type(input) ~= "table" then
		track(input)
		return true
	end
	local key = input[1]
	if key == nil then
		error("No tracking keys in table supplied to [[Module:debug/track]].", 2)
	end
	local i = 1
	repeat
		track(key)
		i = i + 1
		key = input[i]
	until key == nil
	return true
end