Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.

Module:Navplate/Manufacturers: Difference between revisions

From the Star Citizen Wiki, the fidelity™ encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 4: Line 4:
-- TODO:
-- TODO:
-- - Make functions generic
-- - Make functions generic
-- - Support multiple conditions
-- - Support more SMW inline query conditions other than category
-- - Support more SMW inline query conditions other than category
-- - Implement i18n
-- - Implement i18n
Line 19: Line 20:
local navplate = require( 'Module:Navplate' )
local navplate = require( 'Module:Navplate' )
local template = mw.loadJsonData( 'Module:Navplate/Manufacturers/data.json' );
local template = mw.loadJsonData( 'Module:Navplate/Manufacturers/data.json' );
local mfu = require( 'Module:Manufacturer' )._manufacturer


--- Queries the SMW Store
--- Queries the SMW Store
--- @param string conditions for SMW query
--- @return table
--- @return table
function methodtable.getSmwData( self, category )
function methodtable.getSmwData( self, conditions )
-- Cache multiple calls
-- Cache multiple calls
if self.smwData ~= nil then
if self.smwData ~= nil then
Line 30: Line 33:
local askData = {
local askData = {
'[[:+]]',
'[[:+]]',
'[[' .. self.category .. ']]',
'[[' .. conditions .. ']]',
'?#-=page',
'?#-=page',
'?Category#'
'?Category#'
Line 65: Line 68:
local conditions = navplateRow[ 'conditions' ]
local conditions = navplateRow[ 'conditions' ]


if conditions ~= nil then
for _, result in pairs( data ) do
for _, result in pairs( data ) do
-- Match category
-- Match category
local categories = result[ 'Category' ]
if categories ~= nil and type( categories ) == 'table' then
local categories = result[ 'Category' ]
for _, category in pairs( categories ) do
if categories ~= nil and type( categories ) == 'table' then
if category == conditions then
for _, category in pairs( categories ) do
-- Create row if it does not exist already
if category == conditions then
if navplateData[ i ] == nil then
-- Create row if it does not exist already
navplateData[ i ] = {
if navplateData[ i ] == nil then
label = label,
navplateData[ i ] = {
pages = {}
label = label,
}
pages = {}
}
end
table.insert( navplateData[ i ][ 'pages' ], result[ 'page' ] )
end
end
table.insert( navplateData[ i ][ 'pages' ], result[ 'page' ] )
end
end
end
end
Line 96: Line 101:
--- @return string
--- @return string
function methodtable.make( self )
function methodtable.make( self )
local manufacturer = mfu( self.frameArgs[ 1 ] ) and mfu( self.frameArgs[ 1 ] ).name or self.frameArgs[ 1 ]

if manufacturer == nil then
local hatnote = require( 'Module:Hatnote' )._hatnote
return hatnote(
'Missing manufacturer parmeter',
{ icon = 'WikimediaUI-Error.svg' }
)
end

local args = {
local args = {
subtitle = 'Products of',
subtitle = 'Products of',
title = 'Manufacturer'
title = manufacturer
}
}


local data = self:getSmwData( self.category )
local conditions = 'Category:' .. manufacturer

local data = self:getSmwData( conditions )
if data ~= nil then
if data ~= nil then
local navplateData = self:buildNavplateData( data )
local navplateData = self:buildNavplateData( data )
Line 120: Line 137:
args = args
args = args
})
})
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
end


Line 125: Line 149:
---
---
--- @return table NavplateManufacturers
--- @return table NavplateManufacturers
function NavplateManufacturers.new( self, frameArgs )
function NavplateManufacturers.new( self )
local instance = {
local instance = {}

category = 'Category:Greycat Industrial'
}
setmetatable( instance, metatable )
setmetatable( instance, metatable )

return instance
return instance
end
end
Line 140: Line 164:
function NavplateManufacturers.main( frame )
function NavplateManufacturers.main( frame )
local instance = NavplateManufacturers:new()
local instance = NavplateManufacturers:new()
instance:setFrame( frame )


return instance:make()
return instance:make()

Revision as of 00:59, 6 February 2024

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Navplate/Manufacturers/doc. Changes can be proposed in the talk page.

-- This is barebone but the idea is to make a generic module that
-- build any navplate from template defined from data.json
--
-- TODO:
-- - Make functions generic
-- - Support multiple conditions
-- - Support more SMW inline query conditions other than category
-- - Implement i18n
-- - Support group headers
-- - Support icon

local NavplateManufacturers = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local common = require( 'Module:Common' )
local navplate = require( 'Module:Navplate' )
local template = mw.loadJsonData( 'Module:Navplate/Manufacturers/data.json' );
local mfu = require( 'Module:Manufacturer' )._manufacturer

--- Queries the SMW Store
--- @param string conditions for SMW query
--- @return table
function methodtable.getSmwData( self, conditions )
	-- Cache multiple calls
    if self.smwData ~= nil then
        return self.smwData
    end

	local askData = {
		'[[:+]]',
		'[[' .. conditions .. ']]',
		'?#-=page',
		'?Category#'
	}

	local data = mw.smw.ask( askData )

	if data == nil or data[ 1 ] == nil then
        return nil
    end
	
	--mw.logObject( data )

	-- Init self.smwData
	if self.smwData == nil then
		self.smwData = {}
	end

	self.smwData = data

	return self.smwData
end

--- Build a table of data that represents the navplate from SMW data based on the template
---
--- @return table
function methodtable.buildNavplateData( self, data )
	local navplateData = {}

	-- Lua tables has no concept of order, need to iterate it manually
	local i = 1
	for _, navplateRow in pairs( template[ 'content' ] ) do
		local label = navplateRow[ 'label' ]
		local conditions = navplateRow[ 'conditions' ]

		if conditions ~= nil then
			for _, result in pairs( data ) do
				-- Match category
				local categories = result[ 'Category' ]
				if categories ~= nil and type( categories ) == 'table' then
					for _, category in pairs( categories ) do
						if category == conditions then
							-- Create row if it does not exist already
							if navplateData[ i ] == nil then
								navplateData[ i ] = {
									label = label,
									pages = {}
								}
							end
							table.insert( navplateData[ i ][ 'pages' ], result[ 'page' ] )
						end
					end
				end
			end
		end

		if navplateData[ i ] ~= nil then
			i = i + 1
		end
	end

	return navplateData
end

--- Outputs the table
---
--- @return string
function methodtable.make( self )
	local manufacturer = mfu( self.frameArgs[ 1 ] ) and mfu( self.frameArgs[ 1 ] ).name or self.frameArgs[ 1 ]

	if manufacturer == nil then
		local hatnote = require( 'Module:Hatnote' )._hatnote
		return hatnote(
			'Missing manufacturer parmeter',
			{ icon = 'WikimediaUI-Error.svg' }
		)
	end

	local args = {
		subtitle = 'Products of',
		title = manufacturer
	}

	local conditions = 'Category:' .. manufacturer

	local data = self:getSmwData( conditions )
	if data ~= nil then
		local navplateData = self:buildNavplateData( data )
		mw.logObject( navplateData )

		if navplateData ~= nil then
			for i, row in ipairs( navplateData ) do
				args[ 'label' .. i ] = row[ 'label' ]
				-- Probably there is a cleaner way but it works :D
				args[ 'list' .. i ] = '[[' .. table.concat( row[ 'pages' ], ']][[' ) .. ']]'
			end
		end
	end

	-- mw.logObject( args )

    return navplate.navplateTemplate({
		args = args
    })
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

--- New Instance
---
--- @return table NavplateManufacturers
function NavplateManufacturers.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end


--- "Main" entry point
---
--- @param frame table Invocation frame
--- @return string
function NavplateManufacturers.main( frame )
    local instance = NavplateManufacturers:new()
	instance:setFrame( frame )

    return instance:make()
end


return NavplateManufacturers