Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Knowledge Space WP Plugin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
alg
Knowledge Space WP Plugin
Commits
c8bfb641
Commit
c8bfb641
authored
3 years ago
by
Maximilian Giller
Browse files
Options
Downloads
Patches
Plain Diff
Implemented primitive database storage
parent
b52466ea
No related branches found
No related tags found
No related merge requests found
Pipeline
#54759
passed
3 years ago
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
datasets/datasets.php
+28
-73
28 additions, 73 deletions
datasets/datasets.php
datasets/ks-datasets-database.php
+72
-0
72 additions, 0 deletions
datasets/ks-datasets-database.php
knowledge-space-database.php
+2
-51
2 additions, 51 deletions
knowledge-space-database.php
with
102 additions
and
124 deletions
datasets/datasets.php
+
28
−
73
View file @
c8bfb641
<?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"
,
"k
g
_update_space"
);
// Fires only for logged-in-users
add_action
(
"wp_ajax_update_space"
,
"k
s
_update_space"
);
// Fires only for logged-in-users
//add_action("wp_ajax_nopriv_update_space", 'update_space' ); // Fires for everyone
function
k
g
_update_space
()
{
function
k
s
_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"
]);
k
g_store_new_graph
(
$graph
,
$_POST
[
"space"
]
);
k
s_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
;
}
This diff is collapsed.
Click to expand it.
datasets/ks-datasets-database.php
0 → 100644
+
72
−
0
View file @
c8bfb641
<?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
;
}
This diff is collapsed.
Click to expand it.
knowledge-space-database.php
+
2
−
51
View file @
c8bfb641
...
...
@@ -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
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment