Module:Commons link: Difference between revisions
Richardpruen (talk | contribs) m 1 revision imported |
Richardpruen (talk | contribs) m 1 revision imported: Templates and CSS files |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
require('strict') | |||
-- 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 generateWarning = require('Module:If preview')._warning | |||
local p = {} | local p = {} | ||
Line 17: | Line 21: | ||
end | end | ||
function _lcfirst( | local function _lcfirst(s) | ||
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2) | |||
end | end | ||
local function | -- 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 | end | ||
Line 147: | Line 164: | ||
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 | ||
-- P910 is the "topic's main category". Look for commons sitelink there | -- P910 is the "topic's main category". Look for commons sitelink there | ||
Line 174: | Line 180: | ||
if categoryLink and categoryLink ~= fallback then | if categoryLink and categoryLink ~= fallback then | ||
consistent = false | consistent = false | ||
qid = nil | |||
else | else | ||
categoryLink = fallback | 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 | ||
end | end | ||
return categoryLink, consistent, commonsSitelink | 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 | end | ||
Line 185: | Line 212: | ||
-- qid = QID to lookup in wikidata (for testing only) | -- qid = QID to lookup in wikidata (for testing only) | ||
-- Returns: | -- Returns: | ||
-- filename at Commons if so, | -- filename at Commons if so, false if not | ||
function p._hasGallery(qid) | function p._hasGallery(qid) | ||
local galleryLink, consistent = p._hasGalleryConsistent(qid) | |||
return consistent and galleryLink | |||
local galleryLink, consistent = | |||
end | end | ||
-- Does the article have a | -- Does the article have a Commons category? Is wikidata consistent for that? | ||
-- Arguments: | -- Arguments: | ||
-- qid = QID to lookup in wikidata (for testing only) | -- qid = QID to lookup in wikidata (for testing only) | ||
-- prefix = whether to add "Category:" to return string (default true) | -- prefix = whether to add "Category:" to return string (default true) | ||
-- Returns: | -- Returns: | ||
-- filename at Commons | -- filename at Commons, bool: consistent | ||
function p. | function p._hasCategoryConsistent(qid,prefix) | ||
if prefix == nil then | if prefix == nil then | ||
prefix = true | prefix = true | ||
Line 209: | Line 231: | ||
wp_title, wp_ns, qid = _getTitleQID(qid) | wp_title, wp_ns, qid = _getTitleQID(qid) | ||
local categoryLink, consistent = _lookupCategory(qid,true) | local categoryLink, consistent = _lookupCategory(qid,true) | ||
if categoryLink and | if categoryLink and prefix then | ||
categoryLink = "Category:"..categoryLink | |||
end | end | ||
return | return categoryLink, consistent | ||
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) | |||
local categoryLink, consistent = p._hasCategoryConsistent(qid,prefix) | |||
return consistent and categoryLink | |||
end | end | ||
Line 222: | Line 252: | ||
-- 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 | ||
-- 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 237: | Line 266: | ||
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 | ||
wp_title, wp_ns, qid = _getTitleQID(qid) | wp_title, wp_ns, qid = _getTitleQID(qid) | ||
local commonsLink = nil | local commonsLink = nil | ||
local consistent = true | local consistent = true | ||
Line 256: | Line 282: | ||
-- 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) | |||
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 274: | Line 303: | ||
-- Arguments: | -- Arguments: | ||
-- default = use as Commons link, don't access wikidata | -- default = use as Commons link, don't access wikidata | ||
-- search = string to search for | -- 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) | -- 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 "[[Commons:"..default.."|"..( | return "[[Commons:"..default.."|".._formatResult(default,formatting).."]]" | ||
end | end | ||
if search then | if search then | ||
return "[[Commons:Special:Search/"..search.."|"..( | return "[[Commons:Special:Search/"..search.."|".._formatResult(search,formatting).."]]" | ||
end | end | ||
local wp_title, wp_ns | local wp_title, wp_ns | ||
wp_title, wp_ns, qid = _getTitleQID(qid) | wp_title, wp_ns, qid = _getTitleQID(qid) | ||
local trackingCats = "" | local trackingCats = "" | ||
local galleryLink, consistent, commonsSitelink = _lookupGallery(qid,true) | local galleryLink, consistent, commonsSitelink = _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 | ||
return "[[Commons:"..galleryLink.."|"..( | return "[[Commons:"..galleryLink.."|".._formatResult(galleryLink,formatting).."]]" | ||
end | end | ||
if not consistent and wp_ns == "" then | if not consistent and wp_ns == "" then | ||
Line 304: | Line 331: | ||
categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink) | categoryLink, consistent = _lookupCategory(qid,false,commonsSitelink) | ||
if categoryLink and consistent then | if categoryLink and consistent then | ||
return "[[Commons:Category:"..categoryLink.."|"..( | return "[[Commons:Category:"..categoryLink.."|".._formatResult(categoryLink,formatting).."]]"..trackingCats | ||
end | end | ||
if not consistent and wp_ns == "" then | if not consistent and wp_ns == "" then | ||
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | ||
end | 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 | end | ||
Line 334: | Line 346: | ||
-- 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 | ||
-- 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, oneSearch, formatting, qid | |||
) | |||
local wp_title, wp_ns | local wp_title, wp_ns | ||
wp_title, wp_ns, qid = _getTitleQID(qid) | wp_title, wp_ns, qid = _getTitleQID(qid) | ||
categoryText = categoryText or "category" | categoryText = categoryText or "category" | ||
local trackingCats = "" | local trackingCats = "" | ||
local galleryLink, galleryConsistent | local galleryLink, galleryConsistent | ||
Line 371: | Line 380: | ||
end | end | ||
local firstLink | 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 | 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 409: | ||
s2 = mw.ustring.gsub(s2,"^[^:]+:","") | s2 = mw.ustring.gsub(s2,"^[^:]+:","") | ||
return s1 == 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 | end | ||
Line 420: | Line 479: | ||
return "" | return "" | ||
end | end | ||
-- determine title and namespace of wikidata and wp article | -- determine title and namespace of wikidata and wp article | ||
local wikidata = nil | local wikidata = nil | ||
local consistent = nil | |||
-- Tracking code works for all 4 cases of states of fetchGallery/Category | -- Tracking code works for all 4 cases of states of fetchGallery/Category | ||
-- fetchGallery takes precedence | -- fetchGallery takes precedence | ||
if fetchGallery then | if fetchGallery then | ||
wikidata = p. | wikidata, consistent = p._hasGalleryConsistent(qid) | ||
if default or not fetchCategory or (consistent and wikidata) then | |||
return selectTrackingCat(galleryTrackingCats,wikidata,consistent, | |||
default,title) | |||
end | |||
end | end | ||
if fetchCategory then | |||
local 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) | |||
local 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 | |||
return | |||
end | end | ||
Line 472: | Line 531: | ||
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 478: | Line 537: | ||
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, _createFormatting(args), args.qid | |||
) | |||
if args.tracking then | if args.tracking then | ||
local default = nil | local default = nil | ||
Line 484: | Line 545: | ||
default = "Category:"..args[1] | default = "Category:"..args[1] | ||
end | end | ||
retval = retval..p._tracking(default,false,true,args.qid) | retval = retval..p._tracking(default, false, true, args.qid) | ||
end | end | ||
return retval | return retval | ||
Line 491: | Line 552: | ||
function p.getGalleryOrCategory(frame) | function p.getGalleryOrCategory(frame) | ||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | ||
local retval = p._getGalleryOrCategory(args[1],args. | local retval = p._getGalleryOrCategory( | ||
args[1], args.search, args.fallback, _createFormatting(args), args.qid | |||
) | |||
if args.tracking then | if args.tracking then | ||
retval = retval..p._tracking(args[1],true,true,args.qid) | retval = retval..p._tracking(args[1],true,true,args.qid) | ||
Line 515: | Line 578: | ||
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. | args.categoryText, args.oneSearch, _createFormatting(args), args.qid) | ||
end | end | ||
function p.tracking(frame) | function p.tracking(frame) | ||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | ||
return p._tracking(args[1],args.fetchGallery,args.fetchCategory,args.qid) | return p._tracking(args[1], args.fetchGallery, args.fetchCategory, args.qid) | ||
end | end | ||
return p | return p |