Content added Content deleted
(use cargo instead) |
m (fixed query) |
||
(27 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
local QueryBuilder = require('Module:QueryBuilder') |
|||
local cargo = mw.ext.cargo |
|||
local VariablesLua = mw.ext.VariablesLua |
|||
local p = {} |
local p = {} |
||
local image_cache_key = 'it_cache__%s_image' |
|||
⚫ | |||
local pagename_cache_key = 'it_cache__%s_page' |
|||
⚫ | |||
⚫ | |||
local function getCacheValues(item) |
|||
local image = VariablesLua.var(string.format(image_cache_key, item)) |
|||
local pageName = VariablesLua.var(string.format(pagename_cache_key, item)) |
|||
return { |
|||
image = image, |
|||
page_name = pageName |
|||
} |
|||
end |
end |
||
function |
local function setCacheValues(item, image, pageName) |
||
VariablesLua.vardefine(string.format(image_cache_key, item), image) |
|||
VariablesLua.vardefine(string.format(pagename_cache_key, item), pageName) |
|||
end |
|||
⚫ | |||
⚫ | |||
local item = string.lower(args['item'] or '') |
local item = string.lower(args['item'] or '') |
||
local image = nil |
local image = nil |
||
local link = nil |
local link = nil |
||
⚫ | |||
local icon_size = '80px' |
|||
if item then |
if item then |
||
local |
local values = getCacheValues(item) |
||
local fields = '_pageName, image' |
|||
local args = { |
|||
where = string.format("item_name = '%s'", item), |
|||
} |
|||
local result = cargo.query( tables, fields, args ) |
|||
⚫ | |||
⚫ | |||
link = values['pageName'] |
|||
⚫ | |||
if image == nil or image == '' or link == nil or link == '' then |
|||
local qb = QueryBuilder.new() |
|||
⚫ | |||
:setTables('items') |
|||
:setFields([[ |
|||
_pageName, |
|||
thumbnail |
|||
]]) |
|||
:addWhere('item_name', '=', item) |
|||
local results = qb:query() |
|||
⚫ | |||
image = results[1]['thumbnail'] |
|||
⚫ | |||
setCacheValues(item, thumb, link) |
|||
end |
|||
end |
end |
||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
image = args['image'] or image, |
|||
size = args['size'], |
|||
⚫ | |||
count = args['count'] |
|||
} |
|||
end |
|||
function p._main(args) |
|||
local thumb = args['image'] or '' |
|||
local thumbSize = args['size'] or '64px' |
|||
local link = args['link'] or '' |
|||
⚫ | |||
local root = mw.html.create('div') |
local root = mw.html.create('div') |
||
root: |
root:addClass('item-thumbnail') |
||
⚫ | |||
['position'] = 'relative', |
|||
['height'] = |
['height'] = thumbSize, |
||
['width'] = |
['width'] = thumbSize |
||
}) |
|||
:wikitext(string.format( |
:wikitext(string.format( |
||
'[[File:%s| |
'[[File:%s|%s|link=%s|%s]]', |
||
thumb, |
|||
thumbSize, |
|||
link, |
link, |
||
link |
link |
||
Line 48: | Line 87: | ||
if count and tonumber(count) > 1 then |
if count and tonumber(count) > 1 then |
||
root:tag('div') |
root:tag('div') |
||
:addClass('item-count') |
|||
⚫ | |||
:wikitext('×' .. count) |
|||
['right'] = 0, |
|||
['position'] = 'absolute', |
|||
['padding'] = '0 5px', |
|||
['background'] = '#00cdba', |
|||
['border-radius'] = '5px 0 5px 0', |
|||
['user-select'] = 'none' |
|||
⚫ | |||
:wikitext('x' .. count) |
|||
end |
end |
||
return |
return |
||
mw.getCurrentFrame():extensionTag{ |
|||
name = 'templatestyles', |
|||
args = { src = 'Template:Item thumbnail/styles.css' } |
|||
} .. tostring(root) |
|||
end |
end |
||
Latest revision as of 02:48, 1 September 2023
local getArgs = require('Module:Arguments').getArgs
local QueryBuilder = require('Module:QueryBuilder')
local VariablesLua = mw.ext.VariablesLua
local p = {}
local image_cache_key = 'it_cache__%s_image'
local pagename_cache_key = 'it_cache__%s_page'
local function getCacheValues(item)
local image = VariablesLua.var(string.format(image_cache_key, item))
local pageName = VariablesLua.var(string.format(pagename_cache_key, item))
return {
image = image,
page_name = pageName
}
end
local function setCacheValues(item, image, pageName)
VariablesLua.vardefine(string.format(image_cache_key, item), image)
VariablesLua.vardefine(string.format(pagename_cache_key, item), pageName)
end
function p.main(frame)
local args = getArgs(frame)
local item = string.lower(args['item'] or '')
local image = nil
local link = nil
if item then
local values = getCacheValues(item)
image = values['image']
link = values['pageName']
if image == nil or image == '' or link == nil or link == '' then
local qb = QueryBuilder.new()
qb
:setTables('items')
:setFields([[
_pageName,
thumbnail
]])
:addWhere('item_name', '=', item)
local results = qb:query()
if #results > 0 then
image = results[1]['thumbnail']
link = results[1]['_pageName']
setCacheValues(item, thumb, link)
end
end
end
return p._main{
image = args['image'] or image,
size = args['size'],
link = args['link'] or link,
count = args['count']
}
end
function p._main(args)
local thumb = args['image'] or ''
local thumbSize = args['size'] or '64px'
local link = args['link'] or ''
local count = args['count'] or 1
local root = mw.html.create('div')
root:addClass('item-thumbnail')
:css({
['height'] = thumbSize,
['width'] = thumbSize
})
:wikitext(string.format(
'[[File:%s|%s|link=%s|%s]]',
thumb,
thumbSize,
link,
link
))
if count and tonumber(count) > 1 then
root:tag('div')
:addClass('item-count')
:wikitext('×' .. count)
end
return
mw.getCurrentFrame():extensionTag{
name = 'templatestyles',
args = { src = 'Template:Item thumbnail/styles.css' }
} .. tostring(root)
end
return p