/
face-tag-importer.php
296 lines (216 loc) · 7.83 KB
/
face-tag-importer.php
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
<?php
/**
* Face Tag Import Script
*
* @author: Robert Chapin
* @copyright 2020 by Robert Chapin
* @license GPL
*
* 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 <https://www.gnu.org/licenses/>.
*/
define( 'IMPORT_FILE_PATH', 'tree.ged' );
define( 'CONFIG_FILE_PATH', 'data/config.ini.php' );
if ( ! is_readable( CONFIG_FILE_PATH ) ) {
exit ('Unable to read file ' . CONFIG_FILE_PATH);
}
$config_file = file_get_contents( CONFIG_FILE_PATH );
$config = array();
$matches = array();
preg_match_all("#\n([a-z]{6})=\"([^\"]++)\"#", $config_file, $matches, PREG_SET_ORDER);
foreach( $matches as $match ) {
$config[$match[1]] = $match[2];
}
unset($matches, $match, $config_file);
if ( empty( $config['dbtype'] ) ) {
// Older versions did not have a dbtype entry and this is still valid in webtrees 2.0.
$config['dbtype'] = 'mysql';
} elseif ( 'mysql' !== $config['dbtype'] ) {
exit ('Unable to find MySQL config for webtrees.');
}
$pre = $config['tblpfx'];
if ( ! is_readable( IMPORT_FILE_PATH ) ) {
exit ('Unable to read file ' . IMPORT_FILE_PATH);
}
$gedcom_file = file_get_contents( IMPORT_FILE_PATH );
// BEGIN FTB7 extraction.
//split file on pattern '\r\n0 '
$root_records = preg_split( '#\r\n0 #', $gedcom_file );
$root_count = count( $root_records ) - 1;
echo "Found $root_count root records.<br>";
unset( $gedcom_file );
//Then throw out any strings that don't begin with '\r\n0 @I\d@ INDI'
foreach ( $root_records as $key => $record ) {
if ( 1 !== preg_match( '#^@I\\d+@ INDI\\r\\n#', $record ) ) {
unset( $root_records[$key] );
}
}
//For each INDI record:
/* Match pattern...
2 _POSITION 278 62 443 281
2 _PHOTO_RIN MH:P39
but also
2 _POSITION 352 306 552 573
2 _ALBUM @A9@
2 _PHOTO_RIN MH:P2390
*/
$face_tags = array();
$indi_count = count( $root_records );
$tag_count = 0;
echo "Found $indi_count total individuals in the tree.<br>";
foreach ( $root_records as $record ) {
if ( false === strpos( $record, '_POSITION' ) ) {
continue;
}
$id_end = strpos( $record, '@', 2 );
if ( false === $id_end ) {
continue;
}
$id = substr( $record, 1, $id_end - 1 );
//split record on pattern '\r\n1 '
$obje_records = preg_split( '#\r\n1 #', $record );
//Then throw out any strings that don't begin with '\r\n1 OBJE'
foreach ( $obje_records as $key => $obje ) {
if ( 'OBJE' !== substr( $obje, 0, 4 ) || false === strpos( $obje, "\r\n2 _POSITION" ) || false === strpos( $obje, "\r\n2 _PHOTO_RIN" ) ) {
unset( $obje_records[$key] );
}
}
foreach ( $obje_records as $obje ) {
$matches = array();
if ( 1 === preg_match( '#\\r\\n2 _POSITION (\\d+) (\\d+) (\\d+) (\\d+)\\r\\n#', $obje, $matches ) ) {
/* Expected $matches like...
array(5) {
[0]=>
string(31) "
2 _POSITION 352 306 552 573
"
[1]=>
string(3) "352"
[2]=>
string(3) "306"
[3]=>
string(3) "552"
[4]=>
string(3) "573"
}
*/
$coords = array( $matches[1], $matches[2], $matches[3], $matches[4] );
} else {
continue;
}
$matches = array();
if ( 1 === preg_match( '#\\r\\n2 _PHOTO_RIN (MH:P\\d+)\\r\\n#', $obje, $matches ) ) {
/* Expected $matches like...
array(2) {
[0]=>
string(25) "
2 _PHOTO_RIN MH:P2390
"
[1]=>
string(8) "MH:P2390"
}
*/
// Save the INDI reference and the _POSITION data into an array indexed by the _PHOTO_RIN.
$face_tags[$matches[1]][$id] = $coords;
$tag_count++;
}
}
}
unset( $root_records, $record, $obje_records, $obje );
$photo_count = count( $face_tags );
echo "Found $tag_count face tags in $photo_count photos.<br>";
// Extract all _PHOTO_RIN values from webtrees gedcom values.
// This will allow each FTB face tag to be associated with a webtrees media
// object using a simple array.
// mysql> SELECT `m_id` FROM `wt_media` WHERE `m_gedcom` LIKE '%\n1 \\_PHOTO\\_RIN MH:P44\n%';
// $sql = "SELECT `m_id` FROM `wt_media` WHERE `m_gedcom` LIKE '%\\n1 \\\\_PHOTO\\\\_RIN MH:P44\\n%'";
$id_to_rin = array();
$db = new mysqli( $config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname'], $config['dbport'] );
if ( $db->connect_error ) {
exit ( "Database error: " . $db->connect_error );
}
$media = $db->query( "SELECT `m_id`, `m_gedcom` FROM `{$pre}media` " );
if ( false === $media ) {
exit ( "Database error: " . $db->error );
}
while( $row = $media->fetch_row() ) {
$id = $row[0];
$gedcom = $row[1];
$matches = array();
if ( 1 === preg_match( '#\\n1 _PHOTO_RIN (MH:P\\d+)\\n#', $gedcom, $matches ) ) {
/* Expected $matches like...
array(2) {
[0]=>
string(25) "
1 _PHOTO_RIN MH:P2390
"
[1]=>
string(8) "MH:P2390"
}
*/
$id_to_rin[$id] = $matches[1];
}
}
$media->free();
// END FTB7 extraction.
// BEGIN WT import.
// INSERT the _POSITION record(s) for this photo.
/* mysql> SELECT * FROM wt_media_faces;
+------+------------------------------------------------------------------------------------------------------+--------+--------------------+
| f_id | f_coordinates | f_m_id | f_m_filename |
+------+------------------------------------------------------------------------------------------------------+--------+--------------------+
| 1 | [{"pid":"I1","coords":["308","178","473","395"]},{"pid":"I3164","coords":["191","213","354","428"]}] | X74641 | P2015_699_1048.jpg |
+------+------------------------------------------------------------------------------------------------------+--------+--------------------+
*/
// For each _PHOTO_RIN:
$db_queue = array();
$empty_counter = 0;
foreach ( $face_tags as $rin => $links ) {
// Find the database record for this photo.
$media_id = array_search( $rin, $id_to_rin );
if ( false === $media_id ) {
echo 'The $media_id was not found for RIN ' . $rin . '.<br>';
$empty_counter++;
continue;
}
// Generate the JSON string for this photo.
$plugin_data = array();
foreach ( $links as $id => $coords ) {
$plugin_data[] = array( 'pid' => $id, 'coords' => $coords );
}
$db_queue[$media_id] = json_encode( $plugin_data );
}
unset( $face_tags, $id_to_rin );
echo '$db_queue array contains ' . count( $db_queue ) . ' entries.<br>';
// For each photo, convert the JSON string to a SQL string.
$values = array();
foreach ( $db_queue as $media_id => $plugin_data ) {
$values[] = '("' . $db->escape_string( $media_id ) . '", "' . $db->escape_string( $plugin_data ) . '", "")';
}
unset( $db_queue );
// Now build the INSERT statement and use it to add all face tag records to the database.
$values = implode( ',', $values );
echo 'Query size is ' . strlen( $values ) . '.<br>';
$result = $db->query( "TRUNCATE `{$pre}media_faces`" );
if ( false === $result ) {
exit ( "Database error: " . $db->error );
}
$result = $db->query( "INSERT INTO {$pre}media_faces (f_m_id, f_coordinates, f_m_filename) VALUES $values" );
if ( false === $result ) {
exit ( "Database error: " . $db->error );
}
unset( $values );
// END WT import.
echo 'Done.<br><a href="../">Return Home</a>';
return;
?>