diff --git a/datasets/datasets.php b/datasets/datasets.php index 653951b1cd248662dc96b68d3674f91de1721337..70502aa22ad600eeb165af9faa4f1051fbb86ee1 100644 --- a/datasets/datasets.php +++ b/datasets/datasets.php @@ -1,43 +1,50 @@ <?php +require_once(__DIR__ . "/ks-datasets-database.php"); $EMPTY_SPACE = '{"links":[],"nodes":[]}'; -$SPACES_DIR = __DIR__."/spaces/"; -add_action("wp_ajax_get_space", "kg_get_space"); // Fires only for logged-in-users -add_action("wp_ajax_nopriv_get_space", 'kg_get_space' ); // Fires for everyone -function kg_get_space() { - $file_path = kg_get_space_file_path($_POST["space"]); +add_action("wp_ajax_get_space", "ks_get_space"); // Fires only for logged-in-users +add_action("wp_ajax_nopriv_get_space", 'ks_get_space' ); // Fires for everyone +function ks_get_space() { + $name = ks_escape_space_name($_POST["space"]); + + $space = ks_select_space($name); // If it doesn't exist, create new empty space - if (!file_exists($file_path)) { - kg_create_empty_space($file_path); + if ($space == NULL) { + global $EMPTY_SPACE; + ks_insert_space($name, $EMPTY_SPACE); + $space = ks_select_space($name); } - $content = file_get_contents($file_path); - echo $content; + echo $space->graph; wp_die(); } -add_action("wp_ajax_list_spaces", "kg_list_spaces"); // Fires only for logged-in-users -function kg_list_spaces() { - $spaces = kg_get_list_of_spaces(); +add_action("wp_ajax_list_spaces", "ks_list_spaces"); // Fires only for logged-in-users +function ks_list_spaces() { + $spaces = array(); + foreach (ks_select_all_spaces() as $space) { + $spaces[] = $space->name; + } + $payload = array("spaces" => $spaces); - echo json_encode($payload); wp_die(); } -add_action("wp_ajax_update_space", "kg_update_space"); // Fires only for logged-in-users +add_action("wp_ajax_update_space", "ks_update_space"); // Fires only for logged-in-users //add_action("wp_ajax_nopriv_update_space", 'update_space' ); // Fires for everyone -function kg_update_space() { +function ks_update_space() { // Check user capabilities if (current_user_can("edit_posts")) { // Use json encoding. $graph = stripslashes($_POST["graph"]); + $name = ks_escape_space_name($_POST["space"]); - kg_store_new_graph($graph, $_POST["space"]); + ks_insert_or_update_graph($name, $graph); wp_die(); } else { @@ -45,62 +52,10 @@ function kg_update_space() { } } -function kg_store_new_graph($graph, $space_id) { - $file_path = kg_get_space_file_path($space_id); - $result = file_put_contents($file_path, $graph); - - //echo print_r($_POST); - echo "Saved file at "; - echo $file_path; - echo " "; - echo $result; -} - -function kg_get_space_file_path($space_id) { +function ks_escape_space_name($space_name) { // Cleaning up the space id - $space_id = str_replace("/", "-", $space_id); - $space_id = str_replace("\\", "-", $space_id); - $space_id = str_replace(".", "-", $space_id); - - global $SPACES_DIR; - return $SPACES_DIR.$space_id.".json"; -} - -function kg_get_list_of_spaces() { - global $SPACES_DIR; - $all_files = scandir($SPACES_DIR); - - if ($all_files == false) { - return []; - } - - $spaces = []; - - foreach ($all_files as $file) { - if (endsWith($file, ".json")) { - $spaces[] = substr($file, 0, -strlen(".json")); - } - } - - return $spaces; -} - -function kg_create_empty_space($file_path) { - // Don't do anything, if it exists - if (file_exists($file_path)) { - return; - } - - // Write empty space to file - global $EMPTY_SPACE; - file_put_contents($file_path, $EMPTY_SPACE); -} - -function endsWith( $haystack, $needle ) { - # Source: https://stackoverflow.com/a/834355/7376120 - $length = strlen( $needle ); - if( !$length ) { - return true; - } - return substr( $haystack, -$length ) === $needle; + $space_name = str_replace("/", "-", $space_name); + $space_name = str_replace("\\", "-", $space_name); + $space_name = str_replace(".", "-", $space_name); + return $space_name; } diff --git a/datasets/ks-datasets-database.php b/datasets/ks-datasets-database.php new file mode 100644 index 0000000000000000000000000000000000000000..0a526beb88f24369b965e014587736ebcc2f3e16 --- /dev/null +++ b/datasets/ks-datasets-database.php @@ -0,0 +1,72 @@ +<?php +require_once(__DIR__ . "/../knowledge-space-database.php"); + +function ks_insert_or_update_graph($name, $graph) +{ + // Delete old graph and insert new + global $SPACES_TABLE; + global $wpdb; + $wpdb->delete($SPACES_TABLE, array("name" => $name), array("%s")); + + return ks_insert_space($name, $graph); +} + +function ks_select_all_spaces() +{ + global $SPACES_TABLE; + return ks_select("SELECT * FROM $SPACES_TABLE"); +} + +function ks_select_space($space_name) +{ + global $SPACES_TABLE; + return ks_select_single("SELECT * FROM $SPACES_TABLE WHERE name = %s", $space_name); +} + +function ks_insert_space($name = null, $graph = "") +{ + // If no name given, set default name + if ($name == null) { + // Adding current time to make it somewhat unique + $now = new DateTime(); + $name = "New space - " . $now->format('Y-m-d H:i:s'); + } + + $data = array( + "name" => $name, + "graph" => $graph + ); + $format = array("%s", "%s"); + + global $SPACES_TABLE; + global $wpdb; + $wpdb->insert($SPACES_TABLE, $data, $format); + + return $wpdb->insert_id; +} + +function ks_select($query, $parameters = array()) +{ + global $wpdb; + $sql = $wpdb->prepare($query, $parameters); + return $wpdb->get_results($sql); +} + +function ks_select_single($query, $parameters = array()) +{ + $result = ks_select($query, $parameters); + + if ($result) { + return $result[0]; + } else { + return null; + } +} + +function ks_insert($table, $data) +{ + global $wpdb; + $wpdb->insert($table, $data); + + return $wpdb->insert_id; +} diff --git a/knowledge-space-database.php b/knowledge-space-database.php index 07d5e1379b92921226257ef521626de63678c079..ee250886ba2beea52c3a93d5b86b07060b3f579c 100644 --- a/knowledge-space-database.php +++ b/knowledge-space-database.php @@ -2,14 +2,10 @@ $SPACES_TABLE = ks_get_table_name("spaces"); -$NODES_TABLE = ks_get_table_name("nodes"); -$NODETYPES_TABLE = ks_get_table_name("nodetypes"); -$LINKS_TABLE = ks_get_table_name("links"); -$REFERENCES_TABLE = ks_get_table_name("references"); global $ks_db_version; -$ks_db_version = '1.0'; +$ks_db_version = '0.1'; function ks_get_table_name($table) { @@ -35,56 +31,11 @@ function ks_install() $sql = "CREATE TABLE $SPACES_TABLE ( space_id int(11) NOT NULL AUTO_INCREMENT, name text NOT NULL, - description text NOT NULL, + graph text NOT NULL, PRIMARY KEY (space_id) ) $charset_collate;"; dbDelta($sql); //! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins - - global $NODETYPES_TABLE; - $sql = "CREATE TABLE $NODETYPES_TABLE ( - nodetype_id int(11) NOT NULL AUTO_INCREMENT, - name text NOT NULL, - color bit(24) NOT NULL, - PRIMARY KEY (nodetype_id) - ) $charset_collate;"; - dbDelta($sql); //! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins - - - global $NODES_TABLE; - $sql = "CREATE TABLE $NODES_TABLE ( - node_id int(11) NOT NULL AUTO_INCREMENT, - title text NOT NULL, - description text NOT NULL, - icon_url text, - header_url text, - video_url text, - nodetype_id int(11) NOT NULL, - space_id int(11) NOT NULL, - PRIMARY KEY (node_id) - ) $charset_collate;"; - dbDelta($sql); //! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins - - - global $LINKS_TABLE; - $sql = "CREATE TABLE $LINKS_TABLE ( - link_id int(11) NOT NULL AUTO_INCREMENT, - source_node_id int(11) NOT NULL, - target_node_id int(11) NOT NULL, - PRIMARY KEY (link_id) - ) $charset_collate;"; - dbDelta($sql); //! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins - - - global $REFERENCES_TABLE; - $sql = "CREATE TABLE $REFERENCES_TABLE ( - reference_id int(11) NOT NULL AUTO_INCREMENT, - url text NOT NULL, - node_id int(11) NOT NULL, - PRIMARY KEY (reference_id) - ) $charset_collate;"; - dbDelta($sql); //! Has some weird restrictions for the SQL Query: https://codex.wordpress.org/Creating_Tables_with_Plugins - add_option('ks_db_version', $ks_db_version); }