Module:Commons link: Difference between revisions
Content deleted Content added
fix bug |
Johnrdorazio (talk | contribs) m 1 revision imported |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1:
-- Module to find commons galleries and categories based on wikidata entries
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
local generateWarning = require('Module:If preview')._warning
local p = {}
Line 17 ⟶ 19:
end
local function _lcfirst(
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2)
end
-- Format displayed linktext
-- Arguments:
-- s = string to display
-- formatting = formatting table:
-- formatting.linktext = if defined, override s
-- formatting.lcfirst = lower case the first letter in display
-- formatting.bold = whether to bold the display
-- formatting.italic = whether to italicize the display
-- formatting.nowrap = set nowrapping
-- Returns:
-- formatted string
local function _formatResult(s, formatting)
local resultVal = formatting.linktext or s
if formatting.lcfirst then
resultVal = _lcfirst(resultVal)
end
local style = ""
if formatting.italic then style = "font-style:italic; " end
if formatting.bold then style = style.."font-weight:bold; " end
if formatting.nowrap then style = style.."white-space:nowrap; " end
if style ~= "" then
resultVal = '<span style="'..mw.text.trim(style)..'">'..resultVal..'</span>'
end
return resultVal
end
Line 27 ⟶ 52:
-- 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 (
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,
end
-- testing-only path: given a qid, determine title
Line 42 ⟶ 69:
-- 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, qidNsText, qid
end
return title, nsText, qid
end
Line 130 ⟶ 162:
if commonsSitelink and mw.ustring.sub(commonsSitelink,1,9) == "Category:" then
categoryLink = mw.ustring.sub(commonsSitelink,10)
end
-- P910 is the "topic's main category". Look for commons sitelink there
Line 157 ⟶ 178:
if categoryLink and categoryLink ~= fallback then
consistent = false
qid = nil
else
categoryLink = fallback
end
end
-- P373 is the "commons category" property for this article. This is
-- a low-quality field, so should only be used as a last resort.
if categoryLink == nil and _validQID(qid) then
local P373 = mw.wikibase.getBestStatements(qid, "P373")[1]
if P373 and P373.mainsnak.datavalue then
categoryLink = P373.mainsnak.datavalue.value
consistent = true -- P373 is never used if anything else is available
end
end
return categoryLink, consistent, commonsSitelink
end
-- Does the article have a Commons gallery, and is it consistent?
-- Arguments:
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- filename at Commons, bool: is wikidata consistent for this article?
function p._hasGalleryConsistent(qid)
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
return _lookupGallery(qid,true)
end
Line 168 ⟶ 210:
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- filename at Commons if so,
function p._hasGallery(qid)
local galleryLink, consistent = p._hasGalleryConsistent(qid)
return consistent and galleryLink
end
-- Does the article have a Commons category? Is wikidata consistent for that?
-- Arguments:
-- qid = QID to lookup in wikidata (for testing only)
-- prefix = whether to add "Category:" to return string (default true)
-- Returns:
-- filename at Commons, bool: consistent
function p._hasCategoryConsistent(qid,prefix)
if prefix == nil then
prefix = true
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local
if
categoryLink = "Category:"..categoryLink
end
return
end
Line 182 ⟶ 238:
-- 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)
local categoryLink, consistent = p._hasCategoryConsistent(qid,prefix)
return consistent and categoryLink
end
Line 198 ⟶ 250:
-- namespace = namespace in Commons ("" for galleries)
-- default = use as Commons link, don't access wikidata
-- search = string to search for
--
-- formatting = formatting parameters
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- formatted wikilink to Commons in specified namespace
function p._getCommons(namespace,default
local nsColon
if not namespace or namespace == "" then
Line 212 ⟶ 264:
end
if default then
return "[[Commons:"..nsColon..default.."|"..
end
if search then
return "[[Commons:Special:Search/"..nsColon..search.."|"..
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local commonsLink = nil
local consistent = true
Line 230 ⟶ 280:
-- use wikidata if consistent
if commonsLink and consistent then
return "[[Commons:"..nsColon..commonsLink.."|"..
end
-- if not consistent, fall back to search and add to tracking cat
-- construct default result (which searches for title)
local searchResult = "[[Commons:Special:Search/"..nsColon..(fallback or wp_title)
.."|".._formatResult(fallback or wp_title,formatting).."]]"
if not consistent and wp_ns == "" then
local friendlyNS
if nsColon == "" then
Line 248 ⟶ 301:
-- Arguments:
-- default = use as Commons link, don't access wikidata
-- search = string to search for
-- fallback = string to search for if wikidata lookup fails
-- formatting = formatting parameters
-- qid = QID to lookup in wikidata (for testing only)
-- Returns:
-- formatted wikilink to Commons "best" landing page
function p._getGalleryOrCategory(default,
if default then
return "[[Commons:"..default.."|".._formatResult(
end
if search then
return "[[Commons:Special:Search/"..search.."|".._formatResult(
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local trackingCats = ""
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.."|".._formatResult(
end
if not consistent and wp_ns ==
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]"
end
Line 277 ⟶ 329:
categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink)
if categoryLink and consistent then
return "[[Commons:Category:"..categoryLink.."|".._formatResult(
end
if not consistent and wp_ns ==
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]"
end
-- return search result looking for title as last attempt
return "[[Commons:Special:Search/" .. (fallback or wp_title) ..
"|" .. _formatResult(fallback or wp_title,formatting) .. "]]" .. trackingCats
end
Line 307 ⟶ 344:
-- defaultCategory = default category link to use, instead of wikidata
-- categoryText = if both gallery and category, text to use in category link ("category" by default)
--
-- formatting = formatting parameters
-- qid = qid of page to lookup in wikidata (testing only)
function p._getGalleryAndCategory(defaultGallery, defaultCategory,
categoryText, oneSearch, formatting, qid
)
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
categoryText = categoryText or "category"
local trackingCats = ""
local galleryLink, galleryConsistent
Line 329 ⟶ 363:
end
local galleryGood = galleryLink and galleryConsistent
if not galleryConsistent and wp_ns ==
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]"
end
Line 340 ⟶ 374:
end
local categoryGood = categoryLink and categoryConsistent
if not categoryConsistent and wp_ns ==
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]"
end
local firstLink
-- construct default result (which searches for title)
local searchResult = "[[Commons:Special:Search/"..wp_title.."|".._formatResult(wp_title,formatting).."]]"
if not oneSearch then
searchResult = searchResult.." ([[Commons:Special:Search/Category:"..wp_title.."|"..categoryText.."]])"
end
local linkText = nil
if galleryGood then
firstLink = galleryLink
linkText =
elseif categoryGood then
firstLink = "Category:"..categoryLink
linkText =
else
return searchResult..trackingCats
end
local resultVal =
if galleryGood and categoryGood then
resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."|"..categoryText.."]])"
end
return resultVal..trackingCats
end
-- Compare two titles with their namespaces stripped
local function titleMatch(s1,s2)
s1 = s1 or ""
s2 = s2 or ""
s1 = mw.ustring.gsub(s1,"^[^:]+:","")
s2 = mw.ustring.gsub(s2,"^[^:]+:","")
return s1 == s2
end
local galleryTrackingCats = {
commons_link_on_wikidata = '[[Category:Commons link is on Wikidata]]',
commons_link_defined_as_pagename = '[[Category:Commons link is defined as the pagename]]',
commons_link_locally_defined = '[[Category:Commons link is locally defined]]',
commons_link_from_wikidata = '[[Category:Commons link from Wikidata]]',
commons_link_is_pagename = '[[Category:Commons link is the pagename]]',
inconsistent = '[[Category:Inconsistent wikidata for Commons gallery]]'
}
local categoryTrackingCats = {
commons_link_on_wikidata = '[[Category:Commons category link is on Wikidata]]',
commons_link_defined_as_pagename = '[[Category:Commons category link is defined as the pagename]]',
commons_link_locally_defined = '[[Category:Commons category link is locally defined]]',
commons_link_from_wikidata = '[[Category:Commons category link from Wikidata]]',
commons_link_is_pagename = '[[Category:Commons category link is the pagename]]',
inconsistent = '[[Category:Inconsistent wikidata for Commons category]]'
}
local function selectTrackingCat(trackingCats,wikidata,consistent,default,title)
if not consistent then
return trackingCats.inconsistent
end
if default then
-- construct warning message
if default == wikidata then
return trackingCats.commons_link_on_wikidata
end
local warning = ""
if wikidata then
warning = generateWarning({
"Commons link does not match Wikidata – [[Template:Commons_category#Resolving_discrepancies|please check]]"
})
end
if titleMatch(default,title) then
return trackingCats.commons_link_defined_as_pagename .. warning
end
return trackingCats.commons_link_locally_defined .. warning
end
if wikidata then
return trackingCats.commons_link_from_wikidata
end
return trackingCats.commons_link_is_pagename
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
-- determine title and namespace of wikidata and wp article
local wikidata = nil
local consistent = nil
-- Tracking code works for all 4 cases of states of fetchGallery/Category
-- fetchGallery takes precedence
if fetchGallery then
wikidata, consistent = p._hasGalleryConsistent(qid)
if default or not fetchCategory or (consistent and wikidata) then
return selectTrackingCat(galleryTrackingCats,wikidata,consistent,
default,title)
end
end
if fetchCategory then
cat_wikidata, cat_consistent = p._hasCategoryConsistent(qid,true)
if not fetchGallery or (cat_consistent and cat_wikidata) then
return selectTrackingCat(categoryTrackingCats,cat_wikidata,
cat_consistent,default,title)
end
return selectTrackingCat(galleryTrackingCats,wikidata,consistent,
default,title)
end
return "" -- nothing fetched, nothing tracked
end
local function _createFormatting(args)
formatting = {}
formatting.linktext = args.linktext
formatting.lcfirst = yesNo(args.lcfirst)
formatting.bold = yesNo(args.bold)
formatting.italic = yesNo(args.italic)
formatting.nowrap = yesNo(args.nowrap)
return formatting
end
Line 363 ⟶ 515:
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
Line 377 ⟶ 529:
function p.getGallery(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._getCommons("",args[1],args.
end
Line 383 ⟶ 535:
function p.getCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
args.search, args.fallback, _createFormatting(args), args.qid
)
if args.tracking then
local default = nil
if args[1] then
default = "Category:"..args[1]
end
retval = retval..p._tracking(default, false, true, args.qid)
end
return retval
end
function p.getGalleryOrCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
args[1], args.search, args.fallback, _createFormatting(args), args.qid
)
if args.tracking then
retval = retval..p._tracking(args[1],true,true,args.qid)
end
return retval
end
Line 408 ⟶ 576:
function p.getGalleryAndCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._getGalleryAndCategory(args[1], args[2],
args.
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
|