if categoryLink and prefix then
categoryLink = "Category:"..categoryLink
end
return categoryLink, consistent
end  
-- Does the article have a corresponding Commons category?
-- Arguments:
-- qid = QID to lookup in wikidata (for testing only)
-- prefix = whether to add "Category:" to return string (default true)
-- Returns:
-- filename at Commons if so, blank if not
function p._hasCategory(qid,prefix)
local categoryLink, consistent = p._hasCategoryConsistent(qid,prefix)
return consistent and categoryLink
end  
-- Create Commons link corresponding to current article
-- Arguments:
-- namespace = namespace in Commons ("" for galleries)
-- default = use as Commons link, don't access wikidata
-- search = string to search for
-- fallback = string to search for if wikidata fails
-- formatting = formatting parameters
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- formatted wikilink to Commons in specified namespace
function p._getCommons(namespace,default,search,fallback,formatting,qid)
local nsColon
if not namespace or namespace == "" then
nsColon = ""
else
nsColon = namespace..":"
end
if default then
return "".._formatResult(default,formatting)..""
end
if search then
return "".._formatResult(search,formatting)..""
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local commonsLink = nil
local consistent = true
if nsColon == "" then
commonsLink, consistent = _lookupGallery(qid,true)
elseif namespace:lower == "category" then
commonsLink, consistent = _lookupCategory(qid,true)
end
-- use wikidata if consistent
if commonsLink and consistent then
return "".._formatResult(commonsLink,formatting)..""
end
-- if not consistent, fall back to search and add to tracking cat
-- construct default result (which searches for title)
local searchResult = "".._formatResult(fallback or wp_title,formatting)..""
if not consistent and wp_ns == "" then
local friendlyNS
if nsColon == "" then
friendlyNS = "gallery"
else
friendlyNS = namespace:lower
end
searchResult = searchResult..""
end
return searchResult
end  
-- Returns "best" Commons link: first look for gallery, then try category
-- Arguments:
-- default = use as Commons link, don't access wikidata
-- search = string to search for
-- fallback = string to search for if wikidata lookup fails
-- formatting = formatting parameters
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- formatted wikilink to Commons "best" landing page
function p._getGalleryOrCategory(default, search, fallback, formatting, qid)
if default then
return "".._formatResult(default,formatting)..""
end
if search then
return "".._formatResult(search,formatting)..""
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local trackingCats = ""
local galleryLink, consistent, commonsSitelink = _lookupGallery(qid,true)
-- use wikidata if either sitelink or P935 exist, and they both agree
if galleryLink and consistent then
return "".._formatResult(galleryLink,formatting)..""
end
if not consistent and wp_ns == "" then
trackingCats = ""
end
-- if gallery is not good, fall back looking for category
local categoryLink
categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink)
if categoryLink and consistent then
return "".._formatResult(categoryLink,formatting)..""..trackingCats
end
if not consistent and wp_ns == "" then
trackingCats = trackingCats..""
end
-- return search result looking for title as last attempt
return "" .. _formatResult(fallback or wp_title,formatting) .. "" .. trackingCats
end  
-- Return link(s) Commons gallery, or category, or both from wikidata
-- Arguments:
-- defaultGallery = default gallery link to use, instead of wikidata
-- defaultCategory = default category link to use, instead of wikidata
-- categoryText = if both gallery and category, text to use in category link ("category" by default)
-- oneSearch = only emit one search result
-- formatting = formatting parameters
-- qid = qid of page to lookup in wikidata (testing only)
function p._getGalleryAndCategory(defaultGallery, defaultCategory,
categoryText, oneSearch, formatting, qid
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
categoryText = categoryText or "category"
local trackingCats = ""
local galleryLink, galleryConsistent
local commonsSitelink = nil
if defaultGallery then
galleryLink = defaultGallery
galleryConsistent = true
else
galleryLink, galleryConsistent, commonsSitelink = _lookupGallery(qid,true)
end
local galleryGood = galleryLink and galleryConsistent
if not galleryConsistent and wp_ns == "" then
trackingCats = ""
end
local categoryLink, categoryConsistent
if defaultCategory then
categoryLink = defaultCategory
categoryConsistent = true
else
categoryLink, categoryConsistent = _lookupCategory(qid,defaultGallery,commonsSitelink)
end
local categoryGood = categoryLink and categoryConsistent
if not categoryConsistent and wp_ns == "" then
trackingCats = trackingCats..""
end
local firstLink
-- construct default result (which searches for title)
local searchResult = "".._formatResult(wp_title,formatting)..""
if not oneSearch then
searchResult = searchResult.." ("..categoryText..")"
end
local linkText = nil
if galleryGood then
firstLink = galleryLink
linkText = galleryLink
elseif categoryGood then
firstLink = "Category:"..categoryLink
linkText = categoryLink
else
return searchResult..trackingCats
end
local resultVal = "".._formatResult(linkText,formatting)..""
if galleryGood and categoryGood then
resultVal = resultVal.." ("..categoryText..")"
end
return resultVal..trackingCats
end  
-- Compare two titles with their namespaces stripped
local function titleMatch(s1,s2)
s1 = s1 or ""
s2 = s2 or ""
s1 = mw.ustring.gsub(s1,"^[^:]+:","")
s2 = mw.ustring.gsub(s2,"^[^:]+:","")
return s1 == s2
end  
local galleryTrackingCats = {
commons_link_on_wikidata = '',
commons_link_defined_as_pagename = '',
commons_link_locally_defined = '',
commons_link_from_wikidata = '',
commons_link_is_pagename = '',
inconsistent = ''
}  
local categoryTrackingCats = {
commons_link_on_wikidata = '',
commons_link_defined_as_pagename = '',
commons_link_locally_defined = '',
commons_link_from_wikidata = '',
commons_link_is_pagename = '',
inconsistent = ''
}  
local function selectTrackingCat(trackingCats,wikidata,consistent,default,title)
if not consistent then
return trackingCats.inconsistent
end
if default then
-- construct warning message
if default == wikidata then
return trackingCats.commons_link_on_wikidata
end
local warning = ""
if wikidata then
warning = generateWarning({
"Commons link does not match Wikidata – please check"
})
end
if titleMatch(default,title) then
return trackingCats.commons_link_defined_as_pagename .. warning
end
return trackingCats.commons_link_locally_defined .. warning
end
if wikidata then
return trackingCats.commons_link_from_wikidata
end
return trackingCats.commons_link_is_pagename
end  
-- Figure out tracking categories and editor warnings
-- Arguments:
-- default = Commons link argument passed to template
-- fetchGallery = whether to fetch a gallery from Wikidata
-- fetchCategory = whether to fetch a category from Wikidata
-- qid = force a qid for testing
-- Returns:
-- tracking category and possible user warning
---
-- Note: the logic for the tracking is quite different than the logic
-- for generating Commons links (above). Thus, it is separated into another
-- function for code clarity and maintainability. This should not seriously
-- affect performance: server time is dominated by fetching wikidata entities,
-- and those entities should be cached and shared between the Commons generating
-- code and this tracking code.
function p._tracking(default, fetchGallery, fetchCategory, qid)
local title, wp_ns, wp_qid = _getTitleQID(qid,true)
if wp_ns ~= "" then
title = wp_ns..":"..title
end
-- only track if test or namespace=article or namespace=category
if not (qid or wp_ns == "" or wp_ns == "Category") then
return ""
end  
-- determine title and namespace of wikidata and wp article