public function post_river() { if ($response = $this->checkAuth(Input::getPath()->part(5), Input::get('key'))) { return $response; } $triggerInstalled = false; $schema = Input::getPath()->part(6); $table = Input::getPath()->part(7); $index = $schema; $type = $table; $db = Input::getPath()->part(5); $fullIndex = $db . "_" . $index; $fullTable = $schema . "." . $table; $insert = Input::get('insert') ?: "t"; $triggerSchema = Input::get('ts') ?: $schema; $triggerTable = Input::get('tt') ?: $table; $installTrigger = false; if (mb_substr($type, 0, 1, 'utf-8') == "_") { $type = "a" . $type; } $model = new \app\inc\Model(); $relationCheck = $model->isTableOrView($triggerSchema . "." . $triggerTable); if (!$relationCheck["success"]) { return array("success" => false, "message" => "Trigger table doesn't exists", "code" => "406"); } else { if ($relationCheck["data"] == "table") { $installTrigger = true; } } $relationType = $model->isTableOrView($fullTable); $priObj = $model->getPrimeryKey($fullTable); $priKey = $priObj["attname"]; $model->close(); // Close the PDO connection $pl = file_get_contents(\app\conf\App::$param["path"] . "/app/scripts/sql/notify_transaction.sql"); // TODO check if sprintf is needed $pl = sprintf($pl, $priKey, $priKey, $priKey); $result = $model->execQuery($pl, "PG"); if (!$result) { $response['success'] = false; return $response; } // Drop the trigger $pl = "DROP TRIGGER IF EXISTS _gc2_notify_transaction_trigger ON {$triggerSchema}.{$triggerTable}"; $result = $model->execQuery($pl, "PG"); // Define default settings for the new index $defaultSettings = array("settings" => array("number_of_shards" => 1, "analysis" => array("analyzer" => array("str_search_analyzer" => array("type" => "custom", "tokenizer" => "whitespace", "filter" => array("0" => "lowercase")), "str_index_analyzer" => array("type" => "custom", "tokenizer" => "whitespace", "filter" => array("0" => "lowercase", "1" => "substring"))), "filter" => array("substring" => array("type" => "edgeNGram", "min_gram" => 1, "max_gram" => 255))))); // Check if there are custom settings if (!($settings = @file_get_contents(\app\conf\App::$param["path"] . "/app/conf/elasticsearch_settings.json"))) { $settings = json_encode($defaultSettings); } $es = new \app\models\Elasticsearch(); // Delete the type $res = $es->delete($fullIndex, $type); $obj = json_decode($res["json"], true); if (isset($obj["error"]) && $obj["error"] != false) { // If type not already exists we just ignore the error. // TODO check if type exist before deleting it. /*$response['success'] = false; $response['message'] = $obj["error"]; $response['code'] = $obj["status"]; return $response;*/ } // Create the index with settings $res = $es->createIndex($fullIndex, $settings); $obj = json_decode($res["json"], true); if (isset($obj["error"]) && $obj["error"] != false) { // If index already exists we just ignore it. // TODO check if index already exists before creating it. /*$response['success'] = false; $response['message'] = $obj["error"]; $response['code'] = $obj["status"]; return $response;*/ } // Mappings from the table $map = $es->createMapFromTable($fullTable); $res = $es->map($fullIndex, $type, json_encode($map)); $obj = json_decode($res["json"], true); if (isset($obj["error"]) && $obj["error"] != false) { $response['success'] = false; $response['message'] = $obj["error"]; $response['code'] = $obj["status"]; return $response; } // Bulk insert if ($insert == "t") { $sql = "SELECT * FROM {$fullTable}"; $api = new \app\models\Sql_to_es("4326"); $api->execQuery("set client_encoding='UTF8'", "PDO"); $res = $api->sql($sql, $index, $type, $priKey, $db); if (!$res["success"]) { return $res; } $res["Indexed"] = true; } else { $res = array("succes" => true, "indexed" => false, "message" => "Indexing skipped"); } // Create the trigger $triggerInstalledIn = null; if ($relationType["data"] == "table" || $installTrigger) { $pl = "CREATE TRIGGER _gc2_notify_transaction_trigger AFTER INSERT OR UPDATE OR DELETE ON {$triggerSchema}.{$triggerTable} FOR EACH ROW EXECUTE PROCEDURE _gc2_notify_transaction('{$priKey}', '{$schema}','{$table}')"; $result = $model->execQuery($pl, "PG"); if (!$result) { $response['success'] = false; return $response; } $triggerInstalled = true; $triggerInstalledIn = "{$triggerSchema}.{$triggerTable}"; } $res["_index"] = $fullIndex; $res["_type"] = $type; $res["relation"] = $relationType["data"]; $res["trigger_installed"] = $triggerInstalled; $res["trigger_installed_in"] = $triggerInstalledIn; return $res; }