Module:LinkParser: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode Tag: Reverted |
||
| Line 114: | Line 114: | ||
wikiLinkCache[cacheKey] = result | wikiLinkCache[cacheKey] = result | ||
return result | return result | ||
end | |||
-- Creates a {{fullurl:...}} wikitext string with URL-encoded parameters. This is necessary for safely passing values with spaces or special characters in query strings. | |||
-- @param target The target page name. | |||
-- @param params A table of query parameters {key1 = value1, key2 = value2}. | |||
-- @return Wikitext for the fullurl invocation. | |||
function p.createFullUrlWikitext(target, params) | |||
if not target or target == "" then | |||
return "" | |||
end | |||
local queryParts = {} | |||
if params then | |||
-- For consistent ordering, sort keys | |||
local keys = {} | |||
for k in pairs(params) do | |||
table.insert(keys, k) | |||
end | |||
table.sort(keys) | |||
for _, key in ipairs(keys) do | |||
local value = params[key] | |||
-- URL-encode the value for the query string per MediaWiki best practices | |||
-- Use tostring() to gracefully handle non-string values | |||
local encodedValue = mw.uri.encode(tostring(value), 'QUERY') | |||
table.insert(queryParts, string.format('%s=%s', key, encodedValue)) | |||
end | |||
end | |||
local queryString = table.concat(queryParts, '&') | |||
if queryString ~= "" then | |||
return string.format('{{fullurl:%s|%s}}', target, queryString) | |||
else | |||
return string.format('{{fullurl:%s}}', target) | |||
end | |||
end | end | ||