<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FHike395%2FCommonsLink2</id>
	<title>Module:Sandbox/Hike395/CommonsLink2 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FHike395%2FCommonsLink2"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Hike395/CommonsLink2&amp;action=history"/>
	<updated>2026-07-29T11:16:12Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Hike395/CommonsLink2&amp;diff=145519&amp;oldid=prev</id>
		<title>imported&gt;Hike395: use stripped title for search string</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Hike395/CommonsLink2&amp;diff=145519&amp;oldid=prev"/>
		<updated>2020-02-21T09:09:49Z</updated>

		<summary type="html">&lt;p&gt;use stripped title for search string&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&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;
local function _getTitleQID(qid)&lt;br /&gt;
	local titleObject = mw.title.getCurrentTitle()&lt;br /&gt;
	qid = (qid or &amp;quot;&amp;quot;):upper()&lt;br /&gt;
	-- look up qid for current page (if not testing)&lt;br /&gt;
	if qid == &amp;quot;&amp;quot; then&lt;br /&gt;
		qid = mw.wikibase.getEntityIdForCurrentPage()&lt;br /&gt;
		return titleObject.text, titleObject.namespace, qid&lt;br /&gt;
	end&lt;br /&gt;
	-- testing-only path: given a qid, determine title&lt;br /&gt;
	-- use namespace from current page (to suppress tracking cat)&lt;br /&gt;
	local title = mw.wikibase.sitelink(qid)&lt;br /&gt;
	-- string any namespace from sitelink&lt;br /&gt;
	local firstColon = mw.ustring.find(title,&amp;#039;:&amp;#039;,1,true)&lt;br /&gt;
	if firstColon then&lt;br /&gt;
		title = mw.ustring.sub(title,firstColon+1)&lt;br /&gt;
	end&lt;br /&gt;
	return title, titleObject.namespace, 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;
	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 qid then&lt;br /&gt;
		return nil&lt;br /&gt;
	end&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;
	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;
	-- P373 is the &amp;quot;commons category&amp;quot; property for this article&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;
		P373 = P373.mainsnak.datavalue.value&lt;br /&gt;
		if categoryLink and categoryLink ~= P373 then&lt;br /&gt;
			consistent = false&lt;br /&gt;
			qid = nil  -- stop searching on inconsistent data&lt;br /&gt;
		else&lt;br /&gt;
			categoryLink = P373&lt;br /&gt;
		end&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;
		else&lt;br /&gt;
			categoryLink = fallback&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;
&lt;br /&gt;
-- Create Commons gallery link corresponding to current article&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   default = use as Commons link, don&amp;#039;t access wikidata&lt;br /&gt;
--   linktext = text to display in link&lt;br /&gt;
--   search = string to search for&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   formatted wikilink to Commons gallery&lt;br /&gt;
function p._getGallery(default,linktext,search,qid)&lt;br /&gt;
	if default then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..default..&amp;quot;|&amp;quot;..(linktext or default)..&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;..(linktext or search)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local title, ns&lt;br /&gt;
	title, ns, qid = _getTitleQID(qid)&lt;br /&gt;
	-- construct default result (which searches for title)&lt;br /&gt;
	local searchResult = &amp;quot;[[Commons:Special:Search/&amp;quot;..title..&amp;quot;|&amp;quot;..(linktext or title)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	if qid then&lt;br /&gt;
		local galleryLink, consistent = _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;..(linktext or galleryLink)..&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;
		if not consistent and ns == 0 then&lt;br /&gt;
			searchResult = searchResult..&amp;quot;[[Category:Inconsistent wikidata for Commons gallery]]&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return searchResult&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Create Commons category link corresponding to current article&lt;br /&gt;
-- Arguments:&lt;br /&gt;
--   default = use as Commons category link, don&amp;#039;t access wikidata&lt;br /&gt;
--   linktext = text to display in link&lt;br /&gt;
--   search = string to search for&lt;br /&gt;
--   qid = QID to lookup in wikidata (for testing only)&lt;br /&gt;
-- Returns:&lt;br /&gt;
--   formatted wikilink to Commons gallery&lt;br /&gt;
function p._getCategory(default,linktext,search,qid)&lt;br /&gt;
	if default then&lt;br /&gt;
		return &amp;quot;[[Commons:Category:&amp;quot;..default..&amp;quot;|&amp;quot;..(linktext or default)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if search then&lt;br /&gt;
		return &amp;quot;[[Commons:Special:Search/Category:&amp;quot;..search..&amp;quot;|&amp;quot;..(linktext or search)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
    local title, ns&lt;br /&gt;
    title, ns, qid = _getTitleQID(qid)&lt;br /&gt;
	-- construct default result (which searches for title in Category space)&lt;br /&gt;
	local searchResult = &amp;quot;[[Commons:Special:Search/Category:&amp;quot;..title..&amp;quot;|&amp;quot;..(linktext or title)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	if qid then&lt;br /&gt;
		local categoryLink, consistent = _lookupCategory(qid,true)&lt;br /&gt;
		-- if any of sitelink category, P373, and P910/P1754 commons sitelinks exist,&lt;br /&gt;
		-- use it. But don&amp;#039;t use if they don&amp;#039;t agree with each other&lt;br /&gt;
		if categoryLink and consistent then&lt;br /&gt;
			return &amp;quot;[[Commons:Category:&amp;quot;..categoryLink..&amp;quot;|&amp;quot;..(linktext or categoryLink)..&amp;quot;]]&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
		-- if not consistent, fall back to search, but add tracking category&lt;br /&gt;
		if not consistent and ns == 0 then&lt;br /&gt;
			searchResult = searchResult..&amp;quot;[[Category:Inconsistent wikidata for Commons category]]&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return searchResult&lt;br /&gt;
end&lt;br /&gt;
&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;
--   linktext = text to display in link&lt;br /&gt;
--   search = string to search for&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,linktext,search,qid)&lt;br /&gt;
	if default then&lt;br /&gt;
		return &amp;quot;[[Commons:&amp;quot;..default..&amp;quot;|&amp;quot;..(linktext or default)..&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;..(linktext or search)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local title, ns&lt;br /&gt;
	title, ns, qid = _getTitleQID(qid)&lt;br /&gt;
	-- construct default result (which searches for title)&lt;br /&gt;
	local searchResult = &amp;quot;[[Commons:Special:Search/&amp;quot;..title..&amp;quot;|&amp;quot;..(linktext or title)..&amp;quot;]]&amp;quot;&lt;br /&gt;
	local trackingCats = &amp;quot;&amp;quot;&lt;br /&gt;
	if qid then&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;..(linktext or galleryLink)..&amp;quot;]]&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
		if not consistent and ns == 0 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;..(linktext or categoryLink)..&amp;quot;]]&amp;quot;..trackingCats&lt;br /&gt;
		end&lt;br /&gt;
		if not consistent and ns == 0 then&lt;br /&gt;
			trackingCats = trackingCats..&amp;quot;[[Category:Inconsistent wikidata for Commons category]]&amp;quot;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return searchResult..trackingCats&lt;br /&gt;
end&lt;br /&gt;
&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])&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._getGallery(args[1],args.linktext,args.search,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;
	return p._getCategory(args[1],args.linktext,args.search,args.qid)&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;
	return p._getGalleryOrCategory(args[1],args.linktext,args.search,args.qid)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Hike395</name></author>
	</entry>
</feed>