Модуль:Песочница/Abiyoyo/Autosorting/ConfigProcessor: различия между версиями

[отпатрулированная версия][отпатрулированная версия]
Содержимое удалено Содержимое добавлено
Нет описания правки
Нет описания правки
Строка 20:
 
--------------------------------------------------------------------------------
--- FiltersFunc maps
-- called from processFilters() as filterFunc(options, frameArgs)
--------------------------------------------------------------------------------
 
-- filter funcs declarations
 
--[[
local isEmptyParam, isNotEmptyParam, isEqParam, isNotEqParam, isNamespace
local isNotNamespace
 
cp.filters = {
local FILTER_FUNCTIONS = {
['parameter-is-empty'] = isEmptyParam,
['parameter-not-empty'] = isNotEmptyParam,
Строка 36 ⟶ 37 :
default = function() return true end,
}
--]]
--------------------------------------------------------------------------------
--- Filters
-- called from processFilters() as filterFunc(options, frameArgs)
--------------------------------------------------------------------------------
--[[
 
function cp.isEmptyParam(options, frameArgs)
Строка 50 ⟶ 57 :
function cp.isNotEmptyParam(options, frameArgs)
debLog:write('invoked', 'isNotEmptyParam')
return not cp.isEmptyParam(options, frameArgs)
end
 
Строка 81 ⟶ 88 :
end
 
--]]
--- Вызывает один за другим фильтры из массива filters
-- Если фильтров нет или что-то сконфигурировано неверно, возвращает true
-- Если хотя бы один из фильтров отработал и выдал false, возвращает false
-- Если все фильтры вернули true, возвращает true
-- @function processFilters
-- @param filtMap таблица-список фильтров вида {<filterID> = <func>, ...}
-- @param filters массив с фильтрами вида
-- { {name = <filterID>, options = {<opt1> = val1, ... } ... }
-- @param frameArgs таблица с параметрами фрейма
-- @return true если все фильтры вернули true или проблемы с параметрами
-- false если хотя бы один вернул false
function cp.processFilters(filtMap, filters, frameArgs)
debLog:write('invoked ', 'processFilters')
if type(filtMap) ~= 'table' then return true end
if type(filters) ~= 'table' then return true end
function cp.default() return true end
 
for i, filter in ipairs(filters) do
debLog:write('processing name: '.. tostring(filter.id), 'processFilters')
local filterFunc = filtMap[filter.id] or filtMap[default] or default
if not filterFunc(filter.options, frameArgs) then
return false
end
end
return true
end
 
--------------------------------------------------------------------------------
-- General processing
--------------------------------------------------------------------------------
--- Loads config from frame arguments or default
function cp:getConfig(configPath)
if configPath ~= nil and configPath ~= '' then
local success, result = pcall(mw.loadData, configPath)
if success then
debLog:write('Config loaded: '..configPath, 'getConfig')
self.config = result
else
debLog:write('Config load failed', 'getConfig', 'warn')
end
return success, result
end
return nil
end
--- Gets global options frome config node and sets them to global table
function cp:setGlobalOptions(node)
node = node or self.config.global
debLog:write('invoked','cp:setGlobalOptions')
if type(node) ~= 'table' then return nil end
if type(node.global) ~= 'table' then return nil end
for k, option in pairs(node.global) do
if option == 'ignoreNSChecks' then
self.globals.ignoreNSChecks = self.globals.ignoreNSChecks or option
else
self.globals[k] = v
end
end
debLog:write('Globals set: '..mw.dumpObject(self.globals), 'setGlobalOptions')
return self.globals
end
 
--- Takes a named node in config with name 'name from node 'upNode'
Строка 150 ⟶ 98 :
-- @param name string
-- @return table
local function cp.getConfigNode(upNode, name)
if type(name) ~= 'string' or name == '' then
return nil, 'Name invalid'
Строка 164 ⟶ 112 :
-- @param upNode - previous node. e.g. 'a' for: a = { <name> = { } }
-- @param name string
local function cp.getConfigNodeWithDefaults(upNode, name)
local node = getConfigNode(upNode, name)
local default = upNode['default']
Строка 185 ⟶ 133 :
-- @param node
-- @return table or nil
local function cp.getFilters(node)
local filters = cp.getConfigNode(node, 'filters')
if type(filters) ~= 'table' then return nil end
return filters
end
 
--- Вызывает один за другим фильтры из массива filters
--- Gets preset 'name' from table 'node'
-- Если фильтров нет или что-то сконфигурировано неверно, возвращает true
-- @function getPreset
-- Если хотя бы один из фильтров отработал и выдал false, возвращает false
-- @param node
-- Если все фильтры вернули true, возвращает true
-- @return table or nil
-- @function processFilters
function cp.getPreset(node, name)
-- @param filtMap таблица-список фильтров вида {<filterID> = <func>, ...}
local preset = cp.getConfigNodeWithDefaults(node, name)
-- @param filters массив с фильтрами вида
if type(preset) ~= 'table' then return nil end
-- { {name = <filterID>, options = {<opt1> = val1, ... } ... }
return preset
-- @param frameArgs таблица с параметрами фрейма
-- @return true если все фильтры вернули true или проблемы с параметрами
-- false если хотя бы один вернул false
local function processFilters(filtMap, filters, frameArgs)
debLog:write('invoked ', 'processFilters')
if type(filtMap) ~= 'table' then return true end
if type(filters) ~= 'table' then return true end
function cp.default() return true end
 
for i, filter in ipairs(filters) do
debLog:write('processing name: '.. tostring(filter.id), 'processFilters')
local filterFunc = filtMap[filter.id] or filtMap[default] or default
if not filterFunc(filter.options, frameArgs) then
return false
end
end
return true
end
 
Строка 204 ⟶ 171 :
-- array with those of them, which are also exist as indexes in 'ruleMap'
-- @return array with valid rules
local function cp.getRules(ruleMap, node)
local rules = getConfigNode(node, 'rules')
if type(rules) ~= 'table' then return nil end
Строка 230 ⟶ 197 :
--@param preset table with preset
--@return result of a called function or nil
local function cp.processRule(node, frameArgs, preset)
local myname = 'processRule'
debLog:write('Invoked', myname)
Строка 252 ⟶ 219 :
---Processes rules from array of IDs in ruleList
--@return concatenated result of rules
local function cp.processRuleList(ruleList, node, frameArgs, preset)
local result = ''
for _, ruleName in ipairs(ruleList) do
Строка 266 ⟶ 233 :
 
--------------------------------------------------------------------------------
--- Methods
-- Методы
--------------------------------------------------------------------------------
 
-- временная обертка для старых методов, потом содержимое перенести в main
--- Load config from path 'configPath'
function cp.pcall_main(func, ...)
--@param configPath string
local success, mainstate, result = pcall(p._main, unpack(arg))
if--@return nottrue on success thenor or nil
function cp:getConfig(configPath)
debLog:write(mainstate, '_main', 'error')
if configPath == nil or configPath == '' then
return ''
debLog:write('No log provided', 'cp:getConfig', 'error')
elseif not mainstate then
return nil
debLog:write(result, '_main')
return ''
else
return result
end
local success, result = pcall(mw.loadData, configPath)
if success then
self.config = result
debLog:write('Config loaded: '..configPath, 'cp:getConfig')
return true
end
debLog:write(result, 'cp:getConfig', 'error')
return nil
end
--- Get 'options' frome config and sets them to 'globals' table of 'self'
--@param node - 'config' node
--@return globals table or nil if nothing is set
function cp:setGlobalOptions(node)
node = node or self.config
debLog:write('invoked','cp:setGlobalOptions')
debLog:write(self,'cp:setGlobalOptions')
if type(node) ~= 'table' then return nil end
if type(node.options) ~= 'table' then return nil end
for k, option in pairs(node.options) do
if option == 'ignoreNSChecks' then
self.globals.ignoreNSChecks = self.globals.ignoreNSChecks or option
else
self.globals[k] = option
end
end
debLog:write('Globals set: '..mw.dumpObject(self.globals), 'setGlobalOptions')
return self.globals
end
 
--- Global options processing
--@param frameArgs
--@return true on ok, false on checks failed
function cp:processGlobalOptions(frameArgs)
local nocat = frameArgs[self.globals.nocatParamName]
if nocat ~= nil and nocat ~= '' then
self.globals.nocat = nocat
debLog:write('Categorization denied', 'cp:processGlobalOptions')
return false
end
local from = frameArgs[self.globals.fromParamName]
if from ~= nil and from ~= '' then
if not mw.wikibase.isValidEntityId(from) then
debLog:write('EntityID not valid: '.. from, 'processGlobalOptions')
return false
end
if not mw.wikibase.entityExists(from) then
debLog:write('Entity does not exist:'.. from, 'processGlobalOptions')
return false
end
self.globals.from = from
end
return true
end
 
--- Get preset 'name' from table 'node' and enriches it with default values
--@function getPreset
--@param name string
--@return table or nil
function cp:preparePreset(name)
node = self.config.presets
local workPreset = getConfigNodeWithDefaults(node, name)
if type(preset) ~= 'table' then
return nil
end
self.workPreset = workPreset
return true
end
 
--- Init filter map table
--@param filterMap table
--@return true or false
function cp:initFilterMap(filterMap)
if type(filterMap) ~= 'table' then return nil end
self.filterMap = filterMap
return true
end
 
---Process filters from work preset
--@param frameArgs
--@return true on ok, false on checks failed
function cp:processPresetFilters(frameArgs)
local filters = getFilters(self.workPreset)
return processFilters(self.filterMap, filters, frameArgs)
end
 
---Get valid rules' list
--@param
--@return
function cp:prepareRuleStack()
return --getRules(ruleMap, node)
end
 
--- Init
--@param
--@return
function cp:init()
return getRules(ruleMap, node)
end
 
function cp:process()
return
end