Update languages.lua

When handling phrases get the specific phrase instead of all of them. Remove the code that tried to build the XML from the file system. Replace it with a 'not found' response so that FreeSWITCH will check the filesystem for the XML of the phrase that was not found.
This commit is contained in:
FusionPBX
2016-07-24 00:09:48 -06:00
committed by GitHub
parent 2c0452d591
commit 32f803a1b3
@@ -27,12 +27,16 @@
-- Contributor(s): -- Contributor(s):
-- Mark J Crane <markjcrane@fusionpbx.com> -- Mark J Crane <markjcrane@fusionpbx.com>
--general functions
require "resources.functions.is_uuid";
--set the default --set the default
continue = true; continue = true;
--get the action --get the action
--action = params:getHeader("action"); --action = params:getHeader("action");
language = params:getHeader("lang"); language = params:getHeader("lang");
macro_name = params:getHeader("macro_name");
--additional information --additional information
--event_calling_function = params:getHeader("Event-Calling-Function"); --event_calling_function = params:getHeader("Event-Calling-Function");
@@ -40,7 +44,7 @@
--determine the correction action to perform --determine the correction action to perform
--get the cache --get the cache
if (trim(api:execute("module_exists", "mod_memcache")) == "true") then if (trim(api:execute("module_exists", "mod_memcache")) == "true") then
XML_STRING = trim(api:execute("memcache", "get languages:" .. language)); XML_STRING = trim(api:execute("memcache", "get languages:" .. language..":" .. macro_name));
--freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. XML_STRING .. "\n"); --freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. XML_STRING .. "\n");
if (XML_STRING == "-ERR NOT FOUND") or (XML_STRING == "-ERR CONNECTION FAILURE") then if (XML_STRING == "-ERR NOT FOUND") or (XML_STRING == "-ERR CONNECTION FAILURE") then
source = "database"; source = "database";
@@ -62,6 +66,7 @@
--build the XML string from the database --build the XML string from the database
if (source == "database") then if (source == "database") then
--database connection --database connection
if (continue) then if (continue) then
--connect to the database --connect to the database
@@ -94,115 +99,100 @@
--set the xml array and then concatenate the array to a string --set the xml array and then concatenate the array to a string
if (continue) then if (continue) then
--build the xml
local xml = {}
table.insert(xml, [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>]]);
table.insert(xml, [[<document type="freeswitch/xml">]]);
table.insert(xml, [[ <section name="languages">]]);
table.insert(xml, [[ <language name="]]..language..[[" say-module="]]..language..[[" sound-prefix="]]..sounds_dir..[[/]]..language..[[/us/callie" tts-engine="cepstral" tts-voice="callie">]]);
table.insert(xml, [[ <phrases>]]);
table.insert(xml, [[ <macros>]]);
sql = "SELECT * FROM v_phrases as p, v_phrase_details as d "; -- if macro_name is a uuid get from the phrase details
sql = sql .. "WHERE d.domain_uuid = '" .. domain_uuid .. "' "; if (is_uuid(macro_name)) then
sql = sql .. "AND p.phrase_language like '".. language .."%' "; --define the xml table
sql = sql .. "AND p.phrase_uuid = d.phrase_uuid "; local xml = {}
sql = sql .. "AND p.phrase_enabled = 'true' ";
sql = sql .. "ORDER BY d.domain_uuid, p.phrase_uuid, d.phrase_detail_order ASC "; --get hte xml
if (debug["sql"]) then table.insert(xml, [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>]]);
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); table.insert(xml, [[<document type="freeswitch/xml">]]);
end table.insert(xml, [[ <section name="languages">]]);
previous_phrase_uuid = ""; table.insert(xml, [[ <language name="]]..language..[[" say-module="]]..language..[[" sound-prefix="]]..sounds_dir..[[/]]..language..[[/us/callie" tts-engine="cepstral" tts-voice="callie">]]);
match_tag = "open"; table.insert(xml, [[ <phrases>]]);
x = 0; table.insert(xml, [[ <macros>]]);
dbh:query(sql, function(row)
--phrase_uuid,domain_uuid,phrase_name,phrase_language sql = "SELECT * FROM v_phrases as p, v_phrase_details as d ";
--phrase_description,phrase_enabled,phrase_detail_uuid sql = sql .. "WHERE d.domain_uuid = '" .. domain_uuid .. "' ";
--phrase_detail_group,phrase_detail_tag,phrase_detail_pattern sql = sql .. "AND p.phrase_uuid = '".. macro_name .."' ";
--phrase_detail_function,phrase_detail_data,phrase_detail_method sql = sql .. "AND p.phrase_language = '".. language .."' ";
--phrase_detail_type,phrase_detail_order sql = sql .. "AND p.phrase_uuid = d.phrase_uuid ";
if (previous_phrase_uuid ~= row.phrase_uuid) then sql = sql .. "AND p.phrase_enabled = 'true' ";
sql = sql .. "ORDER BY d.domain_uuid, p.phrase_uuid, d.phrase_detail_order ASC ";
-- if (debug["sql"]) then
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n");
-- end
previous_phrase_uuid = "";
match_tag = "open";
x = 0;
dbh:query(sql, function(row)
--phrase_uuid,domain_uuid,phrase_name,phrase_language
--phrase_description,phrase_enabled,phrase_detail_uuid
--phrase_detail_group,phrase_detail_tag,phrase_detail_pattern
--phrase_detail_function,phrase_detail_data,phrase_detail_method
--phrase_detail_type,phrase_detail_order
if (previous_phrase_uuid ~= row.phrase_uuid) then
if (x > 0) then
table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]);
end
table.insert(xml, [[ <macro name="]]..row.phrase_uuid..[[">]]);
table.insert(xml, [[ <input pattern=\"(.*)\">]]);
table.insert(xml, [[ <match>]]);
match_open_tag = true
end
table.insert(xml, [[ <action function="]]..row.phrase_detail_function..[[" data="]]..row.phrase_detail_data..[["/>]]);
previous_phrase_uuid = row.phrase_uuid;
x = x + 1;
end);
if (x > 0) then if (x > 0) then
table.insert(xml, [[ </match>]]); table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]); table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]); table.insert(xml, [[ </macro>]]);
end end
table.insert(xml, [[ <macro name="]]..row.phrase_uuid..[[">]]); table.insert(xml, [[ </macros>]]);
table.insert(xml, [[ <input pattern=\"(.*)\">]]);
table.insert(xml, [[ <match>]]); --output xml & close previous file
match_open_tag = true table.insert(xml, [[ </phrases>]]);
end table.insert(xml, [[ </language>]]);
table.insert(xml, [[ <action function="]]..row.phrase_detail_function..[[" data="]]..row.phrase_detail_data..[["/>]]); table.insert(xml, [[ </section>]]);
previous_phrase_uuid = row.phrase_uuid; table.insert(xml, [[</document>]]);
x = x + 1; XML_STRING = table.concat(xml, "\n");
end);
if (x > 0) then
table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]);
end end
--if nil do nt include language xml from the file system the phrases directory is not set in default settings - category: switch name: phrases_dir or its false --send not found
--it also can be nil if config.lua is not writable so that it can be defined in it if (XML_STRING == nil) then
if (phrases_dir ~= nil) then XML_STRING = [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
--read root xml language file, parse included xml files <document type="freeswitch/xml">
local xml_file_paths = {} <section name="result">
local file_handle = io.open(phrases_dir.."/"..language.."/"..language..".xml", "r"); <result status="not found" />
if (file_handle ~= nil) then </section>
for file_line in file_handle:lines() do </document>]];
if (string.find(file_line, 'cmd="include" data="', 0, true) ~= nil) then
pos_beg = string.find(file_line, 'cmd="include" data="', 0, true) + 20;
pos_end = string.find(file_line, '"/>', 0, true) - 1;
xml_file_path = string.sub(file_line, pos_beg, pos_end);
table.insert(xml_file_paths, phrases_dir.."/"..language.."/"..xml_file_path);
--freeswitch.consoleLog("notice", "file path = "..xml_file_path.."\n");
end
end
file_handle:close();
end
--iterate array of file paths, get contents of other xml macro files
for key, xml_file_path in pairs(xml_file_paths) do
if (file_exists(xml_file_path)) then
xml_file = io.open(xml_file_path, "r");
if (xml_file ~= nil) then
xml_file_content = xml_file:read("*a");
xml_file_content = string.gsub(xml_file_content, "<include>", '');
xml_file_content = string.gsub(xml_file_content, "</include>", '');
table.insert(xml, xml_file_content);
--freeswitch.consoleLog("notice", "file contents...\n\n"..xml_file_content.."\n");
end
xml_file:close();
end
end
end end
--output xml & close previous file --log to the console
table.insert(xml, [[ </macros>]]); --freeswitch.consoleLog("notice", "[xml_handler] language " .. XML_STRING .. " \n");
table.insert(xml, [[ </phrases>]]);
table.insert(xml, [[ </language>]]);
table.insert(xml, [[ </section>]]);
table.insert(xml, [[</document>]]);
XML_STRING = table.concat(xml, "\n");
--freeswitch.consoleLog("notice", "[xml_handler] language " .. XML_STRING .. " \n")
--close the database connection --close the database connection
dbh:release(); dbh:release();
--set the cache --set the cache
if (domain_name) then if (domain_name) then
result = trim(api:execute("memcache", "set languages:" .. language .. " '"..XML_STRING:gsub("'", "&#39;").."' "..expire["languages"])); result = trim(api:execute("memcache", "set languages:" .. language .. ":" .. macro_name .." '"..XML_STRING:gsub("'", "&#39;").."' "..expire["languages"]));
end end
--send the xml to the console --send the xml to the console
if (debug["xml_string"]) then if (debug["xml_string"]) then
local file = assert(io.open(temp_dir .. "/xml_handler-" .. language .. ".xml", "w")); local file = assert(io.open(temp_dir .. "/xml_handler-" .. language .. "-" .. macro_name ..".xml", "w"));
file:write(XML_STRING); file:write(XML_STRING);
file:close(); file:close();
end end
--send to the console --send to the console
if (debug["cache"]) then if (debug["cache"]) then
freeswitch.consoleLog("notice", "[xml_handler] languages:" .. language .. " source: database\n"); freeswitch.consoleLog("notice", "[xml_handler] languages:" .. language .. ":" .. macro_name .." source: database\n");
end end
end end
end end
@@ -217,7 +207,7 @@
--send to the console --send to the console
if (debug["cache"]) then if (debug["cache"]) then
if (XML_STRING) then if (XML_STRING) then
freeswitch.consoleLog("notice", "[xml_handler] language:" .. language .. " source: memcache \n"); freeswitch.consoleLog("notice", "[xml_handler] language:" .. language .. ":" .. macro_name .." source: memcache \n");
end end
end end
end end
@@ -236,4 +226,4 @@
--send the xml to the console --send the xml to the console
if (debug["xml_string"]) then if (debug["xml_string"]) then
freeswitch.consoleLog("notice", "[xml_handler] XML_STRING: \n" .. XML_STRING .. "\n"); freeswitch.consoleLog("notice", "[xml_handler] XML_STRING: \n" .. XML_STRING .. "\n");
end end