Anonymous user
Module:Sister project links: Difference between revisions
reverse display order of wikivoyage and wikiversity
Richardpruen (talk | contribs) m (1 revision imported) |
imported>Hike395 (reverse display order of wikivoyage and wikiversity) |
||
Line 1: | Line 1: | ||
require(' | require('strict') | ||
-- Module to create sister project link box | -- Module to create sister project link box | ||
Line 17: | Line 17: | ||
voy="Wikivoyage-Logo-v3-icon.svg", | voy="Wikivoyage-Logo-v3-icon.svg", | ||
v="Wikiversity logo 2017.svg", | v="Wikiversity logo 2017.svg", | ||
species="Wikispecies-logo.svg", | |||
iw="Wikipedia-logo-v2.svg", | |||
iw1="Wikipedia-logo-v2.svg", | |||
iw2="Wikipedia-logo-v2.svg", | |||
d="Wikidata-logo.svg", | d="Wikidata-logo.svg", | ||
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 | local prefixList = {'wikt', 'c', 'n', 'q', 's', 'b', 'v', 'voy', | ||
'species', 'species_author', 'm', 'mw'} | 'species', 'species_author', 'iw', 'iw1', 'iw2', 'd', 'm', 'mw'} | ||
local sisterName = { | local sisterName = { | ||
Line 34: | Line 37: | ||
voy="Wikivoyage", | voy="Wikivoyage", | ||
v="Wikiversity", | v="Wikiversity", | ||
species="Wikispecies", | |||
iw="Wikipedia", | |||
iw1="Wikipedia", | |||
iw2="Wikipedia", | |||
d="Wikidata", | d="Wikidata", | ||
m="Meta-Wiki", | m="Meta-Wiki", | ||
mw="MediaWiki"} | mw="MediaWiki"} | ||
Line 48: | Line 54: | ||
voy="Travel guides", | voy="Travel guides", | ||
v="Resources", | v="Resources", | ||
species="Taxa", | species="Taxa", | ||
species_author="Authorship", | species_author="Authorship", | ||
iw="Edition", | |||
iw1="Edition", | |||
iw2="Edition", | |||
d="Data", | |||
m="Discussions", | m="Discussions", | ||
mw="Documentation" | mw="Documentation" | ||
Line 64: | Line 73: | ||
voy='auto', | voy='auto', | ||
v=true, | v=true, | ||
species='auto', | species='auto', | ||
species_author=false, | species_author=false, | ||
iw=false, | |||
iw1=false, | |||
iw2=false, | |||
d=false, | |||
m=false, | m=false, | ||
mw=false | mw=false | ||
Line 86: | Line 98: | ||
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"} | ||
local inSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true) | |||
-- Function to add "-sand" to classes when called from sandbox | -- Function to add "-sand" to classes when called from sandbox | ||
local function sandbox( | local function sandbox(s) | ||
return | return inSandbox and s.."-sand" or s | ||
end | end | ||
Line 167: | Line 181: | ||
end | end | ||
-- Function to get sitelink for a wiki | |||
-- Arguments: | |||
-- prefix = prefix string for wiki to lookup | |||
-- qid = QID of entity to search for, current page entity by default | |||
local function fetchWikidata(prefix,qid) | |||
local sisterDbName = sisterDb[prefix] | |||
return sisterDbName and getSitelink(sisterDbName,qid) | |||
end | |||
-- Function to generate the sister link itself | -- Function to generate the sister link itself | ||
Line 184: | Line 206: | ||
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 | ||
end | end | ||
local sitelink = args.sitelink or fetchWikidata(args.sisterPrefix,args.qid) | |||
local sitelink = args.sitelink or ( | |||
if args.auto and not sitelink and args[1][2] == nil then | if args.auto and not sitelink and args[1][2] == nil then | ||
return nil --- in auto mode, if link is blank and no sitelink, then skip | return nil --- in auto mode, if link is blank and no sitelink, then skip | ||
Line 214: | Line 235: | ||
end | end | ||
end | end | ||
return {prefix=args.sisterPrefix, link=link, information=args.information} | return {prefix=args.sisterPrefix, link=link, logo=args.logo, name=args.name, | ||
information=args.information} | |||
end | end | ||
-- Function to handle special case of commons link | |||
local function commonsLinks(args, commonsPage) | local function commonsLinks(args, commonsPage) | ||
-- use [[Module:Commons link]] to determine best commons link | -- use [[Module:Commons link]] to determine best commons link | ||
Line 226: | Line 249: | ||
local commonsSearch = "Search/"..(args.commonscat and "Category:" or "")..args[1] | local commonsSearch = "Search/"..(args.commonscat and "Category:" or "")..args[1] | ||
return {link=cLink, search=commonsSearch} | return {link=cLink, search=commonsSearch} | ||
end | |||
-- Function to handle special case for "author" and "cookbook" | |||
local function handleSubtype(args) | |||
local ns = args.ns | |||
local ns_len = mw.ustring.len(ns) | |||
local result = {} | |||
result.sitelink = fetchWikidata(args.prefix, args.qid) | |||
local subtype = false | |||
if args.page then | |||
if mw.ustring.sub(args.page,1,ns_len) == ns then | |||
subtype = true | |||
elseif args.subtype then | |||
result.page = ns..args.page | |||
subtype = true | |||
end | |||
elseif result.sitelink then | |||
subtype = mw.ustring.sub(result.sitelink,1,ns_len) == ns | |||
elseif args.subtype then | |||
result.search = "Search/"..ns..args.default | |||
subtype = true | |||
end | |||
if subtype then | |||
result.info = args.info | |||
end | |||
return result | |||
end | end | ||
Line 246: | Line 295: | ||
local sitelink = prefix == 'd' and args.qid | local sitelink = prefix == 'd' and args.qid | ||
local page = args[prefix] | local page = args[prefix] | ||
if prefix == 's' | local info = sisterInfo[prefix] | ||
-- special case handling of author and cookbook | |||
local subtype = nil | |||
if prefix == 's' then | |||
subtype = handleSubtype({prefix='s',qid=args.qid,subtype=args.author,page=page[1], | |||
ns='Author:',info=nil,default=args[1]}) | |||
elseif prefix == 'b' then | |||
subtype = handleSubtype({prefix='b',qid=args.qid,subtype=args.cookbook,page=page[1], | |||
ns='Cookbook:',info='Recipes',default=args[1]}) | |||
end | |||
if subtype then | |||
page[1] = subtype.page or page[1] | |||
search = subtype.search or search | |||
sitelink = subtype.sitelink or sitelink | |||
info = subtype.info or info | |||
end | end | ||
if prefix == 'voy' then | |||
if prefix == 'voy' | if not args.bar then | ||
info = "Travel information" | |||
end | |||
if page[1] then | |||
if mw.ustring.match(page[1],"phrasebook") then | |||
info = "Phrasebook" | |||
end | |||
elseif page[2] or args.auto then | |||
sitelink = sitelink or fetchWikidata('voy',args.qid) | |||
if sitelink and mw.ustring.match(sitelink,"phrasebook") then | |||
info = "Phrasebook" | |||
end | |||
end | |||
end | end | ||
info = args.information or info | info = args.information or info | ||
Line 260: | Line 333: | ||
end | end | ||
prefix = (prefix == 'species_author' and 'species') or prefix | prefix = (prefix == 'species_author' and 'species') or prefix | ||
local logo = logo[prefix] | |||
local name = sisterName[prefix] | |||
if mw.ustring.sub(prefix,1,2) == 'iw' then | |||
local lang = nil | |||
local iw_arg = args[prefix] | |||
if iw_arg[1] then | |||
lang = iw_arg[1] | |||
elseif iw_arg[2] then | |||
local P424 = mw.wikibase.getBestStatements(args.qid, "P424")[1] | |||
if P424 and P424.mainsnak.datavalue then | |||
lang = P424.mainsnak.datavalue.value | |||
end | |||
end | |||
if lang == nil then | |||
return nil | |||
end | |||
prefix = ':'..lang | |||
page[1] = "" | |||
page[2] = true | |||
local langname = mw.language.fetchLanguageName( lang, 'en') | |||
if langname then | |||
info = langname..' '..info | |||
end | |||
end | |||
return genSisterLink({ | return genSisterLink({ | ||
page, | page, | ||
auto=args.auto, | auto=args.auto, | ||
qid=args.qid, | qid=args.qid, | ||
logo=logo, | |||
name=name, | |||
sitelink=sitelink, | sitelink=sitelink, | ||
default=default, | default=default, | ||
Line 269: | Line 368: | ||
search=search, | search=search, | ||
information=info}, tracking) | information=info}, tracking) | ||
end | |||
local function templatestyles_page(is_bar) | |||
local sandbox = inSandbox and 'sandbox/' or '' | |||
if is_bar then | |||
return mw.ustring.format( | |||
'Module:Sister project links/bar/%sstyles.css', | |||
sandbox | |||
) | |||
end | |||
return mw.ustring.format( | |||
'Module:Sister project links/%sstyles.css', | |||
sandbox | |||
) | |||
end | end | ||
Line 278: | Line 391: | ||
-- args.style: CSS style string appended to end of default CSS | -- args.style: CSS style string appended to end of default CSS | ||
-- args.display: boldface name to display | -- args.display: boldface name to display | ||
local function | local function createSisterBox(sisterList, args) | ||
local list = mw.html.create('ul') | |||
local | |||
for i, link in ipairs(sisterList) do | for i, link in ipairs(sisterList) do | ||
local li = | local li = list: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:addClass(sandbox( | logoSpan:addClass(sandbox("sister-logo")) | ||
logoSpan:wikitext("[[File:".. | logoSpan:wikitext("[[File:"..link.logo.."|27x27px|middle|link=|alt=]]") | ||
-- html element for link | -- html element for link | ||
local linkspan = li:tag('span') | local linkspan = li:tag('span') | ||
linkspan:addClass(sandbox( | linkspan:addClass(sandbox("sister-link")) | ||
local linkText = "[["..link.prefix..":"..link.link.."|"..link.information .."]] from ".. | local linkText = "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..link.name | ||
linkspan:wikitext(linkText) | linkspan:wikitext(linkText) | ||
end | end | ||
return | list:allDone() | ||
return sideBox({ | |||
role = 'navigation', | |||
labelledby = 'sister-projects', | |||
class = sandbox("sister-box") .. ' sistersitebox plainlinks', | |||
position = args.position, | |||
style = args.style, | |||
abovestyle = args.collapsible and 'clear: both' or nil, | |||
above = mw.ustring.format( | |||
"<b>%s</b> at Wikipedia's [[Wikipedia:Wikimedia sister projects|<span id=\"sister-projects\">sister projects</span>]]", | |||
args.display or args[1] | |||
), | |||
text = tostring(list), | |||
collapsible = args.collapsible, | |||
templatestyles = templatestyles_page() | |||
}) | |||
end | end | ||
Line 337: | Line 429: | ||
nav:addClass( 'noprint') | nav:addClass( 'noprint') | ||
nav:addClass( 'metadata') | nav:addClass( 'metadata') | ||
nav:addClass( sandbox( | nav:addClass( sandbox('sister-bar')) | ||
nav:attr( 'role', 'navigation' ) | nav:attr( 'role', 'navigation' ) | ||
nav:attr( 'aria-label' , 'sister-projects' ) | nav:attr( 'aria-label' , 'sister-projects' ) | ||
local header = nav:tag('div') | local header = nav:tag('div') | ||
header:addClass(sandbox( | header:addClass(sandbox('sister-bar-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]) | ||
Line 347: | Line 439: | ||
headerText = headerText..'<span id="sister-projects" style="white-space:nowrap;">sister projects</span>]]:' | headerText = headerText..'<span id="sister-projects" style="white-space:nowrap;">sister projects</span>]]:' | ||
header:wikitext(headerText) | header:wikitext(headerText) | ||
local container = nav:tag(' | if #sisterList == 1 and args.trackSingle then | ||
container:addClass(sandbox( | header:wikitext("[[Category:Pages with single-entry sister bar]]") | ||
end | |||
local container = nav:tag('ul') | |||
container:addClass(sandbox('sister-bar-content')) | |||
for _, link in ipairs(sisterList) do | for _, link in ipairs(sisterList) do | ||
local item = container:tag(' | local item = container:tag('li') | ||
item:addClass(sandbox( | item:addClass(sandbox('sister-bar-item')) | ||
local logoSpan = item:tag('span') | local logoSpan = item:tag('span') | ||
logoSpan:addClass(sandbox( | logoSpan:addClass(sandbox('sister-bar-logo')) | ||
logoSpan:wikitext("[[File:".. | logoSpan:wikitext("[[File:"..link.logo.."|21x19px|link=|alt=]]") | ||
local linkSpan = item:tag('span') | local linkSpan = item:tag('span') | ||
linkSpan:addClass(sandbox( | linkSpan:addClass(sandbox('sister-bar-link')) | ||
linkSpan:wikitext("<b>[["..link.prefix..":"..link.link.."|"..link.information .."]]</b> from ".. | linkSpan:wikitext("<b>[["..link.prefix..":"..link.link.."|"..link.information .."]]</b> from "..link.name) | ||
end | end | ||
return nav | return nav | ||
Line 364: | Line 459: | ||
function p._main(args) | function p._main(args) | ||
local titleObject = mw.title.getCurrentTitle() | local titleObject = mw.title.getCurrentTitle() | ||
local ns = titleObject.namespace | |||
-- find qid, either supplied with args, from search string, or from current page | -- 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 or mw.wikibase.getEntityIdForTitle(args[1] or "") or mw.wikibase.getEntityIdForCurrentPage() | ||
args.qid = args.qid and args.qid:upper() | args.qid = args.qid and args.qid:upper() | ||
-- search string defaults to PAGENAME | -- search string defaults to PAGENAME | ||
args[1] = args[1] or titleObject.text | args[1] = args[1] or mw.wikibase.getSitelink(args.qid or "") or titleObject.text | ||
-- handle redundant "commons"/"c" prefix | -- handle redundant "commons"/"c" prefix | ||
args.c = args.c or args.commons | args.c = args.c or args.commons | ||
Line 375: | Line 471: | ||
args[k] = canonicalize(args[k]) | args[k] = canonicalize(args[k]) | ||
end | end | ||
-- Canonicalize cookbook | |||
args.cookbook = canonicalize(args.cookbook) | |||
args.b = mergeArgs({args.b,args.cookbook}) | |||
args.cookbook = args.cookbook[2] | |||
-- handle trackSingle parameter | |||
if args.trackSingle == nil then | |||
args.trackSingle = true | |||
end | |||
if ns ~= 0 and ns ~= 14 then | |||
args.trackSingle = false | |||
end | |||
-- Canonicalize general parameters | -- Canonicalize general parameters | ||
for _,k in pairs({"auto","commonscat","author","bar","tracking","sandbox"}) do | for _,k in pairs({"auto","commonscat","author","bar","tracking","sandbox","trackSingle"}) 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 = (args.tracking or | local tracking = (args.tracking or ns == 0) and {} | ||
local sisterList = {} | local sisterList = {} | ||
local prefix | local prefix | ||
Line 390: | Line 497: | ||
end | end | ||
end | end | ||
local box = mw.html.create( | local box = mw.html.create() | ||
if args.bar and #sisterList > 0 then | if args.bar and #sisterList > 0 then | ||
box = createSisterBar(sisterList,args) | box:wikitext(mw.getCurrentFrame():extensionTag{ | ||
name = 'templatestyles', args = { src = templatestyles_page(true) } | |||
}) | |||
box:node(createSisterBar(sisterList,args)) | |||
elseif #sisterList == 1 then | 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] | ||
local link = "[["..sister.prefix..":"..sister.link.."|<b><i>"..(args.display or args[1]).."</i></b>]]" | local link = "[["..sister.prefix..":"..sister.link.."|<b><i>"..(args.display or args[1]).."</i></b>]]" | ||
box:wikitext(sideBox({position=args.position, | if sister.name == 'Commons' then | ||
image="[[File:".. | sister.name = 'Wikimedia Commons' -- make single sister commons box look like {{Commons}} | ||
metadata='no', | end | ||
text= | local text = sister.name.." has "..mw.ustring.lower(sister.information).." related to "..link | ||
if sister.name == 'Wikipedia' then -- make single sister interwiki box look like {{InterWiki}} | |||
text = "[["..sister.prefix..":"..sister.link.."|<b><i>"..sister.information.."</i></b>]] at [[Wikipedia]], the free encyclopedia" | |||
end | |||
box:wikitext(sideBox({ | |||
role = 'navigation', | |||
position=args.position, | |||
image="[[File:"..sister.logo.."|40x40px|class=noviewer|alt=|link=]]", | |||
metadata='no', | |||
class='plainlinks sistersitebox', | |||
text=text, | |||
templatestyles = templatestyles_page() | |||
})) | |||
elseif #sisterList > 0 then | elseif #sisterList > 0 then | ||
-- else use sister box if non-empty | -- else use sister box if non-empty | ||
box | box:wikitext(createSisterBox(sisterList,args)) | ||
end | end | ||
if #sisterList == 0 and args.auto then | if #sisterList == 0 and args.auto then | ||
Line 419: | Line 540: | ||
end | end | ||
end | end | ||
return tostring(box) | |||
return | |||
end | end | ||
Line 441: | Line 556: | ||
args[k] = canonicalize(args[k]) | args[k] = canonicalize(args[k]) | ||
end | end | ||
-- Canonicalize cookbook | |||
args.cookbook = canonicalize(args.cookbook) | |||
args.b = mergeArgs({args.b,args.cookbook}) | |||
args.cookbook = args.cookbook[2] | |||
-- Canonicalize general parameters | -- Canonicalize general parameters | ||
for _,k in pairs({"auto","commonscat","author"}) do | for _,k in pairs({"auto","commonscat","author"}) do | ||
Line 452: | Line 571: | ||
return "" | return "" | ||
end | end | ||
return "[["..link.prefix..":"..link.link.."|"..link.information .."]] from ".. | return "[["..link.prefix..":"..link.link.."|"..link.information .."]] from "..link.name | ||
end | end | ||
-- Template entry point for generating one sister link | -- Template entry point for generating one sister link | ||
function p.link(frame) | function p.link(frame) | ||
local args = getArgs(frame | local args = getArgs(frame) | ||
return p._sisterlink(args) | return p._sisterlink(args) | ||
end | end | ||
return p | return p |