Модуль:External data

Материал из свободной русской энциклопедии «Традиция»
Перейти к навигации Перейти к поиску

Для документации этого модуля может быть создана страница Модуль:External data/doc

--[[
	Обёртки над функциями расширения External Data
--]]

local lang = mw.language.getContentLanguage ()
local find, split = mw.ustring.find or string.find, mw.text.split or string.split
local dump = mw.dumpObject

-- Функции расширения ExternalData:
local fetch_pf, show_pf, loop_pf = '#get_web_data', '#external_value', '#display_external_table'

-- Подключение к источнику данных:
local function connect (frame, query, format, mappings, cache, stale)
	local mappings_str = ''
	for to, from in pairs (mappings) do
		if to and from then
			mappings_str = mappings_str .. to .. '=' .. from .. ','
		end
	end	
	local params = {
		format				= format
	  , url					= query .. '\n'
	  , data				= mappings_str
	  , ['cache seconds']	= cache and tostring (cache)
	}
	if format == 'XML' then
		params [#params + 1] = 'use xpath'
	end
	if stale then
		params [#params + 1] = 'use stale cache'
	end
	local connect = frame:callParserFunction (fetch_pf, params)
	if connect and connect ~= '' then
		-- this is an error:
		return false, connect
	else
		-- successful connection:
		return query
	end
end	-- local function connect (frame, query, format, mappings, cache, stale)
	
-- Однократое извлечение данных успешного подключения:
local function fetch (frame, query, format, mappings, cache, stale)
	local real_path, error = connect (frame, query, format, mappings, cache, stale)
	if real_path then
		local data = {}
		for key, _ in pairs (mappings) do
			local value = frame:callParserFunction (show_pf, key)
			if value and value ~= '' and not find (value, '^<span class="error">') then
				data [key] = tonumber (value) or value
			end
		end	-- for key, _ in pairs (mappings)
		return data, real_path, lang:formatDate ('d xg Y', nil, true)
	else
		return nil, nil, lang:formatDate ('d xg Y', nil, true)
			 , error .. '\n{{' .. show_pf .. ':...}}'
	end	-- if real_path
end	-- local function fetch (frame, query, format, mappings, cache, stale)

-- Множественное извлечение данных успешного подключения:
local function fetch_all (frame, query, format, mappings, cache, stale, template)
	local real_path, error = connect (frame, query, format, mappings, cache, stale)
	if real_path then
		local params = {}
		for key, _ in pairs (mappings) do
			params [#params + 1] = key .. '=' .. key
		end
		local separator = '(-*-)'
		local pf_params = {''
		  , template	= template
		  , data		= table.concat (params, ',')
		  , delimiter	= separator
		}
		local glued = frame:callParserFunction (loop_pf, pf_params)
		if glued and glued ~= '' and not find (glued, '^<span class="error">') then
			return split (glued, separator, true), real_path, lang:formatDate ('d xg Y', nil, true)
		else
			error = (error or '') .. ', ' .. (glued or '')
				 .. '\n{{' .. loop_pf .. '(params = )' .. dump (pf_params) .. '}}'
		end
	end	-- if real_path
	return nil, nil, lang:formatDate ('d xg Y', nil, true), error
end	-- local function fetch_all (frame, query, format, mappings, cache, stale, template)

return {
	fetch		= fetch
  , fetch_all	=	fetch_all
}