Module:Commons link: Difference between revisions
From All Skies Encyclopaedia
imported>Hike395 (add comment) |
imported>Hike395 (unify formatting across all entry points) |
||
Line 1: | Line 1: | ||
-- Module to find commons galleries and categories based on wikidata entries |
-- Module to find commons galleries and categories based on wikidata entries |
||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
local yesNo = require('Module:Yesno') |
|||
local p = {} |
local p = {} |
||
Line 17: | Line 18: | ||
end |
end |
||
function _lcfirst(doit,s) |
local function _lcfirst(doit,s) |
||
if doit then |
if doit then |
||
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2) |
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2) |
||
end |
end |
||
return s |
return s |
||
⚫ | |||
-- Format displayed linktext |
|||
⚫ | |||
⚫ | |||
-- formatting = formatting table: |
|||
-- formatting.linktext = if defined, override s |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- formatting.nowrap = set nowrapping |
|||
⚫ | |||
-- formatted string |
|||
⚫ | |||
⚫ | |||
resultVal = _lcfirst(formatting.lcfirst,resultVal) |
|||
⚫ | |||
⚫ | |||
if formatting.nowrap then |
|||
resultVal = '<span style="white-space:nowrap;">' .. resultVal .. "</span>" |
|||
end |
|||
⚫ | |||
end |
end |
||
Line 207: | Line 230: | ||
return nil |
return nil |
||
end |
end |
||
-- Create Commons link corresponding to current article |
-- Create Commons link corresponding to current article |
||
Line 212: | Line 236: | ||
-- namespace = namespace in Commons ("" for galleries) |
-- namespace = namespace in Commons ("" for galleries) |
||
-- default = use as Commons link, don't access wikidata |
-- default = use as Commons link, don't access wikidata |
||
-- linktext = text to display in link |
|||
-- search = string to search for |
-- search = string to search for |
||
-- fallback = string to search for if wikidata fails |
-- fallback = string to search for if wikidata fails |
||
-- formatting = formatting parameters |
|||
⚫ | |||
-- qid = QID to lookup in wikidata (for testing only) |
-- qid = QID to lookup in wikidata (for testing only) |
||
-- Returns: |
-- Returns: |
||
-- formatted wikilink to Commons in specified namespace |
-- formatted wikilink to Commons in specified namespace |
||
function p._getCommons(namespace,default |
function p._getCommons(namespace,default,search,fallback,formatting,qid) |
||
local nsColon |
local nsColon |
||
if not namespace or namespace == "" then |
if not namespace or namespace == "" then |
||
Line 227: | Line 250: | ||
end |
end |
||
if default then |
if default then |
||
return "[[Commons:"..nsColon..default.."|".. |
return "[[Commons:"..nsColon..default.."|".._formatResult(default,formatting).."]]" |
||
end |
end |
||
if search then |
if search then |
||
return "[[Commons:Special:Search/"..nsColon..search.."|".. |
return "[[Commons:Special:Search/"..nsColon..search.."|".._formatResult(search,formatting).."]]" |
||
end |
end |
||
local wp_title, wp_ns |
local wp_title, wp_ns |
||
Line 243: | Line 266: | ||
-- use wikidata if consistent |
-- use wikidata if consistent |
||
if commonsLink and consistent then |
if commonsLink and consistent then |
||
return "[[Commons:"..nsColon..commonsLink.."|".. |
return "[[Commons:"..nsColon..commonsLink.."|".._formatResult(commonsLink,formatting).."]]" |
||
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 |
||
-- construct default result (which searches for title) |
-- construct default result (which searches for title) |
||
local searchResult = "[[Commons:Special:Search/"..nsColon..(fallback or wp_title) |
local searchResult = "[[Commons:Special:Search/"..nsColon..(fallback or wp_title) |
||
.."|".. |
.."|".._formatResult(fallback or wp_title,formatting).."]]" |
||
if not consistent and wp_ns == "" then |
if not consistent and wp_ns == "" then |
||
local friendlyNS |
local friendlyNS |
||
Line 259: | Line 282: | ||
end |
end |
||
return searchResult |
return searchResult |
||
⚫ | |||
-- Make a string bold, italic, or both |
|||
⚫ | |||
⚫ | |||
-- bold = make it bold |
|||
-- italic = make it italic |
|||
⚫ | |||
-- string modified with html tags |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 278: | Line 287: | ||
-- Arguments: |
-- Arguments: |
||
-- default = use as Commons link, don't access wikidata |
-- default = use as Commons link, don't access wikidata |
||
-- linktext = text to display in link |
|||
-- search = string to search for |
-- search = string to search for |
||
-- fallback = string to search for if wikidata lookup fails |
-- fallback = string to search for if wikidata lookup fails |
||
-- formatting = formatting parameters |
|||
⚫ | |||
⚫ | |||
-- qid = QID to lookup in wikidata (for testing only) |
-- qid = QID to lookup in wikidata (for testing only) |
||
-- Returns: |
-- Returns: |
||
-- formatted wikilink to Commons "best" landing page |
-- formatted wikilink to Commons "best" landing page |
||
function p._getGalleryOrCategory(default |
function p._getGalleryOrCategory(default, search, fallback, formatting, qid) |
||
if default then |
if default then |
||
return _formatResult( |
return "[[Commons:"..default.."|".._formatResult(default,formatting).."]]" |
||
"[[Commons:"..default.."|"..(linktext or default).."]]", bold, italic |
|||
) |
|||
end |
end |
||
if search then |
if search then |
||
return _formatResult( |
return "[[Commons:Special:Search/"..search.."|".._formatResult(search,formatting).."]]" |
||
"[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]", bold, italic |
|||
) |
|||
end |
end |
||
local wp_title, wp_ns |
local wp_title, wp_ns |
||
Line 303: | Line 306: | ||
-- 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 |
||
return _formatResult( |
return "[[Commons:"..galleryLink.."|".._formatResult(galleryLink,formatting).."]]" |
||
"[[Commons:"..galleryLink.."|"..(linktext or galleryLink).."]]", bold, italic |
|||
) |
|||
end |
end |
||
if not consistent and wp_ns == "" then |
if not consistent and wp_ns == "" then |
||
Line 314: | Line 315: | ||
categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink) |
categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink) |
||
if categoryLink and consistent then |
if categoryLink and consistent then |
||
return _formatResult( |
return "[[Commons:Category:"..categoryLink.."|".._formatResult(categoryLink,formatting).."]]"..trackingCats |
||
"[[Commons:Category:"..categoryLink.."|"..(linktext or categoryLink).."]]", bold, italic |
|||
) ..trackingCats |
|||
end |
end |
||
if not consistent and wp_ns == "" then |
if not consistent and wp_ns == "" then |
||
Line 322: | Line 321: | ||
end |
end |
||
-- return search result looking for title as last attempt |
-- return search result looking for title as last attempt |
||
return |
return "[[Commons:Special:Search/" .. (fallback or wp_title) .. |
||
"|" .. ( |
"|" .. _formatResult(fallback or wp_title,formatting) .. "]]" .. trackingCats |
||
end |
end |
||
Line 331: | Line 330: | ||
-- defaultCategory = default category link to use, instead of wikidata |
-- defaultCategory = default category link to use, instead of wikidata |
||
-- categoryText = if both gallery and category, text to use in category link ("category" by default) |
-- categoryText = if both gallery and category, text to use in category link ("category" by default) |
||
-- |
-- oneSearch = only emit one search result |
||
-- formatting = formatting parameters |
|||
-- italic = whether to make first link italic |
|||
-- qid = qid of page to lookup in wikidata (testing only) |
-- qid = qid of page to lookup in wikidata (testing only) |
||
function p._getGalleryAndCategory(defaultGallery, defaultCategory, |
function p._getGalleryAndCategory(defaultGallery, defaultCategory, |
||
categoryText, |
categoryText, oneSearch, formatting, qid |
||
) |
) |
||
local wp_title, wp_ns |
local wp_title, wp_ns |
||
Line 366: | Line 365: | ||
local firstLink |
local firstLink |
||
-- construct default result (which searches for title) |
-- construct default result (which searches for title) |
||
local searchResult = |
local searchResult = "[[Commons:Special:Search/"..wp_title.."|".._formatResult(wp_title,formatting).."]]" |
||
if not oneSearch then |
if not oneSearch then |
||
searchResult = searchResult.." ([[Commons:Special:Search/Category:"..wp_title.."|"..categoryText.."]])" |
searchResult = searchResult.." ([[Commons:Special:Search/Category:"..wp_title.."|"..categoryText.."]])" |
||
end |
end |
||
local linkText = nil |
|||
if galleryGood then |
if galleryGood then |
||
firstLink = galleryLink |
firstLink = galleryLink |
||
linkText = |
linkText = galleryLink |
||
elseif categoryGood then |
elseif categoryGood then |
||
firstLink = "Category:"..categoryLink |
firstLink = "Category:"..categoryLink |
||
linkText = |
linkText = categoryLink |
||
else |
else |
||
return searchResult..trackingCats |
return searchResult..trackingCats |
||
end |
end |
||
local resultVal = |
local resultVal = "[[Commons:"..firstLink.."|".._formatResult(linkText,formatting).."]]" |
||
if galleryGood and categoryGood then |
if galleryGood and categoryGood then |
||
resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."|"..categoryText.."]])" |
resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."|"..categoryText.."]])" |
||
Line 394: | Line 394: | ||
return s1 == s2 |
return s1 == s2 |
||
end |
end |
||
-- Figure out tracking categories and editor warnings |
-- Figure out tracking categories and editor warnings |
||
Line 473: | Line 471: | ||
end |
end |
||
return out |
return out |
||
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 |
end |
||
Line 492: | Line 500: | ||
function p.getGallery(frame) |
function p.getGallery(frame) |
||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
||
return p._getCommons("",args[1] |
return p._getCommons("",args[1],args.search,args.fallback,_createFormatting(args),args.qid) |
||
end |
end |
||
Line 498: | Line 506: | ||
function p.getCategory(frame) |
function p.getCategory(frame) |
||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
||
local retval = p._getCommons("Category", args[1], |
local retval = p._getCommons("Category", args[1], |
||
args.search, args.fallback, args |
args.search, args.fallback, _createFormatting(args), args.qid |
||
) |
) |
||
if args.tracking then |
if args.tracking then |
||
Line 514: | Line 522: | ||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
||
local retval = p._getGalleryOrCategory( |
local retval = p._getGalleryOrCategory( |
||
args[1] |
args[1], args.search, args.fallback, _createFormatting(args), args.qid |
||
) |
) |
||
if args.tracking then |
if args.tracking then |
||
Line 539: | Line 547: | ||
function p.getGalleryAndCategory(frame) |
function p.getGalleryAndCategory(frame) |
||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) |
||
return p._getGalleryAndCategory(args[1], args[2], |
return p._getGalleryAndCategory(args[1], args[2], |
||
args.categoryText, args. |
args.categoryText, args.oneSearch, _createFormatting(args), args.qid) |
||
end |
end |
||
Revision as of 09:36, 13 May 2021
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 yesNo = require('Module:Yesno')
local p = {}
-- Check if string is a valid QID
-- Argument: QID to check
-- Returns: valid (bool)
local function _validQID(qid)
return qid and mw.ustring.find(qid,"^[Qq]%d+$")
end
-- Check if string is a valid wikidata property string
-- Argument: property string to check
-- Returns: valid (bool)
local function _validProp(prop)
return prop and mw.ustring.find(prop,"^[Pp]%d+$")
end
local 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
-- 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
resultVal = _lcfirst(formatting.lcfirst,resultVal)
if formatting.italic then resultVal = "<i>" .. resultVal .. "</i>" end
if formatting.bold then resultVal = "<b>" .. resultVal .. "</b>" end
if formatting.nowrap then
resultVal = '<span style="white-space:nowrap;">' .. resultVal .. "</span>"
end
return resultVal
end
-- Get title, namespace, and QID for current page
-- 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 (string), 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, nsText, qid
end
-- testing-only path: given a qid, determine title
-- always use namespace from current page (to suppress tracking cat)
qid = qid:upper()
local title = mw.wikibase.getSitelink(qid) or ""
-- 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
-- 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)
if not _validQID(qid) then
return nil, true, nil
end
qid = qid:upper()
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 _validQID(qid) or not _validProp(property) then
return nil
end
qid = qid:upper()
property = property:upper()
-- 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)
if not _validQID(qid) then
return nil, true, nil
end
qid = qid:upper()
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
-- 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
-- Arguments:
-- namespace = namespace in Commons ("" for galleries)
-- default = use as Commons link, don't access wikidata
-- search = string to search for
-- fallback = string to search for if wikidata fails
-- 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,search,fallback,formatting,qid)
local nsColon
if not namespace or namespace == "" then
nsColon = ""
else
nsColon = namespace..":"
end
if default then
return "[[Commons:"..nsColon..default.."|".._formatResult(default,formatting).."]]"
end
if search then
return "[[Commons:Special:Search/"..nsColon..search.."|".._formatResult(search,formatting).."]]"
end
local wp_title, wp_ns
wp_title, wp_ns, qid = _getTitleQID(qid)
local commonsLink = nil
local consistent = true
if nsColon == "" then
commonsLink, consistent = _lookupGallery(qid,true)
elseif namespace:lower() == "category" then
commonsLink, consistent = _lookupCategory(qid,true)
end
-- use wikidata if consistent
if commonsLink and consistent then
return "[[Commons:"..nsColon..commonsLink.."|".._formatResult(commonsLink,formatting).."]]"
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
friendlyNS = "gallery"
else
friendlyNS = namespace:lower()
end
searchResult = searchResult.."[[Category:Inconsistent wikidata for Commons "..friendlyNS.."]]"
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
-- 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, search, fallback, formatting, qid)
if default then
return "[[Commons:"..default.."|".._formatResult(default,formatting).."]]"
end
if search then
return "[[Commons:Special:Search/"..search.."|".._formatResult(search,formatting).."]]"
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(galleryLink,formatting).."]]"
end
if not consistent and wp_ns == "" 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.."|".._formatResult(categoryLink,formatting).."]]"..trackingCats
end
if not consistent and wp_ns == "" then
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
-- Return link(s) Commons gallery, or category, or both from wikidata
-- Arguments:
-- defaultGallery = default gallery link to use, instead of wikidata
-- defaultCategory = default category link to use, instead of wikidata
-- categoryText = if both gallery and category, text to use in category link ("category" by default)
-- oneSearch = only emit one search result
-- 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
local commonsSitelink = nil
if defaultGallery then
galleryLink = defaultGallery
galleryConsistent = true
else
galleryLink, galleryConsistent, 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
-- 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 = galleryLink
elseif categoryGood then
firstLink = "Category:"..categoryLink
linkText = categoryLink
else
return searchResult..trackingCats
end
local resultVal = "[[Commons:"..firstLink.."|".._formatResult(linkText,formatting).."]]"
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
-- 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
-- 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")
local function pick(categories)
if default then
-- construct warning message
local warning = require('Module:If preview')._warning({
"Commons link does not match Wikidata – [[Template:Commons_category#Resolving_discrepancies|please check]]"
})
if default == wikidata then
return categories.commons_link_on_wikidata
end
if titleMatch(default,title) then
return categories.commons_link_defined_as_pagename .. warning
end
return categories.commons_link_locally_defined .. warning
end
if wikidata then
return categories.commons_link_from_wikidata
end
return categories.commons_link_is_pagename
end
local out
if not fetchGallery and fetchCategory then
out = pick({
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]]'
})
else
out = pick({
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]]'
})
end
return out
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
-- 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
-- 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._getCommons("",args[1],args.search,args.fallback,_createFormatting(args),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})
local retval = p._getCommons("Category", args[1],
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})
local retval = p._getGalleryOrCategory(
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
function p.hasGallery(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._hasGallery(args.qid) or ""
end
function p.hasCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._hasCategory(args.qid) or ""
end
function p.hasGalleryOrCategory(frame)
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false})
return p._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.categoryText, args.oneSearch, _createFormatting(args), 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