Changes
fix tracking logic, allow tracking on getCategory()
local function _validProp(prop)
return prop and mw.ustring.find(prop,"^[Pp]%d+$")
end
function _lcfirst(doit,s)
if doit then
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2)
end
return s
end
local function warning(msg)
local frame = mw.getCurrentFrame():getParent()
local html = ""
if frame:preprocess( "{{REVISIONID}}" ) == "" then
html = '<div style="color:red"><strong>Warning:</strong> '..msg
html = html..' <small>(this message is shown only in preview)</small></div>'
end
return html
end
-- Arguments:
-- qid = testing only: get title of alternative page with QID=qid
-- nsQid = whether to return the ns of the qid page or current
-- Returns:
-- title, namespace (numberstring), qid of current page (or test page)local function _getTitleQID(qid,nsQid)
local titleObject = mw.title.getCurrentTitle()
-- look up qid for current page (if not testing)
local nsText = mw.ustring.gsub(titleObject.nsText,"_"," ")
if not _validQID(qid) then
qid = mw.wikibase.getEntityIdForCurrentPage()
return titleObject.text, titleObject.namespacensText, qid
end
-- testing-only path: given a qid, determine title
-- strip any namespace from sitelink
local firstColon = mw.ustring.find(title,':',1,true)
local qidNsText = ""
if firstColon then
qidNsText = mw.ustring.sub(title,1,firstColon-1)
title = mw.ustring.sub(title,firstColon+1)
end
if nsQid then return title, titleObject.namespaceqidNsText, qid end return title, nsText, qid
end
end
-- Does the article have a corresponding Commons gallery?
-- Arguments:
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- filename at Commons if so, nil if not
function p._hasGallery(qid)
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local galleryLink, consistent = _lookupGallery(qid,true)
if galleryLink and consistent then
return galleryLink
end
return nil
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)
if prefix == nil then
prefix = true
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local categoryLink, consistent = _lookupCategory(qid,true)
if categoryLink and consistent then
if prefix then
categoryLink = "Category:"..categoryLink
end
return categoryLink
end
return nil
end
-- Create Commons link corresponding to current article
-- linktext = text to display in link
-- search = string to search for
-- lcfirst = lower case the first letter in linktext
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- formatted wikilink to Commons in specified namespace
function p._getCommons(namespace,default,linktext,search,lcfirst,qid)
local nsColon
if not namespace or namespace == "" then
end
if default then
return "[[Commons:"..nsColon..default.."|".._lcfirst(lcfirst,linktext or default).."]]"
end
if search then
return "[[Commons:Special:Search/"..nsColon..search.."|".._lcfirst(lcfirst,linktext or search).."]]"
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
-- construct default result (which searches for title)
local searchResult = "[[Commons:Special:Search/"..nsColon..wp_title.."|".._lcfirst(lcfirst,linktext or wp_title).."]]"
local commonsLink = nil
local consistent = true
-- use wikidata if consistent
if commonsLink and consistent then
return "[[Commons:"..nsColon..commonsLink.."|".._lcfirst(lcfirst,linktext or commonsLink).."]]"
end
-- if not consistent, fall back to search and add to tracking cat
if not consistent and wp_ns == 0 "" then
local friendlyNS
if nsColon == "" then
return "[[Commons:"..galleryLink.."|"..(linktext or galleryLink).."]]"
end
if not consistent and wp_ns == 0 "" then
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]"
end
return "[[Commons:Category:"..categoryLink.."|"..(linktext or categoryLink).."]]"..trackingCats
end
if not consistent and wp_ns == 0 "" then
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]"
end
end
-- Does the article have Make a corresponding Commons gallery string bold, italic, or category?both
-- Arguments:
-- qid s = QID string to lookup in wikidata (for testing only)format-- bold = make it bold-- italic = make it italic
-- Returns:
-- string modified with html tagslocal function _formatResult(s,bold,italic) local resultVal = "yes" if sobold then resultVal = "<b>" end if italic then resultVal = resultVal.."<i>" end resultVal = resultVal..s if italic then resultVal = resultVal.."</i>" end if bold then resultVal = resultVal.."</b>" end return resultValend -- Return link(s) Commons gallery, or category, or both from wikidata-- Arguments:-- defaultGallery = default gallery link to use, blank instead of wikidata-- defaultCategory = default category link to use, instead of wikidata-- categoryText = if notboth gallery and category, text to use in category link ("category" by default)-- bold = whether to make first link bold-- italic = whether to make first link italic-- qid = qid of page to lookup in wikidata (testing only)function p._hasGalleryOrCategory_getGalleryAndCategory(defaultGallery,defaultCategory,linkText,categoryText,bold,italic,oneSearch,qid)
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
categoryText = categoryText or "category" -- construct default result (which searches for title) local searchResult = _formatResult("[[Commons:Special:Search/"..wp_title.."|"..(linkText or wp_title).."]]",bold,italic) if not oneSearch then searchResult = searchResult.." ([[Commons:Special:Search/Category:"..wp_title.."|"..categoryText.."]])" end local trackingCats = "" local galleryLink, galleryConsistent local commonsSitelink = nil if defaultGallery then galleryLink = defaultGallery galleryConsistent = true else galleryLink, consistentgalleryConsistent, commonsSitelink = _lookupGallery(qid,true) end local galleryGood = galleryLink and galleryConsistent if not galleryConsistent and wp_ns == "" then trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" 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.."[[Category:Inconsistent wikidata for Commons category]]" end local firstLink if galleryGood then firstLink = galleryLink and consistent linkText = linkText or galleryLink elseif categoryGood then return firstLink = "yesCategory:"..categoryLink linkText = linkText or categoryLink else return searchResult..trackingCats
end
local categoryLink categoryLink, consistent resultVal = _lookupCategory_formatResult(qid"[[Commons:"..firstLink.."|"..linkText.."]]",falsebold,commonsSitelinkitalic) if categoryLink galleryGood and consistent categoryGood then return resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."yes|"..categoryText.."]])"
end
return resultVal..trackingCatsend -- Compare two titles with their namespaces strippedlocal function titleMatch(s1,s2) s1 = s1 or "" s2 = s2 or "" s1 = mw.ustring.gsub(s1,"^[^:]+:","") s2 = mw.ustring.gsub(s2,"^[^:]+:","") return s1 == s2
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
-- construct warning message
local msg = "Commons link does not match Wikidata"
msg = msg.."– [[Template:Commons_category#Resolving_discrepancies|please check]]"
local prefix = "[[Category:Commons "
if not fetchGallery and fetchCategory then
prefix = prefix.."category "
end
prefix = prefix.."link "
-- determine title and namespace of wikidata and wp article
local wikidata = nil
-- Tracking code works for all 4 cases of states of fetchGallery/Category
-- fetchGallery takes precedence
if fetchGallery then
wikidata = p._hasGallery(qid)
end
if wikidata == nil and fetchCategory then
wikidata = p._hasCategory(qid,true)
end
local wp_cat = (wp_ns == "Category")
if default then
if default == wikidata then
return prefix.."is on Wikidata]]"
end
if titleMatch(default,title) then
return prefix.."is defined as the pagename]]"..warning(msg)
end
return prefix.."is locally defined]]"..warning(msg)
end
if wikidata then
return prefix.."from Wikidata]]"
end
return prefix.."is the pagename]]"
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],args[2])
return text..","..ns..","..(qid or "nil")
end
function p.getGallery(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._getCommons("",args[1],args.linktext,args.search,args.lcfirst,args.qid)
end
function p.getCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
end
function p.getGalleryOrCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
end
function p.hasGalleryOrCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._hasGalleryOrCategory_hasGallery(args.qid)or p._hasCategory(args.qid) or ""
end
function p.getGalleryAndCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._getGalleryAndCategory(args[1],args[2],args.linktext,args.categoryText,
args.bold,args.italic,args.oneSearch,args.qid)
end
function p.tracking(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._tracking(args[1],args.fetchGallery,args.fetchCategory,args.qid)
end
return p