Модуль:Песочница/Abiyoyo/Autosorting/ConfigProcessor: различия между версиями
[отпатрулированная версия] | [отпатрулированная версия] |
Содержимое удалено Содержимое добавлено
Abiyoyo (обсуждение | вклад) Нет описания правки |
Abiyoyo (обсуждение | вклад) Нет описания правки |
||
Строка 20:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- filter funcs declarations
--[[
local isEmptyParam, isNotEmptyParam, isEqParam, isNotEqParam, isNamespace
local isNotNamespace
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
--]]
--------------------------------------------------------------------------------
-- General processing
--------------------------------------------------------------------------------
--- Takes a named node in config with name 'name from node 'upNode'
Строка 150 ⟶ 98 :
-- @param name string
-- @return table
local function
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
local node = getConfigNode(upNode, name)
local default = upNode['default']
Строка 185 ⟶ 133 :
-- @param node
-- @return table or nil
local function
local filters =
if type(filters) ~= 'table' then return nil end
return filters
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
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
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
local myname = 'processRule'
debLog:write('Invoked', myname)
Строка 252 ⟶ 219 :
---Processes rules from array of IDs in ruleList
--@return concatenated result of rules
local function
local result = ''
for _, ruleName in ipairs(ruleList) do
Строка 266 ⟶ 233 :
--------------------------------------------------------------------------------
--- Methods
--------------------------------------------------------------------------------
--- Load config from path 'configPath'
--@param configPath string
function cp:getConfig(configPath)
if configPath == nil or configPath == '' then
debLog:write('No log provided', 'cp:getConfig', 'error')
return nil
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
|