پرش به محتوا

پودمان:AlborzSports: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
 
خط ۳: خط ۳:
-- Wiki Alborz Sports Information System
-- Wiki Alborz Sports Information System
-- Core Engine
-- Core Engine
-- Version 2.2
-- Version 2.2.2 (Fixed Entity Detection Priority)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local p = {}
local p = {}
خط ۱۳: خط ۱۳:
-- CONFIG
-- CONFIG
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local DEFAULT_IMAGE_WIDTH =
local DEFAULT_IMAGE_WIDTH = data.meta.imageWidth or "280px"
data.meta.imageWidth or "280px"
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
-- UTILITY FUNCTIONS
خط ۴۱: خط ۴۰:
end
end
for key, value in pairs(source.args) do
for key, value in pairs(source.args) do
local cleanKey =
local cleanKey = mw.text.trim(tostring(key))
mw.text.trim(tostring(key))
local cleanValue = clean(value)
local cleanValue =
clean(value)
if cleanKey ~= "" and cleanValue then
if cleanKey ~= "" and cleanValue then
args[cleanKey] = cleanValue
args[cleanKey] = cleanValue
خط ۵۷: خط ۵۴:
local result = {}
local result = {}
for key, value in pairs(args) do
for key, value in pairs(args) do
local field =
local field = data.lookup[key] or key
data.lookup[key] or key
result[field] = value
result[field] = value
end
end
خط ۶۴: خط ۶۰:
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ENTITY ALIASES
-- LOCAL ENTITY ALIASES (تکمیل‌کننده Data برای پشتیبانی کامل فارسی)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local entityAliases = {
local localEntityAliases = {
-- People
-- People
player = "player",
player = "player",
خط ۷۸: خط ۷۴:
manager = "manager",
manager = "manager",
["مدیر ورزشی"] = "manager",
["مدیر ورزشی"] = "manager",
["مدیر"] = "manager",
-- Clubs
-- Clubs
club = "club",
club = "club",
خط ۸۴: خط ۸۲:
["باشگاه"] = "club",
["باشگاه"] = "club",
["باشگاه ورزشی"] = "club",
["باشگاه ورزشی"] = "club",
-- Academy
-- Academy
academy = "academy",
academy = "academy",
["آکادمی"] = "academy",
["آکادمی"] = "academy",
["آکادمی ورزشی"] = "academy",
["آکادمی ورزشی"] = "academy",
-- Facilities
-- Facilities
stadium = "stadium",
stadium = "stadium",
خط ۱۰۶: خط ۱۰۶:
facility = "facility",
facility = "facility",
["مجموعه"] = "facility",
["مجموعه"] = "facility",
-- Organization / Board
-- Organization / Board
board = "board",
board = "board",
خط ۱۱۱: خط ۱۱۲:
["هیئت"] = "board",
["هیئت"] = "board",
["هیئت ورزشی"] = "board",
["هیئت ورزشی"] = "board",
-- Competition
-- Competition
competition = "competition",
competition = "competition",
خط ۱۲۳: خط ۱۲۵:


----------------------------------------------------------------
----------------------------------------------------------------
-- DEFAULT ENTITY
-- EXPLICIT ENTITY TYPE
--
-- اگر کاربر صریحاً entity_type داده باشد،
-- بالاترین اولویت را دارد.
----------------------------------------------------------------
----------------------------------------------------------------
if not entity then
if entity then
entity = data.defaults.entity_type
entity = (data.entityAliases and data.entityAliases[entity])
end
or localEntityAliases[entity]
or entity


----------------------------------------------------------------
if data.entities[entity] then
-- NORMALIZE ENTITY
return entity
----------------------------------------------------------------
end
entity = entityAliases[entity] or entity
 
----------------------------------------------------------------
-- VALID ENTITY
----------------------------------------------------------------
if data.entities[entity] then
return entity
end
end


خط ۱۴۵: خط ۱۴۴:
----------------------------------------------------------------
----------------------------------------------------------------
local subtype = clean(args.subtype)
local subtype = clean(args.subtype)
if subtype then
if subtype then
-- باشگاه
local lowerSubtype = mw.ustring.lower(subtype)
if mw.ustring.find(subtype, "باشگاه") or mw.ustring.find(mw.ustring.lower(subtype), "club") then
 
------------------------------------------------------------
-- CLUB
------------------------------------------------------------
if mw.ustring.find(subtype, "باشگاه")
or mw.ustring.find(lowerSubtype, "club") then
return "club"
return "club"
end
end
-- آکادمی
 
if mw.ustring.find(subtype, "آکادمی") or mw.ustring.find(mw.ustring.lower(subtype), "academy") then
------------------------------------------------------------
-- ACADEMY
------------------------------------------------------------
if mw.ustring.find(subtype, "آکادمی")
or mw.ustring.find(lowerSubtype, "academy") then
return "academy"
return "academy"
end
end
-- ورزشگاه
 
if mw.ustring.find(subtype, "ورزشگاه") or mw.ustring.find(mw.ustring.lower(subtype), "stadium") then
------------------------------------------------------------
-- STADIUM
------------------------------------------------------------
if mw.ustring.find(subtype, "ورزشگاه")
or mw.ustring.find(lowerSubtype, "stadium") then
return "stadium"
return "stadium"
end
end
-- سالن ورزشی
 
if mw.ustring.find(subtype, "سالن") or mw.ustring.find(mw.ustring.lower(subtype), "gym") then
------------------------------------------------------------
-- GYM
------------------------------------------------------------
if mw.ustring.find(subtype, "سالن")
or mw.ustring.find(lowerSubtype, "gym") then
return "gym"
return "gym"
end
end
-- استخر
 
if mw.ustring.find(subtype, "استخر") or mw.ustring.find(mw.ustring.lower(subtype), "pool") then
------------------------------------------------------------
-- SWIMMING POOL
------------------------------------------------------------
if mw.ustring.find(subtype, "استخر")
or mw.ustring.find(lowerSubtype, "pool") then
return "pool"
return "pool"
end
end
-- زورخانه
 
if mw.ustring.find(subtype, "زورخانه") then
------------------------------------------------------------
-- ZURKHANEH
------------------------------------------------------------
if mw.ustring.find(subtype, "زورخانه")
or mw.ustring.find(lowerSubtype, "zurkhaneh") then
return "zurkhaneh"
return "zurkhaneh"
end
end
-- مدرسه ورزشی
 
if mw.ustring.find(subtype, "مدرسه") then
------------------------------------------------------------
-- SPORTS SCHOOL
------------------------------------------------------------
if mw.ustring.find(subtype, "مدرسه")
or mw.ustring.find(lowerSubtype, "school") then
return "sports_school"
return "sports_school"
end
end
-- مجموعه ورزشی
 
if mw.ustring.find(subtype, "مجموعه") then
------------------------------------------------------------
-- SPORTS COMPLEX
------------------------------------------------------------
if mw.ustring.find(subtype, "مجموعه")
or mw.ustring.find(lowerSubtype, "complex") then
return "sports_complex"
return "sports_complex"
end
end
-- هیئت
 
if mw.ustring.find(subtype, "هیئت") then
------------------------------------------------------------
-- SPORTS BOARD
------------------------------------------------------------
if mw.ustring.find(subtype, "هیئت")
or mw.ustring.find(lowerSubtype, "board") then
return "board"
return "board"
end
end
خط ۱۸۵: خط ۲۲۲:


----------------------------------------------------------------
----------------------------------------------------------------
-- INFERENCE FROM FIELDS (اگر subtype هم نبود)
-- INFERENCE FROM FIELDS
----------------------------------------------------------------
----------------------------------------------------------------
if args.established
if args.established
خط ۱۹۶: خط ۲۳۳:
or args.facilities
or args.facilities
or args.services
or args.services
or (args.address and args.city)
or (args.address and args.city) then
then
 
-- اگر فیلدهای سازمانی وجود داشت → باشگاه/مجموعه
------------------------------------------------------------
-- STADIUM
------------------------------------------------------------
if args.stadium or args.capacity then
if args.stadium or args.capacity then
return "stadium"
return "stadium"
end
end
------------------------------------------------------------
-- CLUB
------------------------------------------------------------
return "club"
return "club"
end
end


----------------------------------------------------------------
----------------------------------------------------------------
-- SAFE FALLBACK
-- DEFAULT ENTITY
----------------------------------------------------------------
----------------------------------------------------------------
local defaultEntity = data.defaults.entity_type or "player"
local defaultEntity = data.defaults.entity_type or "player"
defaultEntity = entityAliases[defaultEntity] or defaultEntity
 
defaultEntity = (data.entityAliases and data.entityAliases[defaultEntity])
or localEntityAliases[defaultEntity]
or defaultEntity


if data.entities[defaultEntity] then
if data.entities[defaultEntity] then
خط ۲۱۵: خط ۲۶۱:
end
end


----------------------------------------------------------------
-- FINAL SAFE FALLBACK
----------------------------------------------------------------
return "player"
return "player"
end
end
خط ۲۲۴: خط ۲۷۳:
return args.name
return args.name
end
end
local entityData =
local entityData = data.entities[entity]
data.entities[entity]
if entityData and entityData.title then
if entityData and entityData.title then
return entityData.title
return entityData.title
خط ۲۳۸: خط ۲۸۶:
return nil
return nil
end
end
local width =
local width = args.image_width or DEFAULT_IMAGE_WIDTH
args.image_width
local caption = args.caption or ""
or DEFAULT_IMAGE_WIDTH
local caption =
args.caption
or ""
return {
return {
type = "image",
type = "image",
خط ۲۵۷: خط ۳۰۱:
local fields = {}
local fields = {}
for _, item in ipairs(layout) do
for _, item in ipairs(layout) do
local label =
local label = item[1]
item[1]
local key = item[2]
local key =
local value = clean(args[key])
item[2]
local value =
clean(args[key])
----------------------------------------------------------------
-- ONLY ADD EXISTING VALUES
----------------------------------------------------------------
if value then
if value then
table.insert(
table.insert(fields, {
fields,
{
label = label,
label = label,
value = value
value = value
}
})
)
end
end
end
end
خط ۲۸۳: خط ۳۱۸:
local function buildGroups(entity, args)
local function buildGroups(entity, args)
local groups = {}
local groups = {}
local entityData =
local entityData = data.entities[entity]
data.entities[entity]
if not entityData then
if not entityData then
return groups
return groups
end
end
----------------------------------------------------------------
 
-- USE ONLY ENTITY-SPECIFIC LAYOUTS
--
-- IMPORTANT:
-- "base" is NOT automatically added.
-- It is displayed only when explicitly included
-- in the entity's layouts.
----------------------------------------------------------------
for _, layoutName in ipairs(entityData.layouts or {}) do
for _, layoutName in ipairs(entityData.layouts or {}) do
local layout =
local layout = data.layouts[layoutName]
data.layouts[layoutName]
if layout then
if layout then
local fields =
local fields = buildFields(layout, args)
buildFields(
layout,
args
)
----------------------------------------------------------------
-- DO NOT DISPLAY EMPTY GROUPS
----------------------------------------------------------------
if #fields > 0 then
if #fields > 0 then
local title =
local title = data.layoutTitles[layoutName] or layoutName
data.layoutTitles[layoutName]
table.insert(groups, {
or layoutName
table.insert(
groups,
{
name = title,
name = title,
fields = fields
fields = fields
}
})
)
end
end
end
end
خط ۳۲۸: خط ۳۴۲:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function buildMedals(args)
local function buildMedals(args)
local medals =
local medals = clean(args.medaltemplates)
clean(args.medaltemplates)
if not medals then
if not medals then
medals =
medals = clean(args.medals)
clean(args.medals)
end
end
if not medals then
if not medals then
return nil
return nil
end
end
local frame =
 
mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
local content =
local content = frame:expandTemplate{
frame:expandTemplate{
title = "Infobox medal templates",
title = "Infobox medal templates",
args = {
args = {
medals = medals,
medals = medals,
title =
title = args.medaltemplates_title or "",
args.medaltemplates_title
or "",
expand = "yes"
expand = "yes"
}
}
}
}
content =
content = clean(content)
clean(content)
if not content then
if not content then
return nil
return nil
خط ۳۶۲: خط ۳۷۰:
local function buildInfobox(entity, args)
local function buildInfobox(entity, args)
local box = {}
local box = {}
----------------------------------------------------------------
box.title = getTitle(entity, args)
-- TITLE
 
----------------------------------------------------------------
local image = buildImage(args)
box.title =
getTitle(
entity,
args
)
----------------------------------------------------------------
-- IMAGE
----------------------------------------------------------------
local image =
buildImage(args)
if image then
if image then
box.image = image
box.image = image
end
end
----------------------------------------------------------------
 
-- GROUPS
box.groups = buildGroups(entity, args)
----------------------------------------------------------------
box.medals = buildMedals(args)
box.groups =
 
buildGroups(
entity,
args
)
----------------------------------------------------------------
-- MEDALS
----------------------------------------------------------------
box.medals =
buildMedals(args)
return box
return box
end
end
خط ۳۹۸: خط ۳۸۷:
local function renderValue(value)
local function renderValue(value)
if type(value) == "table" then
if type(value) == "table" then
return table.concat(
return table.concat(value, "، ")
value,
"، "
)
end
end
return tostring(value)
return tostring(value)
end
--------------------------------------------------------------------------------
-- ESCAPE HTML
--------------------------------------------------------------------------------
local function escape(value)
if value == nil then
return ""
end
return mw.text.nowiki(
tostring(value)
)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۴۲۱: خط ۳۹۶:
local function renderInfobox(box)
local function renderInfobox(box)
local result = {}
local result = {}
----------------------------------------------------------------
 
-- OPEN TABLE
table.insert(result, '<table class="sport-infobox">')
----------------------------------------------------------------
 
table.insert(
-- Title
result,
'<table class="sport-infobox">'
)
----------------------------------------------------------------
-- TITLE
----------------------------------------------------------------
if box.title then
if box.title then
table.insert(
table.insert(result,
result,
'<tr class="sport-infobox-title">' ..
'<tr class="sport-infobox-title">' ..
'<th colspan="2">' .. box.title .. '</th></tr>'
'<th colspan="2">' ..
box.title ..
'</th></tr>'
)
)
end
end
----------------------------------------------------------------
 
-- IMAGE
-- Image
----------------------------------------------------------------
if box.image then
if box.image then
local image =
local image = box.image
box.image
local file = image.image
local file =
local size = image.size or DEFAULT_IMAGE_WIDTH
image.image
local caption = image.caption or ""
local size =
 
image.size
table.insert(result,
or DEFAULT_IMAGE_WIDTH
'<tr><td colspan="2" class="sport-infobox-image">' ..
local caption =
'[[File:' .. file .. '|' .. size .. ']]'
image.caption
or ""
table.insert(
result,
'<tr>' ..
'<td colspan="2" class="sport-infobox-image">' ..
'[[File:' ..
file ..
'|' ..
size ..
']]'
)
)
if caption ~= "" then
if caption ~= "" then
table.insert(
table.insert(result,
result,
'<div class="sport-infobox-caption">' .. caption .. '</div>'
'<div class="sport-infobox-caption">' ..
caption ..
'</div>'
)
)
end
end
table.insert(
table.insert(result, '</td></tr>')
result,
'</td></tr>'
)
end
end
----------------------------------------------------------------
 
-- GROUPS
-- Groups
----------------------------------------------------------------
for _, group in ipairs(box.groups) do
for _, group in ipairs(box.groups) do
----------------------------------------------------------------
table.insert(result,
-- GROUP HEADER
'<tr class="sport-infobox-header">' ..
----------------------------------------------------------------
'<th colspan="2">' .. group.name .. '</th></tr>'
table.insert(
result,
'<tr class="sport-infobox-header">' ..
'<th colspan="2">' ..
group.name ..
'</th></tr>'
)
)
----------------------------------------------------------------
-- GROUP FIELDS
----------------------------------------------------------------
for _, field in ipairs(group.fields) do
for _, field in ipairs(group.fields) do
table.insert(
table.insert(result,
result,
'<tr>' ..
'<tr>' ..
'<th class="sport-infobox-label">' .. field.label .. '</th>' ..
'<th class="sport-infobox-label">' ..
'<td class="sport-infobox-value">' .. renderValue(field.value) .. '</td>' ..
field.label ..
'</tr>'
'</th>' ..
'<td class="sport-infobox-value">' ..
renderValue(field.value) ..
'</td>' ..
'</tr>'
)
)
end
end
end
end
----------------------------------------------------------------
 
-- MEDALS
-- Medals
--
-- IMPORTANT:
-- Medals remain INSIDE the infobox table.
----------------------------------------------------------------
if box.medals then
if box.medals then
table.insert(
table.insert(result,
result,
'<tr><td colspan="2" class="sport-infobox-medals">' ..
'<tr>' ..
box.medals .. '</td></tr>'
'<td colspan="2" class="sport-infobox-medals">' ..
box.medals ..
'</td>' ..
'</tr>'
)
)
end
end
----------------------------------------------------------------
 
-- CLOSE TABLE
table.insert(result, '</table>')
----------------------------------------------------------------
return table.concat(result, "\n")
table.insert(
result,
'</table>'
)
return table.concat(
result,
"\n"
)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۵۴۰: خط ۴۵۷:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.main(frame)
function p.main(frame)
----------------------------------------------------------------
local args = getArgs(frame)
-- GET ARGUMENTS
args = normalize(args)
----------------------------------------------------------------
local entity = getEntity(args)
local args =
local box = buildInfobox(entity, args)
getArgs(frame)
----------------------------------------------------------------
-- NORMALIZE INPUT
----------------------------------------------------------------
args =
normalize(args)
----------------------------------------------------------------
-- ENTITY
----------------------------------------------------------------
local entity =
getEntity(args)
----------------------------------------------------------------
-- BUILD INFOBOX
----------------------------------------------------------------
local box =
buildInfobox(
entity,
args
)
----------------------------------------------------------------
-- RENDER
----------------------------------------------------------------
return renderInfobox(box)
return renderInfobox(box)
end
end
خط ۵۷۸: خط ۴۷۳:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.test(frame)
function p.test(frame)
local args =
local args = getArgs(frame)
getArgs(frame)
args = normalize(args)
----------------------------------------------------------------
local entity = getEntity(args)
-- NORMALIZE
local entityData = data.entities[entity]
----------------------------------------------------------------
 
args =
normalize(args)
----------------------------------------------------------------
-- ENTITY
----------------------------------------------------------------
local entity =
getEntity(args)
----------------------------------------------------------------
-- ENTITY DATA
----------------------------------------------------------------
local entityData =
data.entities[entity]
if not entityData then
if not entityData then
return "Entity not found: " .. tostring(entity)
return "Entity not found: " .. tostring(entity)
end
end
----------------------------------------------------------------
 
-- RESULT
----------------------------------------------------------------
local result = {}
local result = {}
table.insert(
table.insert(result, "Entity: " .. entity)
result,
table.insert(result, "Title: " .. tostring(entityData.title))
"Entity: " ..
table.insert(result, "Layouts: " .. table.concat(entityData.layouts or {}, ", "))
entity
table.insert(result, "Subtype: " .. tostring(args.subtype or "NOT FOUND"))
)
table.insert(result, "Coach: " .. tostring(args.coach or "NOT FOUND"))
table.insert(
table.insert(result, "Medals: " .. tostring(args.medaltemplates or args.medals or "NOT FOUND"))
result,
 
"Title: " ..
return table.concat(result, "\n")
tostring(
entityData.title
)
)
table.insert(
result,
"Layouts: " ..
table.concat(
entityData.layouts or {},
", "
)
)
----------------------------------------------------------------
-- DEBUG TYPE
----------------------------------------------------------------
table.insert(
result,
"Subtype: " ..
tostring(
args.subtype
or "NOT FOUND"
)
)
----------------------------------------------------------------
-- DEBUG COACH
----------------------------------------------------------------
table.insert(
result,
"Coach: " ..
tostring(
args.coach
or "NOT FOUND"
)
)
----------------------------------------------------------------
-- DEBUG MEDALS
----------------------------------------------------------------
table.insert(
result,
"Medals: " ..
tostring(
args.medaltemplates
or args.medals
or "NOT FOUND"
)
)
return table.concat(
result,
"\n"
)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۶۶۷: خط ۴۹۸:
local result = {}
local result = {}
for key, value in pairs(data.entities) do
for key, value in pairs(data.entities) do
table.insert(
table.insert(result, key .. " : " .. tostring(value.title))
result,
key ..
" : " ..
tostring(
value.title
)
)
end
end
table.sort(result)
table.sort(result)
return table.concat(
return table.concat(result, "\n")
result,
"\n"
)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۶۸۶: خط ۵۰۷:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function p.version()
function p.version()
return
return data.version.name .. " v" .. data.version.major .. "." .. data.version.minor
data.version.name ..
" v" ..
data.version.major ..
"." ..
data.version.minor
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

نسخهٔ کنونی تا ‏۱۰ اوت ۲۰۲۶، ساعت ۱۸:۲۹

توضیحات این پودمان می‌تواند در پودمان:AlborzSports/توضیحات قرار گیرد.

--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Core Engine
-- Version 2.2.2 (Fixed Entity Detection Priority)
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzSports/Data")
--------------------------------------------------------------------------------
-- CONFIG
--------------------------------------------------------------------------------
local DEFAULT_IMAGE_WIDTH = data.meta.imageWidth or "280px"
--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
--------------------------------------------------------------------------------
local function clean(value)
	if value == nil then
		return nil
	end
	if type(value) == "string" then
		value = mw.text.trim(value)
		if value == "" then
			return nil
		end
	end
	return value
end
--------------------------------------------------------------------------------
-- GET ARGUMENTS
--------------------------------------------------------------------------------
local function getArgs(frame)
	local args = {}
	local source = frame
	local parent = frame:getParent()
	if parent and next(parent.args) then
		source = parent
	end
	for key, value in pairs(source.args) do
		local cleanKey = mw.text.trim(tostring(key))
		local cleanValue = clean(value)
		if cleanKey ~= "" and cleanValue then
			args[cleanKey] = cleanValue
		end
	end
	return args
end
--------------------------------------------------------------------------------
-- NORMALIZE INPUT
--------------------------------------------------------------------------------
local function normalize(args)
	local result = {}
	for key, value in pairs(args) do
		local field = data.lookup[key] or key
		result[field] = value
	end
	return result
end
--------------------------------------------------------------------------------
-- LOCAL ENTITY ALIASES (تکمیل‌کننده Data برای پشتیبانی کامل فارسی)
--------------------------------------------------------------------------------
local localEntityAliases = {
	-- People
	player = "player",
	athlete = "player",
	["بازیکن"] = "player",
	["ورزشکار"] = "player",
	coach = "coach",
	["مربی"] = "coach",
	referee = "referee",
	["داور"] = "referee",
	manager = "manager",
	["مدیر ورزشی"] = "manager",
	["مدیر"] = "manager",

	-- Clubs
	club = "club",
	sports_club = "club",
	sport_club = "club",
	["باشگاه"] = "club",
	["باشگاه ورزشی"] = "club",

	-- Academy
	academy = "academy",
	["آکادمی"] = "academy",
	["آکادمی ورزشی"] = "academy",

	-- Facilities
	stadium = "stadium",
	["ورزشگاه"] = "stadium",
	gym = "gym",
	gymnasium = "gym",
	["سالن"] = "gym",
	["سالن ورزشی"] = "gym",
	pool = "pool",
	swimming_pool = "pool",
	["استخر"] = "pool",
	zurkhaneh = "zurkhaneh",
	["زورخانه"] = "zurkhaneh",
	sports_school = "sports_school",
	["مدرسه ورزشی"] = "sports_school",
	sports_complex = "sports_complex",
	["مجموعه ورزشی"] = "sports_complex",
	facility = "facility",
	["مجموعه"] = "facility",

	-- Organization / Board
	board = "board",
	sports_board = "board",
	["هیئت"] = "board",
	["هیئت ورزشی"] = "board",

	-- Competition
	competition = "competition",
	["مسابقه"] = "competition",
	["مسابقات"] = "competition"
}
--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------
local function getEntity(args)
	local entity = clean(args.entity_type)

	----------------------------------------------------------------
	-- EXPLICIT ENTITY TYPE
	--
	-- اگر کاربر صریحاً entity_type داده باشد،
	-- بالاترین اولویت را دارد.
	----------------------------------------------------------------
	if entity then
		entity = (data.entityAliases and data.entityAliases[entity])
			or localEntityAliases[entity]
			or entity

		if data.entities[entity] then
			return entity
		end
	end

	----------------------------------------------------------------
	-- INFERENCE FROM SUBTYPE (نوع)
	----------------------------------------------------------------
	local subtype = clean(args.subtype)

	if subtype then
		local lowerSubtype = mw.ustring.lower(subtype)

		------------------------------------------------------------
		-- CLUB
		------------------------------------------------------------
		if mw.ustring.find(subtype, "باشگاه")
			or mw.ustring.find(lowerSubtype, "club") then
			return "club"
		end

		------------------------------------------------------------
		-- ACADEMY
		------------------------------------------------------------
		if mw.ustring.find(subtype, "آکادمی")
			or mw.ustring.find(lowerSubtype, "academy") then
			return "academy"
		end

		------------------------------------------------------------
		-- STADIUM
		------------------------------------------------------------
		if mw.ustring.find(subtype, "ورزشگاه")
			or mw.ustring.find(lowerSubtype, "stadium") then
			return "stadium"
		end

		------------------------------------------------------------
		-- GYM
		------------------------------------------------------------
		if mw.ustring.find(subtype, "سالن")
			or mw.ustring.find(lowerSubtype, "gym") then
			return "gym"
		end

		------------------------------------------------------------
		-- SWIMMING POOL
		------------------------------------------------------------
		if mw.ustring.find(subtype, "استخر")
			or mw.ustring.find(lowerSubtype, "pool") then
			return "pool"
		end

		------------------------------------------------------------
		-- ZURKHANEH
		------------------------------------------------------------
		if mw.ustring.find(subtype, "زورخانه")
			or mw.ustring.find(lowerSubtype, "zurkhaneh") then
			return "zurkhaneh"
		end

		------------------------------------------------------------
		-- SPORTS SCHOOL
		------------------------------------------------------------
		if mw.ustring.find(subtype, "مدرسه")
			or mw.ustring.find(lowerSubtype, "school") then
			return "sports_school"
		end

		------------------------------------------------------------
		-- SPORTS COMPLEX
		------------------------------------------------------------
		if mw.ustring.find(subtype, "مجموعه")
			or mw.ustring.find(lowerSubtype, "complex") then
			return "sports_complex"
		end

		------------------------------------------------------------
		-- SPORTS BOARD
		------------------------------------------------------------
		if mw.ustring.find(subtype, "هیئت")
			or mw.ustring.find(lowerSubtype, "board") then
			return "board"
		end
	end

	----------------------------------------------------------------
	-- INFERENCE FROM FIELDS
	----------------------------------------------------------------
	if args.established
		or args.manager
		or args.owner
		or args.president
		or args.ceo
		or args.stadium
		or args.capacity
		or args.facilities
		or args.services
		or (args.address and args.city) then

		------------------------------------------------------------
		-- STADIUM
		------------------------------------------------------------
		if args.stadium or args.capacity then
			return "stadium"
		end

		------------------------------------------------------------
		-- CLUB
		------------------------------------------------------------
		return "club"
	end

	----------------------------------------------------------------
	-- DEFAULT ENTITY
	----------------------------------------------------------------
	local defaultEntity = data.defaults.entity_type or "player"

	defaultEntity = (data.entityAliases and data.entityAliases[defaultEntity])
		or localEntityAliases[defaultEntity]
		or defaultEntity

	if data.entities[defaultEntity] then
		return defaultEntity
	end

	----------------------------------------------------------------
	-- FINAL SAFE FALLBACK
	----------------------------------------------------------------
	return "player"
end
--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------
local function getTitle(entity, args)
	if args.name then
		return args.name
	end
	local entityData = data.entities[entity]
	if entityData and entityData.title then
		return entityData.title
	end
	return data.meta.title
end
--------------------------------------------------------------------------------
-- IMAGE HANDLING
--------------------------------------------------------------------------------
local function buildImage(args)
	if not args.image then
		return nil
	end
	local width = args.image_width or DEFAULT_IMAGE_WIDTH
	local caption = args.caption or ""
	return {
		type = "image",
		image = args.image,
		size = width,
		caption = caption
	}
end
--------------------------------------------------------------------------------
-- FIELD BUILDER
--------------------------------------------------------------------------------
local function buildFields(layout, args)
	local fields = {}
	for _, item in ipairs(layout) do
		local label = item[1]
		local key = item[2]
		local value = clean(args[key])
		if value then
			table.insert(fields, {
				label = label,
				value = value
			})
		end
	end
	return fields
end
--------------------------------------------------------------------------------
-- GROUP BUILDER
--------------------------------------------------------------------------------
local function buildGroups(entity, args)
	local groups = {}
	local entityData = data.entities[entity]
	if not entityData then
		return groups
	end

	for _, layoutName in ipairs(entityData.layouts or {}) do
		local layout = data.layouts[layoutName]
		if layout then
			local fields = buildFields(layout, args)
			if #fields > 0 then
				local title = data.layoutTitles[layoutName] or layoutName
				table.insert(groups, {
					name = title,
					fields = fields
				})
			end
		end
	end
	return groups
end
--------------------------------------------------------------------------------
-- MEDAL BUILDER
--------------------------------------------------------------------------------
local function buildMedals(args)
	local medals = clean(args.medaltemplates)
	if not medals then
		medals = clean(args.medals)
	end
	if not medals then
		return nil
	end

	local frame = mw.getCurrentFrame()
	local content = frame:expandTemplate{
		title = "Infobox medal templates",
		args = {
			medals = medals,
			title = args.medaltemplates_title or "",
			expand = "yes"
		}
	}
	content = clean(content)
	if not content then
		return nil
	end
	return content
end
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------
local function buildInfobox(entity, args)
	local box = {}
	box.title = getTitle(entity, args)

	local image = buildImage(args)
	if image then
		box.image = image
	end

	box.groups = buildGroups(entity, args)
	box.medals = buildMedals(args)

	return box
end
--------------------------------------------------------------------------------
-- RENDER VALUE
--------------------------------------------------------------------------------
local function renderValue(value)
	if type(value) == "table" then
		return table.concat(value, "، ")
	end
	return tostring(value)
end
--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------
local function renderInfobox(box)
	local result = {}

	table.insert(result, '<table class="sport-infobox">')

	-- Title
	if box.title then
		table.insert(result,
			'<tr class="sport-infobox-title">' ..
			'<th colspan="2">' .. box.title .. '</th></tr>'
		)
	end

	-- Image
	if box.image then
		local image = box.image
		local file = image.image
		local size = image.size or DEFAULT_IMAGE_WIDTH
		local caption = image.caption or ""

		table.insert(result,
			'<tr><td colspan="2" class="sport-infobox-image">' ..
			'[[File:' .. file .. '|' .. size .. ']]'
		)
		if caption ~= "" then
			table.insert(result,
				'<div class="sport-infobox-caption">' .. caption .. '</div>'
			)
		end
		table.insert(result, '</td></tr>')
	end

	-- Groups
	for _, group in ipairs(box.groups) do
		table.insert(result,
			'<tr class="sport-infobox-header">' ..
			'<th colspan="2">' .. group.name .. '</th></tr>'
		)
		for _, field in ipairs(group.fields) do
			table.insert(result,
				'<tr>' ..
				'<th class="sport-infobox-label">' .. field.label .. '</th>' ..
				'<td class="sport-infobox-value">' .. renderValue(field.value) .. '</td>' ..
				'</tr>'
			)
		end
	end

	-- Medals
	if box.medals then
		table.insert(result,
			'<tr><td colspan="2" class="sport-infobox-medals">' ..
			box.medals .. '</td></tr>'
		)
	end

	table.insert(result, '</table>')
	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame)
	args = normalize(args)
	local entity = getEntity(args)
	local box = buildInfobox(entity, args)
	return renderInfobox(box)
end
--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
--------------------------------------------------------------------------------
function p.infobox(frame)
	return p.main(frame)
end
--------------------------------------------------------------------------------
-- DEBUG FUNCTION
--------------------------------------------------------------------------------
function p.test(frame)
	local args = getArgs(frame)
	args = normalize(args)
	local entity = getEntity(args)
	local entityData = data.entities[entity]

	if not entityData then
		return "Entity not found: " .. tostring(entity)
	end

	local result = {}
	table.insert(result, "Entity: " .. entity)
	table.insert(result, "Title: " .. tostring(entityData.title))
	table.insert(result, "Layouts: " .. table.concat(entityData.layouts or {}, ", "))
	table.insert(result, "Subtype: " .. tostring(args.subtype or "NOT FOUND"))
	table.insert(result, "Coach: " .. tostring(args.coach or "NOT FOUND"))
	table.insert(result, "Medals: " .. tostring(args.medaltemplates or args.medals or "NOT FOUND"))

	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- GET ENTITY LIST
--------------------------------------------------------------------------------
function p.entities()
	local result = {}
	for key, value in pairs(data.entities) do
		table.insert(result, key .. " : " .. tostring(value.title))
	end
	table.sort(result)
	return table.concat(result, "\n")
end
--------------------------------------------------------------------------------
-- GET VERSION
--------------------------------------------------------------------------------
function p.version()
	return data.version.name .. " v" .. data.version.major .. "." .. data.version.minor
end
--------------------------------------------------------------------------------
-- EXPORT
--------------------------------------------------------------------------------
return p