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; }
public function get_index() { $dir = App::$param['path'] . "app/tmp/" . Connection::$param["postgisdb"] . "/__vectors"; $safeName = \app\inc\Model::toAscii($_REQUEST['name'], array(), "_"); $skipFailures = $_REQUEST["ignoreerrors"] == "true" ? true : false; $append = $_REQUEST["append"] == "true" ? true : false; $overwrite = $_REQUEST["overwrite"] == "true" ? true : false; if (is_numeric($safeName[0])) { $safeName = "_" . $safeName; } //Check if file is .zip $zipCheck1 = explode(".", $_REQUEST['file']); $zipCheck2 = array_reverse($zipCheck1); if (strtolower($zipCheck2[0]) == "zip" || strtolower($zipCheck2[0]) == "rar") { $ext = array("shp", "tab", "geojson", "gml", "kml", "mif", "gdb"); $folderArr = array(); $safeNameArr = array(); for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $folderArr[] = $zipCheck1[$i]; } $folder = implode(".", $folderArr); if (strtolower($zipCheck2[0]) == "zip") { // ZIP start $zip = new \ZipArchive(); $res = $zip->open($dir . "/" . $_REQUEST['file']); if ($res === false) { $response['success'] = false; $response['message'] = "Could not unzip file"; return Response::json($response); } $zip->extractTo($dir . "/" . $folder); $zip->close(); // ZIP end } if (strtolower($zipCheck2[0]) == "rar") { // RAR start $rar_file = rar_open($dir . "/" . $_REQUEST['file']); if (!$rar_file) { $response['success'] = false; $response['message'] = "Could not unrar file"; return Response::json($response); } $list = rar_list($rar_file); foreach ($list as $file) { $entry = rar_entry_get($rar_file, $file); $file->extract($dir . "/" . $folder); // extract to the current dir } rar_close($rar_file); // RAR end } if ($handle = opendir($dir . "/" . $folder)) { while (false !== ($entry = readdir($handle))) { if ($entry !== "." && $entry !== "..") { $zipCheck1 = explode(".", $entry); $zipCheck2 = array_reverse($zipCheck1); if (in_array(strtolower($zipCheck2[0]), $ext)) { $_REQUEST['file'] = $folder . "/" . $entry; for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $safeNameArr[] = $zipCheck1[$i]; } $safeName = \app\inc\Model::toAscii(implode(".", $safeNameArr), array(), "_"); break; } $_REQUEST['file'] = $folder; } } } } $srid = $_REQUEST['srid'] ?: "4326"; $encoding = $_REQUEST['encoding'] ?: "LATIN1"; switch ($_REQUEST['type']) { case "Point": $type = "point"; break; case "Polygon": $type = "multipolygon"; break; case "Line": $type = "multilinestring"; break; case "Geometry": $type = "geometry"; break; default: $type = "PROMOTE_TO_MULTI"; break; } $model = new \app\inc\Model(); $tableExist = $model->isTableOrView(Connection::$param["postgisschema"] . "." . $safeName); $tableExist = $tableExist["success"]; if ($tableExist == true && $overwrite == false && $append == false) { $response['success'] = false; $response['message'] = "'{$safeName}' exists already, use 'Overwrite'"; $response['code'] = 406; return $response; } if ($_REQUEST["append"] == "true") { $sql = "DELETE FROM " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { $response['success'] = false; $response['message'] = "Could not delete from {$safeName}"; $response['code'] = 406; return $response; } } $cmd = "PGCLIENTENCODING={$encoding} ogr2ogr " . ($skipFailures ? "-skipfailures " : " ") . ($append ? "-append " : " ") . ($overwrite == true && $append == false ? "-overwrite " : " ") . "-dim 2 " . ($append ? "" : "-lco 'GEOMETRY_NAME=the_geom' ") . ($append ? "" : "-lco 'FID=gid' ") . ($append ? "" : "-lco 'PRECISION=NO' ") . ($append ? "" : "-lco 'PG_USE_COPY=YES' ") . "-a_srs 'EPSG:{$srid}' " . "-f 'PostgreSQL' PG:'host=" . Connection::$param["postgishost"] . " user="******"postgisuser"] . " password="******"postgispw"] . " dbname=" . Connection::$param["postgisdb"] . " active_schema=" . Connection::$param["postgisschema"] . "' " . "'" . $dir . "/" . $_REQUEST['file'] . "' " . "-nln {$safeName} " . "-nlt {$type}"; exec($cmd . ' 2>&1', $out, $err); $geoType = $model->getGeometryColumns(Connection::$param["postgisschema"] . "." . $safeName, "type"); $key = Connection::$param["postgisschema"] . "." . $safeName . ".the_geom"; $class = new \app\models\Classification($key); $arr = $class->getAll(); // Set layer editable $join = new \app\models\Table("settings.geometry_columns_join"); $json = '{"data":{"editable":true,"_key_":"' . $key . '"}}'; $data = (array) json_decode(urldecode($json)); $join->updateRecord($data, "_key_"); if (empty($arr['data'])) { $class->insert(); $class->update("0", \app\models\Classification::createClass($geoType)); } $def = new \app\models\Tile($key); $arr = $def->get(); if (empty($arr['data'][0])) { $json = '{ "theme_column":"", "label_column":"", "query_buffer":"", "opacity":"", "label_max_scale":"", "label_min_scale":"", "meta_tiles":false, "meta_size":"3", "meta_buffer":"10", "ttl":""}'; $def->update($json); } if ($out[0] == "") { $response['success'] = true; $response['message'] = "Layer <b>{$safeName}</b> is created"; $response['type'] = $geoType; // Bust cache, in case of layer already exist \app\controllers\Tilecache::bust(Connection::$param["postgisschema"] . "." . $safeName); } else { $response['success'] = false; $response['message'] = $safeName . ": Some thing went wrong. Check the log."; $response['out'] = $out[0]; Session::createLog($out, $_REQUEST['file']); // Make sure the table is dropped if not skipping failures and it didn't exists before if ($skipFailures == false && $tableExist == false) { $sql = "DROP TABLE " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { } } } $response['cmd'] = $cmd; return $response; }