モジュール:サンドボックス/MysteryPedia/test

-- Settings
local HOUR_INTERVALS = 1 -- 3
local NUMBER_OF_WANTEDPAGES_SEGMENTS = 2	-- NOTE: excepting the 'basic' page
-- 
local function requireAny(...)
	for i, fullName in ipairs({...}) do
		local succeeded, instance = pcall(require, fullName)
		if succeeded then
			return instance
		end
	end
	error(table.concat({...}, ", ") .. " not found.")
end
local export = {}
local util = requireAny("Module:Utility", "Module:サンドボックス/MysteryPedia/Utility")
function export.order(frame)
	local orderNumber = frame.args1 or error("Arguments need at least one.")
	local hours = math.floor(os.time() / 3600)
	local timeslot = math.floor(hours / HOUR_INTERVALS)
	local index = timeslot % NUMBER_OF_WANTEDPAGES_SEGMENTS + 1
	local restricted = (timeslot % 2 == 0)
	
	local moduleName = util.getModuleName()
	local basic = mw.loadData(moduleName .. "/data/wanted pages/basic")
	local array = mw.loadData(moduleName .. "/data/wanted pages/" .. index)
	-- NOTE: objects generated by mw.loadData do not support the # operator.
	local max1 = util.size(basic)
	local max2 = util.size(array)
	
	mw.logObject({ hours = hours, timeslot = timeslot, index = index, "max basic" = max1, "max page" .. index = max2 })
	
	-- This function must return a random word, which is the same value until the time-slot has changed.
	for i = 1, math.huge, 1 do
		local n = math.floor(timeslot * i * math.exp(orderNumber) % (max1 * 2 + max2 + 1))
		local word = basicn or basicn - max1 or arrayn - (max1 * 2)
		if not word then
			break
		end
		-- PENDING:
		-- In this time-slot, hiragana and kanji are highly recommended.
--
		if restricted then
			-- I know this is a rude way. I don't want exactness this time.
			if not mw.ustring.match(word, "^一-龠ぁ-ゞ+$") then
				word = nil
			end
		end
--
		if word then
			local succeeded, t = pcall(mw.title.new, word)
			if succeeded and not t.exists then
				return ":" .. word .. ""
			end
		end
	end
	return '(nothing)'
end
function export.show_codepoint(frame)
	local d = mw.ustring.codepoint(frame.args1)
	
	return frame:expandTemplate({
		title = "unicode code",
		args = {
			"16進" = mw.ustring.format("%X", d),
			"10進" = d,
		}})
end
local function xxchar(lang, frame)
	local parameters = frame.args
	local sequence
	-- NOTE: frame.args does not support the # operator.
	if parameters1 then
		if not parameters2 and mw.ustring.len(parameters1) > 1 then -- maybe  passed
			sequence = mw.text.split(parameters1, )
		else -- conventional use
			sequence = parameters
		end
	else
		error("Arguments need at least one.")
		return 
	end
	
	local validator = requireAny("Module:User:CodeCat/isValidPageName", "Module:IsValidPageName")
	local buffer = {"}")
	return frame:preprocess(table.concat(buffer))
end
-- If you called  - the Frame object has arguments passed.
-- You can pass arguments via a template. In this case the parent Frame object has arguments passed. See wikt:en:Template:categorize.
local function getInvoker(frame)
	if frame.args1 and not frame.args'auto' then
		return frame
	else
		return frame:getParent()
	end
end
function export.xxchars(frame)
	local lang = frame.args1 or error("Arguments need at least one.")
	local title = frame.args2 or mw.title.getCurrentTitle().text
	
	return xxchar(lang, frame:newChild({ args = {title} }))
end
-- @deprecated
function export.jachar(frame)
	return xxchar("ja", getInvoker(frame))
end
function export.auto_categorize(frame)
	local sortKey = frame.args1
	local title = mw.title.getCurrentTitle().fullText
	local data = mw.loadData(util.getModuleName() .. "/data/categories")
	
	return util.categorizeFromData(data, title, sortKey)
end
function export.kanji_variants(frame)
	local a = frame.args
	local buffer = {"* 異体字 : "}
	for k, v in pairs(frame:getParent().args) do
		if #buffer > 1 then
			table.insert(buffer, a"delimiter")
		end
		if type(k) == "number" then
			table.insert(buffer, mw.ustring.format(a"glyph", "" .. v .. ""))
		else
			table.insert(buffer, mw.ustring.format(a"glyph", "" .. k .. ""))
			local s = v
			if v ~= "異体字" then
				s = "" .. v .. ""
			end
			table.insert(buffer, mw.ustring.format(a"classification", s))
		end
	end
	return frame:preprocess(table.concat(buffer))
end
function export.defaultsort(frame)	--  or something
	error("Not Implemented")
end
local console = util.import(util.getModuleName("Console"))
do
	if console then
		console.attach(export)
		console.define({
			'data' =
				function(index)
					return mw.loadData(util.getModuleName() .. "/data/wanted pages/" .. (index or "basic"))
				end,
			})
	end
end
return export