Importer, Bürokraten, Moderatoren (CommentStreams), Strukturierte-Diskussionen-Bots, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
855
Bearbeitungen
(use require('strict') instead of require('Module:No globals')) |
K (3 Versionen von skanwiki:Modul:Commons_link importiert) |
||
Zeile 1: | Zeile 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 p = {} | local p = {} | ||
Zeile 21: | Zeile 17: | ||
end | end | ||
function _lcfirst(doit,s) | |||
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2 | if doit then | ||
return mw.ustring.lower(mw.ustring.sub(s,1,1))..mw.ustring.sub(s,2) | |||
end | end | ||
return s | |||
return | |||
end | end | ||
Zeile 54: | Zeile 27: | ||
-- Arguments: | -- Arguments: | ||
-- qid = testing only: get title of alternative page with QID=qid | -- qid = testing only: get title of alternative page with QID=qid | ||
-- Returns: | -- Returns: | ||
-- title, namespace ( | -- title, namespace (number), qid of current page (or test page) | ||
local function _getTitleQID(qid | local function _getTitleQID(qid) | ||
local titleObject = mw.title.getCurrentTitle() | local titleObject = mw.title.getCurrentTitle() | ||
-- look up qid for current page (if not testing) | -- look up qid for current page (if not testing) | ||
if not _validQID(qid) then | if not _validQID(qid) then | ||
qid = mw.wikibase.getEntityIdForCurrentPage() | qid = mw.wikibase.getEntityIdForCurrentPage() | ||
return titleObject.text, | return titleObject.text, titleObject.namespace, qid | ||
end | end | ||
-- testing-only path: given a qid, determine title | -- testing-only path: given a qid, determine title | ||
Zeile 71: | Zeile 42: | ||
-- 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) | ||
if firstColon then | if firstColon then | ||
title = mw.ustring.sub(title,firstColon+1) | title = mw.ustring.sub(title,firstColon+1) | ||
end | end | ||
return title, titleObject.namespace, qid | |||
end | end | ||
Zeile 164: | Zeile 130: | ||
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 | |||
-- 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 | 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 | ||
Zeile 180: | Zeile 157: | ||
if categoryLink and categoryLink ~= fallback then | if categoryLink and categoryLink ~= fallback then | ||
consistent = false | consistent = false | ||
else | else | ||
categoryLink = fallback | categoryLink = fallback | ||
end | end | ||
end | end | ||
return categoryLink, consistent, commonsSitelink | return categoryLink, consistent, commonsSitelink | ||
end | end | ||
Zeile 212: | Zeile 168: | ||
-- 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, nil if not | ||
function p._hasGallery(qid) | function p._hasGallery(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) | ||
local | local galleryLink, consistent = _lookupGallery(qid,true) | ||
if | if galleryLink and consistent then | ||
return galleryLink | |||
end | end | ||
return | return nil | ||
end | end | ||
Zeile 240: | Zeile 182: | ||
-- Arguments: | -- Arguments: | ||
-- 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, blank if not | -- filename at Commons if so, blank if not | ||
function p._hasCategory(qid, | function p._hasCategory(qid) | ||
local categoryLink, consistent = | local wp_title, wp_ns | ||
return | wp_title, wp_ns, qid = _getTitleQID(qid) | ||
local categoryLink, consistent = _lookupCategory(qid,true) | |||
if categoryLink and consistent then | |||
return "Category:"..categoryLink | |||
end | |||
return nil | |||
end | end | ||
Zeile 252: | Zeile 198: | ||
-- 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 | ||
-- | -- lcfirst = lower case the first letter in linktext | ||
-- 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,search, | function p._getCommons(namespace,default,linktext,search,lcfirst,qid) | ||
local nsColon | local nsColon | ||
if not namespace or namespace == "" then | if not namespace or namespace == "" then | ||
Zeile 266: | Zeile 212: | ||
end | end | ||
if default then | if default then | ||
return "[[Commons:"..nsColon..default.."|".. | return "[[Commons:"..nsColon..default.."|".._lcfirst(lcfirst,linktext or default).."]]" | ||
end | end | ||
if search then | if search then | ||
return "[[Commons:Special:Search/"..nsColon..search.."|".. | return "[[Commons:Special:Search/"..nsColon..search.."|".._lcfirst(lcfirst,linktext or search).."]]" | ||
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) | ||
-- 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 commonsLink = nil | ||
local consistent = true | local consistent = true | ||
Zeile 282: | Zeile 230: | ||
-- 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.."|".._lcfirst(lcfirst,linktext or commonsLink).."]]" | ||
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 | ||
if not consistent and wp_ns == 0 then | |||
if not consistent and wp_ns == | |||
local friendlyNS | local friendlyNS | ||
if nsColon == "" then | if nsColon == "" then | ||
Zeile 303: | Zeile 248: | ||
-- 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 | ||
-- 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, search | function p._getGalleryOrCategory(default,linktext,search,qid) | ||
if default then | if default then | ||
return "[[Commons:"..default.."|".. | return "[[Commons:"..default.."|"..(linktext or default).."]]" | ||
end | end | ||
if search then | if search then | ||
return "[[Commons:Special:Search/"..search.."|".. | return "[[Commons:Special:Search/"..search.."|"..(linktext or search).."]]" | ||
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) | ||
-- construct default result (which searches for title) | |||
local searchResult = "[[Commons:Special:Search/"..wp_title.."|"..(linktext or wp_title).."]]" | |||
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.."|"..(linktext or galleryLink).."]]" | ||
end | end | ||
if not consistent and wp_ns == | if not consistent and wp_ns == 0 then | ||
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" | trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" | ||
end | end | ||
Zeile 331: | Zeile 277: | ||
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.."|"..(linktext or categoryLink).."]]"..trackingCats | ||
end | end | ||
if not consistent and wp_ns == | if not consistent and wp_ns == 0 then | ||
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | ||
end | end | ||
-- | return searchResult..trackingCats | ||
end | |||
-- Make a string bold, italic, or both | |||
-- Arguments: | |||
-- s = string to format | |||
-- bold = make it bold | |||
-- italic = make it italic | |||
-- Returns: | |||
-- string modified with html tags | |||
local function _formatResult(s,bold,italic) | |||
local resultVal = "" | |||
if bold 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 resultVal | |||
end | end | ||
Zeile 346: | Zeile 307: | ||
-- 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) | ||
-- | -- bold = whether to make first link bold | ||
-- | -- 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,linkText,categoryText,bold,italic,oneSearch,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" | ||
-- 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 trackingCats = "" | ||
local galleryLink, galleryConsistent | local galleryLink, galleryConsistent | ||
Zeile 365: | Zeile 329: | ||
end | end | ||
local galleryGood = galleryLink and galleryConsistent | local galleryGood = galleryLink and galleryConsistent | ||
if not galleryConsistent and wp_ns == | if not galleryConsistent and wp_ns == 0 then | ||
trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" | trackingCats = "[[Category:Inconsistent wikidata for Commons gallery]]" | ||
end | end | ||
Zeile 376: | Zeile 340: | ||
end | end | ||
local categoryGood = categoryLink and categoryConsistent | local categoryGood = categoryLink and categoryConsistent | ||
if not categoryConsistent and wp_ns == | if not categoryConsistent and wp_ns == 0 then | ||
trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | trackingCats = trackingCats.."[[Category:Inconsistent wikidata for Commons category]]" | ||
end | end | ||
local firstLink | local firstLink | ||
if galleryGood then | if galleryGood then | ||
firstLink = galleryLink | firstLink = galleryLink | ||
linkText = galleryLink | linkText = linkText or galleryLink | ||
elseif categoryGood then | elseif categoryGood then | ||
firstLink = "Category:"..categoryLink | firstLink = "Category:"..categoryLink | ||
linkText = categoryLink | linkText = linkText or categoryLink | ||
else | else | ||
return searchResult..trackingCats | return searchResult..trackingCats | ||
end | end | ||
local resultVal = "[[Commons:"..firstLink.."|".. | local resultVal = _formatResult("[[Commons:"..firstLink.."|"..linkText.."]]",bold,italic) | ||
if galleryGood and categoryGood then | if galleryGood and categoryGood then | ||
resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."|"..categoryText.."]])" | resultVal = resultVal.." ([[Commons:Category:"..categoryLink.."|"..categoryText.."]])" | ||
end | end | ||
return resultVal..trackingCats | return resultVal..trackingCats | ||
end | end | ||
Zeile 517: | Zeile 363: | ||
function p.getTitleQID(frame) | function p.getTitleQID(frame) | ||
local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | local args = getArgs(frame,{frameOnly=true,parentOnly=false,parentFirst=false}) | ||
local text, ns, qid = _getTitleQID(args[1 | local text, ns, qid = _getTitleQID(args[1]) | ||
return text..","..ns..","..(qid or "nil") | return text..","..ns..","..(qid or "nil") | ||
end | end | ||
Zeile 531: | Zeile 377: | ||
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],args. | return p._getCommons("",args[1],args.linktext,args.search,args.lcfirst,args.qid) | ||
end | end | ||
Zeile 537: | Zeile 383: | ||
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}) | ||
return p._getCommons("Category",args[1],args.linktext,args.search,args.lcfirst,args.qid) | |||
end | end | ||
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}) | ||
return p._getGalleryOrCategory(args[1],args.linktext,args.search,args.qid) | |||
end | end | ||
Zeile 578: | Zeile 408: | ||
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.linktext,args.categoryText, | ||
args.bold,args.italic,args.oneSearch,args.qid) | |||
end | end | ||
return p | return p |