-- [[Module:Portal/tasvirlar]] ostmodullari:
-- [[Module:Portal/tasvirlar/a]] - "A".
-- [[Module:Portal/tasvirlar/b]] - "B".
-- [[Module:Portal/tasvirlar/c]] - "C".
-- [[Module:Portal/tasvirlar/d]] - "D".
-- [[Module:Portal/tasvirlar/e]] - "E".
-- [[Module:Portal/tasvirlar/f]] - "F".
-- [[Module:Portal/tasvirlar/g]] - "G".
-- [[Module:Portal/tasvirlar/h]] - "H".
-- [[Module:Portal/tasvirlar/i]] - "I".
-- [[Module:Portal/tasvirlar/j]] - "J".
-- [[Module:Portal/tasvirlar/k]] - "K".
-- [[Module:Portal/tasvirlar/l]] - "L".
-- [[Module:Portal/tasvirlar/m]] - "M".
-- [[Module:Portal/tasvirlar/n]] - "N".
-- [[Module:Portal/tasvirlar/o]] - "O".
-- [[Module:Portal/tasvirlar/p]] - "P".
-- [[Module:Portal/tasvirlar/q]] - "Q".
-- [[Module:Portal/tasvirlar/r]] - "R".
-- [[Module:Portal/tasvirlar/s]] - "S".
-- [[Module:Portal/tasvirlar/t]] - "T".
-- [[Module:Portal/tasvirlar/u]] - "U".
-- [[Module:Portal/tasvirlar/v]] - "V".
-- [[Module:Portal/tasvirlar/w]] - "W".
-- [[Module:Portal/tasvirlar/x]] - "X".
-- [[Module:Portal/tasvirlar/y]] - "Y".
-- [[Module:Portal/tasvirlar/z]] - "Z".
-- [[Module:Portal/tasvirlar/boshqa]] -
-- [[Module:Portal/tasvirlar/aliases]] -
local p = {}
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true)
local sandbox = isSandbox and '/qumloq' or ''
local function sandboxVersion(s)
return isSandbox and s..'-sand' or s
end
local templatestyles = 'Module:Portal'..sandbox..'/styles.css'
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local badNamespaces = {'user','template','draft','wikipedia'}
local function checkTracking(title)
local thisPage = title or mw.title.getCurrentTitle()
if thisPage.isTalkPage then
return false
end
local ns = thisPage.nsText:lower()
for _, v in ipairs(badNamespaces) do
if ns == v then
return false
end
end
return true
end
local function matchImagePage(s)
if type(s) ~= 'string' or #s < 1 then return end
local firstLetter = mw.ustring.sub(s, 1, 1)
local imagePage
if mw.ustring.find(firstLetter, '^[a-z]') then
imagePage = 'Module:Portal/tasvirlar/' .. firstLetter .. sandbox
else
imagePage = 'Module:Portal/tasvirlar/boshqa' .. sandbox
end
return mw.loadData(imagePage)[s]
end
local function getAlias(s)
local aliasData = mw.loadData('Module:Portal/tasvirlar/aliases'..sandbox)
for portal, aliases in pairs(aliasData) do
for _, alias in ipairs(aliases) do
if alias == s then
return portal
end
end
end
end
local function getImageName(s)
local default = 'Portal-puzzle.svg|link=|alt='
if type(s) ~= 'string' or #s < 1 then
return default
end
s = mw.ustring.lower(s)
return matchImagePage(s) or matchImagePage(getAlias(s)) or default
end
function p._checkPortals(portals, args)
local trackingCat = ''
local errMsg = nil
local trackingEnabled = args.tracking and checkTracking()
args.minPortals = args.minPortals or 1
args.maxPortals = args.maxPortals or -1
if #portals < args.minPortals then
errMsg = 'please specify at least '..args.minPortals..' portal'..(args.minPortals > 1 and 'lar' or '')
trackingCat = (trackingEnabled and '' or '')
return portals, trackingCat, errMsg
end
if args.maxPortals >= 0 and #portals > args.maxPortals then
errMsg = 'too many portals (maximum = '..args.maxPortals..')'
trackingCat = (trackingEnabled and '' or '')
return portals, trackingCat, errMsg
end
if not args.redlinks or trackingEnabled then
local existingPortals = {}
for _, portal in ipairs(portals) do
local portalTitle = mw.title.new(portal,"Portal")
if portalTitle and portalTitle.exists then
table.insert(existingPortals,portal)
elseif trackingEnabled then
trackingCat = ""
end
end
portals = args.redlinks and portals or existingPortals
if #portals == 0 and trackingEnabled then
trackingCat = trackingCat..""
end
end
return portals, trackingCat, errMsg
end
local function portalBox(args)
return mw.html.create('ul')
:attr('role', 'navigation')
:attr('aria-label', 'Portallar')
:addClass('noprint')
:addClass(args.error and '' or sandboxVersion('portalbox'))
:addClass(args.border and sandboxVersion('portalborder') or '')
:addClass(sandboxVersion(args.left and 'portalleft' or 'portalright'))
:css('margin', args.margin or nil)
:newline()
end
local function fillBox(root, contents)
for _, item in ipairs(contents) do
local entry = root:tag('li')
entry:addClass(sandboxVersion('portalbox-entry'))
local image = entry:tag('span')
image:addClass(sandboxVersion('portalbox-image'))
image:wikitext(item[1])
local link = entry:tag('span')
link:addClass(sandboxVersion('portalbox-link'))
link:wikitext(item[2])
end
return root
end
function p._portal(portals, args)
if args.redlinks == 'include' then args.redlinks = true end
args.addBreak = args['break']
for key, default in pairs({left=false,tracking=true,nominimum=false,
redlinks=false,addBreak=false,border=true}) do
if args[key] == nil then args[key] = default end
args[key] = yesno(args[key], default)
end
local root = portalBox(args)
local trackingCat = ''
local errMsg = nil
args.minPortals = args.nominimum and 0 or 1
args.maxPortals = -1
portals, trackingCat, errMsg = p._checkPortals(portals, args)
root:wikitext(trackingCat)
if errMsg then
if args.border then
args.error = true
root = portalBox(args)
root:wikitext(trackingCat)
local errTag = root:tag('strong')
errTag:addClass('error')
errTag:css('padding','0.2em')
errTag:wikitext('Xato: '..errMsg)
end
return tostring(root)
end
if #portals == 0 then
return trackingCat
end
local contents = {}
for _, portal in ipairs(portals) do
local image = string.format('[[Fayl:%s|32x28px|class=noviewer]]',
getImageName(portal))
local link = string.format('[[Portal:%s|%s%sportali]]',
portal, portal, args.addBreak and '<br />' or ' ')
table.insert(contents, {image, link})
end
return tostring(fillBox(root, contents))
end
function p._demo(imageList, args)
for key, default in pairs({left=false,border=true}) do
if args[key] == nil then args[key] = default end
args[key] = yesno(args[key], default)
end
local root = portalBox(args)
local contents = {}
for _, fn in ipairs(imageList) do
local image = string.format('[[Fayl:%s|32x28px|class=noviewer]]',fn)
local link = string.format('[[:Fayl:%s|%s]]',fn,fn)
table.insert(contents,{image,link})
end
return tostring(fillBox(root,contents))
end
function p._image(portal,nostrip)
if type(portal) == 'table' then
portal = portal[1]
end
local name = getImageName(portal)
local strip = not yesno(nostrip)
return strip and name:match('^(.-)|') or name
end
local function getAllImageTables()
local images = {}
for i, subpage in ipairs{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'other'} do
images[i] = mw.loadData('Module:Portal/tasvirlar/' .. subpage .. sandbox)
end
return images
end
function p._displayAll(portals, args)
local lang = mw.language.getContentLanguage()
local count = 1
for _, imageTable in ipairs(getAllImageTables()) do
for portal in pairs(imageTable) do
portals[count] = lang:ucfirst(portal)
count = count + 1
end
end
return p._portal(portals, args)
end
function p._imageDupes()
local exists, dupes = {}, {}
for _, imageTable in ipairs(getAllImageTables()) do
for portal, image in pairs(imageTable) do
if not exists[image] then
exists[image] = portal
else
table.insert(dupes, string.format('"[[:Fayl:%s|%s]]" nomli fayl "%s" va "%s" portallarida ishlatilgan.', image, image, exists[image], portal))
end
end
end
if #dupes < 1 then
return 'Duplikat tasvirlar topilmadi.'
else
return 'Quyidagi duplikat tasvirlar topildi:\n* ' .. table.concat(dupes, '\n* ')
end
end
local function processPortalArgs(args)
args = type(args) == 'table' and args or {}
local portals = {}
local namedArgs = {}
for k, v in pairs(args) do
if type(k) == 'number' and type(v) == 'string' then
table.insert(portals, k)
elseif type(k) ~= 'number' then
namedArgs[k] = v
end
end
table.sort(portals)
for i, v in ipairs(portals) do
portals[i] = args[v]
end
return portals, namedArgs
end
function p._processPortalArgs(args)
return processPortalArgs(args)
end
function p.image(frame)
local origArgs = getArgs(frame)
local portals, args = processPortalArgs(origArgs)
return p._image(portals[1],args.border)
end
function p.demo(frame)
local args = getArgs(frame)
local styles = frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} }
return styles..p._demo(args,args)
end
local function makeWrapper(funcName)
return function (frame)
local origArgs = getArgs(frame)
local portals, args = processPortalArgs(origArgs)
local results = ''
if funcName == '_portal' or funcName == '_displayAll' then
results = frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} }
end
return results .. p[funcName](portals, args)
end
end
for _, funcName in ipairs{'portal', 'imageDupes', 'displayAll'} do
p[funcName] = makeWrapper('_' .. funcName)
end
return p