Conan Exiles Wiki
Register
([ST] define cache)
([ST] inpLemma -> str, fail copy-paste)
Line 58: Line 58:
 
local fields = '_pageName,image,dlcPackage'
 
local fields = '_pageName,image,dlcPackage'
 
local args = {
 
local args = {
where = 'uniqueName = "' .. inpLemma .. '" AND _pageName=uniqueName',
+
where = 'uniqueName = "' .. str .. '" AND _pageName=uniqueName',
 
groupBy = '_pageName',
 
groupBy = '_pageName',
 
limit = 1
 
limit = 1

Revision as of 13:06, 12 October 2020

local util_cache = require("Module:CacheUtil")
local util_vars = require("Module:VarsUtil")
local cache = require('mw.ext.LuaCache')
local PREFIX = 'ITEMLINK_01'
local lang = mw.getLanguage('en')

local p = {}
local h = {}
local cargo = mw.ext.cargo

function p.Main( frame )
	local text = ""
	local inpLemma = frame.args[1]
	local inpText  = frame.args[2]

	local vars = h.getFromVariablesOrCache(inpLemma)
	local r = vars[1]
	if r == nil then
		text = '[[' .. h.switchString(inpLemma, inpText) .. ']]'
	else
		if r['dlcPackage'] ~= "" then
			text = '<span style="position: relative; display: inline-block;"><span style="display: block;">[[File:' .. r['image'] .. '|32px|link=' .. r['_pageName'] .. '|frameless]]</span><span style="position: absolute; display: block; top: 0; bottom: 0; left: 0; right: 0;">[[File:T DLC icon.png|32px|link=' .. r['_pageName'] .. ']]</span></span>'
		else
			text = '[[File:' .. r['image'] .. '|32px|link=' .. r['_pageName'] .. '|frameless]]'
		end
		text = text .. '&nbsp;[['.. r['_pageName'] .. '|' .. h.switchString(r['_pageName'], inpText) ..']]'
	end
	return text
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.getFromVariablesOrCache(str)
	local key = PREFIX .. lang:lc(str)
	return util_vars.getObject(key) or h.getFromCacheOrCargo(str, key)
end

function h.getFromCacheOrCargo(str, key)
	local vars = cache.get(key) or h.getFromCargo(str, key)
	if vars.exists == nil then
		vars.exists = true
	end
	util_vars.setObject(key, vars)
	return vars
end

function h.getFromCargo(str, key)
	local tables = 'Item'
	local fields = '_pageName,image,dlcPackage'
	local args = {
		where = 'uniqueName = "' .. str .. '" AND _pageName=uniqueName',
		groupBy = '_pageName',
		limit = 1
	}
	local result = cargo.query( tables, fields, args )
	cache.set(key, vars)
	return vars
end

function h.switchString(str1, str2)
	local ret = ""
	if str2 == nil then
		ret = str1
	else
		if str2 == '' then
			ret = str1
		else 
			ret = str2
		end
	end
	return ret
end

return p