Module:UtilsPage: Difference between revisions
Jump to navigation
Jump to search
PhantomCaleb (talk | contribs) (fix tests broken after domain move) |
PhantomCaleb (talk | contribs) No edit summary |
||
Line 97: | Line 97: | ||
return string.gsub(page, ":?".. namespace..":", "") | return string.gsub(page, ":?".. namespace..":", "") | ||
end | end | ||
return p | return p |
Revision as of 02:06, 22 September 2022
local p = {}
local h = {}
local utilsCargo = require("Module:UtilsCargo")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
function p.exists(fullPageName, noRedirect)
local anchorStart = string.find(fullPageName, "#")
if anchorStart then
fullPageName = string.sub(fullPageName, 1, anchorStart - 1)
end
local queryResults = utilsCargo.query("_pageData", "_pageName, _isRedirect", {
where = utilsCargo.allOf({
_pageName = fullPageName
})
})
return #queryResults > 0 and (not noRedirect or queryResults[1]._isRedirect == "0")
end
function p.fullUrl(page, queryParams)
local baseUrl = mw.site.server .. mw.site.scriptPath .. "/"
local pageUrl = baseUrl .. mw.uri.encode(page, "WIKI")
if queryParams then
local encodedParams = {}
for k, v in pairs(queryParams) do
local param = k .. "=" .. mw.uri.encode(tostring(v), "QUERY")
table.insert(encodedParams, param)
end
local queryStr = "?" .. table.concat(encodedParams, "&")
pageUrl = pageUrl .. queryStr
end
return pageUrl
end
local SEPARATOR = "#"
function p.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(k) == "number" then
dplArgs = dplArgs .. h.appendArg(v.param, v.value)
elseif type(v) == "table" then
for _, andedValue in ipairs(v) do
dplArgs = dplArgs .. h.appendArg(k, andedValue)
end
else
dplArgs = dplArgs .. h.appendArg(k, v)
end
end
dplArgs = dplArgs .. h.appendArg("format", SEPARATOR..",,%PAGE%" .. SEPARATOR .. ",")
local result = mw.getCurrentFrame():preprocess("{{#dpl:" .. dplArgs .. "}}")
if not utilsString.endsWith(result, SEPARATOR) then
return {}
end
result = string.gsub(result, SEPARATOR .. ":", SEPARATOR) -- strip : prefix from Category results
result = utilsString.trim(result, SEPARATOR)
result = utilsString.split(result, SEPARATOR)
return result
end
function h.appendArg(param, value)
value = tostring(value)
value = string.gsub(value, "\|", "{{!}}")
if param and value then
return "|" .. param .. "=" .. value .. "\n"
else
return ""
end
end
function p.inCategory(category, fullPageName)
if (not category) or (not fullPageName) then
return false
end
local title = mw.title.new(fullPageName)
local dplResult = p.dpl({
category= p.stripNamespace(category),
namespace= title.nsText,
title= title.text,
})
return #dplResult ~= 0
end
function p.inNamespace(namespaces, fullPageName)
if type(namespaces) == "string" then
namespaces = {namespaces}
end
local title = fullPageName and mw.title.new(fullPageName) or mw.title.getCurrentTitle()
return utilsTable.includes(namespaces, title.nsText)
end
function p.stripNamespace(page, namespace)
if not namespace then
namespace = "[^:]*"
end
return string.gsub(page, ":?".. namespace..":", "")
end
return p