Content deleted Content added
Alistair3149 (talk | contribs) No edit summary |
Alistair3149 (talk | contribs) No edit summary |
||
Line 8: | Line 8: | ||
metatable.__index = methodtable |
metatable.__index = methodtable |
||
local i18n = require( 'Module:i18n' ):new() |
|||
local TNT = require( 'Module:Translate' ):new() |
local TNT = require( 'Module:Translate' ):new() |
||
local common = require( 'Module:Common' ) |
local common = require( 'Module:Common' ) |
||
Line 23: | Line 24: | ||
local moduleCache = {} |
local moduleCache = {} |
||
--- Wrapper function for Module:i18n.translate |
|||
--- |
|||
--- @param key string The translation key |
|||
--- @return string If the key was not found, the key is returned |
|||
local function t( key ) |
|||
return i18n:translate( key ) |
|||
end |
|||
Line 135: | Line 145: | ||
) |
) |
||
setData[ |
setData[ t( 'SMW_Name' ) ] = self.frameArgs[ translate( 'ARG_Name' ) ] or common.removeTypeSuffix( |
||
mw.title.getCurrentTitle().text, |
mw.title.getCurrentTitle().text, |
||
config.name_suffixes |
config.name_suffixes |
||
) |
) |
||
if type( setData[ |
if type( setData[ t( 'SMW_Manufacturer' ) ] ) == 'string' then |
||
local man = manufacturer( setData[ |
local man = manufacturer( setData[ t( 'SMW_Manufacturer' ) ] ) |
||
if man ~= nil then man = man.name end |
if man ~= nil then man = man.name end |
||
setData[ |
setData[ t( 'SMW_Manufacturer' ) ] = man or setData[ t( 'SMW_Manufacturer' ) ] |
||
setData[ |
setData[ t( 'SMW_Manufacturer' ) ] = mw.ustring.format( '[[%s]]', setData[ t( 'SMW_Manufacturer' ) ] ) |
||
end |
end |
||
Line 158: | Line 168: | ||
if self.apiData.sub_type ~= 'UNDEFINED' then |
if self.apiData.sub_type ~= 'UNDEFINED' then |
||
-- SMW_Type is already set prior if self.apiData.type exists |
-- SMW_Type is already set prior if self.apiData.type exists |
||
setData[ |
setData[ t( 'SMW_Type' ) ] = mw.ustring.format( '%s.%s', setData[ t( 'SMW_Type' ) ], self.apiData.sub_type ) |
||
end |
end |
||
Line 168: | Line 178: | ||
local descType = descObj.type |
local descType = descObj.type |
||
-- FIXME: This only works for English, need some way to get only the English text for comparison since descType is always in English |
-- FIXME: This only works for English, need some way to get only the English text for comparison since descType is always in English |
||
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( setData[ |
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( setData[ t( 'SMW_Type' ) ] ) ) ) |
||
-- If the type in item description is different than what we compose out of type and subtype data, record it in subtype |
-- If the type in item description is different than what we compose out of type and subtype data, record it in subtype |
||
-- TODO: We should make a common function to sanitize strings for comaprison (e.g. lowercase, remove all the space) |
-- TODO: We should make a common function to sanitize strings for comaprison (e.g. lowercase, remove all the space) |
||
if mw.ustring.lower( descType ) ~= mw.ustring.lower( itemType ) then |
if mw.ustring.lower( descType ) ~= mw.ustring.lower( itemType ) then |
||
setData[ |
setData[ t( 'SMW_Subtype' ) ] = descType |
||
end |
end |
||
end |
end |
||
Line 181: | Line 191: | ||
end |
end |
||
runModuleFN( setData[ |
runModuleFN( setData[ t( 'SMW_Type' ) ], 'addSmwProperties', { self.apiData, self.frameArgs, setData } ) |
||
mw.logObject( setData, 'SET' ) |
mw.logObject( setData, 'SET' ) |
||
Line 195: | Line 205: | ||
function methodtable.getSmwData( self ) |
function methodtable.getSmwData( self ) |
||
-- Use cached data if possible, SMW queries are expensive |
-- Use cached data if possible, SMW queries are expensive |
||
if self.smwData ~= nil and self.smwData[ |
if self.smwData ~= nil and self.smwData[ t( 'SMW_Name' ) ] ~= nil then |
||
return self.smwData |
return self.smwData |
||
end |
end |
||
Line 241: | Line 251: | ||
local function getManufacturer() |
local function getManufacturer() |
||
if smwData[ |
if smwData[ t( 'SMW_Manufacturer' ) ] == nil then return end |
||
local mfu = manufacturer( smwData[ |
local mfu = manufacturer( smwData[ t( 'SMW_Manufacturer' ) ] ) |
||
if mfu == nil then return '[[' .. smwData[ |
if mfu == nil then return '[[' .. smwData[ t( 'SMW_Manufacturer' ) ] .. ']]' end |
||
return infobox.showDescIfDiff( |
return infobox.showDescIfDiff( |
||
table.concat( { '[[', smwData[ |
table.concat( { '[[', smwData[ t( 'SMW_Manufacturer' ) ], '|', mfu.name , ']]' } ), |
||
mfu.code |
mfu.code |
||
) |
) |
||
Line 253: | Line 263: | ||
local function getType() |
local function getType() |
||
if smwData[ |
if smwData[ t( 'SMW_Type' ) ] == nil then return end |
||
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( smwData[ |
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( smwData[ t( 'SMW_Type' ) ] ) ) ) |
||
if mw.ustring.find( itemType, 'type_' ) then |
if mw.ustring.find( itemType, 'type_' ) then |
||
itemType = smwData[ |
itemType = smwData[ t( 'SMW_Type' ) ] |
||
end |
end |
||
Line 265: | Line 275: | ||
local function getSize() |
local function getSize() |
||
if smwData[ |
if smwData[ t( 'SMW_Size' ) ] == nil then return end |
||
return 'S' .. smwData[ |
return 'S' .. smwData[ t( 'SMW_Size' ) ] |
||
end |
end |
||
local function getClass() |
local function getClass() |
||
if smwData[ |
if smwData[ t( 'SMW_Class' ) ] == nil then return end |
||
local classKey = mw.ustring.lower( smwData[ |
local classKey = mw.ustring.lower( smwData[ t( 'SMW_Class' ) ] ) |
||
local class = translate( mw.ustring.format( 'class_%s', classKey ) ) |
local class = translate( mw.ustring.format( 'class_%s', classKey ) ) |
||
if smwData[ |
if smwData[ t( 'SMW_Grade' ) ] ~= nil then |
||
class = class .. ' (' .. smwData[ |
class = class .. ' (' .. smwData[ t( 'SMW_Grade' ) ] .. ')' |
||
end |
end |
||
Line 332: | Line 342: | ||
infobox:renderHeader( { |
infobox:renderHeader( { |
||
title = smwData[ |
title = smwData[ t( 'SMW_Name' ) ], |
||
--- e.g. Aegis Dynamics (AEGS) |
--- e.g. Aegis Dynamics (AEGS) |
||
subtitle = getManufacturer(), |
subtitle = getManufacturer(), |
||
Line 348: | Line 358: | ||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Subtype' ), |
label = translate( 'LBL_Subtype' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_Subtype' ) ], |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
Line 356: | Line 366: | ||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Occupancy' ), |
label = translate( 'LBL_Occupancy' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_Occupancy' ) ], |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Inventory' ), |
label = translate( 'LBL_Inventory' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_Inventory' ) ], |
||
} ) |
} ) |
||
}, |
}, |
||
Line 367: | Line 377: | ||
local pageIdentifier = self.frameArgs[ translate( 'ARG_SmwQueryName' ) ] or mw.title.getCurrentTitle().fullText |
local pageIdentifier = self.frameArgs[ translate( 'ARG_SmwQueryName' ) ] or mw.title.getCurrentTitle().fullText |
||
runModuleFN( smwData[ |
runModuleFN( smwData[ t( 'SMW_Type' ) ], 'addInfoboxData', { infobox, smwData, pageIdentifier } ) |
||
--- Dimensions |
--- Dimensions |
||
Line 375: | Line 385: | ||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Length' ), |
label = translate( 'LBL_Length' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_EntityLength' ) ], |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Width' ), |
label = translate( 'LBL_Width' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_EntityWidth' ) ], |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Height' ), |
label = translate( 'LBL_Height' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_EntityHeight' ) ], |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = translate( 'LBL_Mass' ), |
label = translate( 'LBL_Mass' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_Mass' ) ], |
||
} ) |
} ) |
||
}, |
}, |
||
Line 398: | Line 408: | ||
content = { |
content = { |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = |
label = t( 'SMW_UUID' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_UUID' ) ], |
||
row = true, |
row = true, |
||
spacebetween = true |
spacebetween = true |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = |
label = t( 'SMW_ClassName' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_ClassName' ) ], |
||
row = true, |
row = true, |
||
spacebetween = true |
spacebetween = true |
||
} ), |
} ), |
||
infobox:renderItem( { |
infobox:renderItem( { |
||
label = |
label = t( 'SMW_GameBuild' ), |
||
data = smwData[ |
data = smwData[ t( 'SMW_GameBuild' ) ], |
||
row = true, |
row = true, |
||
spacebetween = true |
spacebetween = true |
||
Line 419: | Line 429: | ||
--- Actions section |
--- Actions section |
||
if smwData[ |
if smwData[ t( 'SMW_UUID' ) ] then |
||
infobox:renderSection( { |
infobox:renderSection( { |
||
class = 'infobox__section--actions infobox__section--hasBackground', |
class = 'infobox__section--actions infobox__section--hasBackground', |
||
Line 428: | Line 438: | ||
desc = translate( 'actions_find_item_text' ), |
desc = translate( 'actions_find_item_text' ), |
||
-- FIXME: Make this configurable? |
-- FIXME: Make this configurable? |
||
link = 'https://finder.cstone.space/search/' .. smwData[ |
link = 'https://finder.cstone.space/search/' .. smwData[ t( 'SMW_UUID' ) ] |
||
} ) |
} ) |
||
} |
} |
||
Line 456: | Line 466: | ||
} ) |
} ) |
||
return infobox:renderInfobox( nil, smwData[ |
return infobox:renderInfobox( nil, smwData[ t( 'SMW_Name' ) ] ) |
||
end |
end |
||
Line 474: | Line 484: | ||
--- Error: No description SMW property |
--- Error: No description SMW property |
||
if smwData[ |
if smwData[ t( 'SMW_Description' ) ] == nil then |
||
return require( 'Module:Mbox' )._mbox( |
return require( 'Module:Mbox' )._mbox( |
||
translate( 'error_no_description_title' ), |
translate( 'error_no_description_title' ), |
||
Line 482: | Line 492: | ||
end |
end |
||
return '<blockquote>' .. smwData[ |
return '<blockquote>' .. smwData[ t( 'SMW_Description' ) ] .. '</blockquote>' |
||
end |
end |
||
Line 500: | Line 510: | ||
local output = {} |
local output = {} |
||
local uuid = smwData[ |
local uuid = smwData[ t( 'SMW_UUID' ) ] |
||
if uuid then |
if uuid then |
||
-- Create find item button |
-- Create find item button |
||
Line 554: | Line 564: | ||
--- Only set category if category_type value exists |
--- Only set category if category_type value exists |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Type' ) ] ~= nil then |
||
local typeCategory = translate( 'category_' .. mw.ustring.lower( self.smwData[ |
local typeCategory = translate( 'category_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] ) ) |
||
if typeCategory ~= nil and typeCategory ~= 'category_' .. mw.ustring.lower( self.smwData[ |
if typeCategory ~= nil and typeCategory ~= 'category_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] ) then |
||
table.insert( self.categories, typeCategory ) |
table.insert( self.categories, typeCategory ) |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Size' ) ] ~= nil then |
||
addSubcategory( typeCategory, |
addSubcategory( typeCategory, t( 'SMW_Size' ) .. ' ' .. self.smwData[ t( 'SMW_Size' ) ] ) |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Grade' ) ] ~= nil then |
||
addSubcategory( typeCategory, |
addSubcategory( typeCategory, t( 'SMW_Grade' ) .. ' ' .. self.smwData[ t( 'SMW_Grade' ) ] ) |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Subtype' ) ] ~= nil then |
||
addSubcategory( typeCategory, self.smwData[ |
addSubcategory( typeCategory, self.smwData[ t( 'SMW_Subtype' ) ] ) |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Class' ) ] ~= nil then |
||
addSubcategory( typeCategory, self.smwData[ |
addSubcategory( typeCategory, self.smwData[ t( 'SMW_Class' ) ] ) |
||
end |
end |
||
end |
end |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Manufacturer' ) ] ~= nil then |
||
local manufacturer = mw.ustring.gsub( self.smwData[ |
local manufacturer = mw.ustring.gsub( self.smwData[ t( 'SMW_Manufacturer' ) ], '%[+', '' ) |
||
manufacturer = mw.ustring.gsub( manufacturer, '%]+', '' ) |
manufacturer = mw.ustring.gsub( manufacturer, '%]+', '' ) |
||
Line 587: | Line 597: | ||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_UUID' ) ] == nil then |
||
table.insert( self.categories, translate( 'error_category_item_missing_uuid' ) ) |
table.insert( self.categories, translate( 'error_category_item_missing_uuid' ) ) |
||
end |
end |
||
runModuleFN( self.smwData[ |
runModuleFN( self.smwData[ t( 'SMW_Type' ) ], 'addCategories', { self.categories, self.frameArgs, self.smwData } ) |
||
end |
end |
||
Line 600: | Line 610: | ||
local itemType = translate( 'type_item' ) |
local itemType = translate( 'type_item' ) |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Type' ) ] ~= nil then |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Subtype' ) ] ~= nil then |
||
-- TODO: Localize subtype |
-- TODO: Localize subtype |
||
itemType = self.smwData[ |
itemType = self.smwData[ t( 'SMW_Subtype' ) ] |
||
else |
else |
||
local itemTypeKey = 'type_' .. mw.ustring.lower( self.smwData[ |
local itemTypeKey = 'type_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] ) |
||
if translate( itemTypeKey ) ~= nil and translate( itemTypeKey ) ~= itemTypeKey then |
if translate( itemTypeKey ) ~= nil and translate( itemTypeKey ) ~= itemTypeKey then |
||
itemType = translate( itemTypeKey ) |
itemType = translate( itemTypeKey ) |
||
Line 615: | Line 625: | ||
shortdesc = itemType |
shortdesc = itemType |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Class' ) ] ~= nil then |
||
shortdesc = mw.ustring.format( '%s %s', |
shortdesc = mw.ustring.format( '%s %s', |
||
string.lower( self.smwData[ |
string.lower( self.smwData[ t( 'SMW_Class' ) ] ), |
||
shortdesc |
shortdesc |
||
) |
) |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Grade' ) ] ~= nil then |
||
shortdesc = translate( 'shortdesc_grade', false, self.smwData[ |
shortdesc = translate( 'shortdesc_grade', false, self.smwData[ t( 'SMW_Grade' ) ], shortdesc ) |
||
end |
end |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Size' ) ] ~= nil then |
||
shortdesc = mw.ustring.format( 'S%d %s', |
shortdesc = mw.ustring.format( 'S%d %s', |
||
self.smwData[ |
self.smwData[ t( 'SMW_Size' ) ], |
||
shortdesc |
shortdesc |
||
) |
) |
||
Line 634: | Line 644: | ||
--- Manufacturer |
--- Manufacturer |
||
if self.smwData[ |
if self.smwData[ t( 'SMW_Manufacturer' ) ] ~= nil and self.smwData[ t( 'SMW_Manufacturer' ) ] ~= 'Unknown manufacturer' then |
||
local mfuname = self.smwData[ |
local mfuname = self.smwData[ t( 'SMW_Manufacturer' ) ] |
||
local man = manufacturer( mfuname ) |
local man = manufacturer( mfuname ) |
||
--- Use short name if possible |
--- Use short name if possible |
||
Line 645: | Line 655: | ||
--- Submodule override |
--- Submodule override |
||
shortdesc = runModuleFN( |
shortdesc = runModuleFN( |
||
self.smwData[ |
self.smwData[ t( 'SMW_Type' ) ], |
||
'getShortDescription', |
'getShortDescription', |
||
{ self.frameArgs, self.smwData }, |
{ self.frameArgs, self.smwData }, |
Revision as of 21:52, 8 May 2024
This documentation is transcluded from Module:Item/doc. Changes can be proposed in the talk page.
Module:Item is shared across the Star Citizen Wikis.
This module is shared across the Star Citizen Wikis. Any changes should also be relayed to the GitHub repository.
Module:Item loads configuration from Module:Item/config.json.
This module can be configurated from the config.json subpage.
Module:Item loads messages from Module:Item/i18n.json.
This module is designed to be language-neutral. All of the messages are saved in the i18n.json subpage.
This module is unused.
This module is neither invoked by a template nor required/loaded by another module. If this is in error, make sure to add
{{Documentation}}
/{{No documentation}}
to the calling template's or parent's module documentation.Module:Item requires 15 modules.
Function list |
---|
L 32 — t L 42 — translate L 53 — runModuleFN L 84 — makeSmwQueryObject L 108 — methodtable.getApiDataForCurrentPage L 132 — methodtable.setSemanticProperties L 205 — methodtable.getSmwData L 233 — methodtable.getInfobox L 252 — getManufacturer L 264 — getType L 276 — getSize L 281 — getClass L 295 — getOfficialSites L 312 — getCommunitySites L 473 — methodtable.getDescription L 498 — methodtable.getAvailability L 549 — methodtable.setFrame L 556 — methodtable.setCategories L 561 — addSubcategory L 608 — methodtable.setShortDescription L 671 — methodtable.saveApiData L 678 — methodtable.makeDebugOutput L 708 — methodtable.getCategories L 724 — Item.new L 739 — Item.loadApiData L 764 — Item.infobox L 781 — Item.description L 792 — Item.availability L 803 — Item.main L 828 — Item.test |
Submodules:
- Module:Item/CharArmor
- Module:Item/Food
- Module:Item/FPSConsumable
- Module:Item/VehicleItem
- Module:Item/WeaponAttachment
- Module:Item/WeaponPersonal
require( 'strict' )
local Item = {}
local metatable = {}
local methodtable = {}
metatable.__index = methodtable
local i18n = require( 'Module:i18n' ):new()
local TNT = require( 'Module:Translate' ):new()
local common = require( 'Module:Common' )
local manufacturer = require( 'Module:Manufacturer' )._manufacturer
local hatnote = require( 'Module:Hatnote' )._hatnote
local data = mw.loadJsonData( 'Module:Item/data.json' )
local config = mw.loadJsonData( 'Module:Item/config.json' )
local lang
if config.module_lang then
lang = mw.getLanguage( config.module_lang )
else
lang = mw.getContentLanguage()
end
local moduleCache = {}
--- Wrapper function for Module:i18n.translate
---
--- @param key string The translation key
--- @return string If the key was not found, the key is returned
local function t( key )
return i18n:translate( key )
end
--- Wrapper function for Module:Translate.translate
---
--- @param key string The translation key
--- @param addSuffix boolean|nil Adds a language suffix if config.smw_multilingual_text is true
--- @return string If the key was not found in the .tab page, the key is returned
local function translate( key, addSuffix, ... )
return TNT:translate( 'Module:Item/i18n.json', config, key, addSuffix, {...} ) or key
end
--- Invokes a method on the required module, if the modules type matches targetType
--- Utilizes the moduleCache to only load modules once
---
--- @param targetType string|boolean The type to check against extension_modules.type or 'true' to run against all modules
--- @param methodName string The method to invoke
--- @param args table Arguments passed to the method
local function runModuleFN( targetType, methodName, args, returnsData )
returnsData = returnsData or false
for _, module in ipairs( data.extension_modules ) do
if targetType == true or ( module.type ~= nil and type( module.type ) == 'table' ) then
for _, type in ipairs( module.type ) do
if module ~= nil and ( targetType == true or targetType == type ) then
if moduleCache[ module.name ] == nil then
local success, mod = pcall( require, module.name )
if success then
moduleCache[ module.name ] = mod
end
end
module = moduleCache[ module.name ]
if module ~= nil then
local result = module[ methodName ]( unpack( args ) )
if returnsData then
return result
end
end
end
end
end
end
end
--- Creates the object that is used to query the SMW store
---
--- @param page string the item page containing data
--- @return table
local function makeSmwQueryObject( page )
local query = {
string.format( '[[%s]]', page ),
'?Page image#-=image'
}
require( 'Module:Common/SMW' ).addSmwAskProperties(
query,
translate,
config,
data
)
runModuleFN( true, 'addSmwAskProperties', { query } )
table.insert( query, 'limit=1' )
return query
end
--- Request Api Data
--- Using current subpage name without item type suffix
--- @return table|nil
function methodtable.getApiDataForCurrentPage( self )
local api = require( 'Module:Common/Api' )
local query = self.frameArgs[ translate( 'ARG_UUID' ) ] or self.frameArgs[ translate( 'ARG_Name' ) ] or common.removeTypeSuffix(
mw.title.getCurrentTitle().text,
config.name_suffixes
)
local success, json = pcall( mw.text.jsonDecode, mw.ext.Apiunto.get_raw( 'v2/items/' .. query, {
include = data.includes,
locale = config.api_locale
} ) )
if not success or api.checkResponseStructure( json, true, false ) == false then return end
self.apiData = json[ 'data' ]
self.apiData = api.makeAccessSafe( self.apiData )
return self.apiData
end
--- Base Properties that are shared across all items
--- @return table SMW Result
function methodtable.setSemanticProperties( self )
local setData = {}
local smwCommon = require( 'Module:Common/SMW' )
smwCommon.addSmwProperties(
self.apiData,
self.frameArgs,
setData,
translate,
config,
data,
'Item'
)
setData[ t( 'SMW_Name' ) ] = self.frameArgs[ translate( 'ARG_Name' ) ] or common.removeTypeSuffix(
mw.title.getCurrentTitle().text,
config.name_suffixes
)
if type( setData[ t( 'SMW_Manufacturer' ) ] ) == 'string' then
local man = manufacturer( setData[ t( 'SMW_Manufacturer' ) ] )
if man ~= nil then man = man.name end
setData[ t( 'SMW_Manufacturer' ) ] = man or setData[ t( 'SMW_Manufacturer' ) ]
setData[ t( 'SMW_Manufacturer' ) ] = mw.ustring.format( '[[%s]]', setData[ t( 'SMW_Manufacturer' ) ] )
end
-- Set properties with API data
if self.apiData ~= nil and self.apiData.uuid ~= nil then
--- Commodity
local commodity = require( 'Module:Commodity' ):new()
commodity:addShopData( self.apiData )
if self.apiData.type ~= nil and self.apiData.sub_type ~= nil then
-- Merge subtype into type, like how the game handles it
if self.apiData.sub_type ~= 'UNDEFINED' then
-- SMW_Type is already set prior if self.apiData.type exists
setData[ t( 'SMW_Type' ) ] = mw.ustring.format( '%s.%s', setData[ t( 'SMW_Type' ) ], self.apiData.sub_type )
end
local descData = self.apiData.description_data
if descData ~= nil then
for _, descObj in ipairs( descData ) do
-- Check if there are item type localization
if descObj.name == 'Item Type' or descObj.name == 'Type' then
local descType = descObj.type
-- FIXME: This only works for English, need some way to get only the English text for comparison since descType is always in English
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( setData[ t( 'SMW_Type' ) ] ) ) )
-- If the type in item description is different than what we compose out of type and subtype data, record it in subtype
-- TODO: We should make a common function to sanitize strings for comaprison (e.g. lowercase, remove all the space)
if mw.ustring.lower( descType ) ~= mw.ustring.lower( itemType ) then
setData[ t( 'SMW_Subtype' ) ] = descType
end
end
end
end
end
end
runModuleFN( setData[ t( 'SMW_Type' ) ], 'addSmwProperties', { self.apiData, self.frameArgs, setData } )
mw.logObject( setData, 'SET' )
self.setData = setData
return mw.smw.set( setData )
end
--- Queries the SMW Store
--- @return table
function methodtable.getSmwData( self )
-- Use cached data if possible, SMW queries are expensive
if self.smwData ~= nil and self.smwData[ t( 'SMW_Name' ) ] ~= nil then
return self.smwData
end
local queryName = self.frameArgs[ translate( 'ARG_SmwQueryName' ) ] or
mw.title.getCurrentTitle().fullText
local smwData = mw.smw.ask( makeSmwQueryObject( queryName ) )
if smwData == nil or smwData[ 1 ] == nil then
return hatnote( mw.ustring.format(
'%s[[%s]]',
translate( 'error_no_data_text' ),
translate( 'error_category_script_error' )
),
{ icon = 'WikimediaUI-Error.svg' }
)
end
self.smwData = smwData[ 1 ]
return self.smwData
end
--- Creates the infobox
function methodtable.getInfobox( self )
local smwData = self:getSmwData()
local infobox = require( 'Module:InfoboxNeue' ):new( {
placeholderImage = config.placeholder_image
} )
local tabber = require( 'Module:Tabber' ).renderTabber
mw.logObject( smwData, 'infoboxSmwData' )
--- SMW Data load error
--- Infobox data should always have Name property
if type( smwData ) ~= 'table' then
return infobox:renderInfobox( infobox:renderMessage( {
title = translate( 'error_no_infobox_data_title' ),
desc = translate( 'error_no_data_text' ),
} ) )
end
local function getManufacturer()
if smwData[ t( 'SMW_Manufacturer' ) ] == nil then return end
local mfu = manufacturer( smwData[ t( 'SMW_Manufacturer' ) ] )
if mfu == nil then return '[[' .. smwData[ t( 'SMW_Manufacturer' ) ] .. ']]' end
return infobox.showDescIfDiff(
table.concat( { '[[', smwData[ t( 'SMW_Manufacturer' ) ], '|', mfu.name , ']]' } ),
mfu.code
)
end
local function getType()
if smwData[ t( 'SMW_Type' ) ] == nil then return end
local itemType = translate( mw.ustring.format( 'type_%s', mw.ustring.lower( smwData[ t( 'SMW_Type' ) ] ) ) )
if mw.ustring.find( itemType, 'type_' ) then
itemType = smwData[ t( 'SMW_Type' ) ]
end
return mw.ustring.format( '[[%s]]', itemType )
end
local function getSize()
if smwData[ t( 'SMW_Size' ) ] == nil then return end
return 'S' .. smwData[ t( 'SMW_Size' ) ]
end
local function getClass()
if smwData[ t( 'SMW_Class' ) ] == nil then return end
local classKey = mw.ustring.lower( smwData[ t( 'SMW_Class' ) ] )
local class = translate( mw.ustring.format( 'class_%s', classKey ) )
if smwData[ t( 'SMW_Grade' ) ] ~= nil then
class = class .. ' (' .. smwData[ t( 'SMW_Grade' ) ] .. ')'
end
return class
end
--- Other sites
local function getOfficialSites()
local links = {}
for _, site in ipairs( data.official_sites ) do
local query = smwData[ translate( site.attribute ) ]
if query ~= nil then
table.insert( links, infobox:renderLinkButton( {
label = translate( site.label ),
link = query
} ) )
end
end
return links
end
local function getCommunitySites()
local links = {}
for _, site in ipairs( data.community_sites ) do
local query = smwData[ translate( site.data ) ]
if query ~= nil then
if site.data == 'SMW_ClassName' or site.data == 'SMW_UUID' then
query = mw.ustring.lower( query )
elseif site.data == 'SMW_ShipMatrixName' then
query = mw.uri.encode( query, 'PATH' )
end
if site.label == 'FleetYards' then
query = mw.ustring.lower( mw.ustring.gsub( query, '%%20', '-' ) )
end
table.insert( links, infobox:renderLinkButton( {
label = site.label,
link = mw.ustring.format( site.format, query )
} ) )
end
end
return links
end
local image = self.frameArgs[ translate( 'ARG_Image' ) ] or self.frameArgs[ 'image' ] or smwData[ 'image' ]
infobox:renderImage( image )
infobox:renderHeader( {
title = smwData[ t( 'SMW_Name' ) ],
--- e.g. Aegis Dynamics (AEGS)
subtitle = getManufacturer(),
badge = getSize()
} )
--- Type, Size, Class
infobox:renderSection( {
content = {
infobox:renderItem( {
label = translate( 'LBL_Type' ),
data = getType(),
} ),
infobox:renderItem( {
label = translate( 'LBL_Subtype' ),
data = smwData[ t( 'SMW_Subtype' ) ],
} ),
infobox:renderItem( {
label = translate( 'LBL_Class' ),
data = getClass(),
} ),
infobox:renderItem( {
label = translate( 'LBL_Occupancy' ),
data = smwData[ t( 'SMW_Occupancy' ) ],
} ),
infobox:renderItem( {
label = translate( 'LBL_Inventory' ),
data = smwData[ t( 'SMW_Inventory' ) ],
} )
},
col = 2
} )
local pageIdentifier = self.frameArgs[ translate( 'ARG_SmwQueryName' ) ] or mw.title.getCurrentTitle().fullText
runModuleFN( smwData[ t( 'SMW_Type' ) ], 'addInfoboxData', { infobox, smwData, pageIdentifier } )
--- Dimensions
infobox:renderSection( {
title = translate( 'LBL_Dimensions' ),
content = {
infobox:renderItem( {
label = translate( 'LBL_Length' ),
data = smwData[ t( 'SMW_EntityLength' ) ],
} ),
infobox:renderItem( {
label = translate( 'LBL_Width' ),
data = smwData[ t( 'SMW_EntityWidth' ) ],
} ),
infobox:renderItem( {
label = translate( 'LBL_Height' ),
data = smwData[ t( 'SMW_EntityHeight' ) ],
} ),
infobox:renderItem( {
label = translate( 'LBL_Mass' ),
data = smwData[ t( 'SMW_Mass' ) ],
} )
},
col = 3
} )
--- Metadata section
infobox:renderSection( {
class = 'infobox__section--metadata infobox__section--hasBackground',
content = {
infobox:renderItem( {
label = t( 'SMW_UUID' ),
data = smwData[ t( 'SMW_UUID' ) ],
row = true,
spacebetween = true
} ),
infobox:renderItem( {
label = t( 'SMW_ClassName' ),
data = smwData[ t( 'SMW_ClassName' ) ],
row = true,
spacebetween = true
} ),
infobox:renderItem( {
label = t( 'SMW_GameBuild' ),
data = smwData[ t( 'SMW_GameBuild' ) ],
row = true,
spacebetween = true
} )
}
} )
--- Actions section
if smwData[ t( 'SMW_UUID' ) ] then
infobox:renderSection( {
class = 'infobox__section--actions infobox__section--hasBackground',
content = {
infobox:renderItem( {
icon = 'WikimediaUI-Search.svg',
data = translate( 'actions_find_item_title' ),
desc = translate( 'actions_find_item_text' ),
-- FIXME: Make this configurable?
link = 'https://finder.cstone.space/search/' .. smwData[ t( 'SMW_UUID' ) ]
} )
}
} )
end
--- Footer
infobox:renderFooter( {
button = {
icon = 'WikimediaUI-Globe.svg',
label = translate( 'LBL_OtherSites' ),
type = 'popup',
content = infobox:renderSection( {
content = {
infobox:renderItem( {
label = translate( 'LBL_OfficialSites' ),
data = table.concat( getOfficialSites(), '' )
} ),
infobox:renderItem( {
label = translate( 'LBL_CommunitySites' ),
data = table.concat( getCommunitySites(), '' )
} ),
},
class = 'infobox__section--linkButtons',
}, true )
}
} )
return infobox:renderInfobox( nil, smwData[ t( 'SMW_Name' ) ] )
end
--- Creates the wikitext for the Item description template
function methodtable.getDescription( self )
local smwData = self:getSmwData()
--- Error: No SMW Data
if type( smwData ) ~= 'table' then
return require( 'Module:Mbox' )._mbox(
translate( 'error_no_description_title' ),
translate( 'error_no_data_text' ),
{ icon = 'WikimediaUI-Error.svg' }
)
end
--- Error: No description SMW property
if smwData[ t( 'SMW_Description' ) ] == nil then
return require( 'Module:Mbox' )._mbox(
translate( 'error_no_description_title' ),
translate( 'error_no_description_text' ),
{ icon = 'WikimediaUI-Error.svg' }
)
end
return '<blockquote>' .. smwData[ t( 'SMW_Description' ) ] .. '</blockquote>'
end
--- Creates the wikitext for the Item availability template
function methodtable.getAvailability( self )
local smwData = self:getSmwData()
--- Error: No SMW Data
if type( smwData ) ~= 'table' then
return require( 'Module:Mbox' )._mbox(
translate( 'error_no_availability_title' ),
translate( 'error_no_data_text' ),
{ icon = 'WikimediaUI-Error.svg' }
)
end
local output = {}
local uuid = smwData[ t( 'SMW_UUID' ) ]
if uuid then
-- Create find item button
local icon = mw.html.create( 'div' ):addClass( 'citizen-ui-icon mw-ui-icon-wikimedia-search' )
local label = mw.html.create( 'div' )
label
:addClass( 't-finditemuif__label' )
:tag( 'div' )
:addClass( 't-finditemuif__title' )
:wikitext( translate( 'actions_find_item_title' ) )
:done()
:tag( 'div' )
:addClass( 't-finditemuif__subtitle' )
:wikitext( translate( 'actions_find_item_text' ) )
:allDone()
local chervon = mw.html.create( 'div' ):addClass( 'citizen-ui-icon mw-ui-icon-wikimedia-collapse' )
local container = mw.html.create( 'div' )
container
:addClass( 't-finditemuif' )
:wikitext( string.format(
'[https://finder.cstone.space/search/%s %s%s%s]',
uuid,
tostring( icon ),
tostring( label ),
tostring( chervon )
) )
table.insert( output, tostring( container ) .. mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Template:Item availability/styles.css' }
} )
end
return table.concat( output )
end
--- Set the frame and load args
--- @param frame table
function methodtable.setFrame( self, frame )
self.currentFrame = frame
self.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end
--- Sets the main categories for this object
function methodtable.setCategories( self )
if config.set_categories == false then
return
end
local function addSubcategory( s1, s2 )
table.insert( self.categories, mw.ustring.format( '%s (%s)', s1, s2 ) )
end
--- Only set category if category_type value exists
if self.smwData[ t( 'SMW_Type' ) ] ~= nil then
local typeCategory = translate( 'category_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] ) )
if typeCategory ~= nil and typeCategory ~= 'category_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] ) then
table.insert( self.categories, typeCategory )
if self.smwData[ t( 'SMW_Size' ) ] ~= nil then
addSubcategory( typeCategory, t( 'SMW_Size' ) .. ' ' .. self.smwData[ t( 'SMW_Size' ) ] )
end
if self.smwData[ t( 'SMW_Grade' ) ] ~= nil then
addSubcategory( typeCategory, t( 'SMW_Grade' ) .. ' ' .. self.smwData[ t( 'SMW_Grade' ) ] )
end
if self.smwData[ t( 'SMW_Subtype' ) ] ~= nil then
addSubcategory( typeCategory, self.smwData[ t( 'SMW_Subtype' ) ] )
end
if self.smwData[ t( 'SMW_Class' ) ] ~= nil then
addSubcategory( typeCategory, self.smwData[ t( 'SMW_Class' ) ] )
end
end
end
if self.smwData[ t( 'SMW_Manufacturer' ) ] ~= nil then
local manufacturer = mw.ustring.gsub( self.smwData[ t( 'SMW_Manufacturer' ) ], '%[+', '' )
manufacturer = mw.ustring.gsub( manufacturer, '%]+', '' )
table.insert( self.categories, manufacturer )
else
table.insert( self.categories, translate( 'error_category_item_missing_manufacturer' ) )
end
if self.smwData[ t( 'SMW_UUID' ) ] == nil then
table.insert( self.categories, translate( 'error_category_item_missing_uuid' ) )
end
runModuleFN( self.smwData[ t( 'SMW_Type' ) ], 'addCategories', { self.categories, self.frameArgs, self.smwData } )
end
--- Sets the short description for this object
function methodtable.setShortDescription( self )
local shortdesc = ''
local itemType = translate( 'type_item' )
if self.smwData[ t( 'SMW_Type' ) ] ~= nil then
if self.smwData[ t( 'SMW_Subtype' ) ] ~= nil then
-- TODO: Localize subtype
itemType = self.smwData[ t( 'SMW_Subtype' ) ]
else
local itemTypeKey = 'type_' .. mw.ustring.lower( self.smwData[ t( 'SMW_Type' ) ] )
if translate( itemTypeKey ) ~= nil and translate( itemTypeKey ) ~= itemTypeKey then
itemType = translate( itemTypeKey )
end
end
itemType = mw.ustring.lower( itemType )
end
shortdesc = itemType
if self.smwData[ t( 'SMW_Class' ) ] ~= nil then
shortdesc = mw.ustring.format( '%s %s',
string.lower( self.smwData[ t( 'SMW_Class' ) ] ),
shortdesc
)
end
if self.smwData[ t( 'SMW_Grade' ) ] ~= nil then
shortdesc = translate( 'shortdesc_grade', false, self.smwData[ t( 'SMW_Grade' ) ], shortdesc )
end
if self.smwData[ t( 'SMW_Size' ) ] ~= nil then
shortdesc = mw.ustring.format( 'S%d %s',
self.smwData[ t( 'SMW_Size' ) ],
shortdesc
)
end
--- Manufacturer
if self.smwData[ t( 'SMW_Manufacturer' ) ] ~= nil and self.smwData[ t( 'SMW_Manufacturer' ) ] ~= 'Unknown manufacturer' then
local mfuname = self.smwData[ t( 'SMW_Manufacturer' ) ]
local man = manufacturer( mfuname )
--- Use short name if possible
if man ~= nil and man.shortname ~= nil then mfuname = man.shortname end
shortdesc = translate( 'shortdesc_manufactured_by', false, shortdesc, mfuname )
end
--- Submodule override
shortdesc = runModuleFN(
self.smwData[ t( 'SMW_Type' ) ],
'getShortDescription',
{ self.frameArgs, self.smwData },
true
) or shortdesc
if type( shortdesc ) == 'string' and shortdesc ~= '' then
shortdesc = lang:ucfirst( shortdesc )
self.currentFrame:callParserFunction( 'SHORTDESC', shortdesc )
end
end
--- Save Api Data to SMW store
function methodtable.saveApiData( self )
self:getApiDataForCurrentPage()
self:setSemanticProperties()
end
--- Generates debug output
function methodtable.makeDebugOutput( self )
local debug = require( 'Module:Common/Debug' )
self.smwData = nil
local smwData = self:getSmwData()
local queryName = self.frameArgs[ translate( 'ARG_SmwQueryName' ) ] or
self.frameArgs[ translate( 'ARG_Name' ) ] or
mw.title.getCurrentTitle().fullText
return debug.collapsedDebugSections({
{
title = 'SMW Query',
content = debug.convertSmwQueryObject( makeSmwQueryObject( queryName ) ),
},
{
title = 'SMW Data',
content = smwData,
tag = 'pre',
},
{
title = 'Frame Args',
content = self.frameArgs,
tag = 'pre',
},
})
end
--- Get the wikitext valid categories for this item
function methodtable.getCategories( self )
local mapped = {}
for _, category in pairs( self.categories ) do
if mw.ustring.sub( category, 1, 2 ) ~= '[[' then
category = mw.ustring.format( '[[Category:%s]]', category )
end
table.insert( mapped, category )
end
return table.concat( mapped )
end
--- New Instance
function Item.new( self )
local instance = {
categories = {}
}
setmetatable( instance, metatable )
return instance
end
--- Load data from api.star-citizen.wiki and save it to SMW
---
--- @param frame table Invocation frame
--- @return string|nil
function Item.loadApiData( frame )
local instance = Item:new()
instance:setFrame( frame )
instance:saveApiData()
local debugOutput
if instance.frameArgs[ 'debug' ] ~= nil then
local debug = require( 'Module:Common/Debug' )
debugOutput = debug.collapsedDebugSections({
{
title = 'SMW Set Data',
content = mw.getCurrentFrame():callParserFunction( '#tag', { 'nowiki', mw.dumpObject( instance.setData or {} ) } ),
},
})
end
return debugOutput
end
--- Generates an infobox based on passed frame args and SMW data
---
--- @param frame table Invocation frame
--- @return string
function Item.infobox( frame )
local instance = Item:new()
instance:setFrame( frame )
local debugOutput = ''
if instance.frameArgs[ 'debug' ] ~= nil then
debugOutput = instance:makeDebugOutput()
end
return tostring( instance:getInfobox() ) .. debugOutput
end
--- Implements the item description template
---
--- @param frame table Invocation frame
--- @return string
function Item.description( frame )
local instance = Item:new()
instance:setFrame( frame )
return tostring( instance:getDescription() )
end
--- Implements the item availability template
---
--- @param frame table Invocation frame
--- @return string
function Item.availability( frame )
local instance = Item:new()
instance:setFrame( frame )
return tostring( instance:getAvailability() )
end
--- "Main" entry point for templates that saves the API Data and outputs the infobox
---
--- @param frame table Invocation frame
--- @return string
function Item.main( frame )
local instance = Item:new()
instance:setFrame( frame )
instance:saveApiData()
local debugOutput = ''
local interwikiLinks = ''
if instance.frameArgs[ 'debug' ] ~= nil then
debugOutput = instance:makeDebugOutput()
end
local infobox = tostring( instance:getInfobox() )
if instance.smwData ~= nil then
instance:setCategories()
instance:setShortDescription()
interwikiLinks = common.generateInterWikiLinks( mw.title.getCurrentTitle().text )
end
return infobox .. debugOutput .. instance:getCategories() .. interwikiLinks
end
---
function Item.test( page )
page = page or 'Cirrus'
local instance = Item:new()
instance.frameArgs = {}
instance.frameArgs[ translate( 'ARG_Name' ) ] = page
instance.frameArgs[ translate( 'ARG_SmwQueryName' ) ] = page
instance:saveApiData()
instance:getInfobox()
end
return Item