Skip to content
Snippets Groups Projects
Commit 89dc1ad2 authored by Maximilian Giller's avatar Maximilian Giller :squid:
Browse files

Merge branch 'feature-primitive-database'

parents 7046f230 46e7ead9
No related branches found
No related tags found
No related merge requests found
Pipeline #54762 passed
node_modules
.parcel-cache
build
.vscode
<?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;
}
<?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;
}
......@@ -93,6 +93,26 @@
</br>
<input type="number" onkeypress="return (event.charCode !=8 && event.charCode ==0 || (event.charCode >= 48 && event.charCode <= 57))" value="5" id="stop-physics-delay" name="stop-physics-delay" class="small-width">
</input>
</br>
</br>
<h3>Import Space</h3>
<label for="import-space-area">Space JSON</label>
</br>
<textarea id="import-space-area" name="import-space-area" class="bottom-space">
</textarea>
</br>
<label for="import-space-name-text">Space Name</label>
</br>
<input type="text" id="import-space-name-text" name="import-space-name-text" class="bottom-space">
</input>
</br>
<button id="import-space-btn" name="import-space-btn" class="bottom-space">Import</button>
</br>
</br>
</div>
</section>
</div>
\ No newline at end of file
import jQuery from "jquery";
import { state } from "./editor";
import { listAllSpaces } from "../../datasets/datasets";
import { listAllSpaces, saveGraphJson } from "../../datasets/datasets";
import { SPACE } from "../../config";
/**
......@@ -11,18 +11,62 @@ export function initInteractions() {
state.clearSelectedItems();
});
// Fill space dropdown
jQuery("button#import-space-btn").on("click", () =>
importSpaceFromInterface()
);
loadSpacesList();
}
function loadSpacesList() {
var selectContainer = jQuery("#space-id-select");
selectContainer.empty();
listAllSpaces().then((spaces) => {
spaces.forEach(space => {
spaces.forEach((space) => {
var selectedTxt = "";
if (space === SPACE) {
selectedTxt = "selected ";
}
var child = "<option " + selectedTxt + "value=\"" + space + "\">" + space + "</option>"
var child =
"<option " +
selectedTxt +
'value="' +
space +
'">' +
space +
"</option>";
selectContainer.append(child);
});
});
selectContainer.val(SPACE);
}
function importSpaceFromInterface() {
var json = undefined;
try {
console.log(jQuery("#import-space-area").val());
json = JSON.parse(jQuery("#import-space-area").val());
} catch (e) {
console.log(e);
alert('"Space JSON" not valid. Check console for details.');
return;
}
var spaceName = jQuery("#import-space-name-text").val();
if (spaceName.length == 0) {
alert('"Space Name" cannot be empty.');
return;
}
saveGraphJson(spaceName, json)
.then(() => {
loadSpacesList();
alert("Space imported!");
})
.catch((ex) => {
console.log(ex);
alert(
"Something went wrong, could not import space. Check console for further details."
);
});
}
<?php
$SPACES_TABLE = ks_get_table_name("spaces");
global $ks_db_version;
$ks_db_version = '0.1';
function ks_get_table_name($table)
{
global $wpdb;
return $wpdb->prefix . "ks_" . $table;
}
register_activation_hook(__FILE__, 'ks_install');
function ks_install()
{
global $wpdb;
global $ks_db_version;
$installed_ver = get_option("ks_db_version");
if ($installed_ver == $ks_db_version) {
return;
}
$charset_collate = $wpdb->get_charset_collate();
require_once(__DIR__ . '/../../../wp-admin/includes/upgrade.php');
global $SPACES_TABLE;
$sql = "CREATE TABLE $SPACES_TABLE (
space_id int(11) NOT NULL AUTO_INCREMENT,
name 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
add_option('ks_db_version', $ks_db_version);
}
// Handling upgrade
function ks_update_db_check()
{
global $ks_db_version;
if (get_site_option('ks_db_version') != $ks_db_version) {
ks_install();
}
}
add_action('plugins_loaded', 'ks_update_db_check');
......@@ -123,6 +123,7 @@ function kg_editor_admin_add_page()
}
require_once(__DIR__ . "/knowledge-space-database.php");
require_once(__DIR__ . '/datasets/datasets.php');
//add_action('wp_enqueue_scripts', 'kg_load_css');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment