<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.cern.ch/index.php?action=history&amp;feed=atom&amp;title=Module%3ACommons_link</id>
	<title>Module:Commons link - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.cern.ch/index.php?action=history&amp;feed=atom&amp;title=Module%3ACommons_link"/>
	<link rel="alternate" type="text/html" href="https://wiki.cern.ch/index.php?title=Module:Commons_link&amp;action=history"/>
	<updated>2026-04-04T08:54:21Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.cern.ch/index.php?title=Module:Commons_link&amp;diff=7713&amp;oldid=prev</id>
		<title>Vigen: 1 revision imported from :wikipedia:Module:Commons_link</title>
		<link rel="alternate" type="text/html" href="https://wiki.cern.ch/index.php?title=Module:Commons_link&amp;diff=7713&amp;oldid=prev"/>
		<updated>2026-03-19T08:09:35Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported from &lt;a href=&quot;https://en.wikipedia.org/wiki/Module:Commons_link&quot; class=&quot;extiw&quot; title=&quot;wikipedia:Module:Commons link&quot;&gt;wikipedia:Module:Commons_link&lt;/a&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 08:09, 19 March 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Vigen</name></author>
	</entry>
	<entry>
		<id>https://wiki.cern.ch/index.php?title=Module:Commons_link&amp;diff=7712&amp;oldid=prev</id>
		<title>wikipedia&gt;Hike395: lazy load module</title>
		<link rel="alternate" type="text/html" href="https://wiki.cern.ch/index.php?title=Module:Commons_link&amp;diff=7712&amp;oldid=prev"/>
		<updated>2024-10-24T10:43:16Z</updated>

		<summary type="html">&lt;p&gt;lazy load module&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;require(&amp;#039;strict&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
-- Module to find commons galleries and categories based on wikidata entries&lt;br /&gt;
local getArgs = require(&amp;#039;Module:Arguments&amp;#039;).getArgs&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- Check if string is a valid QID&lt;br /&gt;
-- Argument: QID to check&lt;br /&gt;
-- Returns: valid (bool)&lt;br /&gt;
local function _validQID(qid)&lt;br /&gt;
	return qid and mw.ustring.find(qid,&amp;quot;^[Qq]%d+$&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Check if string is a valid wikidata property string&lt;br /&gt;
-- Argument: property string to check&lt;br /&gt;
-- Returns: valid (bool)&lt;br /&gt;
local function _validProp(prop)&lt;br /&gt;
	return prop and mw.ustring.find(prop,&amp;quot;^[Pp]%d+$&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function _lcfirst(s)&lt;br /&gt;
	return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Format displayed linktext&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   s = string to display&lt;br /&gt;
--   formatting = formatting table:&lt;br /&gt;
--    formatting.linktext = if defined, override s&lt;br /&gt;
--    formatting.lcfirst = lower case the first letter in display&lt;br /&gt;
--    formatting.bold = whether to bold the display&lt;br /&gt;
--    formatting.italic = whether to italicize the display&lt;br /&gt;
--    formatting.nowrap = set nowrapping&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   formatted string&lt;br /&gt;
local function _formatResult(s, formatting)&lt;br /&gt;
	local resultVal = formatting.linktext or s&lt;br /&gt;
	if formatting.lcfirst then&lt;br /&gt;
		resultVal = _lcfirst(resultVal)&lt;br /&gt;
	end&lt;br /&gt;
    local style = &amp;quot;&amp;quot;&lt;br /&gt;
	if formatting.italic then style = &amp;quot;font-style:italic; &amp;quot; end&lt;br /&gt;
	if formatting.bold then style = style..&amp;quot;font-weight:bold; &amp;quot; end&lt;br /&gt;
	if formatting.nowrap then style = style..&amp;quot;white-space:nowrap; &amp;quot; end&lt;br /&gt;
    if style ~= &amp;quot;&amp;quot; then&lt;br /&gt;
    	resultVal = &amp;#039;&amp;lt;span style=&amp;quot;&amp;#039;..mw.text.trim(style)..&amp;#039;&amp;quot;&amp;gt;&amp;#039;..resultVal..&amp;#039;&amp;lt;/span&amp;gt;&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
	return resultVal&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Get title, namespace, and QID for current page&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = testing only: get title of alternative page with QID=qid&lt;br /&gt;
--   nsQid = whether to return the ns of the qid page or current&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   title, namespace (string), qid of current page (or test page)&lt;br /&gt;
local function _getTitleQID(qid,nsQid)&lt;br /&gt;
	local titleObject = mw.title.getCurrentTitle()&lt;br /&gt;
	-- look up qid for current page (if not testing)&lt;br /&gt;
	local nsText = string.gsub(titleObject.nsText,&amp;quot;_&amp;quot;,&amp;quot; &amp;quot;) -- [[phab:T369784]]&lt;br /&gt;
	if not _validQID(qid) then&lt;br /&gt;
		qid = mw.wikibase.getEntityIdForCurrentPage()&lt;br /&gt;
		return titleObject.text, nsText, qid&lt;br /&gt;
	end&lt;br /&gt;
	-- testing-only path: given a qid, determine title&lt;br /&gt;
	-- always use namespace from current page (to suppress tracking cat)&lt;br /&gt;
	qid = qid:upper()&lt;br /&gt;
	local title = mw.wikibase.getSitelink(qid) or &amp;quot;&amp;quot;&lt;br /&gt;
	-- strip any namespace from sitelink&lt;br /&gt;
	local firstColon = mw.ustring.find(title,&amp;#039;:&amp;#039;,1,true)&lt;br /&gt;
	local qidNsText = &amp;quot;&amp;quot;&lt;br /&gt;
	if firstColon then&lt;br /&gt;
		qidNsText = mw.ustring.sub(title,1,firstColon-1)&lt;br /&gt;
		title = mw.ustring.sub(title,firstColon+1)&lt;br /&gt;
	end&lt;br /&gt;
	if nsQid then&lt;br /&gt;
		return title, qidNsText, qid&lt;br /&gt;
	end&lt;br /&gt;
	return title, nsText, qid&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Lookup Commons gallery in Wikidata&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID of current article&lt;br /&gt;
--   fetch = whether to lookup Commons sitelink (bool)&lt;br /&gt;
--   commonsSitelink = default value for Commons sitelink&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   categoryLink = name of Commons category, nil if nothing is found&lt;br /&gt;
--   consistent = multiple wikidata fields are examined: are they consistent?&lt;br /&gt;
--   commonsSitelink = commons sitelink for current article&lt;br /&gt;
local function _lookupGallery(qid,fetch,commonsSitelink)&lt;br /&gt;
	if not _validQID(qid) then&lt;br /&gt;
		return nil, true, nil&lt;br /&gt;
	end&lt;br /&gt;
	qid = qid:upper()&lt;br /&gt;
	local galleryLink = nil&lt;br /&gt;
	local consistent = true&lt;br /&gt;
	-- look up commons sitelink for article, use if not category&lt;br /&gt;
	if fetch then&lt;br /&gt;
		commonsSitelink = mw.wikibase.getSitelink(qid,&amp;quot;commonswiki&amp;quot;) or commonsSitelink&lt;br /&gt;
	end&lt;br /&gt;
	if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) ~= &amp;quot;Category:&amp;quot; then&lt;br /&gt;
		galleryLink = commonsSitelink&lt;br /&gt;
	end&lt;br /&gt;
	-- P935 is the &amp;quot;commons gallery&amp;quot; property for this article&lt;br /&gt;
	local P935 = mw.wikibase.getBestStatements(qid, &amp;quot;P935&amp;quot;)[1]&lt;br /&gt;
	if P935 and P935.mainsnak.datavalue then&lt;br /&gt;
		local gallery = P935.mainsnak.datavalue.value&lt;br /&gt;
		if galleryLink and galleryLink ~= gallery then&lt;br /&gt;
			consistent = false&lt;br /&gt;
		else&lt;br /&gt;
			galleryLink = gallery&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return galleryLink, consistent, commonsSitelink&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find fallback category by looking up Commons sitelink of different page&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--    qid = QID for current article&lt;br /&gt;
--    property = property that refers to other article whose sitelink to return&lt;br /&gt;
-- Returns: either category-stripped name of article, or nil&lt;br /&gt;
local function _lookupFallback(qid,property)&lt;br /&gt;
	if not _validQID(qid) or not _validProp(property) then&lt;br /&gt;
		return nil&lt;br /&gt;
	end&lt;br /&gt;
	qid = qid:upper()&lt;br /&gt;
	property = property:upper()&lt;br /&gt;
	-- If property exists on current article, get value (other article qid)&lt;br /&gt;
	local value = mw.wikibase.getBestStatements(qid, property)[1]&lt;br /&gt;
	if value and value.mainsnak.datavalue and value.mainsnak.datavalue.value.id then&lt;br /&gt;
		-- Look up Commons sitelink of other article&lt;br /&gt;
		local sitelink = mw.wikibase.getSitelink(value.mainsnak.datavalue.value.id,&amp;quot;commonswiki&amp;quot;)&lt;br /&gt;
		-- Check to see if it starts with &amp;quot;Category:&amp;quot;. If so, strip it and return&lt;br /&gt;
		if sitelink and mw.ustring.sub(sitelink,1,9) == &amp;quot;Category:&amp;quot; then&lt;br /&gt;
			return mw.ustring.sub(sitelink,10)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find Commons category by looking in wikidata&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID of current article&lt;br /&gt;
--   fetch = whether to lookup Commons sitelink (bool)&lt;br /&gt;
--   commonsSitelink = default value for Commons sitelink&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   categoryLink = name of Commons category, nil if nothing is found&lt;br /&gt;
--   consistent = multiple wikidata fields are examined: are they consistent?&lt;br /&gt;
--   commonsSitelink = commons sitelink for current article&lt;br /&gt;
local function _lookupCategory(qid, fetch, commonsSitelink)&lt;br /&gt;
	if not _validQID(qid) then&lt;br /&gt;
		return nil, true, nil&lt;br /&gt;
	end&lt;br /&gt;
	qid = qid:upper()&lt;br /&gt;
	local categoryLink = nil&lt;br /&gt;
	local consistent = true&lt;br /&gt;
	-- look up commons sitelink for article, use if starts with &amp;quot;Category:&amp;quot;&lt;br /&gt;
	if fetch then&lt;br /&gt;
		commonsSitelink = mw.wikibase.getSitelink(qid,&amp;quot;commonswiki&amp;quot;) or commonsSitelink&lt;br /&gt;
	end&lt;br /&gt;
	if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) == &amp;quot;Category:&amp;quot; then&lt;br /&gt;
		categoryLink = mw.ustring.sub(commonsSitelink,10)&lt;br /&gt;
	end&lt;br /&gt;
	-- P910 is the &amp;quot;topic&amp;#039;s main category&amp;quot;. Look for commons sitelink there&lt;br /&gt;
	local fallback = _lookupFallback(qid,&amp;quot;P910&amp;quot;)&lt;br /&gt;
	if fallback then&lt;br /&gt;
		if categoryLink and categoryLink ~= fallback then&lt;br /&gt;
			consistent = false&lt;br /&gt;
			qid = nil&lt;br /&gt;
		else&lt;br /&gt;
			categoryLink = fallback&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	-- P1754 is the &amp;quot;list&amp;#039;s main category&amp;quot;. Look for commons sitelink there&lt;br /&gt;
	fallback = _lookupFallback(qid,&amp;quot;P1754&amp;quot;)&lt;br /&gt;
	if fallback then&lt;br /&gt;
		if categoryLink and categoryLink ~= fallback then&lt;br /&gt;
			consistent = false&lt;br /&gt;
			qid = nil&lt;br /&gt;
		else&lt;br /&gt;
			categoryLink = fallback&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
    -- P373 is the &amp;quot;commons category&amp;quot; property for this article. This is&lt;br /&gt;
    -- a low-quality field, so should only be used as a last resort.&lt;br /&gt;
    if categoryLink == nil and _validQID(qid) then&lt;br /&gt;
	    local P373 = mw.wikibase.getBestStatements(qid, &amp;quot;P373&amp;quot;)[1]&lt;br /&gt;
	    if P373 and P373.mainsnak.datavalue then&lt;br /&gt;
		    categoryLink = P373.mainsnak.datavalue.value&lt;br /&gt;
		    consistent = true  -- P373 is never used if anything else is available&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return categoryLink, consistent, commonsSitelink&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Does the article have a Commons gallery, and is it consistent?&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   filename at Commons, bool: is wikidata consistent for this article?&lt;br /&gt;
function p._hasGalleryConsistent(qid)&lt;br /&gt;
	local wp_title, wp_ns&lt;br /&gt;
	wp_title, wp_ns, qid = _getTitleQID(qid)&lt;br /&gt;
	return _lookupGallery(qid,true)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Does the article have a corresponding Commons gallery?&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   filename at Commons if so, false if not&lt;br /&gt;
function p._hasGallery(qid)&lt;br /&gt;
	local galleryLink, consistent = p._hasGalleryConsistent(qid)&lt;br /&gt;
	return consistent and galleryLink&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Does the article have a Commons category? Is wikidata consistent for that?&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
--   prefix = whether to add &amp;quot;Category:&amp;quot; to return string (default true)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   filename at Commons, bool: consistent&lt;br /&gt;
function p._hasCategoryConsistent(qid,prefix)&lt;br /&gt;
	if prefix == nil then&lt;br /&gt;
		prefix = true&lt;br /&gt;
	end&lt;br /&gt;
	local wp_title, wp_ns&lt;br /&gt;
	wp_title, wp_ns, qid = _getTitleQID(qid)&lt;br /&gt;
	local categoryLink, consistent = _lookupCategory(qid,true)&lt;br /&gt;
	if categoryLink and prefix then&lt;br /&gt;
		categoryLink = &amp;quot;Category:&amp;quot;..categoryLink&lt;br /&gt;
	end&lt;br /&gt;
	return categoryLink, consistent&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Does the article have a corresponding Commons category?&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
--   prefix = whether to add &amp;quot;Category:&amp;quot; to return string (default true)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   filename at Commons if so, blank if not&lt;br /&gt;
function p._hasCategory(qid,prefix)&lt;br /&gt;
	local categoryLink, consistent = p._hasCategoryConsistent(qid,prefix)&lt;br /&gt;
	return consistent and categoryLink&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Create Commons link corresponding to current article&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   namespace = namespace in Commons (&amp;quot;&amp;quot; for galleries)&lt;br /&gt;
--   default = use as Commons link, don&amp;#039;t access wikidata&lt;br /&gt;
--   search = string to search for&lt;br /&gt;
--   fallback = string to search for if wikidata fails&lt;br /&gt;
--   formatting = formatting parameters&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   formatted wikilink to Commons in specified namespace&lt;br /&gt;
function p._getCommons(namespace,default,search,fallback,formatting,qid)&lt;br /&gt;
	local nsColon&lt;br /&gt;
	if not namespace or namespace == &amp;quot;&amp;quot; then&lt;br /&gt;
		nsColon = &amp;quot;&amp;quot;&lt;br /&gt;
	else&lt;br /&gt;
		nsColon = namespace..&amp;quot;:&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if default then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..nsColon..default..&amp;quot;|&amp;quot;.._formatResult(default,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if search then&lt;br /&gt;
		return &amp;quot;[[Commons:Special:Search/&amp;quot;..nsColon..search..&amp;quot;|&amp;quot;.._formatResult(search,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local wp_title, wp_ns&lt;br /&gt;
	wp_title, wp_ns, qid = _getTitleQID(qid)&lt;br /&gt;
	local commonsLink = nil&lt;br /&gt;
	local consistent = true&lt;br /&gt;
	if nsColon == &amp;quot;&amp;quot; then&lt;br /&gt;
		commonsLink, consistent = _lookupGallery(qid,true)&lt;br /&gt;
	elseif namespace:lower() == &amp;quot;category&amp;quot; then&lt;br /&gt;
		commonsLink, consistent = _lookupCategory(qid,true)&lt;br /&gt;
	end&lt;br /&gt;
	-- use wikidata if consistent&lt;br /&gt;
	if commonsLink and consistent then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..nsColon..commonsLink..&amp;quot;|&amp;quot;.._formatResult(commonsLink,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	-- if not consistent, fall back to search and add to tracking cat&lt;br /&gt;
	-- construct default result (which searches for title)&lt;br /&gt;
	local searchResult = &amp;quot;[[Commons:Special:Search/&amp;quot;..nsColon..(fallback or wp_title)&lt;br /&gt;
		..&amp;quot;|&amp;quot;.._formatResult(fallback or wp_title,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	if not consistent and wp_ns == &amp;quot;&amp;quot; then&lt;br /&gt;
		local friendlyNS&lt;br /&gt;
		if nsColon == &amp;quot;&amp;quot; then&lt;br /&gt;
			friendlyNS = &amp;quot;gallery&amp;quot;&lt;br /&gt;
		else&lt;br /&gt;
			friendlyNS = namespace:lower()&lt;br /&gt;
		end&lt;br /&gt;
		searchResult = searchResult..&amp;quot;[[Category:Inconsistent wikidata for Commons &amp;quot;..friendlyNS..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	return searchResult&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Returns &amp;quot;best&amp;quot; Commons link: first look for gallery, then try category&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   default = use as Commons link, don&amp;#039;t access wikidata&lt;br /&gt;
--   search = string to search for&lt;br /&gt;
--   fallback = string to search for if wikidata lookup fails&lt;br /&gt;
--   formatting = formatting parameters&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   formatted wikilink to Commons &amp;quot;best&amp;quot; landing page&lt;br /&gt;
function p._getGalleryOrCategory(default, search, fallback, formatting, qid)&lt;br /&gt;
	if default then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..default..&amp;quot;|&amp;quot;.._formatResult(default,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if search then&lt;br /&gt;
		return &amp;quot;[[Commons:Special:Search/&amp;quot;..search..&amp;quot;|&amp;quot;.._formatResult(search,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local wp_title, wp_ns&lt;br /&gt;
	wp_title, wp_ns, qid = _getTitleQID(qid)&lt;br /&gt;
	local trackingCats = &amp;quot;&amp;quot;&lt;br /&gt;
	local galleryLink, consistent, commonsSitelink = _lookupGallery(qid,true)&lt;br /&gt;
	-- use wikidata if either sitelink or P935 exist, and they both agree&lt;br /&gt;
	if galleryLink and consistent then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..galleryLink..&amp;quot;|&amp;quot;.._formatResult(galleryLink,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if not consistent and wp_ns == &amp;quot;&amp;quot; then&lt;br /&gt;
		trackingCats = &amp;quot;[[Category:Inconsistent wikidata for Commons gallery]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	-- if gallery is not good, fall back looking for category&lt;br /&gt;
	local categoryLink&lt;br /&gt;
	categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink)&lt;br /&gt;
	if categoryLink and consistent then&lt;br /&gt;
		return &amp;quot;[[Commons:Category:&amp;quot;..categoryLink..&amp;quot;|&amp;quot;.._formatResult(categoryLink,formatting)..&amp;quot;]]&amp;quot;..trackingCats&lt;br /&gt;
	end&lt;br /&gt;
	if not consistent and wp_ns == &amp;quot;&amp;quot; then&lt;br /&gt;
		trackingCats = trackingCats..&amp;quot;[[Category:Inconsistent wikidata for Commons category]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	-- return search result looking for title as last attempt&lt;br /&gt;
	return &amp;quot;[[Commons:Special:Search/&amp;quot; .. (fallback or wp_title) ..&lt;br /&gt;
		&amp;quot;|&amp;quot; .. _formatResult(fallback or wp_title,formatting) .. &amp;quot;]]&amp;quot; .. trackingCats&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Return link(s) Commons gallery, or category, or both from wikidata&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   defaultGallery = default gallery link to use, instead of wikidata&lt;br /&gt;
--   defaultCategory = default category link to use, instead of wikidata&lt;br /&gt;
--   categoryText = if both gallery and category, text to use in category link (&amp;quot;category&amp;quot; by default)&lt;br /&gt;
--   oneSearch = only emit one search result&lt;br /&gt;
--   formatting = formatting parameters&lt;br /&gt;
--   qid = qid of page to lookup in wikidata (testing only)&lt;br /&gt;
function p._getGalleryAndCategory(defaultGallery, defaultCategory, &lt;br /&gt;
	categoryText, oneSearch, formatting, qid&lt;br /&gt;
	)&lt;br /&gt;
	local wp_title, wp_ns&lt;br /&gt;
	wp_title, wp_ns, qid = _getTitleQID(qid)&lt;br /&gt;
	categoryText = categoryText or &amp;quot;category&amp;quot;&lt;br /&gt;
	local trackingCats = &amp;quot;&amp;quot;&lt;br /&gt;
	local galleryLink, galleryConsistent&lt;br /&gt;
	local commonsSitelink = nil&lt;br /&gt;
	if defaultGallery then&lt;br /&gt;
		galleryLink = defaultGallery&lt;br /&gt;
		galleryConsistent = true&lt;br /&gt;
	else&lt;br /&gt;
		galleryLink, galleryConsistent, commonsSitelink = _lookupGallery(qid,true)&lt;br /&gt;
	end&lt;br /&gt;
	local galleryGood = galleryLink and galleryConsistent&lt;br /&gt;
	if not galleryConsistent and wp_ns == &amp;quot;&amp;quot; then&lt;br /&gt;
		trackingCats = &amp;quot;[[Category:Inconsistent wikidata for Commons gallery]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local categoryLink, categoryConsistent&lt;br /&gt;
	if defaultCategory then&lt;br /&gt;
		categoryLink = defaultCategory&lt;br /&gt;
		categoryConsistent = true&lt;br /&gt;
	else&lt;br /&gt;
		categoryLink, categoryConsistent = _lookupCategory(qid,defaultGallery,commonsSitelink)&lt;br /&gt;
	end&lt;br /&gt;
	local categoryGood = categoryLink and categoryConsistent&lt;br /&gt;
	if not categoryConsistent and wp_ns == &amp;quot;&amp;quot; then&lt;br /&gt;
		trackingCats = trackingCats..&amp;quot;[[Category:Inconsistent wikidata for Commons category]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local firstLink&lt;br /&gt;
	-- construct default result (which searches for title)&lt;br /&gt;
	local searchResult = &amp;quot;[[Commons:Special:Search/&amp;quot;..wp_title..&amp;quot;|&amp;quot;.._formatResult(wp_title,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	if not oneSearch then&lt;br /&gt;
		searchResult = searchResult..&amp;quot; ([[Commons:Special:Search/Category:&amp;quot;..wp_title..&amp;quot;|&amp;quot;..categoryText..&amp;quot;]])&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local linkText = nil&lt;br /&gt;
	if galleryGood then&lt;br /&gt;
		firstLink = galleryLink&lt;br /&gt;
		linkText = galleryLink&lt;br /&gt;
	elseif categoryGood then&lt;br /&gt;
		firstLink = &amp;quot;Category:&amp;quot;..categoryLink&lt;br /&gt;
		linkText = categoryLink&lt;br /&gt;
	else&lt;br /&gt;
		return searchResult..trackingCats&lt;br /&gt;
	end&lt;br /&gt;
	local resultVal = &amp;quot;[[Commons:&amp;quot;..firstLink..&amp;quot;|&amp;quot;.._formatResult(linkText,formatting)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	if galleryGood and categoryGood then&lt;br /&gt;
		resultVal = resultVal..&amp;quot; ([[Commons:Category:&amp;quot;..categoryLink..&amp;quot;|&amp;quot;..categoryText..&amp;quot;]])&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	return resultVal..trackingCats&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Compare two titles with their namespaces stripped&lt;br /&gt;
local function titleMatch(s1,s2)&lt;br /&gt;
	s1 = s1 or &amp;quot;&amp;quot;&lt;br /&gt;
	s2 = s2 or &amp;quot;&amp;quot;&lt;br /&gt;
    s1 = mw.ustring.gsub(s1,&amp;quot;^[^:]+:&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
    s2 = mw.ustring.gsub(s2,&amp;quot;^[^:]+:&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
    return s1 == s2&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local galleryTrackingCats = {&lt;br /&gt;
	commons_link_on_wikidata = &amp;#039;[[Category:Commons link is on Wikidata]]&amp;#039;,&lt;br /&gt;
	commons_link_defined_as_pagename = &amp;#039;[[Category:Commons link is defined as the pagename]]&amp;#039;,&lt;br /&gt;
	commons_link_locally_defined = &amp;#039;[[Category:Commons link is locally defined]]&amp;#039;,&lt;br /&gt;
	commons_link_from_wikidata = &amp;#039;[[Category:Commons link from Wikidata]]&amp;#039;,&lt;br /&gt;
	commons_link_is_pagename = &amp;#039;[[Category:Commons link is the pagename]]&amp;#039;,&lt;br /&gt;
	inconsistent = &amp;#039;[[Category:Inconsistent wikidata for Commons gallery]]&amp;#039;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
local categoryTrackingCats = {&lt;br /&gt;
		commons_link_on_wikidata = &amp;#039;[[Category:Commons category link is on Wikidata]]&amp;#039;,&lt;br /&gt;
		commons_link_defined_as_pagename = &amp;#039;[[Category:Commons category link is defined as the pagename]]&amp;#039;,&lt;br /&gt;
		commons_link_locally_defined = &amp;#039;[[Category:Commons category link is locally defined]]&amp;#039;,&lt;br /&gt;
		commons_link_from_wikidata = &amp;#039;[[Category:Commons category link from Wikidata]]&amp;#039;,&lt;br /&gt;
		commons_link_is_pagename = &amp;#039;[[Category:Commons category link is the pagename]]&amp;#039;,&lt;br /&gt;
		inconsistent = &amp;#039;[[Category:Inconsistent wikidata for Commons category]]&amp;#039;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
local function selectTrackingCat(trackingCats,wikidata,consistent,default,title)&lt;br /&gt;
	if not consistent then&lt;br /&gt;
		return trackingCats.inconsistent&lt;br /&gt;
	end&lt;br /&gt;
	if default then&lt;br /&gt;
	-- construct warning message&lt;br /&gt;
		if default == wikidata then&lt;br /&gt;
			return trackingCats.commons_link_on_wikidata&lt;br /&gt;
		end&lt;br /&gt;
		local warning = &amp;quot;&amp;quot;&lt;br /&gt;
		if wikidata then&lt;br /&gt;
			local generateWarning = require(&amp;#039;Module:If preview&amp;#039;)._warning&lt;br /&gt;
			warning = generateWarning({&lt;br /&gt;
					&amp;quot;Commons link does not match Wikidata – [[Template:Commons_category#Resolving_discrepancies|please check]]&amp;quot;&lt;br /&gt;
				})&lt;br /&gt;
		end&lt;br /&gt;
		if titleMatch(default,title) then&lt;br /&gt;
			return trackingCats.commons_link_defined_as_pagename .. warning&lt;br /&gt;
		end&lt;br /&gt;
		return trackingCats.commons_link_locally_defined .. warning&lt;br /&gt;
	end&lt;br /&gt;
	if wikidata then&lt;br /&gt;
		return trackingCats.commons_link_from_wikidata&lt;br /&gt;
	end&lt;br /&gt;
	return trackingCats.commons_link_is_pagename&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Figure out tracking categories and editor warnings&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   default = Commons link argument passed to template&lt;br /&gt;
--   fetchGallery = whether to fetch a gallery from Wikidata&lt;br /&gt;
--   fetchCategory = whether to fetch a category from Wikidata&lt;br /&gt;
--   qid = force a qid for testing&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   tracking category and possible user warning&lt;br /&gt;
--&lt;br /&gt;
-- Note: the logic for the tracking is quite different than the logic&lt;br /&gt;
-- for generating Commons links (above). Thus, it is separated into another&lt;br /&gt;
-- function for code clarity and maintainability. This should not seriously &lt;br /&gt;
-- affect performance: server time is dominated by fetching wikidata entities,&lt;br /&gt;
-- and those entities should be cached and shared between the Commons generating&lt;br /&gt;
-- code and this tracking code.&lt;br /&gt;
function p._tracking(default, fetchGallery, fetchCategory, qid)&lt;br /&gt;
	local title, wp_ns, wp_qid = _getTitleQID(qid,true)&lt;br /&gt;
	if wp_ns ~= &amp;quot;&amp;quot; then&lt;br /&gt;
		title = wp_ns..&amp;quot;:&amp;quot;..title&lt;br /&gt;
	end&lt;br /&gt;
	-- only track if test or namespace=article or namespace=category&lt;br /&gt;
	if not (qid or wp_ns == &amp;quot;&amp;quot; or wp_ns == &amp;quot;Category&amp;quot;) then&lt;br /&gt;
		return &amp;quot;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- determine title and namespace of wikidata and wp article&lt;br /&gt;
	local wikidata = nil&lt;br /&gt;
	local consistent = nil&lt;br /&gt;
	-- Tracking code works for all 4 cases of states of fetchGallery/Category&lt;br /&gt;
	-- fetchGallery takes precedence&lt;br /&gt;
	if fetchGallery then&lt;br /&gt;
		wikidata, consistent = p._hasGalleryConsistent(qid)&lt;br /&gt;
		if default or not fetchCategory or (consistent and wikidata) then&lt;br /&gt;
			return selectTrackingCat(galleryTrackingCats,wikidata,consistent,&lt;br /&gt;
				                     default,title)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
    if fetchCategory then&lt;br /&gt;
		local cat_wikidata, cat_consistent = p._hasCategoryConsistent(qid,true)&lt;br /&gt;
		if not fetchGallery or (cat_consistent and cat_wikidata) then&lt;br /&gt;
			return selectTrackingCat(categoryTrackingCats,cat_wikidata,&lt;br /&gt;
			                    	 cat_consistent,default,title)&lt;br /&gt;
		end&lt;br /&gt;
		return selectTrackingCat(galleryTrackingCats,wikidata,consistent,&lt;br /&gt;
			                     default,title)&lt;br /&gt;
    end&lt;br /&gt;
	return &amp;quot;&amp;quot; -- nothing fetched, nothing tracked&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function _createFormatting(args)&lt;br /&gt;
	local formatting = {}&lt;br /&gt;
	formatting.linktext = args.linktext&lt;br /&gt;
    local yesNo = require(&amp;#039;Module:Yesno&amp;#039;)&lt;br /&gt;
	formatting.lcfirst = yesNo(args.lcfirst)&lt;br /&gt;
	formatting.bold = yesNo(args.bold)&lt;br /&gt;
	formatting.italic = yesNo(args.italic)&lt;br /&gt;
	formatting.nowrap = yesNo(args.nowrap)&lt;br /&gt;
	return formatting&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Testing-only entry point for _getTitleQID&lt;br /&gt;
function p.getTitleQID(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	local text, ns, qid = _getTitleQID(args[1],args[2])&lt;br /&gt;
	return text..&amp;quot;,&amp;quot;..ns..&amp;quot;,&amp;quot;..(qid or &amp;quot;nil&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Testing-only entry point for _lookupFallback&lt;br /&gt;
function p.lookupFallback(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	local fallback = _lookupFallback(args[1],args[2])&lt;br /&gt;
	return fallback or &amp;quot;nil&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find the Commons gallery page associated with article&lt;br /&gt;
function p.getGallery(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._getCommons(&amp;quot;&amp;quot;,args[1],args.search,args.fallback,_createFormatting(args),args.qid)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find the Commons category page associated with article&lt;br /&gt;
function p.getCategory(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	local retval = p._getCommons(&amp;quot;Category&amp;quot;, args[1], &lt;br /&gt;
		args.search, args.fallback, _createFormatting(args), args.qid&lt;br /&gt;
	)&lt;br /&gt;
	if args.tracking then&lt;br /&gt;
		local default = nil&lt;br /&gt;
		if args[1] then&lt;br /&gt;
			default = &amp;quot;Category:&amp;quot;..args[1]&lt;br /&gt;
		end&lt;br /&gt;
		retval = retval..p._tracking(default, false, true, args.qid)&lt;br /&gt;
	end&lt;br /&gt;
	return retval&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.getGalleryOrCategory(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	local retval = p._getGalleryOrCategory(&lt;br /&gt;
		args[1], args.search, args.fallback, _createFormatting(args), args.qid&lt;br /&gt;
	)&lt;br /&gt;
	if args.tracking then&lt;br /&gt;
		retval = retval..p._tracking(args[1],true,true,args.qid)&lt;br /&gt;
	end&lt;br /&gt;
	return retval&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hasGallery(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._hasGallery(args.qid) or &amp;quot;&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hasCategory(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._hasCategory(args.qid) or &amp;quot;&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.hasGalleryOrCategory(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._hasGallery(args.qid) or p._hasCategory(args.qid) or &amp;quot;&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.getGalleryAndCategory(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._getGalleryAndCategory(args[1], args[2], &lt;br /&gt;
		args.categoryText, args.oneSearch, _createFormatting(args), args.qid)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.tracking(frame)&lt;br /&gt;
	local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})&lt;br /&gt;
	return p._tracking(args[1], args.fetchGallery, args.fetchCategory, args.qid)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>wikipedia&gt;Hike395</name></author>
	</entry>
</feed>