include_once "schema_discoverer.php5";
//include_once "flat_to_hierarchy_converter.php5";
include_once "db_loader.php5";
//include_once "schema_checker.php5";
include_once "db_modifier.php5";
include_once "schema_loader.php5";
set_time_limit(0);
//error_reporting(0);
session_start();
$function = strtolower($_POST["function"]);
if($function == "get_fields") {
$project = strtolower($_POST["project"]);
$db_file = "../project_xml_files/".$_POST["db_file"];
$head_tag = strtolower($_POST["head_tag"]);
$symbols = $_POST["symbols"];
$first_chars = $_POST["first_chars"];
$db_modifier = new db_modifier();
$db_modifier->get_fields($db_file,$head_tag,$symbols,$first_chars);
$vars = $_POST;
$vars["fields"] = $db_modifier->fields;
$vars["strippable_fields"] = $db_modifier->strippable_fields;
$_SESSION["vars"] = $vars;
if(file_exists("../$project/schema.xml")) {
$sl = new schema_loader();
$group = $sl->create_group_structure("../$project/schema.xml","");
$existing_fields = array_merge($group->get_field_names(),$group->get_group_names());
}
else {
$existing_fields = array();
}
$xml = "\n";
$xml .= "";
$xml .= "\n";
foreach($db_modifier->fields as $field) {
$xml .= "\n$field\n";
if(in_array($field,$existing_fields)) {
$xml .= "1\n";
}
else {
$xml .= "0\n";
}
if(in_array($field,$db_modifier->strippable_fields)) {
$xml .= "1\n";
if(in_array($field."_s",$existing_fields)) {
$xml .= "1\n";
}
else {
$xml .= "0\n";
}
}
else {
$xml .= "0\n";
}
$xml .= "\n";
}
$xml .= "";
header("Content-Type: application/xml");
echo $xml;
}
else if($function == "modify") {
$vars = $_SESSION["vars"];
$sort_fields = trim(strtolower($vars["sort_fields"]));
$head_tag = strtolower($vars["head_tag"]);
$project = strtolower($vars["project"]);
$symbols = $vars["symbols"];
$first_chars = $vars["first_chars"];
$fields = $vars["fields"];
$strippable_fields = $vars["strippable_fields"];
$db_file = "../project_xml_files/".$vars["db_file"];
$format_type = strtolower($vars["format_type"]);
$schema = $vars["schema"];
$new_database_name = $vars["new_database_name"];
$alpha_symbols = $vars["alpha_symbols"];
$alpha_first_chars = $vars["alpha_first_chars"];
$not_include = array();
if(array_key_exists("not_include",$_POST)) {
$not_include = $_POST["not_include"];
}
$fields_not_to_strip = array();
if(array_key_exists("fields_not_to_strip",$_POST)) {
$fields_not_to_strip = $_POST["fields_not_to_strip"];
}
$fields = array_diff($fields,$not_include);
$strippable_fields = array_diff($strippable_fields,$not_include,$fields_not_to_strip);
if(!(($not_include == "") && ($fields_not_to_strip == "") && ($this->sort_fields == ""))) {
$new_name = $db_file.".temp";
$db_modifier = new db_modifier();
$db_modifier->modify($db_file,$new_name,$head_tag,
$sort_fields,$alpha_symbols, $alpha_first_chars,
$symbols,$first_chars,$fields,$strippable_fields);
$db_file = $new_name;
}
$path = "../$project";
if(!file_exists($path)) {
mkdir($path,0755);
}
$errors = array();
if($format_type == "has_structure") {
echo "Inferring the form of each entry in order to create a schema...
";
$sd = new schema_discoverer();
$schema = $sd->create_schema($db_file,$head_tag);
$out = fopen("$path/schema.xml","w");
fwrite($out,$schema);
fclose($out);
echo "Determined the schema.
";
}
/*
else if($format_type == "needs_structure") {
$id_field = strtolower($_POST["id_field"]);
$checker = new schema_checker();
echo "Checking for errors in the structure of your database based on the supplied schema...
";
$errors = $checker->check_schema($schema,$db_file,$id_field);
if(count($errors) > 0) {
echo "One or more entries in your database do not match the schema.
";
echo "Please make the necessary changes to the following entries:
";
foreach($errors as $count=>$error) {
echo ($count+1)." $error
";
}
}
else {
echo "No errors found.
";
echo "Converting flat xml to hierarchical xml...
";
$converter = new flat_to_hierarchy_converter();
$converter->produce_xml_with_hierarchy($project,$db_file,$schema,
$new_database_name,"$path/schema.xml");
unlink($db_file);
$db_file = $new_database_name;
echo "Created hierarchical database.
";
}
}
*/
if(count($errors) == 0) {
echo "Loading database into MySQL...
";
$mode = 1;
$dl = new db_loader();
$dl->xml_to_db_bulk($project,$path."/schema.xml",$db_file,$mode);
if(ereg("temp",$db_file)) {
unlink($db_file);
}
echo "Finished loading database. You are now ready to test!
";
}
echo "Click HERE to search your project.
";
echo "Fields included in the database:
";
$sl = new schema_loader();
$group = $sl->create_group_structure("../$project/schema.xml");
$fields = $group->get_field_names();
foreach($fields as $field) {
echo "$field
";
}
if(file_exists("../$project/defaults.xml")) {
$defaults = simplexml_load_file("../$project/defaults.xml");
}
else {
$defaults = simplexml_load_file("../project_default_files/defaults.xml");
}
$defaults->project = $project;
if($sort_fields != "") {
$defaults->sort_order = "alpha";
}
else {
$defaults->sort_order = "";
}
$xml = $defaults->asXML();
$out = fopen("../$project/defaults.xml","w");
fwrite($out,$xml);
fclose($out);
if(!file_exists("../$project/connectives.xml")) {
copy("../project_default_files/connectives.xml","../$project/connectives.xml");
}
if(!file_exists("../$project/reg_exps.xml")) {
copy("../project_default_files/reg_exps.xml","../$project/reg_exps.xml");
}
if(!file_exists("../$project/phrases_and_fields.xml")) {
$sets = simplexml_load_file("../project_default_files/phrases_and_fields.xml");
$set = $sets->SET;
$xml = "\n";
foreach($fields as $field) {
$set->ENGLISH = $field;
$set->VALUE = $field;
$xml .= $set->asXML()."\n";;
}
$xml .= "\n";
$out = fopen("../$project/phrases_and_fields.xml","w");
fwrite($out,$xml);
fclose($out);
}
if(!file_exists("../$project/search_form.xsl")){
copy("../project_default_files/search_form.xsl","../$project/search_form.xsl");
}
}
?>