Jump to content

Module:Commons link: Difference between revisions

no edit summary
(Created page with "-- Module to find commons galleries and categories based on wikidata entries local getArgs = require('Module:Arguments').getArgs local p = {} -- Check if string is a valid QI...")
 
No edit summary
Line 45: Line 45:
local nsText = mw.ustring.gsub(titleObject.nsText,"_"," ")
local nsText = mw.ustring.gsub(titleObject.nsText,"_"," ")
if not _validQID(qid) then
if not _validQID(qid) then
qid = mw.wikibase.getEntityIdForCurrentPage()
return titleObject.text, nsText, qid
return titleObject.text, nsText, qid
end
end
Line 51: Line 50:
-- always use namespace from current page (to suppress tracking cat)
-- always use namespace from current page (to suppress tracking cat)
qid = qid:upper()
qid = qid:upper()
local title = mw.wikibase.getSitelink(qid) or ""
-- strip any namespace from sitelink
-- strip any namespace from sitelink
local firstColon = mw.ustring.find(title,':',1,true)
local firstColon = mw.ustring.find(title,':',1,true)
Line 82: Line 80:
local consistent = true
local consistent = true
-- look up commons sitelink for article, use if not category
-- look up commons sitelink for article, use if not category
if fetch then
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) ~= "Category:" then
commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki") or commonsSitelink
end
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) ~= "Category:" then
galleryLink = commonsSitelink
galleryLink = commonsSitelink
end
end
-- P935 is the "commons gallery" property for this article
-- P935 is the "commons gallery" property for this article
local P935 = mw.wikibase.getBestStatements(qid, "P935")[1]
if P935 and P935.mainsnak.datavalue then
if P935 and P935.mainsnak.datavalue then
local gallery = P935.mainsnak.datavalue.value
local gallery = P935.mainsnak.datavalue.value
Line 113: Line 107:
property = property:upper()
property = property:upper()
-- If property exists on current article, get value (other article qid)
-- 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
if value and value.mainsnak.datavalue and value.mainsnak.datavalue.value.id then
-- Look up Commons sitelink of other article
-- 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
-- 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
if sitelink and mw.ustring.sub(sitelink,1,9) == "Category:" then
return mw.ustring.sub(sitelink,10)
return mw.ustring.sub(sitelink,10)
Line 142: Line 134:
local consistent = true
local consistent = true
-- look up commons sitelink for article, use if starts with "Category:"
-- 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
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) == "Category:" then
categoryLink = mw.ustring.sub(commonsSitelink,10)
categoryLink = mw.ustring.sub(commonsSitelink,10)
end
end
-- P373 is the "commons category" property for this article
-- P373 is the "commons category" property for this article
local P373 = mw.wikibase.getBestStatements(qid, "P373")[1]
if P373 and P373.mainsnak.datavalue then
if P373 and P373.mainsnak.datavalue then
P373 = P373.mainsnak.datavalue.value
P373 = P373.mainsnak.datavalue.value
if categoryLink and categoryLink ~= P373 then
if categoryLink and categoryLink ~= P373 then
1,068

edits