<?php
/**
 * Import Gedcom File
 *
 * Parse a gedcom file into the datastore
 *
 * phpGedView: Genealogy Viewer
 * Copyright (C) 2002 to 2003  John Finlay and Others
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: importgedcom.php,v 1.58 2004/12/08 16:10:00 yalnifj Exp $
 * @package PhpGedView
 * @subpackage Admin
 */

//-- set the building index flag to tell the rest of the program that we are importing and so shouldn't 
//-- perform some of the same checks
$BUILDING_INDEX = true;

require("config.php");

if (!isset($stage)) $stage = 0;
if ((empty($ged))||(!isset($GEDCOMS[$ged]))) $ged = $GEDCOM;

$temp = $THEME_DIR;
$GEDCOM_FILE = $GEDCOMS[$ged]["path"];
$FILE = $ged;
$TITLE = $GEDCOMS[$ged]["title"];
require($GEDCOMS[$ged]["config"]);
if ($LANGUAGE <> $_SESSION["CLANGUAGE"]) $LANGUAGE = $_SESSION["CLANGUAGE"];

if ($PGV_DATABASE=="index") {
	if (!userCanAccept(getUserName())) {
		print_header($pgv_lang["building_indi"]);
		print $pgv_lang["access_denied"];
		print_footer();
		exit;
	}
}
else {
	if (!userGedcomAdmin(getUserName())) {
		print_header($pgv_lang["building_indi"]);
		print $pgv_lang["access_denied"];
		print_footer();
		exit;
	}
}
$temp2 = $THEME_DIR;
$THEME_DIR = $temp;
print_header($pgv_lang["building_indi"]);
$THEME_DIR = $temp2;
$BUILDING_INDEX = true;
print "<span class=\"subheaders\">".$pgv_lang["step4"]." ".$pgv_lang["importing_records"]."</span><br /><br />";
if (isset($GEDCOM_FILE)) {
	if ((!strstr($GEDCOM_FILE, "://"))&&(!file_exists($GEDCOM_FILE))) {
		print "<span class=\"error\"><b>Could not locate gedcom file at $GEDCOM_FILE<br /></b></span>\n";
		unset($GEDCOM_FILE);
	}
}

setup_database($stage);
if ($stage==0) {
	//-- check if this GEDCOM is already being used
	$dataset_exists = check_for_import($FILE);
	if (($dataset_exists)&&(!isset($erase_dataset))) {
	?>
		<br /><br />
		<form action="importgedcom.php" method="post">
			<input type="hidden" name="ged" value="<?php print $ged;?>" />
			<input type="hidden" name="erase_dataset" value="Yes" />
			<span class="error"><?php print $pgv_lang["dataset_exists"]?> <i><?php print $FILE; ?></i>.<br /><br /></span>
			<?php print $pgv_lang["empty_dataset"]?> <br />
			<input type="submit" class="button" value="<?php print $pgv_lang["yes"];?>" /> <input type="button" class="button" value="<?php print $pgv_lang["no"];?>" onclick="window.location='editgedcoms.php';" /><br /><br />
		</form>
	<?php
	print_footer();
	exit;
	}

	if (!isset($erase_dataset)) $erase_dataset="No";
	//-- clear all of the things from the old dataset
	if ($erase_dataset=="Yes") {
		empty_database($FILE);
		//-- erase any of the changes
		foreach($pgv_changes as $cid=>$changes) {
			if ($changes[0]["gedcom"]==$ged) unset($pgv_changes[$cid]);
		}
		write_changes();
	}
	$stage=1;
}

$GEDCOM=$GEDCOM_FILE;
// print "<br />".$pgv_lang["please_be_patient"];
// print "<br /><br />\n".$pgv_lang["reading_file"]." $GEDCOM_FILE<br />\n";
// print $pgv_lang["flushing"]."<br />\n";
flush();

// ------------------------------------------------------ Begin importing data
// -- array of names
if (!isset($indilist)) $indilist = array();
if (!isset($famlist)) $famlist = array();
$sourcelist = array();
$otherlist = array();
if (isset($exectime)){
	$oldtime=time()-$exectime;
	$skip_table=0;
}
else $oldtime=time();

if ($stage==1) {
	if (!isset($record_count)) $record_count=0;
	$i=$record_count;
	$FILE_SIZE = filesize($GEDCOM);
	$fpged = fopen($GEDCOM, "r");
	$BLOCK_SIZE = 1024*4;	//-- 4k bytes per read
	if (!isset($fcontents)) $fcontents = "";
	if (!isset($TOTAL_BYTES)) $TOTAL_BYTES = 0;
	fseek($fpged, $TOTAL_BYTES);
	?>
	
<script type="text/javascript">
<!--
var FILE_SIZE = <?php print $FILE_SIZE; ?>;
function update_progress(bytes) {
	perc = Math.round(100*(bytes / FILE_SIZE));
	if (perc>100) perc = 100;
	progress = document.getElementById("progress_div");
	progress.style.width = perc+"%";
	progress.innerHTML = perc+"%";
}
function complete_progress(time, exectext, go_pedi, go_welc) {
	progress = document.getElementById("progress_header");
	progress.innerHTML = '<?php print "<span class=\"error\"><b>".$pgv_lang["import_complete"]."</b></span><br />";?>'+exectext+' '+time+' '+"<?php print $pgv_lang["sec"]; ?>";
	progress = document.getElementById("link1");
	progress.innerHTML = '<a href="pedigree.php?ged=<?php print preg_replace("/'/", "\'", $ged); ?>">'+go_pedi+'</a>';
	progress = document.getElementById("link2");
	progress.innerHTML = '<a href="index.php?command=gedcom&ged=<?php print preg_replace("/'/", "\'", $ged); ?>">'+go_welc+'</a>';
	progress = document.getElementById("link3");
	progress.innerHTML = '<a href="editgedcoms.php\">'+"<?php print $pgv_lang["manage_gedcoms"]."</a>"; ?>";
}
function wait_progress() {
	progress = document.getElementById("progress_header");
	progress.innerHTML = '<?php print $pgv_lang["please_be_patient"]; ?>';
}
	
//-->
</script>
	
	<?php
	print "<table style=\"width: 800px;\"><tr><td>"; 
	print "<div id=\"progress_header\" class=\"person_box\" style=\"width: 350px; margin: 10px; text-align: center;\">\n";
	print "<b>".$pgv_lang["import_progress"]."</b>";
	print "<div style=\"left: 10px; right: 10px; width: 300px; height: 20px; border: inset #CCCCCC 3px; background-color: #000000;\">\n";
	print "<div id=\"progress_div\" class=\"person_box\" style=\"width: 1%; height: 18px; text-align: center; overflow: hidden;\">1%</div>\n";
	print "</div>\n";
	print "</div>\n";
	print "</td><td style=\"text-align: center;\"><div id=\"link1\">&nbsp;</div>";
	print "<div id=\"link2\">&nbsp;</div><div id=\"link3\">&nbsp;</div></td></tr></table>";
	print "<table cellspacing=\"20px\"><tr><td style=\"vertical-align: top; white-space: nowrap;\">";
	print "<b>".$pgv_lang["reading_file"]." ".$GEDCOM_FILE."</b>";
	$show_table1 = "<table class=\"list_table\"><tr>";
	$show_table1 .= "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["exec_time"]."&nbsp;</td>";
	$show_table1 .= "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["bytes_read"]."&nbsp;</td>\n";
	$show_table1 .= "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["found_record"]."&nbsp;</td>";
//	$show_table1 .= "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["id"]."&nbsp;</td>";
	$show_table1 .= "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["type"]."&nbsp;</td></tr>\n";
	while(!feof($fpged)) {
		$fcontents .= fread($fpged, $BLOCK_SIZE);
		$TOTAL_BYTES += $BLOCK_SIZE;
		$pos1 = 0;
		$listtype= array();
		while($pos1!==false) {
			$pos2 = strpos($fcontents, "\n0", $pos1+1);
			while((!$pos2)&&(!feof($fpged))) {
				$fcontents .= fread($fpged, $BLOCK_SIZE);
				$TOTAL_BYTES += $BLOCK_SIZE;
				$pos2 = strpos($fcontents, "\n0", $pos1+1);
			}
			if ($pos2) $indirec = substr($fcontents, $pos1, $pos2-$pos1);
			else $indirec = substr($fcontents, $pos1);
			$indirec = preg_replace("/\\\/", "/", $indirec);
			if (preg_match("/1 BLOB/", $indirec)==0) import_record(trim($indirec));
			$pos1 = $pos2;
			if (!isset($show_type)){
				$show_type=$type;
				$i_start=1;
				$exectime_start=0;
				$type_BYTES=0;
			}
			$i++;
			if ($show_type!=$type) {
				$newtime = time();
				$exectime = $newtime - $oldtime;
				$show_exectime = $exectime - $exectime_start;
				$show_i=$i-$i_start;
				$type_BYTES=$TOTAL_BYTES-$type_BYTES;
				if (!isset($listtype[$show_type]["type"])) {
					$listtype[$show_type]["exectime"]=$show_exectime;
					$listtype[$show_type]["bytes"]=$type_BYTES;
					$listtype[$show_type]["i"]=$show_i;
					$listtype[$show_type]["type"]=$show_type;
				}
				else {
					$listtype[$show_type]["exectime"]+=$show_exectime;
					$listtype[$show_type]["bytes"]+=$type_BYTES;
					$listtype[$show_type]["i"]+=$show_i;
				}
				$show_type=$type;
				$i_start=$i;
				$exectime_start=$exectime;
				$type_BYTES=$TOTAL_BYTES;
				flush();
			}
			if ($i%100==0) {
				print "<script type=\"text/javascript\">update_progress($TOTAL_BYTES);</script>\n";
				flush();
			}
			$show_gid=$gid;
		}
		foreach($listtype as $type) {
			$show_table1 .= "<tr><td class=\"list_value indent_rtl\" style=\"text-align: right;\">".$type["exectime"]." ".$pgv_lang["sec"]."</td>";
			$show_table1 .= "<td class=\"list_value indent_rtl\" style=\"text-align: right;\">".($type["bytes"]=="0"?"++":$type["bytes"])."</td>\n";
			$show_table1 .= "<td class=\"list_value indent_rtl\" style=\"text-align: right;\">".$type["i"]."</td>";
			$show_table1 .= "<td class=\"list_value\">&nbsp;".$type["type"]."&nbsp;</td></tr>\n";
		}
		$newtime = time();
		$exectime = $newtime - $oldtime;
		$show_table1 .= "<tr><td class=\"list_label indent_rtl\" style=\"text-align: right;\">$exectime ".$pgv_lang["sec"]."</td>";
		$show_table1 .= "<td class=\"list_label indent_rtl\" style=\"text-align: right;\">$TOTAL_BYTES<script type=\"text/javascript\">update_progress($TOTAL_BYTES);</script></td>\n";
		$show_table1 .= "<td class=\"list_label indent_rtl\" style=\"text-align: right;\">".($i-1)."</td>";
		$show_table1 .= "<td class=\"list_label\">&nbsp;</td></tr>\n";
		$fcontents = substr($fcontents, $pos2);
	}
	fclose($fpged);
//	$show_table1 .= "</table></td>\n";
	$show_table1 .= "</table>\n";
	if (isset($skip_table)) print "<br />...";
	else print $show_table1;
	print "</td>\n";
	if ($PGV_DATABASE=="index") $stage=2;
	else $stage=4;
	$record_count=0;
}
if ($stage==2) {
	//-- clear the place cache in mysql mode
	if (isset($placecache)) unset($placecache);
	?>
	
	<script type="text/javascript">
	<!--
	var FILE_SIZE = <?php print count($indilist); ?>;
	function update_progress(bytes) {
		perc = Math.round(100*(bytes / FILE_SIZE));
		progress = document.getElementById("progress_div");
		progress.style.width = perc+"%";
		progress.innerHTML = perc+"%";
	}
	//-->
	</script>
	
	<?php
	$i=0;
	$exectime_start = $exectime;
	print "<td style=\"vertical-align: top; white-space: nowrap;\"><b>".$pgv_lang["updating_is_dead"]."</b><br />\n";
	print "<table class=\"list_table\"><tr>";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["exec_time"]."&nbsp;</td>";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["found_record"]."&nbsp;</td>";
//	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["id"]."&nbsp;</td>";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["type"]."&nbsp;</td></tr>\n";
	foreach($indilist as $gid=>$indi) {
		if ($i>=$record_count) {
			update_isdead($gid, $indi);
			$i++;
		}
		if ($i%100==0) {
			print "<script type=\"text/javascript\">update_progress($i);</script>";
			flush();
		}
	}
	$newtime = time();
	$exectime = $newtime - $oldtime;
	$show_exectime = $exectime - $exectime_start;
	print "<tr><td class=\"list_value indent_rtl\" style=\"text-align: right;\">$show_exectime ".$pgv_lang["sec"]."</td>\n";
	print "<td class=\"list_value indent_rtl\" style=\"text-align: right;\">$i<script type=\"text/javascript\">update_progress($i);</script></td>";
//	print "<td class=\"list_value indent_rtl\">$gid</td>";
	print "<td class=\"list_value\">&nbsp;INDI&nbsp;</td></tr>\n";
	flush();
	print "</table>\n";
//	print "<script type=\"text/javascript\">complete_progress();</script>";
//	flush();
	$stage=3;
	$record_count=0;
}
if ($stage==3) {
	?>
	
	<script type="text/javascript">
	<!--
	var FILE_SIZE = <?php print count($famlist); ?>;
	function update_progress(bytes) {
		perc = Math.round(100*(bytes / FILE_SIZE));
		progress = document.getElementById("progress_div");
		progress.style.width = perc+"%";
		progress.innerHTML = perc+"%";
	}
	//-->
	</script>
	
	<?php
	$i=0;
	$exectime_start = $exectime;
	print "<br /><br /><b>".$pgv_lang["updating_family_names"]."</b><br />\n";
	print "<table class=\"list_table\"><tr>";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["exec_time"]."&nbsp;</td>\n";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["found_record"]."&nbsp;</td>";
//	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["id"]."&nbsp;</td></tr>\n";
	print "<td class=\"list_label\" style=\"vertical-align: top\">&nbsp;".$pgv_lang["type"]."&nbsp;</td></tr>\n";
	foreach($famlist as $gid=>$fam) {
		if ($i>=$record_count) {
			update_family_name($gid, $fam);
			$i++;
		}
		if ($i%100==0) {
			print "<script type=\"text/javascript\">update_progress($i);</script>";
			flush();
		}
	}
	$newtime = time();
	$exectime = $newtime - $oldtime;
	$show_exectime = $exectime - $exectime_start;
	print "<tr><td class=\"list_value indent_rtl\" style=\"text-align: right;\">$show_exectime ".$pgv_lang["sec"]."</td>\n";
	print "<td class=\"list_value indent_rtl\" style=\"text-align: right;\">$i<script type=\"text/javascript\">update_progress($i);</script></td>";
//	print "<td class=\"list_value indent_rtl\">$gid</td></tr>\n";
	print "<td class=\"list_value\">&nbsp;FAM&nbsp;</td></tr>\n";
	print "</table></td>\n";
	print "<script type=\"text/javascript\">wait_progress();</script>";
	flush();
	$stage=4;
	$record_count=0;
}
cleanup_database();
//print "<td style=\"vertical-align: top;\">\n";
//print "<span class=\"error\"><b>".$pgv_lang["import_complete"]."</b></span>\n";
//print "<br /><br />".$pgv_lang["exec_time"]." ".$exectime." ".$pgv_lang["sec"]."\n";
//print "<br /><br /><a href=\"pedigree.php?ged=$ged\">".$pgv_lang["click_here_to_go_to_pedigree_tree"]."</a><br />\n";
//print "</td></tr></table><br />\n";
print "</tr></table>\n";
//print "<script type=\"text/javascript\">complete_progress();</script>";
$exec_text = $pgv_lang["exec_time"];
$go_pedi = $pgv_lang["click_here_to_go_to_pedigree_tree"];
$go_welc = $pgv_lang["welcome_page"];
if ($LANGUAGE=="french" || $LANGUAGE=="italian"){
	print "<script type=\"text/javascript\">complete_progress($exectime, \"$exec_text\", \"$go_pedi\", \"$go_welc\");</script>";
}
else print "<script type=\"text/javascript\">complete_progress($exectime, '$exec_text', '$go_pedi', '$go_welc');</script>";
flush();
print_footer();
?>

