<commit_before><?php

require_once 'LdapUtils.php';

$xml = simplexml_load_file("dades-saga.xml");

$assocs = [
	"id" => "uid", // uidNumber
	"nom" => "givenName",
	"cognom1" => "sn",
	"cognom2" => "sn",
	"adreca" => "street",
	"codipostal" => "postalCode", // organizationalPerson
	"nomlocalitat" => "l",
	"tutor1" => "seeAlso",
	"tutor2" => "seeAlso",
	"provincia" => "st",
	"documentidentitat" => "DNI",
	"sexe" => "gender",
	"datanaixement" => "birthdate"
	// userClass = student, parent,
];

$ldap = new LdapUtils("ester.cat");

$alumnes	= $ldap->XMLParser($xml->alumnes->alumne, $assocs);
$pares		= $ldap->XMLParser($xml->{"tutors-legals"}->{"tutor-legal"}, $assocs);
$profes		= $ldap->XMLParser($xml->personal->personal, $assocs);

echo $ldap->createOU("Groups", FALSE);
echo $ldap->createOU("Users", TRUE);
	echo $ldap->createOU("Alumnes", FALSE);
	echo $ldap->createOU("Professors", FALSE);
	echo $ldap->createOU("Pares", FALSE);

$ldap->cd("ou=Users,ou=Alumnes", TRUE);
foreach($alumnes as $alumne){
	$alumne["sn"] = trim(implode(" ", $alumne["sn"]));
	echo $ldap->createUser($alumne, "uid=" .$alumne["uid"], "persona");
}

$ldap->cd("ou=Users,ou=Pares", TRUE);
foreach($pares as $pare){
	$alumne["sn"] = trim(implode(" ", $alumne["sn"]));
	echo $ldap->createUser($pare, "uid=" .$pare["uid"], "persona");
}

$ldap->cd("ou=Users,ou=Professors", TRUE);
foreach($profes as $profe){
	$alumne["sn"] = trim(implode(" ", $alumne["sn"]));
	echo $ldap->createUser($profe, "uid=" .$profe["uid"], "persona");
}

// ------------------------

function displayldif($array, $ou = "users", $dom = "ester.cat"){
	$uid = "alguno";
	if(isset($array["uid"])){ $uid = $array["uid"]; }
	$dn = "uid=$uid,ou=$ou," .domain2dc($dom);

	// posixAccount inetOrgPerson organizationalPerson top
	$defclass = ["persona"];

	$str = "dn: " .$dn ."\n";
	// $str .= "changetype: add\n"; // HACK
	foreach($defclass as $class){ $str .= "objectClass: $class\n"; }

	foreach($array as $k => $v){
		if(!is_array($v)){ $v = [$v]; }
		foreach($v as $v1){
			$str .= "$k: $v1\n";
		}
	}
	return $str;
}

function fixvals(&$data){
	if(isset($data["gender"])){
		// if($data["gender"] == "H"){ $data["gender"] = "MALE"; }
		// elseif($data["gender"] == "D"){ $data["gender"] = "FEMALE"; }
	}
	if(isset($data["birthdate"])){
		// $data["birthdate"] = date("Y-m-d", strtotime($data["birthdate"]));
		$birth = explode("/", $data["birthdate"]);
		$birth = array_reverse($birth);
		$data["birthdate"] = implode("-", $birth);
	}

	// ---------------
	$user = "";
	// <NAME>
	foreach(explode(" ", $data["givenName"]) as $n){
		$user .= substr($n, 0, 1);
	}
	$ape = $data["sn"];
	if(is_array($ape)){ $ape = $ape[0]; }
	// TODO FIXME si el apellido es "de la Rosa" ?
	$ape = str_replace(" ", "", $ape);
	$user .= $ape;

	$user = strtolower(trim($user));
	$user = sanstr($user); // Remove accents
	// $data["uid"] = $user; // HACK
	// unset($user);
	unset($ape);
	// ---------------

	if(isset($data["sn"]) && is_array($data["sn"])){
		$data["sn"] = trim(implode(" ", $data["sn"]));
	}
	if(isset($data["st"])){
		if($data["st"] == "08"){ $data["st"] = "BARCELONA"; }
	}
	if(isset($data["seeAlso"])){
		$final = array();
		if(!is_array($data["seeAlso"])){ $data["seeAlso"] = [$data["seeAlso"]]; }
		foreach($data["seeAlso"] as $also){
			$final[] = "uid=$also,ou=users," .domain2dc("ester.cat");
		}
		$data["seeAlso"] = $final;
	}

	$data["cn"][] = $data["givenName"] ." " .$data["sn"];
	$data["cn"][] = $user;
}

?>
<commit_msg>WIP Saga.<commit_after><?php

require_once 'LdapUtils.php';

$xml = simplexml_load_file("dades-saga.xml");

$assocs = [
	"id" => "uid", // uidNumber
	"nom" => "givenName",
	"cognom1" => "sn",
	"cognom2" => "sn",
	"adreca" => "street",
	"codipostal" => "postalCode", // organizationalPerson
	"nomlocalitat" => "l",
	"tutor1" => "seeAlso",
	"tutor2" => "seeAlso",
	"provincia" => "st",
	"documentidentitat" => "DNI",
	"sexe" => "gender",
	"datanaixement" => "birthdate"
	// userClass = student, parent,
];

$ldap = new LdapUtils("ester.cat");

$alumnes	= $ldap->XMLParser($xml->alumnes->alumne, $assocs);
$pares		= $ldap->XMLParser($xml->{"tutors-legals"}->{"tutor-legal"}, $assocs);
$profes		= $ldap->XMLParser($xml->personal->personal, $assocs);

echo $ldap->createOU("Groups", FALSE);
echo $ldap->createOU("Users", TRUE);
	echo $ldap->createOU("Alumnes", FALSE);
	echo $ldap->createOU("Professors", FALSE);
	echo $ldap->createOU("Pares", FALSE);

$ldap->cd("ou=Users,ou=Alumnes", TRUE);
foreach($alumnes as $alumne){
	fixvals($alumne);
	echo $ldap->createUser($alumne, "uid=" .$alumne["uid"], "persona");
}

$ldap->cd("ou=Users,ou=Pares", TRUE);
foreach($pares as $pare){
	fixvals($pare);
	echo $ldap->createUser($pare, "uid=" .$pare["uid"], "persona");
}

$ldap->cd("ou=Users,ou=Professors", TRUE);
foreach($profes as $profe){
	fixvals($profe);
	echo $ldap->createUser($profe, "uid=" .$profe["uid"], "persona");
}

// ------------------------

function fixvals(&$data){
	if(isset($data["gender"])){
		// if($data["gender"] == "H"){ $data["gender"] = "MALE"; }
		// elseif($data["gender"] == "D"){ $data["gender"] = "FEMALE"; }
	}
	if(isset($data["birthdate"])){
		// $data["birthdate"] = date("Y-m-d", strtotime($data["birthdate"]));
		$birth = explode("/", $data["birthdate"]);
		$birth = array_reverse($birth);
		$data["birthdate"] = implode("-", $birth);
	}

	// ---------------
	$user = "";
	// <NAME>
	foreach(explode(" ", $data["givenName"]) as $n){
		$user .= substr($n, 0, 1);
	}
	$ape = $data["sn"];
	if(is_array($ape)){ $ape = $ape[0]; }
	// TODO FIXME si el apellido es "de la Rosa" ?
	$ape = str_replace(" ", "", $ape);
	$user .= $ape;

	$user = strtolower(trim($user));
	// $user = sanstr($user); // Remove accents
	// $data["uid"] = $user; // HACK
	// unset($user);
	unset($ape);
	// ---------------

	if(isset($data["sn"]) && is_array($data["sn"])){
		$data["sn"] = trim(implode(" ", $data["sn"]));
	}
	if(isset($data["st"])){
		if($data["st"] == "08"){ $data["st"] = "BARCELONA"; }
	}
	if(isset($data["seeAlso"])){
		$final = array();
		if(!is_array($data["seeAlso"])){ $data["seeAlso"] = [$data["seeAlso"]]; }
		foreach($data["seeAlso"] as $also){
			$final[] = "uid=$also,ou=users," .domain2dc("ester.cat");
		}
		$data["seeAlso"] = $final;
	}

	$data["cn"][] = $data["givenName"] ." " .$data["sn"];
	$data["cn"][] = $user;
}

?>

{'fraction_non_alphanumeric': 0.29807388695926745, 'fraction_numerical': 0.0036311967161351435, 'mean_word_length': 4.566783831282953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '717137', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2608, 'n_words': 434}