Module:Commons link: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Hike395 (typo in comment) |
meta>Hike395 (refactor code, add getGalleryOrCategory, add test entry points, add doc strings) |
||
Line 3: | Line 3: | ||
local p = {} | local p = {} | ||
-- | local function _getTitleQID(qid) | ||
function | local titleObject = mw.title.getCurrentTitle() | ||
local | qid = (qid or ""):upper() | ||
return | -- look up qid for current page (if not testing) | ||
if qid == "" then | |||
qid = mw.wikibase.getEntityIdForCurrentPage() | |||
return titleObject.text, titleObject.namespace, qid | |||
end | |||
-- testing-only path: given a qid, determine title | |||
-- use namespace from current page (to suppress tracking cat) | |||
local title = mw.wikibase.sitelink(qid) | |||
-- string any namespace from sitelink | |||
local firstColon = mw.ustring.find(title,':',1,true) | |||
if firstColon then | |||
title = mw.ustring.sub(title,firstColon+1) | |||
end | |||
return title, titleObject.namespace, qid | |||
end | |||
-- Lookup Commons gallery in Wikidata | |||
-- Arguments: | |||
-- qid = QID of current article | |||
-- fetch = whether to lookup Commons sitelink (bool) | |||
-- commonsSitelink = default value for Commons sitelink | |||
-- Returns: | |||
-- categoryLink = name of Commons category, nil if nothing is found | |||
-- consistent = multiple wikidata fields are examined: are they consistent? | |||
-- commonsSitelink = commons sitelink for current article | |||
local function _lookupGallery(qid,fetch,commonsSitelink) | |||
local galleryLink = nil | |||
local consistent = true | |||
-- look up commons sitelink for article, use if not category | |||
if fetch then | |||
commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki") or commonsSitelink | |||
end | |||
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) ~= "Category:" then | |||
galleryLink = commonsSitelink | |||
end | |||
-- P935 is the "commons gallery" property for this article | |||
local P935 = mw.wikibase.getBestStatements(qid, "P935")[1] | |||
if P935 and P935.mainsnak.datavalue then | |||
local gallery = P935.mainsnak.datavalue.value | |||
if galleryLink and galleryLink ~= gallery then | |||
consistent = false | |||
else | |||
galleryLink = gallery | |||
end | |||
end | |||
return galleryLink, consistent, commonsSitelink | |||
end | |||
-- Find fallback category by looking up Commons sitelink of different page | |||
-- Arguments: | |||
-- qid = QID for current article | |||
-- property = property that refers to other article whose sitelink to return | |||
-- Returns: either category-stripped name of article, or nil | |||
local function _lookupFallback(qid,property) | |||
if not qid then | |||
return nil | |||
end | |||
-- If property exists on current article, get value (other article qid) | |||
local value = mw.wikibase.getBestStatements(qid, property)[1] | |||
if value and value.mainsnak.datavalue and value.mainsnak.datavalue.value.id then | |||
-- Look up Commons sitelink of other article | |||
local sitelink = mw.wikibase.getSitelink(value.mainsnak.datavalue.value.id,"commonswiki") | |||
-- Check to see if it starts with "Category:". If so, strip it and return | |||
if sitelink and mw.ustring.sub(sitelink,1,9) == "Category:" then | |||
return mw.ustring.sub(sitelink,10) | |||
end | |||
end | |||
return nil | |||
end | |||
-- Find Commons category by looking in wikidata | |||
-- Arguments: | |||
-- qid = QID of current article | |||
-- fetch = whether to lookup Commons sitelink (bool) | |||
-- commonsSitelink = default value for Commons sitelink | |||
-- Returns: | |||
-- categoryLink = name of Commons category, nil if nothing is found | |||
-- consistent = multiple wikidata fields are examined: are they consistent? | |||
-- commonsSitelink = commons sitelink for current article | |||
local function _lookupCategory(qid, fetch, commonsSitelink) | |||
local categoryLink = nil | |||
local consistent = true | |||
-- look up commons sitelink for article, use if starts with "Category:" | |||
if fetch then | |||
commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki") or commonsSitelink | |||
end | |||
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) == "Category:" then | |||
categoryLink = mw.ustring.sub(commonsSitelink,10) | |||
end | |||
-- P373 is the "commons category" property for this article | |||
local P373 = mw.wikibase.getBestStatements(qid, "P373")[1] | |||
if P373 and P373.mainsnak.datavalue then | |||
P373 = P373.mainsnak.datavalue.value | |||
if categoryLink and categoryLink ~= P373 then | |||
consistent = false | |||
qid = nil -- stop searching on inconsistent data | |||
else | |||
categoryLink = P373 | |||
end | |||
end | |||
-- P910 is the "topic's main category". Look for commons sitelink there | |||
local fallback = _lookupFallback(qid,"P910") | |||
if fallback then | |||
if categoryLink and categoryLink ~= fallback then | |||
consistent = false | |||
qid = nil | |||
else | |||
categoryLink = fallback | |||
end | |||
end | |||
-- P1754 is the "list's main category". Look for commons sitelink there | |||
fallback = _lookupFallback(qid,"P1754") | |||
if fallback then | |||
if categoryLink and categoryLink ~= fallback then | |||
consistent = false | |||
else | |||
categoryLink = fallback | |||
end | |||
end | |||
return categoryLink, consistent, commonsSitelink | |||
end | end | ||
-- Create Commons gallery link corresponding to current article | |||
-- Arguments: | -- Arguments: | ||
-- default = use as Commons link, don't access wikidata | -- default = use as Commons link, don't access wikidata | ||
Line 14: | Line 135: | ||
-- search = string to search for | -- search = string to search for | ||
-- qid = QID to lookup in wikidata (for testing only) | -- qid = QID to lookup in wikidata (for testing only) | ||
-- Returns: | |||
-- formatted wikilink to Commons gallery | |||
function p._getGallery(default,linktext,search,qid) | function p._getGallery(default,linktext,search,qid) | ||
if default then | if default then | ||
Line 21: | Line 144: | ||
return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" | return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" | ||
end | end | ||
local | local title, ns | ||
title, ns, qid = _getTitleQID(qid) | |||
-- construct default result (which searches for title) | -- construct default result (which searches for title) | ||
local searchResult = "[[Commons:Special:Search/"..title.."|"..(linktext or title).."]]" | local searchResult = "[[Commons:Special:Search/"..title.."|"..(linktext or title).."]]" | ||
if qid then | if qid then | ||
local galleryLink | local galleryLink, consistent = _lookupGallery(qid,true) | ||
-- use wikidata if either sitelink or P935 exist, and they both agree | -- use wikidata if either sitelink or P935 exist, and they both agree | ||
if galleryLink and consistent then | if galleryLink and consistent then | ||
Line 56: | Line 155: | ||
end | end | ||
-- if not consistent, fall back to search and add to tracking cat | -- if not consistent, fall back to search and add to tracking cat | ||
if not consistent and | if not consistent and ns == 0 then | ||
searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons gallery]]" | searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons gallery]]" | ||
end | end | ||
Line 63: | Line 162: | ||
end | end | ||
-- | -- Create Commons category link corresponding to current article | ||
-- Arguments: | -- Arguments: | ||
-- default = use as Commons | -- default = use as Commons category link, don't access wikidata | ||
-- linktext = text to display in link | -- linktext = text to display in link | ||
-- search = string to search for | -- search = string to search for | ||
-- qid = QID to lookup in wikidata (for testing only) | -- qid = QID to lookup in wikidata (for testing only) | ||
-- Returns: | |||
-- formatted wikilink to Commons gallery | |||
function p._getCategory(default,linktext,search,qid) | function p._getCategory(default,linktext,search,qid) | ||
if default then | if default then | ||
Line 81: | Line 177: | ||
return "[[Commons:Special:Search/Category:"..search.."|"..(linktext or search).."]]" | return "[[Commons:Special:Search/Category:"..search.."|"..(linktext or search).."]]" | ||
end | end | ||
local title, ns | |||
title, ns, qid = _getTitleQID(qid) | |||
-- construct default result (which searches for title in Category space) | -- construct default result (which searches for title in Category space) | ||
local searchResult = "[[Commons:Special:Search/Category:"..title.."|"..(linktext or title).."]]" | local searchResult = "[[Commons:Special:Search/Category:"..title.."|"..(linktext or title).."]]" | ||
if qid then | if qid then | ||
local categoryLink | local categoryLink, consistent = _lookupCategory(qid,true) | ||
-- if any of sitelink category, P373, and P910/P1754 commons sitelinks exist, | |||
-- if any of sitelink category, P373, and P910 commons | |||
-- use it. But don't use if they don't agree with each other | -- use it. But don't use if they don't agree with each other | ||
if categoryLink and consistent then | if categoryLink and consistent then | ||
Line 131: | Line 189: | ||
end | end | ||
-- if not consistent, fall back to search, but add tracking category | -- if not consistent, fall back to search, but add tracking category | ||
if not consistent and | if not consistent and ns == 0 then | ||
searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons category]]" | searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons category]]" | ||
end | end | ||
Line 137: | Line 195: | ||
return searchResult | return searchResult | ||
end | end | ||
-- Returns "best" Commons link: first look for gallery, then try category | |||
-- Arguments: | |||
-- default = use as Commons link, don't access wikidata | |||
-- linktext = text to display in link | |||
-- search = string to search for | |||
-- qid = QID to lookup in wikidata (for testing only) | |||
-- Returns: | |||
-- formatted wikilink to Commons "best" landing page | |||
function p._getGalleryOrCategory(default,linktext,search,qid) | |||
if default then | |||
return "[[Commons:"..default.."|"..(linktext or default).."]]" | |||
end | |||
if search then | |||
return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" | |||
end | |||
local title, ns | |||
title, ns, qid = _getTitleQID(qid) | |||
-- construct default result (which searches for title) | |||
local searchResult = "[[Commons:Special:Search/"..title.."|"..(linktext or title).."]]" | |||
local trackingCats = "" | |||
if qid then | |||
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 "[[Commons:"..galleryLink.."|"..(linktext or galleryLink).."]]" | |||
end | |||
if not consistent and ns == 0 then | |||
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" | |||
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 "[[Commons:Category:"..categoryLink.."|"..(linktext or categoryLink).."]]"..trackingCats | |||
end | |||
if not consistent and ns == 0 then | |||
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | |||
end | |||
end | |||
return searchResult..trackingCats | |||
end | |||
-- Testing-only entry point for _getTitleQID | |||
function p.getTitleQID(frame) | |||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | |||
local text, ns, qid = _getTitleQID(args[1]) | |||
return text..","..ns..","..(qid or "nil") | |||
end | |||
-- Testing-only entry point for _lookupFallback | |||
function p.lookupFallback(frame) | |||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | |||
local fallback = _lookupFallback(args[1],args[2]) | |||
return fallback or "nil" | |||
end | |||
-- Find the Commons gallery page associated with article | |||
function p.getGallery(frame) | |||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | |||
return p._getGallery(args[1],args.linktext,args.search,args.qid) | |||
end | |||
-- Find the Commons category page associated with article | |||
function p.getCategory(frame) | |||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | |||
return p._getCategory(args[1],args.linktext,args.search,args.qid) | |||
end | |||
function p.getGalleryOrCategory(frame) | |||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | |||
return p._getGalleryOrCategory(args[1],args.linktext,args.search,args.qid) | |||
end | |||
return p | return p |
Revision as of 09:28, 21 February 2020
Documentation for this module may be created at Module:Commons link/doc
-- Module to find commons galleries and categories based on wikidata entries local getArgs = require('Module:Arguments').getArgs local p = {} local function _getTitleQID(qid) local titleObject = mw.title.getCurrentTitle() qid = (qid or ""):upper() -- look up qid for current page (if not testing) if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() return titleObject.text, titleObject.namespace, qid end -- testing-only path: given a qid, determine title -- use namespace from current page (to suppress tracking cat) local title = mw.wikibase.sitelink(qid) -- string any namespace from sitelink local firstColon = mw.ustring.find(title,':',1,true) if firstColon then title = mw.ustring.sub(title,firstColon+1) end return title, titleObject.namespace, qid end -- Lookup Commons gallery in Wikidata -- Arguments: -- qid = QID of current article -- fetch = whether to lookup Commons sitelink (bool) -- commonsSitelink = default value for Commons sitelink -- Returns: -- categoryLink = name of Commons category, nil if nothing is found -- consistent = multiple wikidata fields are examined: are they consistent? -- commonsSitelink = commons sitelink for current article local function _lookupGallery(qid,fetch,commonsSitelink) local galleryLink = nil local consistent = true -- look up commons sitelink for article, use if not category if fetch then commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki") or commonsSitelink end if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) ~= "Category:" then galleryLink = commonsSitelink end -- P935 is the "commons gallery" property for this article local P935 = mw.wikibase.getBestStatements(qid, "P935")[1] if P935 and P935.mainsnak.datavalue then local gallery = P935.mainsnak.datavalue.value if galleryLink and galleryLink ~= gallery then consistent = false else galleryLink = gallery end end return galleryLink, consistent, commonsSitelink end -- Find fallback category by looking up Commons sitelink of different page -- Arguments: -- qid = QID for current article -- property = property that refers to other article whose sitelink to return -- Returns: either category-stripped name of article, or nil local function _lookupFallback(qid,property) if not qid then return nil end -- If property exists on current article, get value (other article qid) local value = mw.wikibase.getBestStatements(qid, property)[1] if value and value.mainsnak.datavalue and value.mainsnak.datavalue.value.id then -- Look up Commons sitelink of other article local sitelink = mw.wikibase.getSitelink(value.mainsnak.datavalue.value.id,"commonswiki") -- Check to see if it starts with "Category:". If so, strip it and return if sitelink and mw.ustring.sub(sitelink,1,9) == "Category:" then return mw.ustring.sub(sitelink,10) end end return nil end -- Find Commons category by looking in wikidata -- Arguments: -- qid = QID of current article -- fetch = whether to lookup Commons sitelink (bool) -- commonsSitelink = default value for Commons sitelink -- Returns: -- categoryLink = name of Commons category, nil if nothing is found -- consistent = multiple wikidata fields are examined: are they consistent? -- commonsSitelink = commons sitelink for current article local function _lookupCategory(qid, fetch, commonsSitelink) local categoryLink = nil local consistent = true -- look up commons sitelink for article, use if starts with "Category:" if fetch then commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki") or commonsSitelink end if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) == "Category:" then categoryLink = mw.ustring.sub(commonsSitelink,10) end -- P373 is the "commons category" property for this article local P373 = mw.wikibase.getBestStatements(qid, "P373")[1] if P373 and P373.mainsnak.datavalue then P373 = P373.mainsnak.datavalue.value if categoryLink and categoryLink ~= P373 then consistent = false qid = nil -- stop searching on inconsistent data else categoryLink = P373 end end -- P910 is the "topic's main category". Look for commons sitelink there local fallback = _lookupFallback(qid,"P910") if fallback then if categoryLink and categoryLink ~= fallback then consistent = false qid = nil else categoryLink = fallback end end -- P1754 is the "list's main category". Look for commons sitelink there fallback = _lookupFallback(qid,"P1754") if fallback then if categoryLink and categoryLink ~= fallback then consistent = false else categoryLink = fallback end end return categoryLink, consistent, commonsSitelink end -- Create Commons gallery link corresponding to current article -- Arguments: -- default = use as Commons link, don't access wikidata -- linktext = text to display in link -- search = string to search for -- qid = QID to lookup in wikidata (for testing only) -- Returns: -- formatted wikilink to Commons gallery function p._getGallery(default,linktext,search,qid) if default then return "[[Commons:"..default.."|"..(linktext or default).."]]" end if search then return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" end local title, ns title, ns, qid = _getTitleQID(qid) -- construct default result (which searches for title) local searchResult = "[[Commons:Special:Search/"..title.."|"..(linktext or title).."]]" if qid then local galleryLink, consistent = _lookupGallery(qid,true) -- use wikidata if either sitelink or P935 exist, and they both agree if galleryLink and consistent then return "[[Commons:"..galleryLink.."|"..(linktext or galleryLink).."]]" end -- if not consistent, fall back to search and add to tracking cat if not consistent and ns == 0 then searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons gallery]]" end end return searchResult end -- Create Commons category link corresponding to current article -- Arguments: -- default = use as Commons category link, don't access wikidata -- linktext = text to display in link -- search = string to search for -- qid = QID to lookup in wikidata (for testing only) -- Returns: -- formatted wikilink to Commons gallery function p._getCategory(default,linktext,search,qid) if default then return "[[Commons:Category:"..default.."|"..(linktext or default).."]]" end if search then return "[[Commons:Special:Search/Category:"..search.."|"..(linktext or search).."]]" end local title, ns title, ns, qid = _getTitleQID(qid) -- construct default result (which searches for title in Category space) local searchResult = "[[Commons:Special:Search/Category:"..title.."|"..(linktext or title).."]]" if qid then local categoryLink, consistent = _lookupCategory(qid,true) -- if any of sitelink category, P373, and P910/P1754 commons sitelinks exist, -- use it. But don't use if they don't agree with each other if categoryLink and consistent then return "[[Commons:Category:"..categoryLink.."|"..(linktext or categoryLink).."]]" end -- if not consistent, fall back to search, but add tracking category if not consistent and ns == 0 then searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons category]]" end 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 -- linktext = text to display in link -- search = string to search for -- qid = QID to lookup in wikidata (for testing only) -- Returns: -- formatted wikilink to Commons "best" landing page function p._getGalleryOrCategory(default,linktext,search,qid) if default then return "[[Commons:"..default.."|"..(linktext or default).."]]" end if search then return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" end local title, ns title, ns, qid = _getTitleQID(qid) -- construct default result (which searches for title) local searchResult = "[[Commons:Special:Search/"..title.."|"..(linktext or title).."]]" local trackingCats = "" if qid then 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 "[[Commons:"..galleryLink.."|"..(linktext or galleryLink).."]]" end if not consistent and ns == 0 then trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" 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 "[[Commons:Category:"..categoryLink.."|"..(linktext or categoryLink).."]]"..trackingCats end if not consistent and ns == 0 then trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" end end return searchResult..trackingCats end -- Testing-only entry point for _getTitleQID function p.getTitleQID(frame) local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) local text, ns, qid = _getTitleQID(args[1]) return text..","..ns..","..(qid or "nil") end -- Testing-only entry point for _lookupFallback function p.lookupFallback(frame) local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) local fallback = _lookupFallback(args[1],args[2]) return fallback or "nil" end -- Find the Commons gallery page associated with article function p.getGallery(frame) local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) return p._getGallery(args[1],args.linktext,args.search,args.qid) end -- Find the Commons category page associated with article function p.getCategory(frame) local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) return p._getCategory(args[1],args.linktext,args.search,args.qid) end function p.getGalleryOrCategory(frame) local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) return p._getGalleryOrCategory(args[1],args.linktext,args.search,args.qid) end return p