Anonymous user
Module:Sister project links: Difference between revisions
use box-style header in bar, sandbox-ify CSS
Richardpruen (talk | contribs) m (1 revision imported) |
(use box-style header in bar, sandbox-ify CSS) |
||
Line 1: | Line 1: | ||
require('Module:No globals') | |||
-- Module to create sister project link box | -- Module to create sister project link box | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local commonsLink = require('Module:Commons link') | local commonsLink = require('Module:Commons link') | ||
local sideBox = require('Module:Side box')._main | local sideBox = require('Module:Side box')._main | ||
local generateWarning = require('Module:If preview')._warning | |||
local p = {} | local p = {} | ||
Line 18: | Line 21: | ||
m="Wikimedia Community Logo.svg", | m="Wikimedia Community Logo.svg", | ||
mw="MediaWiki-2020-icon.svg"} | mw="MediaWiki-2020-icon.svg"} | ||
local prefixList = {'wikt', 'c', 'n', 'q', 's', 'b', 'voy', 'v', 'd', | |||
'species', 'species_author', 'm', 'mw'} | |||
local sisterName = { | local sisterName = { | ||
wikt="Wiktionary", | wikt="Wiktionary", | ||
c=" | c="Commons", | ||
n="Wikinews", | n="Wikinews", | ||
q="Wikiquote", | q="Wikiquote", | ||
Line 32: | Line 38: | ||
m="Meta-Wiki", | m="Meta-Wiki", | ||
mw="MediaWiki"} | mw="MediaWiki"} | ||
local sisterInfo = { | |||
wikt="Definitions", | |||
c="Media", | |||
n="News", | |||
q="Quotations", | |||
s="Texts", | |||
b="Textbooks", | |||
voy="Travel guides", | |||
v="Resources", | |||
d="Data", | |||
species="Taxa", | |||
species_author="Authorship", | |||
m="Discussions", | |||
mw="Documentation" | |||
} | |||
local defaultSisters = { | |||
wikt=true, | |||
c=true, | |||
n=true, | |||
q=true, | |||
s=true, | |||
b=true, | |||
voy='auto', | |||
v=true, | |||
d=false, | |||
species='auto', | |||
species_author=false, | |||
m=false, | |||
mw=false | |||
} | |||
local sisterDb = { | local sisterDb = { | ||
Line 48: | Line 86: | ||
wdHidden="Pages using Sister project links with hidden wikidata", | wdHidden="Pages using Sister project links with hidden wikidata", | ||
defaultSearch="Pages using Sister project links with default search"} | defaultSearch="Pages using Sister project links with default search"} | ||
-- Function to add "-sand" to classes when called from sandbox | |||
local function sandbox(args, s) | |||
return args.sandbox and s.."-sand" or s | |||
end | |||
-- Function to canonicalize string | -- Function to canonicalize string | ||
Line 70: | Line 113: | ||
return {nil, nil} | return {nil, nil} | ||
end | end | ||
lowerS = s:lower() | local lowerS = s:lower() | ||
-- Check for various forms of "yes" | -- Check for various forms of "yes" | ||
if lowerS == 'yes' or lowerS == 'y' or lowerS == 't' | if lowerS == 'yes' or lowerS == 'y' or lowerS == 't' | ||
Line 137: | Line 180: | ||
-- args.information = type of info sister site contains | -- args.information = type of info sister site contains | ||
-- tracking = tracking table | -- tracking = tracking table | ||
local function | local function genSisterLink(args, tracking) | ||
if args[1][2] == false or (not args.default and args[1][2] == nil) then | if args[1][2] == false or (not args.default and args[1][2] == nil) then | ||
return nil --- either editor specified "no", or "blank" (and default=no), then skip this sister | return nil --- either editor specified "no", or "blank" (and default=no), then skip this sister | ||
Line 151: | Line 194: | ||
return nil --- no link found, just skip | return nil --- no link found, just skip | ||
end | end | ||
if tracking | if tracking then | ||
-- update state for tracking categories | -- update state for tracking categories | ||
if args[1][1] and sitelink then | if args[1][1] and sitelink then | ||
Line 174: | Line 217: | ||
end | end | ||
-- Function to | local function commonsLinks(args, commonsPage) | ||
-- use [[Module:Commons link]] to determine best commons link | |||
local cLink = (not args.commonscat) and commonsLink._hasGallery(args.qid) | |||
or commonsLink._hasCategory(args.qid) | |||
if commonsPage[1] and not mw.ustring.match(commonsPage[1]:lower(),"^category:") then | |||
commonsPage[1] = (args.commonscat and "Category:" or "")..commonsPage[1] | |||
end | |||
local commonsSearch = "Search/"..(args.commonscat and "Category:" or "")..args[1] | |||
return {link=cLink, search=commonsSearch} | |||
end | |||
-- Function to create a sister link, by prefix | |||
-- Arguments: | -- Arguments: | ||
-- | -- prefix = sister prefix (e.g., "c" for commons) | ||
-- args = arguments for this sister (see p._sisterLink above) | -- args = arguments for this sister (see p._sisterLink above) | ||
-- tracking = tracking table | -- tracking = tracking table | ||
local function | local function sisterLink(prefix, args, tracking) | ||
local | -- determine arguments to genSisterLink according to prefix | ||
if | if prefix == 'species_author' and not args.species[1] and args.species[2] and not args.species_author[1] and args.species_author[2] then | ||
return nil | |||
end | |||
local default = defaultSisters[prefix] | |||
if default == 'auto' then | |||
default = args.auto | |||
end | |||
-- Handle exceptions by prefix | |||
local search = ((prefix == 'd' and "ItemByTitle/enwiki/") or "Search/")..args[1] | |||
local sitelink = prefix == 'd' and args.qid | |||
local page = args[prefix] | |||
if prefix == 's' and args.author and page[1] then | |||
page[1] = "Author:"..page[1] | |||
end | end | ||
local info = sisterInfo[prefix] | |||
if prefix == 'voy' and not args.bar and not page[1] and page[2] ~= nil then | |||
info = "Travel information" | |||
end | |||
info = args.information or info | |||
if prefix == 'c' then | |||
local commons = commonsLinks(args, page) | |||
search = commons.search | |||
sitelink = commons.link | |||
end | |||
prefix = (prefix == 'species_author' and 'species') or prefix | |||
return genSisterLink({ | |||
page, | |||
auto=args.auto, | |||
qid=args.qid, | |||
sitelink=sitelink, | |||
default=default, | |||
sisterPrefix = prefix, | |||
search=search, | |||
information=info}, tracking) | |||
end | end | ||
Line 202: | Line 287: | ||
container:addClass("sistersitebox") | container:addClass("sistersitebox") | ||
container:addClass("plainlist") | container:addClass("plainlist") | ||
container:addClass(sandbox(args,"sister-box")) | |||
if args.position and args.position:lower() == "left" then | if args.position and args.position:lower() == "left" then | ||
container:addClass("mbox-small-left") | container:addClass("mbox-small-left") | ||
Line 213: | Line 299: | ||
end | end | ||
end | end | ||
container:cssText(args.style) | container:cssText(args.style) | ||
-- Div for text header | -- Div for text header | ||
local header = container:tag('div') | local header = container:tag('div') | ||
header:css("clear",args.collapsible and "both") | header:css("clear",args.collapsible and "both") | ||
-- pagename in bold as part of header | -- pagename in bold as part of header | ||
local pagename = header:tag('b') | local pagename = header:tag('b') | ||
pagename:wikitext(args.display or args[1]) | pagename:wikitext(args.display or args[1]) | ||
local headerText = "at Wikipedia's [[Wikipedia:Wikimedia sister projects|" | local headerText = "at Wikipedia's [[Wikipedia:Wikimedia sister projects|" | ||
Line 232: | Line 312: | ||
local ul = container:tag('ul') | local ul = container:tag('ul') | ||
ul:addClass(args.collapsible and "mw-collapsible-content") | ul:addClass(args.collapsible and "mw-collapsible-content") | ||
-- pass ul element back to main, so sister links can be added | -- pass ul element back to main, so sister links can be added | ||
return ul | return ul | ||
Line 241: | Line 317: | ||
local function createSisterBox(sisterList,args) | local function createSisterBox(sisterList,args) | ||
container = createContainer(args) | local container = createContainer(args) | ||
for i, link in ipairs(sisterList) do | for i, link in ipairs(sisterList) do | ||
local li = container:tag('li') | local li = container:tag('li') | ||
-- html element for 27px-high logo | -- html element for 27px-high logo | ||
local logoSpan = li:tag('span') | local logoSpan = li:tag('span') | ||
logoSpan: | logoSpan:addClass(sandbox(args,"sister-logo")) | ||
logoSpan:wikitext("[[File:"..logo[link.prefix].."|27x27px|middle|link=|alt=]]") | logoSpan:wikitext("[[File:"..logo[link.prefix].."|27x27px|middle|link=|alt=]]") | ||
-- html element for link | -- html element for link | ||
local linkspan = li:tag('span') | local linkspan = li:tag('span') | ||
linkspan: | linkspan:addClass(sandbox(args,"sister-link")) | ||
local linkText = "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..sisterName[link.prefix] | local linkText = "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..sisterName[link.prefix] | ||
linkspan:wikitext(linkText) | linkspan:wikitext(linkText) | ||
Line 265: | Line 333: | ||
end | end | ||
local function createSisterBar(sisterList,args) | |||
local nav = mw.html.create( 'div' ) | |||
nav:addClass( 'noprint') | |||
nav:addClass( 'metadata') | |||
nav:addClass( sandbox(args,'sister-bar')) | |||
nav:attr( 'role', 'navigation' ) | |||
nav:attr( 'aria-label' , 'sister-projects' ) | |||
local header = nav:tag('div') | |||
header:addClass(sandbox(args,'sister-bar-header')) | |||
local pagename = header:tag('b') | |||
pagename:wikitext(args.display or args[1]) | |||
local headerText = " at Wikipedia's [[Wikipedia:Wikimedia sister projects|" | |||
headerText = headerText..'<span id="sister-projects" style="white-space:nowrap;">sister projects</span>]]:' | |||
header:wikitext(headerText) | |||
local container = nav:tag('div') | |||
container:addClass(sandbox(args,'sister-bar-content')) | |||
for _, link in ipairs(sisterList) do | |||
local item = container:tag('div') | |||
item:addClass(sandbox(args,'sister-bar-item')) | |||
local logoSpan = item:tag('span') | |||
logoSpan:addClass(sandbox(args,'sister-bar-logo')) | |||
logoSpan:wikitext("[[File:"..logo[link.prefix].."|21x19px|link=|alt=]]") | |||
local linkSpan = item:tag('span') | |||
linkSpan:addClass(sandbox(args,'sister-bar-link')) | |||
linkSpan:wikitext("<b>[["..link.prefix..":"..link.link.."|"..link.information .."]]</b> from "..sisterName[link.prefix]) | |||
end | |||
return nav | |||
end | |||
function p._main(args) | function p._main(args) | ||
local titleObject = mw.title.getCurrentTitle() | local titleObject = mw.title.getCurrentTitle() | ||
-- | -- find qid, either supplied with args, from search string, or from current page | ||
args.qid = args.qid or mw.wikibase.getEntityIdForTitle(args[1] or "") or mw.wikibase.getEntityIdForCurrentPage() | |||
args.qid = args.qid and args.qid:upper() | |||
-- search string defaults to PAGENAME | |||
args[1] = args[1] or titleObject.text | |||
-- handle redundant "commons"/"c" prefix | |||
args.c = args.c or args.commons | |||
-- Canonicalize all sister links (handle yes/no/empty) | -- Canonicalize all sister links (handle yes/no/empty) | ||
for _, k in | for _, k in ipairs(prefixList) do | ||
args[k] = canonicalize(args[k]) | args[k] = canonicalize(args[k]) | ||
end | |||
-- Canonicalize general parameters | -- Canonicalize general parameters | ||
for _,k in pairs({"auto","commonscat","author"}) do | for _,k in pairs({"auto","commonscat","author","bar","tracking","sandbox"}) do | ||
args[k] = canonicalize(args[k])[2] | args[k] = canonicalize(args[k])[2] | ||
end | end | ||
-- Initialize tracking categories if main namespace | -- Initialize tracking categories if main namespace | ||
local tracking = | local tracking = (args.tracking or titleObject.namespace == 0) and {} | ||
local sisterList = {} | local sisterList = {} | ||
local prefix | |||
-- Loop through all sister projects, generate possible links | |||
for _, prefix in ipairs(prefixList) do | |||
local link = sisterLink(prefix, args, tracking) | |||
if link then | |||
-- | table.insert(sisterList, link) | ||
end | |||
end | end | ||
local box = mw.html.create('div') | local box = mw.html.create('div') | ||
if #sisterList == 1 then | if args.bar and #sisterList > 0 then | ||
box = createSisterBar(sisterList,args) | |||
elseif #sisterList == 1 then | |||
-- Use single sister box instead of multi-sister box | -- Use single sister box instead of multi-sister box | ||
local sister = sisterList[1] | local sister = sisterList[1] | ||
Line 373: | Line 405: | ||
-- else use sister box if non-empty | -- else use sister box if non-empty | ||
box = createSisterBox(sisterList,args) | box = createSisterBox(sisterList,args) | ||
end | |||
if #sisterList == 0 and args.auto then | |||
box:wikitext(generateWarning({"No sister project links found in Wikidata. Try auto=0"})) | |||
end | end | ||
-- Append tracking categories to container div | -- Append tracking categories to container div | ||
-- Alpha ordering is by sister prefix | -- Alpha ordering is by sister prefix | ||
if tracking | if tracking then | ||
for k, v in pairs(tracking) do | for k, v in pairs(tracking) do | ||
box:wikitext("[[Category:"..trackingType[k].."|"..v.."]]") | box:wikitext("[[Category:"..trackingType[k].."|"..v.."]]") | ||
end | |||
if #sisterList == 0 then | |||
box:wikitext("[[Category:Pages with empty sister project links]]") | |||
end | |||
end | end | ||
return box | local styleFile = 'Module:Sister project links/' | ||
styleFile = styleFile..(args.sandbox and 'sandbox/' or '') | |||
styleFile = styleFile..'styles.css' | |||
return mw.getCurrentFrame():extensionTag{ | |||
name = 'templatestyles', args = { src = styleFile } | |||
} .. tostring(box) | |||
end | end | ||
Line 387: | Line 431: | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame,{frameOnly=false,parentOnly=false,parentFirst=false}) | local args = getArgs(frame,{frameOnly=false,parentOnly=false,parentFirst=false}) | ||
return | return p._main(args) | ||
end | end | ||
-- Lua entry point for generate one sister link | -- Lua entry point for generate one sister link | ||
function p._sisterlink(args) | function p._sisterlink(args) | ||
args[ | local prefix = args.prefix | ||
args | -- Canonicalize all sister links (handle yes/no/empty) | ||
for _, k in ipairs(prefixList) do | |||
args[k] = canonicalize(args[k]) | |||
end | |||
-- Canonicalize general parameters | |||
for _,k in pairs({"auto","commonscat","author"}) do | |||
args[k] = canonicalize(args[k])[2] | |||
end | |||
args[1] = args[1] or mw.title.getCurrentTitle().text | |||
args.qid = args.qid or mw.wikibase.getEntityIdForCurrentPage() | args.qid = args.qid or mw.wikibase.getEntityIdForCurrentPage() | ||
args.qid = args.qid and args.qid:upper() | args.qid = args.qid and args.qid:upper() | ||
local link = sisterLink(args,nil) | local link = sisterLink(prefix, args,nil) | ||
if not link then | |||
return "" | |||
end | |||
return "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..sisterName[link.prefix] | return "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..sisterName[link.prefix] | ||
end | end |