public function __construct() { global $SCRIPT_NAME, $MEDIA_DIRECTORY, $WT_SESSION; // Our cart is an array of items in the session if (!is_array($WT_SESSION->cart)) { $WT_SESSION->cart = array(); } if (!array_key_exists(WT_GED_ID, $WT_SESSION->cart)) { $WT_SESSION->cart[WT_GED_ID] = array(); } $this->action = WT_Filter::get('action'); $this->id = WT_Filter::get('id'); $convert = WT_Filter::get('convert', 'yes|no', 'no'); $this->Zip = WT_Filter::get('Zip'); $this->IncludeMedia = WT_Filter::get('IncludeMedia'); $this->conv_path = WT_Filter::get('conv_path'); $this->privatize_export = WT_Filter::get('privatize_export', 'none|visitor|user|gedadmin', 'visitor'); $this->level1 = WT_Filter::getInteger('level1'); $this->level2 = WT_Filter::getInteger('level2'); $this->level3 = WT_Filter::getInteger('level3'); $others = WT_Filter::get('others'); $this->type = WT_Filter::get('type'); if (($this->privatize_export == 'none' || $this->privatize_export == 'none') && !WT_USER_GEDCOM_ADMIN) { $this->privatize_export = 'visitor'; } if ($this->privatize_export == 'user' && !WT_USER_CAN_ACCESS) { $this->privatize_export = 'visitor'; } if ($this->action == 'add') { if (empty($this->type) && !empty($this->id)) { $this->type = ""; $obj = WT_GedcomRecord::getInstance($this->id); if (is_null($obj)) { $this->id = ""; $this->action = ""; } else { $this->type = strtolower($obj::RECORD_TYPE); } } else { if (empty($this->id)) { $this->action = ""; } } if (!empty($this->id) && $this->type != 'fam' && $this->type != 'indi' && $this->type != 'sour') { $this->action = 'add1'; } } if ($this->action == 'add1') { $obj = WT_GedcomRecord::getInstance($this->id); $this->addClipping($obj); if ($this->type == 'sour') { if ($others == 'linked') { foreach ($obj->linkedIndividuals('SOUR') as $indi) { $this->addClipping($indi); } foreach ($obj->linkedFamilies('SOUR') as $fam) { $this->addClipping($fam); } } } if ($this->type == 'fam') { if ($others == 'parents') { $this->addClipping($obj->getHusband()); $this->addClipping($obj->getWife()); } elseif ($others == "members") { $this->addFamilyMembers(WT_Family::getInstance($this->id)); } elseif ($others == "descendants") { $this->addFamilyDescendancy(WT_Family::getInstance($this->id)); } } elseif ($this->type == 'indi') { if ($others == 'parents') { foreach (WT_Individual::getInstance($this->id)->getChildFamilies() as $family) { $this->addFamilyMembers($family); } } elseif ($others == 'ancestors') { $this->addAncestorsToCart(WT_Individual::getInstance($this->id), $this->level1); } elseif ($others == 'ancestorsfamilies') { $this->addAncestorsToCartFamilies(WT_Individual::getInstance($this->id), $this->level2); } elseif ($others == 'members') { foreach (WT_Individual::getInstance($this->id)->getSpouseFamilies() as $family) { $this->addFamilyMembers($family); } } elseif ($others == 'descendants') { foreach (WT_Individual::getInstance($this->id)->getSpouseFamilies() as $family) { $this->addClipping($family); $this->addFamilyDescendancy($family, $this->level3); } } uksort($WT_SESSION->cart[WT_GED_ID], array('WT_Controller_Clippings', 'compareClippings')); } } elseif ($this->action == 'remove') { unset($WT_SESSION->cart[WT_GED_ID][$this->id]); } elseif ($this->action == 'empty') { $WT_SESSION->cart[WT_GED_ID] = array(); } elseif ($this->action == 'download') { $media = array(); $mediacount = 0; $filetext = gedcom_header(WT_GEDCOM); // Include SUBM/SUBN records, if they exist $subn = WT_DB::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")->execute(array('SUBN', WT_GED_ID))->fetchOne(); if ($subn) { $filetext .= $subn . "\n"; } $subm = WT_DB::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")->execute(array('SUBM', WT_GED_ID))->fetchOne(); if ($subm) { $filetext .= $subm . "\n"; } if ($convert == "yes") { $filetext = str_replace("UTF-8", "ANSI", $filetext); $filetext = utf8_decode($filetext); } switch ($this->privatize_export) { 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; } foreach (array_keys($WT_SESSION->cart[WT_GED_ID]) as $xref) { $object = WT_GedcomRecord::getInstance($xref); if ($object) { // The object may have been deleted since we added it to the cart.... $record = $object->privatizeGedcom($access_level); // Remove links to objects that aren't in the cart preg_match_all('/\\n1 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); foreach ($matches as $match) { if (!array_key_exists($match[1], $WT_SESSION->cart[WT_GED_ID])) { $record = str_replace($match[0], '', $record); } } preg_match_all('/\\n2 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); foreach ($matches as $match) { if (!array_key_exists($match[1], $WT_SESSION->cart[WT_GED_ID])) { $record = str_replace($match[0], '', $record); } } preg_match_all('/\\n3 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); foreach ($matches as $match) { if (!array_key_exists($match[1], $WT_SESSION->cart[WT_GED_ID])) { $record = str_replace($match[0], '', $record); } } $record = convert_media_path($record, $this->conv_path); $savedRecord = $record; // Save this for the "does this file exist" check if ($convert == 'yes') { $record = utf8_decode($record); } switch ($object::RECORD_TYPE) { case 'INDI': $filetext .= $record . "\n"; $filetext .= "1 SOUR @WEBTREES@\n"; $filetext .= "2 PAGE " . WT_SERVER_NAME . WT_SCRIPT_PATH . $object->getRawUrl() . "\n"; break; case 'FAM': $filetext .= $record . "\n"; $filetext .= "1 SOUR @WEBTREES@\n"; $filetext .= "2 PAGE " . WT_SERVER_NAME . WT_SCRIPT_PATH . $object->getRawUrl() . "\n"; break; case 'SOUR': $filetext .= $record . "\n"; $filetext .= "1 NOTE " . WT_SERVER_NAME . WT_SCRIPT_PATH . $object->getRawUrl() . "\n"; break; default: $ft = preg_match_all("/\n\\d FILE (.+)/", $savedRecord, $match, PREG_SET_ORDER); for ($k = 0; $k < $ft; $k++) { // Skip external files and non-existant files if (file_exists(WT_DATA_DIR . $MEDIA_DIRECTORY . $match[$k][1])) { $media[$mediacount] = array(PCLZIP_ATT_FILE_NAME => WT_DATA_DIR . $MEDIA_DIRECTORY . $match[$k][1], PCLZIP_ATT_FILE_NEW_FULL_NAME => $match[$k][1]); $mediacount++; } } $filetext .= trim($record) . "\n"; break; } } } if ($this->IncludeMedia == "yes") { $this->media_list = $media; } $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_SERVER_NAME . WT_SCRIPT_PATH . "\n"; if ($user_id = get_gedcom_setting(WT_GED_ID, 'CONTACT_EMAIL')) { $user = User::find($user_id); $filetext .= "1 AUTH " . $user->getRealName() . "\n"; } $filetext .= "0 TRLR\n"; //-- make sure the preferred line endings are used $filetext = preg_replace("/[\r\n]+/", WT_EOL, $filetext); $this->download_data = $filetext; $this->downloadClipping(); } }
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; }