Module:Commons link: Difference between revisions

No edit summary
No edit summary
 
Line 236: Line 236:
--  formatting = formatting parameters
--  formatting = formatting parameters
--  qid = QID to lookup in wikidata (for testing only)
--  qid = QID to lookup in wikidata (for testing only)
-- Returns:
--  formatted wikilink to Commons in specified namespace
-- Create Commons link based on current article title without redundant "Category:" prefix
-- Arguments:
--  namespace = namespace in Commons ("" for galleries)
--  formatting = formatting parameters
-- Returns:
-- Returns:
--  formatted wikilink to Commons in specified namespace
--  formatted wikilink to Commons in specified namespace
function p._getCommons(namespace, _, _, _, formatting)
function p._getCommons(namespace, _, _, _, formatting)
local titleObject = mw.title.getCurrentTitle()
local titleObject = mw.title.getCurrentTitle()
local nsColon = namespace and namespace ~= "" and (namespace .. ":") or ""
local titleText = titleObject.text
local commonsLink = "Category:" .. titleObject.text
local nsColon = (namespace and namespace ~= "" and namespace .. ":") or ""
return "[[Commons:" .. nsColon .. commonsLink .. "|" .. _formatResult(commonsLink, formatting) .. "]]"
 
-- Only add "Category:" prefix if the title doesn't already start with it
if nsColon == "Category:" and mw.ustring.sub(titleText, 1, 9) ~= "Category:" then
titleText = "Category:" .. titleText
end
 
-- Construct the link
local commonsLink = titleText
return "[[Commons:" .. commonsLink .. "|" .. _formatResult(commonsLink, formatting) .. "]]"
end
end


-- Returns "best" Commons link: first look for gallery, then try category
-- Returns "best" Commons link: first look for gallery, then try category