Модуль:CargoObject

Материал из Справочника наблюдателя
Перейти к:навигация, поиск

(i)      Описание модуля[]

Документация отсутствует!


local p = {}
local Frame, Args
local Err = {}

local global = mw.ext.luaglobal;
local Geobase = global.get( 'Geobase' ) or ''
local Fields = global.get( 'Fields' ) or {}

global.strict( true )

local function chkF( name, val )
	local f = Args[name]
	if ( f or '' ) == '' then
		return nil
	end
	if val == 0  then
		f = tonumber( f )
		if not f then
			Err[#Err+1] = 'Значение поля «' .. name .. '» должно быть целым'
			return nil
		end
	elseif val > 0 then
--		local f1 = mw.text.decode( f )	
--		if string.sub( f, 1, 1 ) == '&' or string.sub( f1, 1, 1 ) == '<' then
--			Err[#Err+1] = 'Преобразовали «' .. f .. '» в «' .. f1 .. '»'
--		end	
		if string.len( f ) > val then
			Err[#Err+1] = 'Значение поля «' .. name .. '» чересчур длинное ('
				.. string.len( f ) .. '/' .. val .. ')<br>'
			return nil
		end
	end
	Fields[f] = true
	return f
end

local precSymbol = { ['-'] = 8, ['?'] = 7, ['+'] = 6, ['#'] = 5 }

local function addrSplit( addr, name )
	if ( addr or '' ) == '' then
		return nil
	end
	local a = mw.text.split( addr, '[\t\r\n\f ]*=[\t\r\n\f ]*' )
	if #a > 4 then
		Err[#Err+1] = 'В поле «' .. name .. '» нарушен формат значения (ПочтовыйАдрес=НазваниеОрганизации=Широта,Долгота=показ) '
		return nil
	end
	local addr, org, geo, show = unpack( a )
	local prec
	if ( geo or '' ) == '' then
		-- доделать yageo под работу со scribunto
		geo = Frame:callParserFunction{ name = '#yageo', args = { Geobase .. a[1],  ''  } }
		prec = string.sub( geo, 1, 1 )
		if not tonumber( prec ) then
			Err[#Err+1] = 'В поле «' .. name .. '» несуществующий адрес «' 
				.. addr .. '» (' .. geo .. ')'
			return nil
		end
		geo = string.sub( geo, 3, -1)
--		Err[#Err+1] = Frame:callParserFunction{ name = '#yageo', args = { Geobase .. a[1],  'req'  } }
	else
		prec = mw.ustring.sub( geo, -1, -1 )
		prec = precSymbol [prec] 
		if prec then
			geo = mw.ustring.sub( geo, 1, -2 )
		else
			prec = 5
		end
	end
	return addr, org, geo, ( show or 'п' ), prec
end

p['Объект'] = function( frame )
	local args = frame:getParent().args
	Frame = frame
	Args = {}
	for k, v in pairs( args ) do
		if type( k ) ~= 'number' then
			Args[mw.ustring.lower( k )] = v
		end
	end

	local number        = chkF( 'номер'         , 12 )
	local mun           = chkF( 'мо'            , 80   )
	local gas           = chkF( 'гас'           , 120  )
	local district      = chkF( 'округ'         , 20   )
	local koib          = chkF( 'коиб'          , 10   )
	local temp          = chkF( 'временный'     , 10   )
	local quantity      = chkF( 'численность'   , 0    )
	local members       = chkF( 'прг'           , 0    )
	local room          = chkF( 'адрес'         , 300  )
	local room_phone    = chkF( 'телефон'       , 60   )
	local office        = chkF( 'адрес офиса'   , 300  )
	local office_phone  = chkF( 'телефон офиса' , 60   )
	local border        = chkF( 'границы'       , 4000 )
	local addendum      = chkF( 'примечание'    , 4000 )
	local x_turnout     = chkF( '#явка'         , 0    )
	local x_before      = chkF( '#досрочно'     , 0    )
	local x_home        = chkF( '#дома'         , 0    )
	local x_loyal       = chkF( '#власть'       , 0    )
	local name          = chkF( 'наименование'  , 300  )
	local site          = chkF( 'сайты'         , 300  )
	local label         = chkF( 'метка'         , 40   )
--	local xp_turnout    = chkF( '#явка%'        , 10   )
--	local xp_before     = chkF( '#досрочно%'    , 10   )
--	local xp_home       = chkF( '#дома%'        , 10   )
--	local xp_loyal      = chkF( '#власть%'      , 10   )
-- Категория = берется из имени подстраницы
-- Для властей и сервисов: тип-наименование-метка-сайт

	local room_addr, room_org, room_geo, room_geo_s, room_geo_p = addrSplit( room, 'адрес' )
	if room_addr then
		room_geo_s = mw.ustring.sub( room_geo_s .. '-', 1, 1 )
		if room_geo_s == 'в' then
			room_geo_s = 1
		elseif room_geo_s == 'н' then
			room_geo_s = 2
		else
			room_geo_s = 0
		end
	end
	local office_addr, office_org, office_geo, office_geo_s, office_geo_p = addrSplit( office, 'адрес офиса' )
	if office_addr then
		office_geo_s = mw.ustring.sub( office_geo_s .. '-', 1, 1 )
		if office_geo_s == 'в' then
			office_geo_s = 1
		elseif office_geo_s == 'н' then
			office_geo_s = 2
		else
			office_geo_s = 0
		end
	end
	if room_phone and string.sub( room_phone, 1, 1 ) ~= '{' then
		room_phone = '{{T|' .. room_phone .. '}}'
	end
	if office_phone and string.sub( office_phone, 1, 1 ) ~= '{' then
		office_phone = '{{T|' .. office_phone .. '}}'
	end
	Frame:callParserFunction{ name = '#cargo_store', args = { '', _table = 'Objects',
			number        = number,
			mun           = mun,
			gas           = gas,
			district      = district,
			koib          = koib,
			temp          = temp,
			quantity      = quantity,
			members       = members,
			room_addr     = room_addr,
			room_org      = room_org,
			room_geo      = room_geo,
			room_geo_s    = room_geo_s,
			room_geo_p    = room_geo_p,
			room_phone    = room_phone,
			office_addr   = office_addr,
			office_org    = office_org,
			office_geo    = office_geo,
			office_geo_s  = office_geo_s,
			office_geo_p  = office_geo_p,
			office_phone  = office_phone,
			border        = border,
			addendum      = addendum,
			x_turnout     = x_turnout,
			x_before      = x_before,
			x_home        = x_home,
			x_loyal       = x_loyal,
			name          = name,
			site          = site,
			label         = label,

	} }
	global.set( 'Fields', Fields )
	if next( Err ) then
		return frame:expandTemplate{ title = 'error', args = { table.concat( Err, '<br>' ) } }
	end
end


p['Геокодирование'] = function( frame )
	local args = frame:getParent().args
	local regdata = mw.loadData( 'Module:RegionData' )
	local district, center, radius, region =
		( args['МО'] or '' ),
		( args['центр'] or '' ),
		( args['радиус'] or '50' ),
		( args['регион'] or '' )
	local tmp = frame:callParserFunction{ name = '#yageo',
		args = { region .. ', ' .. district .. ', ' .. center,  radius  } }
	global.set( 'Geobase', region .. ', ' .. district .. ', ' )
	global.set( 'GeoParam',
		'addressCommon=' .. mw.uri.encode( region .. ', ' .. district, 'PATH' )
		.. '&centerCommon=' .. mw.uri.encode( region .. ', ' .. district, 'PATH' )
		.. '&centerSpecial=' .. mw.uri.encode( center, 'PATH' )
		.. '&radius=' .. radius )
	if region == '' then
		region = frame:callParserFunction{ name = '#var:', args = { 'RegCode' } }
	else
		region = regdata.name2[region].code
	end
	global.set( 'RegionCode', region )

	return frame:callParserFunction{  name = '#cargo_store', args = { '', _table = 'ObjectsPage',
		geobase = global.get( 'Geobase' ),
		geoparam = global.get( 'GeoParam' ),
		region = region,
	} }

end

return p