Update languages.lua

This commit is contained in:
FusionPBX
2018-01-11 15:31:07 -07:00
committed by GitHub
parent 1085ef9093
commit 85e8f8cd83
@@ -1,6 +1,6 @@
-- xml_handler.lua -- xml_handler.lua
-- Part of FusionPBX -- Part of FusionPBX
-- Copyright (C) 2013 Mark J Crane <markjcrane@fusionpbx.com> -- Copyright (C) 2013-2018 Mark J Crane <markjcrane@fusionpbx.com>
-- All rights reserved. -- All rights reserved.
-- --
-- Redistribution and use in source and binary forms, with or without -- Redistribution and use in source and binary forms, with or without
@@ -13,7 +13,7 @@
-- notice, this list of conditions and the following disclaimer in the -- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution. -- documentation and/or other materials provided with the distribution.
-- --
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY -- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, -- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
@@ -33,181 +33,163 @@
--set the default --set the default
continue = true; continue = true;
--additional information
--event_calling_function = params:getHeader("Event-Calling-Function");
--show the params in the console
--if (params:serialize() ~= nil) then
-- freeswitch.consoleLog("notice", "[xml_handler-languages.lua] Params:\n" .. params:serialize() .. "\n");
--end
--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"); macro_name = params:getHeader("macro_name");
--additional information --get the cache
--event_calling_function = params:getHeader("Event-Calling-Function"); local cache = require "resources.functions.cache"
local language_cache_key = "languages:" .. language..":" .. macro_name;
XML_STRING, err = cache.get(language_cache_key)
--determine the correction action to perform --build the XML string from the database
--get the cache if not XML_STRING then
if (trim(api:execute("module_exists", "mod_memcache")) == "true") then
XML_STRING = trim(api:execute("memcache", "get languages:" .. language..":" .. macro_name)); --connect to the database
--freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. XML_STRING .. "\n"); local Database = require "resources.functions.database";
if (XML_STRING == "-ERR NOT FOUND") or (XML_STRING == "-ERR CONNECTION FAILURE") then dbh = Database.new('system');
source = "database";
continue = true; --include json library
else local json
source = "cache"; if (debug["sql"]) then
continue = true; json = require "resources.functions.lunajson"
end end
else
XML_STRING = "";
source = "database";
continue = true;
end
--show the params in the console --exits the script if we didn't connect properly
--if (params:serialize() ~= nil) then assert(dbh:connected());
-- freeswitch.consoleLog("notice", "[xml_handler-languages.lua] Params:\n" .. params:serialize() .. "\n");
--end
--build the XML string from the database --get the domain_uuid
if (source == "database") then if (continue) then
--connect to the database
local Database = require "resources.functions.database";
dbh = Database.new('system');
--include json library
local json
if (debug["sql"]) then
json = require "resources.functions.lunajson"
end
--exits the script if we didn't connect properly
assert(dbh:connected());
--get the domain_uuid
if (continue) then
if (domain_uuid == nil) then
--get the domain_uuid
if (domain_name ~= nil) then
local sql = "SELECT domain_uuid FROM v_domains ";
sql = sql .. "WHERE domain_name = :domain_name ";
local params = {domain_name = domain_name};
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end
dbh:query(sql, params, function(rows)
domain_uuid = rows["domain_uuid"];
end);
end
end
end
--prevent processing for invalid domains
if (domain_uuid == nil) then if (domain_uuid == nil) then
continue = false; --get the domain_uuid
if (domain_name ~= nil) then
local sql = "SELECT domain_uuid FROM v_domains ";
sql = sql .. "WHERE domain_name = :domain_name ";
local params = {domain_name = domain_name};
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end
dbh:query(sql, params, function(rows)
domain_uuid = rows["domain_uuid"];
end);
end
end end
end
--set the xml array and then concatenate the array to a string --prevent processing for invalid domains
if (continue) then if (domain_uuid == nil) then
continue = false;
end
-- if macro_name is a uuid get from the phrase details --set the xml array and then concatenate the array to a string
if (is_uuid(macro_name)) then if (continue) then
--define the xml table
local xml = {}
--get hte xml -- if macro_name is a uuid get from the phrase details
table.insert(xml, [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>]]); if (is_uuid(macro_name)) then
table.insert(xml, [[<document type="freeswitch/xml">]]); --define the xml table
table.insert(xml, [[ <section name="languages">]]); local xml = {}
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>]]);
local sql = "SELECT * FROM v_phrases as p, v_phrase_details as d "; --get hte xml
sql = sql .. "WHERE d.domain_uuid = :domain_uuid "; table.insert(xml, [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>]]);
sql = sql .. "AND p.phrase_uuid = :macro_name "; table.insert(xml, [[<document type="freeswitch/xml">]]);
sql = sql .. "AND p.phrase_language = :language "; table.insert(xml, [[ <section name="languages">]]);
sql = sql .. "AND p.phrase_uuid = d.phrase_uuid "; table.insert(xml, [[ <language name="]]..language..[[" say-module="]]..language..[[" sound-prefix="]]..sounds_dir..[[/]]..language..[[/us/callie" tts-engine="cepstral" tts-voice="callie">]]);
sql = sql .. "AND p.phrase_enabled = 'true' "; table.insert(xml, [[ <phrases>]]);
sql = sql .. "ORDER BY d.domain_uuid, p.phrase_uuid, d.phrase_detail_order ASC "; table.insert(xml, [[ <macros>]]);
local params = {domain_uuid = domain_uuid, macro_name = macro_name, language = language};
if (debug["sql"]) then local sql = "SELECT * FROM v_phrases as p, v_phrase_details as d ";
freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); sql = sql .. "WHERE d.domain_uuid = :domain_uuid ";
end sql = sql .. "AND p.phrase_uuid = :macro_name ";
previous_phrase_uuid = ""; sql = sql .. "AND p.phrase_language = :language ";
match_tag = "open"; sql = sql .. "AND p.phrase_uuid = d.phrase_uuid ";
x = 0; sql = sql .. "AND p.phrase_enabled = 'true' ";
dbh:query(sql, params, function(row) sql = sql .. "ORDER BY d.domain_uuid, p.phrase_uuid, d.phrase_detail_order ASC ";
--phrase_uuid,domain_uuid,phrase_name,phrase_language local params = {domain_uuid = domain_uuid, macro_name = macro_name, language = language};
--phrase_description,phrase_enabled,phrase_detail_uuid if (debug["sql"]) then
--phrase_detail_group,phrase_detail_tag,phrase_detail_pattern freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n");
--phrase_detail_function,phrase_detail_data,phrase_detail_method end
--phrase_detail_type,phrase_detail_order previous_phrase_uuid = "";
if (previous_phrase_uuid ~= row.phrase_uuid) then match_tag = "open";
if (x > 0) then x = 0;
table.insert(xml, [[ </match>]]); dbh:query(sql, params, function(row)
table.insert(xml, [[ </input>]]); --phrase_uuid,domain_uuid,phrase_name,phrase_language
table.insert(xml, [[ </macro>]]); --phrase_description,phrase_enabled,phrase_detail_uuid
end --phrase_detail_group,phrase_detail_tag,phrase_detail_pattern
table.insert(xml, [[ <macro name="]]..row.phrase_uuid..[[">]]); --phrase_detail_function,phrase_detail_data,phrase_detail_method
table.insert(xml, [[ <input pattern=\"(.*)\">]]); --phrase_detail_type,phrase_detail_order
table.insert(xml, [[ <match>]]); if (previous_phrase_uuid ~= row.phrase_uuid) then
match_open_tag = true if (x > 0) then
table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]);
end end
table.insert(xml, [[ <action function="]]..row.phrase_detail_function..[[" data="]]..row.phrase_detail_data..[["/>]]); table.insert(xml, [[ <macro name="]]..row.phrase_uuid..[[">]]);
previous_phrase_uuid = row.phrase_uuid; table.insert(xml, [[ <input pattern=\"(.*)\">]]);
x = x + 1; table.insert(xml, [[ <match>]]);
end); match_open_tag = true
if (x > 0) then
table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]);
end end
table.insert(xml, [[ </macros>]]); 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
table.insert(xml, [[ </match>]]);
table.insert(xml, [[ </input>]]);
table.insert(xml, [[ </macro>]]);
end
table.insert(xml, [[ </macros>]]);
--output xml & close previous file --output xml & close previous file
table.insert(xml, [[ </phrases>]]); table.insert(xml, [[ </phrases>]]);
table.insert(xml, [[ </language>]]); table.insert(xml, [[ </language>]]);
table.insert(xml, [[ </section>]]); table.insert(xml, [[ </section>]]);
table.insert(xml, [[</document>]]); table.insert(xml, [[</document>]]);
XML_STRING = table.concat(xml, "\n"); XML_STRING = table.concat(xml, "\n");
end
--log to the console
--freeswitch.consoleLog("notice", "[xml_handler] language " .. XML_STRING .. " \n");
--close the database connection
dbh:release();
--set the cache
if (domain_name) then
result = trim(api:execute("memcache", "set languages:" .. language .. ":" .. macro_name .." '"..XML_STRING:gsub("'", "&#39;").."' "..expire["languages"]));
end
--send the xml to the console
if (debug["xml_string"]) then
local file = assert(io.open(temp_dir .. "/xml_handler-" .. language .. "-" .. macro_name ..".xml", "w"));
file:write(XML_STRING);
file:close();
end
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[xml_handler] languages:" .. language .. ":" .. macro_name .." source: database\n");
end
end
end
--get the XML string from the cache
if (source == "cache") then
--replace the &#39 back to a single quote
if (XML_STRING) then
XML_STRING = XML_STRING:gsub("&#39;", "'");
end
--send to the console
if (debug["cache"]) then
if (XML_STRING) then
freeswitch.consoleLog("notice", "[xml_handler] language:" .. language .. ":" .. macro_name .." source: memcache \n");
end end
--log to the console
--freeswitch.consoleLog("notice", "[xml_handler] language " .. XML_STRING .. " \n");
--close the database connection
dbh:release();
end
--set the cache
local ok, err = cache.set(language_cache_key, XML_STRING, expire["acl"]);
if debug["cache"] then
if ok then
freeswitch.consoleLog("notice", "[xml_handler] " .. language_cache_key .. " stored in the cache\n");
else
freeswitch.consoleLog("warning", "[xml_handler] " .. language_cache_key .. " can not be stored in the cache: " .. tostring(err) .. "\n");
end end
end end
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[xml_handler] " .. language_cache_key .. " source: database\n");
end
else
--send to the console
if (debug["cache"]) then
freeswitch.consoleLog("notice", "[xml_handler] " .. language_cache_key .. " source: cache\n");
end
end --if XML_STRING
--send the xml to the console
if (debug["xml_string"]) then
local file = assert(io.open(temp_dir .. "/acl.conf.xml", "w"));
file:write(XML_STRING);
file:close();
end
--if the macro does not exist send "not found", don't cache the not found --if the macro does not exist send "not found", don't cache the not found
if (XML_STRING == nil or trim(XML_STRING) == "-ERR NOT FOUND") then if (XML_STRING == nil or trim(XML_STRING) == "-ERR NOT FOUND") then