Module:Util/pages/dpl

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

dpl(args)

This function is wrapper for the DPL parser function.

When constructing queries, keep in mind DPL's configured limits.

  • A single query can return no more than 500 results. (maxResultCount)
  • A single query using category selection may contain no more than 4 categories. (maxCategoryCount)

Returns

  • Array of results.

Examples

#InputOutputResult
1
dpl({
  namespace = "Category",
  titlematch = "Link|Zelda",
})
{"Category:Link", "Category:Zelda"}
Repeating arguments
2
dpl({
  category = "Lynels",
  notcategory = {
    "Sub-Bosses",
    "Enemies in Hyrule Warriors: Age of Calamity",
  },
})
{"Blue Lynel", "Red Lynel"}

local util = {
	strings = {
		endsWith = require("Module:Util/strings/endsWith"),
		split = require("Module:Util/strings/split"),
		trim = require("Module:Util/strings/trim"),
	}
}

local SEPARATOR = "#"
local function dpl(args)
	local dplArgs = ""
	for k, v in pairs(args) do
		if k == "format" or type(v) == "table" and v.value == "format" then
			mw.addWarning("<code>format</code> argument cannot be used here. Format the resulting Lua table instead.")
		elseif type(v) == "table" then
			for _, andedValue in ipairs(v) do
				dplArgs = dplArgs .. appendArg(k, andedValue)
			end
		else
			dplArgs = dplArgs .. appendArg(k, v)
		end
	end
	dplArgs = dplArgs .. appendArg("format", SEPARATOR..",,%PAGE%" .. SEPARATOR .. ",")
	local result = mw.getCurrentFrame():preprocess("{{#dpl:" .. dplArgs .. "}}")
	if not util.strings.endsWith(result, SEPARATOR) then
		return {}
	end
	result = string.gsub(result, SEPARATOR .. ":", SEPARATOR) -- strip : prefix from Category results
	result = util.strings.trim(result, SEPARATOR)
	result = util.strings.split(result, SEPARATOR)
	return result
end

function appendArg(param, value)
	value = tostring(value)
	value = string.gsub(value, "\|", "{{!}}")
	if param and value then
		return "|" .. param .. "=" .. value .. "\n"
	else
		return ""
	end
end

return dpl