<commit_before><?php

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );
include_once( 'utils.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

# operation=rename_node&id=6&title=neuron1+%3Cneuron%3E+

$op = isset( $_REQUEST[ 'operation' ] ) ? $_REQUEST[ 'operation' ] : 0;

$name = isset( $_REQUEST[ 'title' ] ) ? $_REQUEST[ 'title' ] : 0;
$id = isset( $_REQUEST[ 'id' ] ) ? intval($_REQUEST[ 'id' ]) : 0;
$src = isset( $_REQUEST[ 'src' ] ) ? intval($_REQUEST[ 'src' ]) : 0;
$ref = isset( $_REQUEST[ 'ref' ] ) ? intval($_REQUEST[ 'ref' ]) : 0;
$rel = isset( $_REQUEST[ 'rel' ] ) ? $_REQUEST[ 'rel' ] : 0;


# 1. There must be a project id
if ( ! $pid ) {
  echo json_encode( array( 'error' => 'Project closed. Cannot apply operation.' ) );
	return;
}

# 2. There must be a user id
if ( ! $uid ) {
    echo json_encode( array( 'error' => 'You are not logged in.' ) );
	return;
}

# 3. Permissions?
canEditOrExit($db, $uid, $pid);


// Start transaction
if (! $db->begin() ) {
	echo json_encode( array( 'error' => 'Could not start transaction.' ) );
	return;
}




// remove skeleton, i.e. treenodes and corresponding relations 
function remove_skeleton($db, $pid, $skelid) {
	
	$lablid = $db->getRelationId( $pid, "labeled_as" );
	$preid = $db->getRelationId( $pid, "presynaptic_to" );
	$postid = $db->getRelationId( $pid, "postsynaptic_to" );
	
	// labeled_as, presynaptic_to, postsynaptic_to, element_of
	$relarr = array( $lablid, $preid, $postid );
	foreach( $relarr as $val ) {
		$res = $db->getResult('DELETE FROM 
			  "treenode_class_instance" AS "tci"
			 WHERE 
			  "tci"."treenode_id" IN (
			    SELECT "tn"."id"
			    FROM "treenode" AS "tn"
			    INNER JOIN "treenode_class_instance" AS "tci2"
			    ON "tci2"."treenode_id" = "tn"."id" 
			    WHERE "tci2"."class_instance_id" = '.$skelid.' AND "tci2"."project_id" = '.$pid.'
			  ) AND "tci"."relation_id" = '.$val);
    if (false === $res) {
      emitErrorAndExit($db, 'Failed to delete treenode instances for skeleton #'.$skid);
    }
	}
	// remove treenodes from treenode table, should remove the remaining<commit_msg>Removing a skeleton from the object tree now removes also its terminals<commit_after><?php

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );
include_once( 'utils.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

# operation=rename_node&id=6&title=neuron1+%3Cneuron%3E+

$op = isset( $_REQUEST[ 'operation' ] ) ? $_REQUEST[ 'operation' ] : 0;

$name = isset( $_REQUEST[ 'title' ] ) ? $_REQUEST[ 'title' ] : 0;
$id = isset( $_REQUEST[ 'id' ] ) ? intval($_REQUEST[ 'id' ]) : 0;
$src = isset( $_REQUEST[ 'src' ] ) ? intval($_REQUEST[ 'src' ]) : 0;
$ref = isset( $_REQUEST[ 'ref' ] ) ? intval($_REQUEST[ 'ref' ]) : 0;
$rel = isset( $_REQUEST[ 'rel' ] ) ? $_REQUEST[ 'rel' ] : 0;


# 1. There must be a project id
if ( ! $pid ) {
  echo json_encode( array( 'error' => 'Project closed. Cannot apply operation.' ) );
	return;
}

# 2. There must be a user id
if ( ! $uid ) {
    echo json_encode( array( 'error' => 'You are not logged in.' ) );
	return;
}

# 3. Permissions?
canEditOrExit($db, $uid, $pid);


// Start transaction
if (! $db->begin() ) {
	echo json_encode( array( 'error' => 'Could not start transaction.' ) );
	return;
}




// remove skeleton, i.e. treenodes and corresponding relations 
function remove_skeleton($db, $pid, $skelid) {
	
	$lablid = $db->getRelationId( $pid, "labeled_as" );
	$preid = $db->getRelationId( $pid, "presynaptic_to" );
	$postid = $db->getRelationId( $pid, "postsynaptic_to" );

	// delete all terminals of treenodes
    $model_of_id = $db->getRelationId( $pid, "model_of" );
    $res = $db->getResult('SELECT * FROM
			  "treenode_class_instance" AS "tci"
			 WHERE
			  "tci"."treenode_id" IN (
			    SELECT "tn"."id"
			    FROM "treenode" AS "tn"
			    INNER JOIN "treenode_class_instance" AS "tci2"
			    ON "tci2"."treenode_id" = "tn"."id"
			    WHERE "tci2"."class_instance_id" = '.$skelid.' AND "tci2"."project_id" = '.$pid.'
			  ) AND "tci"."relation_id" = '.$model_of_id);
    if (count($res) > 0) {
        // remove all the terminals
        foreach($res as $key => $val) {
            $ids = $db->deleteFrom("class_instance", ' "class_instance"."id" = '.$val['class_instance_id']);
        }
    }
    // -----------

	// labeled_as, presynaptic_to, postsynaptic_to, element_of
	$relarr = array( $lablid, $preid, $postid );
	foreach( $relarr as $val ) {
		$res = $db->getResult('DELETE FROM 
			  "treenode_class_instance" AS "tci"
			 WHERE 
			  "tci"."treenode_id" IN (
			    SELECT "tn"."id"
			    FROM "treenode" AS "tn"
			    INNER JOIN "treenode_class_instance" AS "tci2"
			    ON "tci2"."treenode_id" = "tn"."id" 
			    WHERE "tci2"."class_instance_id" = '.$skelid.' AND "tci2"."project_id" = '.$pid.'
			  ) AND "tci"."relation_id" = '.$val);
    if (false === $res) {
      emitErrorAndExit($db, 'Failed to delete treenode instances for skeleton #'.$skid);
    }
	}
	// remove treenodes from treenode table, should remove the remaining
{'fraction_non_alphanumeric': 0.19338933528122718, 'fraction_numerical': 0.007852447041636231, 'mean_word_length': 3.549003322259136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4876021', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2069, 'n_words': 502}