RheingoldRiver (talk | contribs) ([ST] refactor switchStrin) |
No edit summary |
||
(37 intermediate revisions by 2 users not shown) | |||
Line 11: | Line 11: | ||
function p.Main(frame) |
function p.Main(frame) |
||
− | local args = util_args. |
+ | local args = util_args.mergeKeepEmpty() |
local text = "" |
local text = "" |
||
local inpLemma = args[1] |
local inpLemma = args[1] |
||
− | local inpText = args[2] |
+ | local inpText = args[2] or '' |
+ | local iconOnly = util_args.castAsBool(args.iconOnly) |
||
local key = h.getKey(inpLemma) |
local key = h.getKey(inpLemma) |
||
local result |
local result |
||
+ | |||
if util_args.castAsBool(args.force) then |
if util_args.castAsBool(args.force) then |
||
− | result = h. |
+ | result = h.getFromCargoAndUpdateCache(inpLemma, key) |
else |
else |
||
result = h.getFromVariablesOrCache(inpLemma, key) |
result = h.getFromVariablesOrCache(inpLemma, key) |
||
Line 26: | Line 28: | ||
end |
end |
||
⚫ | |||
+ | -- dlc layer wrapper |
||
if result['dlcPackage'] ~= "" then |
if result['dlcPackage'] ~= "" then |
||
+ | text = '<span class="dlc-image-wrapper">' .. text .. '</span>' |
||
− | text = '<span style="position: relative; display: inline-block;"><span style="display: block;">[[File:' .. result['image'] .. '|32px|link=' .. result['_pageName'] .. '|frameless]]</span><span style="position: absolute; display: block; top: 0; bottom: 0; left: 0; right: 0;">[[File:T DLC icon.png|32px|link=' .. result['_pageName'] .. ']]</span></span>' |
||
− | else |
||
⚫ | |||
end |
end |
||
+ | -- epic border wrapper |
||
⚫ | |||
+ | if util_args.castAsBool(result['epicBorder']) then |
||
+ | text = '<span class="epic-border-wrapper">' .. text .. '</span>' |
||
⚫ | |||
+ | |||
+ | if not iconOnly then |
||
⚫ | |||
+ | util_vars.setVar('IL_O_' .. inpLemma .. '_' .. inpText, text) |
||
+ | end |
||
+ | |||
+ | --text = 'x' .. tostring(args[1]).. '/'..tostring(args[2]).. '/'..tostring(iconOnly).. 'x' |
||
return text |
return text |
||
end |
end |
||
function p.updateCache(frame) |
function p.updateCache(frame) |
||
+ | -- this method returns nothing & just exists to query & update the LuaCache object |
||
+ | -- it's called from the infobox so that when the page is saved, the cache will be updated |
||
local args = util_args.merge() |
local args = util_args.merge() |
||
local str = args[1] |
local str = args[1] |
||
if not str then return end |
if not str then return end |
||
local key = h.getKey(str) |
local key = h.getKey(str) |
||
− | h. |
+ | h.getFromCargoAndUpdateCache(str, key) |
end |
end |
||
Line 63: | Line 77: | ||
function h.getFromCacheOrCargo(str, key) |
function h.getFromCacheOrCargo(str, key) |
||
− | local result = cache.get(key) or h. |
+ | local result = cache.get(key) or h.getFromCargoAndUpdateCache(str, key) |
− | if result.exists == nil then |
||
− | result.exists = true |
||
⚫ | |||
util_vars.setObject(key, result) |
util_vars.setObject(key, result) |
||
return result |
return result |
||
end |
end |
||
− | function h. |
+ | function h.getFromCargoAndUpdateCache(str, key) |
local tables = 'Item' |
local tables = 'Item' |
||
− | local fields = '_pageName,image,dlcPackage' |
+ | local fields = '_pageName,image,epicBorder,dlcPackage' |
− | local |
+ | local query = { |
− | where = 'uniqueName = "' .. str .. '" |
+ | where = 'uniqueName = "' .. str .. '" ', |
− | groupBy = '_pageName', |
||
limit = 1 |
limit = 1 |
||
} |
} |
||
− | local result = cargo.query( tables, fields, |
+ | local result = cargo.query( tables, fields, query )[1] |
cache.set(key, result) |
cache.set(key, result) |
||
return result |
return result |
Revision as of 14:38, 4 November 2020
local util_args = require("Module:ArgsUtil")
local util_cache = require("Module:CacheUtil")
local util_vars = require("Module:VarsUtil")
local cache = require('mw.ext.LuaCache')
local PREFIX = 'ITEMLINK_04'
local lang = mw.getLanguage('en')
local p = {}
local h = {}
local cargo = mw.ext.cargo
function p.Main(frame)
local args = util_args.mergeKeepEmpty()
local text = ""
local inpLemma = args[1]
local inpText = args[2] or ''
local iconOnly = util_args.castAsBool(args.iconOnly)
local key = h.getKey(inpLemma)
local result
if util_args.castAsBool(args.force) then
result = h.getFromCargoAndUpdateCache(inpLemma, key)
else
result = h.getFromVariablesOrCache(inpLemma, key)
end
if not result then
return '[[' .. h.switchString(inpLemma, inpText) .. ']]'
end
local text = '[[File:' .. result['image'] .. '|32px|link=' .. result['_pageName'] .. '|frameless]]'
-- dlc layer wrapper
if result['dlcPackage'] ~= "" then
text = '<span class="dlc-image-wrapper">' .. text .. '</span>'
end
-- epic border wrapper
if util_args.castAsBool(result['epicBorder']) then
text = '<span class="epic-border-wrapper">' .. text .. '</span>'
end
if not iconOnly then
text = text .. ' [['.. result['_pageName'] .. '|' .. h.switchString(result['_pageName'], inpText) ..']]'
util_vars.setVar('IL_O_' .. inpLemma .. '_' .. inpText, text)
end
--text = 'x' .. tostring(args[1]).. '/'..tostring(args[2]).. '/'..tostring(iconOnly).. 'x'
return text
end
function p.updateCache(frame)
-- this method returns nothing & just exists to query & update the LuaCache object
-- it's called from the infobox so that when the page is saved, the cache will be updated
local args = util_args.merge()
local str = args[1]
if not str then return end
local key = h.getKey(str)
h.getFromCargoAndUpdateCache(str, key)
end
-- the following code extracts the result from either:
-- (a) a LuaVariables result stored earlier on the page; or
-- (b) a LuaCache object of the lowercase name; or finally
-- (c) an actual Cargo query to retrieve the information from the page
-- we avoid doing a Cargo query at all costs because Cargo queries are extremely expensive
-- this was not premature optimization; the wiki was actually experiencing errors
-- prior to this optimization being added
-- this code was adapted from Module:Team on Leaguepedia
-- contact River for questions
function h.getKey(str)
return PREFIX .. lang:lc(str)
end
function h.getFromVariablesOrCache(str, key)
return util_vars.getObject(key) or h.getFromCacheOrCargo(str, key)
end
function h.getFromCacheOrCargo(str, key)
local result = cache.get(key) or h.getFromCargoAndUpdateCache(str, key)
util_vars.setObject(key, result)
return result
end
function h.getFromCargoAndUpdateCache(str, key)
local tables = 'Item'
local fields = '_pageName,image,epicBorder,dlcPackage'
local query = {
where = 'uniqueName = "' .. str .. '" ',
limit = 1
}
local result = cargo.query( tables, fields, query )[1]
cache.set(key, result)
return result
end
-- end luacache wrapper
function h.switchString(str1, str2)
if str2 == nil then return str1 end
if str2 == '' then return str1 end
return str2
end
return p