/** * Accpet changed gedcom record into database * * This function gets an updated record from the gedcom file and replaces it in the database * @author John Finlay * @param string $cid The change id of the record to accept */ function accept_changes($cid) { global $pgv_changes, $GEDCOM, $TBLPREFIX, $FILE, $GEDCOMS; global $INDEX_DIRECTORY, $SYNC_GEDCOM_FILE, $fcontents, $manual_save; if (isset($pgv_changes[$cid])) { $changes = $pgv_changes[$cid]; $change = $changes[count($changes) - 1]; if ($GEDCOM != $change["gedcom"]) { $GEDCOM = $change["gedcom"]; } $FILE = $GEDCOM; $gid = $change["gid"]; $gedrec = $change["undo"]; if (empty($gedrec)) { $gedrec = find_gedcom_record($gid); } update_record($gedrec, $change["type"] == "delete"); //-- write the changes back to the gedcom file if ($SYNC_GEDCOM_FILE) { // TODO: We merge CONC lines on import, so need to add them back on export if (!isset($manual_save) || $manual_save == false) { //-- only allow one thread to accept changes at a time // $mutex = new Mutex("accept_changes"); // $mutex->Wait(); } if (empty($fcontents)) { read_gedcom_file(); } if ($change["type"] == "delete") { $pos1 = find_newline_string($fcontents, "0 @{$gid}@"); if ($pos1 !== false) { $pos2 = find_newline_string($fcontents, "0", $pos1 + 5); if ($pos2 === false) { $fcontents = substr($fcontents, 0, $pos1) . '0 TRLR' . PGV_EOL; AddToLog("Corruption found in GEDCOM {$GEDCOM} Attempted to correct"); } else { $fcontents = substr($fcontents, 0, $pos1) . substr($fcontents, $pos2); } } else { AddToLog("Corruption found in GEDCOM {$GEDCOM} Attempted to correct. Deleted gedcom record {$gid} was not found in the gedcom file."); } } elseif ($change["type"] == "append") { $pos1 = find_newline_string($fcontents, "0 TRLR"); $fcontents = substr($fcontents, 0, $pos1) . reformat_record_export($gedrec) . '0 TRLR' . PGV_EOL; } elseif ($change["type"] == "replace") { $pos1 = find_newline_string($fcontents, "0 @{$gid}@"); if ($pos1 !== false) { $pos2 = find_newline_string($fcontents, "0", $pos1 + 5); if ($pos2 === false) { $fcontents = substr($fcontents, 0, $pos1) . '0 TRLR' . PGV_EOL; AddToLog("Corruption found in GEDCOM {$GEDCOM} Attempted to correct"); } else { $fcontents = substr($fcontents, 0, $pos1) . reformat_record_export($gedrec) . substr($fcontents, $pos2); } } else { //-- attempted to replace a record that doesn't exist AddToLog("Corruption found in GEDCOM {$GEDCOM} Attempted to correct. Replaced gedcom record {$gid} was not found in the gedcom file."); $pos1 = find_newline_string($fcontents, "0 TRLR"); $fcontents = substr($fcontents, 0, $pos1) . reformat_record_export($gedrec) . '0 TRLR' . PGV_EOL; AddToLog("Gedcom record {$gid} was appended back to the GEDCOM file."); } } if (!isset($manual_save) || $manual_save == false) { write_file(); // $mutex->Release(); } } if ($change["type"] != "delete") { //-- synchronize the gedcom record with any user account $username = get_user_from_gedcom_xref($GEDCOM, $gid); if ($username && get_user_setting($username, 'sync_gedcom') == 'Y') { $firstname = get_gedcom_value("GIVN", 2, $gedrec); $lastname = get_gedcom_value("SURN", 2, $gedrec); if (empty($lastname)) { $fullname = get_gedcom_value("NAME", 1, $gedrec, "", false); $ct = preg_match("~(.*)/(.*)/~", $fullname, $match); if ($ct > 0) { $firstname = $match[1]; $lastname = $match[2]; } else { $firstname = $fullname; } } //-- SEE [ 1753047 ] Email/sync with account $email = get_gedcom_value("EMAIL", 1, $gedrec); if (empty($email)) { $email = get_gedcom_value("_EMAIL", 1, $gedrec); } if (!empty($email)) { set_user_setting($username, 'email', $email); } set_user_setting($username, 'firstname', $firstname); set_user_setting($username, 'lastname', $lastname); } } unset($pgv_changes[$cid]); if (!isset($manual_save) || $manual_save == false) { write_changes(); } $logline = AddToLog("Accepted change {$cid} " . $change["type"] . " into database"); check_in($logline, $GEDCOM, dirname($GEDCOMS[$GEDCOM]['path'])); if (isset($change["linkpid"])) { accept_changes($change["linkpid"] . "_" . $GEDCOM); } return true; } return false; }
public function exportGedcom($gedcom_file) { // TODO: these functions need to be moved to the GedcomRecord(?) class require_once WT_ROOT . 'includes/functions/functions_export.php'; // To avoid partial trees on timeout/diskspace/etc, write to a temporary file first $tmp_file = $gedcom_file . '.tmp'; $file_pointer = @fopen($tmp_file, 'w'); if ($file_pointer === false) { return false; } $buffer = reformat_record_export(gedcom_header($this->tree_name)); $stmt = WT_DB::prepare("SELECT i_gedcom AS gedcom FROM `##individuals` WHERE i_file = ?" . " UNION ALL " . "SELECT f_gedcom AS gedcom FROM `##families` WHERE f_file = ?" . " UNION ALL " . "SELECT s_gedcom AS gedcom FROM `##sources` WHERE s_file = ?" . " UNION ALL " . "SELECT o_gedcom AS gedcom FROM `##other` WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" . " UNION ALL " . "SELECT m_gedcom AS gedcom FROM `##media` WHERE m_file = ?")->execute(array($this->tree_id, $this->tree_id, $this->tree_id, $this->tree_id, $this->tree_id)); while ($row = $stmt->fetch()) { $buffer .= reformat_record_export($row->gedcom); if (strlen($buffer) > 65535) { fwrite($file_pointer, $buffer); $buffer = ''; } } fwrite($file_pointer, $buffer . '0 TRLR' . WT_EOL); fclose($file_pointer); return @rename($tmp_file, $gedcom_file); }
function export_gedcom($gedcom, $gedout, $exportOptions) { global $GEDCOM; // Temporarily switch to the specified GEDCOM $oldGEDCOM = $GEDCOM; $GEDCOM = $gedcom; $ged_id = get_id_from_gedcom($gedcom); switch ($exportOptions['privatize']) { case 'gedadmin': $access_level = WT_PRIV_NONE; break; case 'user': $access_level = WT_PRIV_USER; break; case 'visitor': $access_level = WT_PRIV_PUBLIC; break; case 'none': $access_level = WT_PRIV_HIDE; break; } $head = gedcom_header($gedcom); if ($exportOptions['toANSI'] == 'yes') { $head = str_replace('UTF-8', 'ANSI', $head); $head = utf8_decode($head); } $head = reformat_record_export($head); fwrite($gedout, $head); // Buffer the output. Lots of small fwrite() calls can be very slow when writing large gedcoms. $buffer = ''; // Generate the OBJE/SOUR/REPO/NOTE records first, as their privacy calcualations involve // database queries, and we wish to avoid large gaps between queries due to MySQL connection timeouts. $tmp_gedcom = ''; $rows = WT_DB::prepare("SELECT 'OBJE' AS type, m_id AS xref, m_file AS gedcom_id, m_gedcom AS gedcom" . " FROM `##media` WHERE m_file=? ORDER BY m_id")->execute(array($ged_id))->fetchAll(); foreach ($rows as $row) { $rec = WT_Media::getInstance($row->xref, $row->gedcom_id, $row->gedcom)->privatizeGedcom($access_level); $rec = convert_media_path($rec, $exportOptions['path']); if ($exportOptions['toANSI'] == 'yes') { $rec = utf8_decode($rec); } $tmp_gedcom .= reformat_record_export($rec); } $rows = WT_DB::prepare("SELECT s_id AS xref, s_file AS gedcom_id, s_gedcom AS gedcom" . " FROM `##sources` WHERE s_file=? ORDER BY s_id")->execute(array($ged_id))->fetchAll(); foreach ($rows as $row) { $rec = WT_Source::getInstance($row->xref, $row->gedcom_id, $row->gedcom)->privatizeGedcom($access_level); if ($exportOptions['toANSI'] == 'yes') { $rec = utf8_decode($rec); } $tmp_gedcom .= reformat_record_export($rec); } $rows = WT_DB::prepare("SELECT o_type AS type, o_id AS xref, o_file AS gedcom_id, o_gedcom AS gedcom" . " FROM `##other` WHERE o_file=? AND o_type!='HEAD' AND o_type!='TRLR' ORDER BY o_id")->execute(array($ged_id))->fetchAll(); foreach ($rows as $row) { switch ($row->type) { case 'NOTE': $record = WT_Note::getInstance($row->xref, $row->gedcom_id, $row->gedcom); break; case 'REPO': $record = WT_Repository::getInstance($row->xref, $row->gedcom_id, $row->gedcom); break; default: $record = WT_GedcomRecord::getInstance($row->xref, $row->gedcom_id, $row->gedcom); break; } $rec = $record->privatizeGedcom($access_level); if ($exportOptions['toANSI'] == 'yes') { $rec = utf8_decode($rec); } $tmp_gedcom .= reformat_record_export($rec); } $rows = WT_DB::prepare("SELECT i_id AS xref, i_file AS gedcom_id, i_gedcom AS gedcom" . " FROM `##individuals` WHERE i_file=? ORDER BY i_id")->execute(array($ged_id))->fetchAll(); foreach ($rows as $row) { $rec = WT_Individual::getInstance($row->xref, $row->gedcom_id, $row->gedcom)->privatizeGedcom($access_level); if ($exportOptions['toANSI'] == 'yes') { $rec = utf8_decode($rec); } $buffer .= reformat_record_export($rec); if (strlen($buffer) > 65536) { fwrite($gedout, $buffer); $buffer = ''; } } $rows = WT_DB::prepare("SELECT f_id AS xref, f_file AS gedcom_id, f_gedcom AS gedcom" . " FROM `##families` WHERE f_file=? ORDER BY f_id")->execute(array($ged_id))->fetchAll(); foreach ($rows as $row) { $rec = WT_Family::getInstance($row->xref, $row->gedcom_id, $row->gedcom)->privatizeGedcom($access_level); if ($exportOptions['toANSI'] == 'yes') { $rec = utf8_decode($rec); } $buffer .= reformat_record_export($rec); if (strlen($buffer) > 65536) { fwrite($gedout, $buffer); $buffer = ''; } } fwrite($gedout, $buffer); fwrite($gedout, $tmp_gedcom); fwrite($gedout, '0 TRLR' . WT_EOL); $GEDCOM = $oldGEDCOM; }