Module:Subject bar
Revision as of 05:55, 19 December 2021 by Tachyony (talk | contribs) (Created page with "require('Module:No globals') local sisterBar = require('Module:Sister project links')._main local portalBar = require('Module:Portal bar')._main local compressSparseArray = require('Module:TableTools').compressSparseArray local p = {} local trackingEnabled = true -- Check whether to do tracking in this namespace -- Returns true unless the page is one of the banned namespaces local function checkTrackingNamespace() local thisPageNS = mw.title.getCurrentTitle().namesp...")
Documentation for this module may be created at Module:Subject bar/doc
require('Module:No globals')
local sisterBar = require('Module:Sister project links')._main
local portalBar = require('Module:Portal bar')._main
local compressSparseArray = require('Module:TableTools').compressSparseArray
local p = {}
local trackingEnabled = true
-- Check whether to do tracking in this namespace
-- Returns true unless the page is one of the banned namespaces
local function checkTrackingNamespace()
local thisPageNS = mw.title.getCurrentTitle().namespace
if (thisPageNS == 1) -- Talk
or (thisPageNS == 2) -- User
or (thisPageNS == 3) -- User talk
or (thisPageNS == 5) -- Wikipedia talk
or (thisPageNS == 7) -- File talk
or (thisPageNS == 11) -- Template talk
or (thisPageNS == 15) -- Category talk
or (thisPageNS == 101) -- Portal talk
or (thisPageNS == 118) -- Draft
or (thisPageNS == 119) -- Draft talk
or (thisPageNS == 829) -- Module talk
then
return false
end
return true
end
-- Check whether to do tracking on this pagename
-- Returns false if the page title matches one of the banned strings
-- Otherwise returns true
local function checkTrackingPagename()
local thisPage = mw.title.getCurrentTitle()
local thisPageLC = mw.ustring.lower(thisPage.text)
if (string.match(thisPageLC, "/archive") ~= nil)
or (string.match(thisPageLC, "/doc") ~= nil)
or (string.match(thisPageLC, "/test") ~= nil) then
return false
end
return true
end
local sisters = {'commons','species','voy','n','wikt','b','q','s','v','d'}
local function findNumericArgs(key, args)
local pattern = "^"..key.."_?(%d+)$" -- pattern to match
local values = {}
for k, v in pairs(args) do --- loop through all arguments
local ord = tonumber(mw.ustring.match(k,pattern)) --- if "foo_?%d+", extract number
if ord then
values[ord] = v
end
end
if args[key] ~= nil then
values[1] = args[key]
end
values = compressSparseArray(values) --- squeeze out gaps/nils in values, keep ordering
return values
end
function p._main(args)
-- Tracking is on by default.
-- It is disabled if any of the following is true
-- 1/ the parameter "tracking" is set to 'no, 'n', or 'false'
-- 2/ the current page fails the namespace tests in checkTrackingNamespace()
-- 3/ the current page fails the pagename tests in checkTrackingPagename()
if (args.tracking == 'no') or (args.tracking == 'n') or (args.tracking == 'false')
or (checkTrackingNamespace() == false) or (checkTrackingPagename() == false) then
trackingEnabled = false
end
local result = ""
local hasPortal = false
for key, _ in pairs(args) do
if mw.ustring.sub(key,1,6) == 'portal' then
hasPortal = true
break
end
end
if hasPortal then
local portalList = findNumericArgs("portal",args)
result = portalBar(portalList, {tracking=trackingEnabled, qid=args.qid})
end
local hasSister = args.auto
for _, sister in ipairs(sisters) do
if hasSister then
break
end
if args[sister] ~= nil then
hasSister = true
end
end
if hasSister then
local sisterArgs = {auto=1, bar=1, tracking=trackingEnabled, qid=args.qid}
for _, t in ipairs(sisters) do
sisterArgs[t] = args[t..'-search'] or args[t]
end
result = result..sisterBar(sisterArgs)
end
return result
end
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking template,
-- or the args passed to #invoke if any exist. Otherwise assume args are
-- being passed directly in from the debug console or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
return p