WoWWiki

Documentation for this module may be created at Module:Statlink/doc

-- Mostly done

local p = {}
 
local type_img_data = mw.loadData( 'Module:Statlink/link_icon_data.json' ) -- load json
local getArgs = require('Dev:Arguments').getArgs
local strlower = string.lower -- Convert to lower case
 
-- Get data from json page given main type name and piece name
function getstatInfo(stat_type, stat_piece)
	stat_type = strlower(stat_type)
 
	local stat_DB = mw.loadData("Module:Statlink/link_icon_data.json")
	local stat_chunk = stat_DB[stat_type]
 
	if stat_chunk and stat_DB[stat_type][stat_piece] then
	   return stat_DB[stat_type][stat_piece]
	end
end
 
function p.Statlink(frame) -- Implements {{Statlink}}
	local args = getArgs(frame, {
	trim = false,
	removeBlanks = false
	})
	local icononly = nil
	local useicon = nil
	local medium = false
	local large = false
	local xlarge = false
	local float_check = args["float"]
	local float = nil
 
	if args["icononly"] then
		icononly = true
	else
		useicon = false
 
		if args["icon"] then
			useicon = true
		else
			useicon = false
		end
	end
 
	if float_check ~= nil then
		float_check = strlower(float_check)
 
		if float_check == "left" then
			float="left"
		end
		if float_check == "right" then
			float="right"
		end
		if float_check == "thumb" then
			float="thumb"
		end
	end
 
	if args["medium"] then
		medium = true
	end
	if args["large"] then
		large = true
	end
	if args["xlarge"] then
		xlarge = true
	end
 
	return p._Statlink(args, icononly, useicon, medium, large, xlarge, float)
end
 
function p._Statlink( args, showonlyicon, showicon, showmedium, showlarge, showxlarge, floating )
	local stat_index = args['type'] -- use type param for type
	if args['type'] == nil then -- use param 1, if no type=
		stat_index = args[1]
	elseif args[1] == nil then -- blank type if no param 1
		stat_index = ''
	end
	if stat_index ~= nil then -- if not nil, make lowercase
		stat_index = string.lower(stat_index)
	end
	local img_filename = 'Inv_misc_questionmark.png'
	local img_size = '21px'
	if showmedium then
		img_size = '32px'
	elseif showlarge then
		img_size = '48px'
	elseif showxlarge then
		img_size = '64px'
	end
	local stat_link = 'Attribute'
	local label = 'Stat'
	local ttip = 'Stat'
	local link_label = nil
	if args['label'] ~= nil then
		link_label = args['label'] -- use label param for type
	elseif args['label'] == nil and args[2] ~= nil then -- use param 2, if no label=
		link_label = args[2]
	end

	-- Build beginning default image wikitext
	local img_wikitext = '[[File:' .. img_filename .. '|' .. img_size
	-- Add floating, if icon only
	if floating ~= nil and showonlyicon then
		img_wikitext = img_wikitext .. '|' .. floating
	end
	-- Add default tooltip and link
	img_wikitext = img_wikitext .. '|' .. label ..'|link=' .. stat_link .. ']]'
 
	if stat_index ~= nil then
		-- strip spaces for indexing
		stat_index = mw.ustring.gsub(stat_index, "%s", "")
 
		-- strip apostrophes for indexing
		stat_index = mw.ustring.gsub(stat_index, "\'", "")
 
		-- strip dashes for indexing
		stat_index = mw.ustring.gsub(stat_index, "\-", "")
	end

	if stat_index ~= nil and getstatInfo(stat_index, "alias") ~= nil then
		stat_index = getstatInfo(stat_index, "alias")
	end
 
	-- Get image size change if it exists
	if stat_index ~= nil and getstatInfo(stat_index, "size") ~= nil then
		img_size = getstatInfo(stat_index, "size")
	end
 
	-- Get link (also for for image)
	if stat_index ~= nil and getstatInfo(stat_index, "link") ~= nil then
		stat_link = getstatInfo(stat_index, "link")
	end
	-- Override JSON link if link param used
	if args['link'] ~= nil then
		stat_link = args['link']
	end

	-- Get label
	if stat_index ~= nil and getstatInfo(stat_index, "label") ~= nil then
		label = getstatInfo(stat_index, "label")
	end
 
	-- Get tooltip
	if stat_index ~= nil and getstatInfo(stat_index, "tip") ~= nil then
		ttip = getstatInfo(stat_index, "tip")
	elseif stat_index ~= nil and getstatInfo(stat_index, "tip") == nil and getstatInfo(stat_index, "label") ~= nil then
		ttip = label
	end
 
	-- Get image filename and build image wikitext
	if stat_index ~= nil and getstatInfo(stat_index, "img") ~= nil then
			img_filename = getstatInfo(stat_index, "img") -- Get image filename
 
			-- Change size to 32px if medium= was specified
			if showmedium then
				img_size = '32px'
			-- Or to 48px if large= was specified
			elseif showlarge then
				img_size = '48px'
			-- Or to 64px if xlarge= was specified
			elseif showxlarge then
				img_size = '64px'
			end
 
			-- Build beginning image wikitext starting with filename and size if specified in JSON
			if getstatInfo(stat_index, "size") ~= nil or showmedium or showlarge then -- add size, if it exists
				img_wikitext = '[[File:' .. img_filename .. '|' .. img_size
			else
				img_wikitext = '[[File:' .. img_filename
			end
 
			-- Add float, if icon only
			if floating ~=nil and showonlyicon then
				img_wikitext = img_wikitext .. '|' .. floating
			end
 
			-- Add tooltip and link
			img_wikitext = img_wikitext .. '|' .. ttip .. '|link=' .. stat_link .. ']]'
	end
 
	-- If label= passed then use it for what link shows
	if link_label ~= nil then
		label = link_label
	end

	-- If label= passed then use it for what link shows
	if link_label ~= nil then
		label = link_label
	end

	local final_link = nil
	-- Build variants
	local img_only_link = img_wikitext
	local img_with_link = img_wikitext .. ' [[' .. stat_link .. '|' .. label .. ']]'
	local link_only = '[[' .. stat_link .. '|' .. label .. ']]'
 
	-- Determine which variant to return
	if showonlyicon then
		final_link = img_only_link
	else
		if showicon then
			final_link = img_with_link
		else
			final_link = link_only
		end
	end
 
	-- Return variation wikitext
	return final_link
end
 
return p