summaryrefslogtreecommitdiff
path: root/app/Functions/FunctionsExport.php
blob: 3514c0eb00cf60abfaf0d548e47f56b34af65a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2015 webtrees development team
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees\Functions;

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;

/**
 * Class FunctionsExport - common functions
 */
class FunctionsExport {
	/**
	 * Tidy up a gedcom record on export, for compatibility/portability.
	 *
	 * @param string $rec
	 *
	 * @return string
	 */
	public static function reformatRecord($rec) {
		global $WT_TREE;

		$newrec = '';
		foreach (preg_split('/[\r\n]+/', $rec, -1, PREG_SPLIT_NO_EMPTY) as $line) {
			// Split long lines
			// The total length of a GEDCOM line, including level number, cross-reference number,
			// tag, value, delimiters, and terminator, must not exceed 255 (wide) characters.
			if (mb_strlen($line) > WT_GEDCOM_LINE_LENGTH) {
				list($level, $tag) = explode(' ', $line, 3);
				if ($tag != 'CONT' && $tag != 'CONC') {
					$level++;
				}
				do {
					// Split after $pos chars
					$pos = WT_GEDCOM_LINE_LENGTH;
					if ($WT_TREE->getPreference('WORD_WRAPPED_NOTES')) {
						// Split on a space, and remove it (for compatibility with some desktop apps)
						while ($pos && mb_substr($line, $pos - 1, 1) != ' ') {
							--$pos;
						}
						if ($pos == strpos($line, ' ', 3) + 1) {
							// No spaces in the data! Can’t split it :-(
							break;
						} else {
							$newrec .= mb_substr($line, 0, $pos - 1) . WT_EOL;
							$line = $level . ' CONC ' . mb_substr($line, $pos);
						}
					} else {
						// Split on a non-space (standard gedcom behaviour)
						while ($pos && mb_substr($line, $pos - 1, 1) == ' ') {
							--$pos;
						}
						if ($pos == strpos($line, ' ', 3)) {
							// No non-spaces in the data! Can’t split it :-(
							break;
						}
						$newrec .= mb_substr($line, 0, $pos) . WT_EOL;
						$line = $level . ' CONC ' . mb_substr($line, $pos);
					}
				} while (mb_strlen($line) > WT_GEDCOM_LINE_LENGTH);
			}
			$newrec .= $line . WT_EOL;
		}

		return $newrec;
	}

	/**
	 * Create a header for a (newly-created or already-imported) gedcom file.
	 *
	 * @param Tree $tree
	 *
	 * @return string
	 */
	public static function gedcomHeader(Tree $tree) {
		// Default values for a new header
		$HEAD = "0 HEAD";
		$SOUR = "\n1 SOUR " . WT_WEBTREES . "\n2 NAME " . WT_WEBTREES . "\n2 VERS " . WT_VERSION;
		$DEST = "\n1 DEST DISKETTE";
		$DATE = "\n1 DATE " . strtoupper(date("d M Y")) . "\n2 TIME " . date("H:i:s");
		$GEDC = "\n1 GEDC\n2 VERS 5.5.1\n2 FORM Lineage-Linked";
		$CHAR = "\n1 CHAR UTF-8";
		$FILE = "\n1 FILE " . $tree->getName();
		$LANG = "";
		$PLAC = "\n1 PLAC\n2 FORM City, County, State/Province, Country";
		$COPR = "";
		$SUBN = "";
		$SUBM = "\n1 SUBM @SUBM@\n0 @SUBM@ SUBM\n1 NAME " . Auth::user()->getUserName(); // The SUBM record is mandatory

		// Preserve some values from the original header
		$record = GedcomRecord::getInstance('HEAD', $tree);
		if ($fact = $record->getFirstFact('PLAC')) {
			$PLAC = "\n1 PLAC\n2 FORM " . $fact->getAttribute('FORM');
		}
		if ($fact = $record->getFirstFact('LANG')) {
			$LANG = $fact->getValue();
		}
		if ($fact = $record->getFirstFact('SUBN')) {
			$SUBN = $fact->getValue();
		}
		if ($fact = $record->getFirstFact('COPR')) {
			$COPR = $fact->getValue();
		}
		// Link to actual SUBM/SUBN records, if they exist
		$subn =
			Database::prepare("SELECT o_id FROM `##other` WHERE o_type=? AND o_file=?")
				->execute(array('SUBN', $tree->getTreeId()))
				->fetchOne();
		if ($subn) {
			$SUBN = "\n1 SUBN @{$subn}@";
		}
		$subm =
			Database::prepare("SELECT o_id FROM `##other` WHERE o_type=? AND o_file=?")
				->execute(array('SUBM', $tree->getTreeId()))
				->fetchOne();
		if ($subm) {
			$SUBM = "\n1 SUBM @{$subm}@";
		}

		return $HEAD . $SOUR . $DEST . $DATE . $GEDC . $CHAR . $FILE . $COPR . $LANG . $PLAC . $SUBN . $SUBM . "\n";
	}

	/**
	 * Prepend the GEDCOM_MEDIA_PATH to media filenames.
	 *
	 * @param string $rec
	 * @param string $path
	 *
	 * @return string
	 */
	public static function convertMediaPath($rec, $path) {
		if ($path && preg_match('/\n1 FILE (.+)/', $rec, $match)) {
			$old_file_name = $match[1];
			// Don’t modify external links
			if (!preg_match('~^(https?|ftp):~', $old_file_name)) {
				// Adding a windows path?  Convert the slashes.
				if (strpos($path, '\\') !== false) {
					$new_file_name = preg_replace('~/+~', '\\', $old_file_name);
				} else {
					$new_file_name = $old_file_name;
				}
				// Path not present - add it.
				if (strpos($new_file_name, $path) === false) {
					$new_file_name = $path . $new_file_name;
				}
				$rec = str_replace("\n1 FILE " . $old_file_name, "\n1 FILE " . $new_file_name, $rec);
			}
		}

		return $rec;
	}

	/**
	 * Export the database in GEDCOM format
	 *
	 * @param Tree $tree Which tree to export
	 * @param resource $gedout Handle to a writable stream
	 * @param string[] $exportOptions Export options are as follows:
	 *                                'privatize':    which Privacy rules apply?  (none, visitor, user, manager)
	 *                                'toANSI':       should the output be produced in ISO-8859-1 instead of UTF-8?  (yes, no)
	 *                                'path':         what constant should prefix all media file paths?  (eg: media/  or c:\my pictures\my family
	 *                                'slashes':      what folder separators apply to media file paths?  (forward, backward)
	 */
	public static function exportGedcom(Tree $tree, $gedout, $exportOptions) {
		switch ($exportOptions['privatize']) {
			case 'gedadmin':
				$access_level = Auth::PRIV_NONE;
				break;
			case 'user':
				$access_level = Auth::PRIV_USER;
				break;
			case 'visitor':
				$access_level = Auth::PRIV_PRIVATE;
				break;
			case 'none':
				$access_level = Auth::PRIV_HIDE;
				break;
		}

		$head = self::gedcomHeader($tree);
		if ($exportOptions['toANSI'] == 'yes') {
			$head = str_replace('UTF-8', 'ANSI', $head);
			$head = utf8_decode($head);
		}
		$head = self::reformatRecord($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       = Database::prepare(
			"SELECT m_id AS xref, m_gedcom AS gedcom" .
			" FROM `##media` WHERE m_file = :tree_id ORDER BY m_id"
		)->execute(array(
			'tree_id' => $tree->getTreeId(),
		))->fetchAll();

		foreach ($rows as $row) {
			$rec = Media::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level);
			$rec = self::convertMediaPath($rec, $exportOptions['path']);
			if ($exportOptions['toANSI'] === 'yes') {
				$rec = utf8_decode($rec);
			}
			$tmp_gedcom .= self::reformatRecord($rec);
		}

		$rows = Database::prepare(
			"SELECT s_id AS xref, s_file AS gedcom_id, s_gedcom AS gedcom" .
			" FROM `##sources` WHERE s_file = :tree_id ORDER BY s_id"
		)->execute(array(
			'tree_id' => $tree->getTreeId(),
		))->fetchAll();

		foreach ($rows as $row) {
			$rec = Source::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level);
			if ($exportOptions['toANSI'] === 'yes') {
				$rec = utf8_decode($rec);
			}
			$tmp_gedcom .= self::reformatRecord($rec);
		}

		$rows = Database::prepare(
			"SELECT o_type AS type, o_id AS xref, o_gedcom AS gedcom" .
			" FROM `##other` WHERE o_file = :tree_id AND o_type NOT IN ('HEAD', 'TRLR') ORDER BY o_id"
		)->execute(array(
			'tree_id' => $tree->getTreeId(),
		))->fetchAll();

		foreach ($rows as $row) {
			switch ($row->type) {
				case 'NOTE':
					$record = Note::getInstance($row->xref, $tree, $row->gedcom);
					break;
				case 'REPO':
					$record = Repository::getInstance($row->xref, $tree, $row->gedcom);
					break;
				default:
					$record = GedcomRecord::getInstance($row->xref, $tree, $row->gedcom);
					break;
			}

			$rec = $record->privatizeGedcom($access_level);
			if ($exportOptions['toANSI'] === 'yes') {
				$rec = utf8_decode($rec);
			}
			$tmp_gedcom .= self::reformatRecord($rec);
		}

		$rows = Database::prepare(
			"SELECT i_id AS xref, i_gedcom AS gedcom" .
			" FROM `##individuals` WHERE i_file = :tree_id ORDER BY i_id"
		)->execute(array(
			'tree_id' => $tree->getTreeId(),
		))->fetchAll();

		foreach ($rows as $row) {
			$rec = Individual::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level);
			if ($exportOptions['toANSI'] === 'yes') {
				$rec = utf8_decode($rec);
			}
			$buffer .= self::reformatRecord($rec);
			if (strlen($buffer) > 65536) {
				fwrite($gedout, $buffer);
				$buffer = '';
			}
		}

		$rows = Database::prepare(
			"SELECT f_id AS xref, f_gedcom AS gedcom" .
			" FROM `##families` WHERE f_file = :tree_id ORDER BY f_id"
		)->execute(array(
			'tree_id' => $tree->getTreeId(),
		))->fetchAll();

		foreach ($rows as $row) {
			$rec = Family::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level);
			if ($exportOptions['toANSI'] === 'yes') {
				$rec = utf8_decode($rec);
			}
			$buffer .= self::reformatRecord($rec);
			if (strlen($buffer) > 65536) {
				fwrite($gedout, $buffer);
				$buffer = '';
			}
		}

		fwrite($gedout, $buffer);
		fwrite($gedout, $tmp_gedcom);
		fwrite($gedout, '0 TRLR' . WT_EOL);
	}
}