-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_update.php
More file actions
452 lines (415 loc) · 15.6 KB
/
batch_update.php
File metadata and controls
452 lines (415 loc) · 15.6 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* Batch Update module for phpGedView
*
* phpGedView: Genealogy Viewer
* Copyright (C) 2008 PGV Development Team. All rights reserved.
*
* 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
*
* @package PhpGedView
* @subpackage Module
* @author Greg Roach
* $Id: batch_update.php 6665 2010-01-03 18:29:30Z volschin $
*/
if (!defined('PGV_PHPGEDVIEW')) {
header('HTTP/1.0 403 Forbidden');
exit;
}
if (!PGV_USER_GEDCOM_ADMIN) {
header('Location: module.php?mod=batch_update');
exit;
}
loadLangFile('batch_update:lang');
require PGV_ROOT.'includes/functions/functions_edit.php';
class batch_update {
var $plugin =null; // Form parameter: chosen plugin
var $xref =null; // Form parameter: record to update
var $action =null; // Form parameter: how to update record
var $data =null; // Form parameter: additional details for $action
var $plugins =null; // Array of available plugins
var $PLUGIN =null; // An instance of a plugin object
var $all_xrefs=null; // An array of all xrefs that might need to be updated
var $prev_xref=null; // The previous xref to process
var $curr_xref=null; // The xref to process
var $next_xref=null; // The next xref to process
var $record =null; // A GedcomRecord object corresponding to $curr_xref
// Main entry point - called by the PGV framework in response to module.php?mod=batch_update
function main() {
global $pgv_lang;
// HTML common to all pages
$html=
mod_print_header($pgv_lang['batch_update']).
self::getJavascript().
'<form id="batch_update_form" action="module.php" method="get">'.
'<input type="hidden" name="mod" value="batch_update">'.
'<input type="hidden" name="xref" value="'.$this->xref.'">'.
'<input type="hidden" name="action" value="">'. // will be set by javascript for next update
'<input type="hidden" name="data" value="">'. // will be set by javascript for next update
'<table class="list_table width100"><tr valign="top">'.
'<td class="list_label">'.$pgv_lang['gedcom_file'].'</td>'.
'<td class="optionbox wrap"><select name="GEDCOM" onchange="reset_reload();">';
$all_gedcoms=get_all_gedcoms();
asort($all_gedcoms);
foreach ($all_gedcoms as $ged_id=>$gedcom) {
$html.='<option value="'.$gedcom.'"'.($ged_id==PGV_GED_ID ? ' selected="selected"' : '').'>'.get_gedcom_setting($ged_id, 'title').'</option>';
}
$html.='</select></td></tr><tr valign="top"><td class="list_label">'.$pgv_lang['batch_update'].':</td><td class="optionbox wrap"><select name="plugin" onchange="reset_reload();">';
if (!$this->plugin) {
$html.='<option value="" selected="selected"></option>';
}
foreach ($this->plugins as $plugin) {
$html.='<option value="'.$plugin.'"'.($plugin==$this->plugin ? ' selected="selected"' : '').'>'.$pgv_lang['bu_'.$plugin].'</option>';
}
$html.='</select><br/><i>'.$pgv_lang['bu_'.$this->plugin.'_desc'].'</i></td></tr>';
// If a plugin is selected, display the details
if ($this->PLUGIN) {
$html.=$this->PLUGIN->getOptionsForm();
if (substr($this->action, -4)=='_all') {
// Reset - otherwise we might "undo all changes", which refreshes the
// page, which makes them all again!
$html.='<script type="text/javascript">reset_reload();</script>';
} else {
if ($this->curr_xref) {
// Create an object, so we can get the latest version of the name.
$object=GedcomRecord::getInstance($this->curr_xref);
$object->setGedcomRecord($this->record);
$html.=
'</table><br/><table class="list_table width100"><tr valign="middle"><td class="list_label center width20">'.
self::createSubmitButton($pgv_lang['prev'], $this->prev_xref).
self::createSubmitButton($pgv_lang['next'], $this->next_xref).
'</td><td class="optionbox width80"><h1><a href="'.$object->getLinkUrl().'">'.$object->getFullName().'</a>'.
'</h1></td>'.
'</tr><tr><td valign="top" class="list_label center width20">'.
'<br/>'.implode('<br/>',$this->PLUGIN->getActionButtons($this->curr_xref, $this->record)).'<br/>'.
'</td><td dir="ltr" align="left" class="optionbox width80">'.
$this->PLUGIN->getActionPreview($this->curr_xref, $this->record);
'</td></tr>';
} else {
$html.='<tr><td colspan=2>'.$pgv_lang['bu_nothing'].'</td></tr>';
}
}
}
$html.='</table></form>';
return $html.mod_print_footer();
}
// Constructor - initialise variables and validate user-input
function __construct() {
$this->plugins=self::getPluginList(); // List of available plugins
$this->plugin =safe_GET('plugin', $this->plugins); // User parameters
$this->xref =safe_GET('xref', PGV_REGEX_XREF);
$this->action =safe_GET('action');
$this->data =safe_GET('data');
// Don't do any processing until a plugin is chosen.
if ($this->plugin) {
require PGV_ROOT.'/modules/batch_update/plugins/'.$this->plugin.'.php';
$this->PLUGIN=new plugin;
$this->PLUGIN->getOptions();
$this->getAllXrefs();
switch ($this->action) {
case '':
break;
case 'update':
$record=self::getLatestRecord($this->xref, $this->all_xrefs[$this->xref]);
if ($this->PLUGIN->doesRecordNeedUpdate($this->xref, $record)) {
$newrecord=$this->PLUGIN->updateRecord($this->xref, $record);
if ($newrecord!=$record) {
if ($newrecord) {
replace_gedrec($this->xref, $newrecord, $this->PLUGIN->chan);
} else {
delete_gedrec($this->xref);
}
}
}
$this->xref=$this->findNextXref($this->xref);
break;
case 'update_all':
foreach ($this->all_xrefs as $xref=>$type) {
$record=self::getLatestRecord($xref, $type);
if ($this->PLUGIN->doesRecordNeedUpdate($xref, $record)) {
$newrecord=$this->PLUGIN->updateRecord($xref, $record);
if ($newrecord!=$record) {
if ($newrecord) {
replace_gedrec($xref, $newrecord, $this->PLUGIN->chan);
} else {
delete_gedrec($xref);
}
}
}
}
$this->xref='';
return;
case 'delete':
$record=self::getLatestRecord($this->xref, $this->all_xrefs[$this->xref]);
if ($this->PLUGIN->doesRecordNeedUpdate($this->xref, $record)) {
delete_gedrec($this->xref);
}
$this->xref=$this->findNextXref($this->xref);
break;
case 'delete_all':
foreach ($this->all_xrefs as $xref=>$type) {
$record=self::getLatestRecord($xref, $type);
if ($this->PLUGIN->doesRecordNeedUpdate($xref, $record)) {
delete_gedrec($xref);
}
}
$xref->xref='';
return;
default:
// Anything else will be handled by the plugin
$this->PLUGIN->performAction($this->xref, $this->record, $this->action, $this->data);
break;
}
// Make sure that our requested record really does need updating.
// It may have been updated in another session, or may not have
// been specified at all.
if (array_key_exists($this->xref, $this->all_xrefs) &&
$this->PLUGIN->doesRecordNeedUpdate($this->xref, self::getLatestRecord($this->xref, $this->all_xrefs[$this->xref]))) {
$this->curr_xref=$this->xref;
}
// The requested record doesn't need updating - find one that does
if (!$this->curr_xref) {
$this->curr_xref=$this->findNextXref($this->xref);
}
if (!$this->curr_xref) {
$this->curr_xref=$this->findPrevXref($this->xref);
}
// If we've found a record to update, get details and look for the next/prev
if ($this->curr_xref) {
$this->record=self::getLatestRecord($this->curr_xref, $this->all_xrefs[$this->curr_xref]);
$this->prev_xref=$this->findPrevXref($this->curr_xref);
$this->next_xref=$this->findNextXref($this->curr_xref);
}
}
}
// Find the next record that needs to be updated
function findNextXref($xref) {
foreach (array_keys($this->all_xrefs) as $key) {
if ($key>$xref) {
$record=self::getLatestRecord($key, $this->all_xrefs[$key]);
if ($this->PLUGIN->doesRecordNeedUpdate($key, $record)) {
return $key;
}
}
}
return null;
}
// Find the previous record that needs to be updated
function findPrevXref($xref) {
foreach (array_reverse(array_keys($this->all_xrefs)) as $key) {
if ($key<$xref) {
$record=self::getLatestRecord($key, $this->all_xrefs[$key]);
if ($this->PLUGIN->doesRecordNeedUpdate($key, $record)) {
return $key;
}
}
}
return null;
}
function getAllXrefs() {
global $TBLPREFIX;
$sql=array();
$vars=array();
foreach ($this->PLUGIN->getRecordTypesToUpdate() as $type) {
switch ($type) {
case 'INDI':
$sql[]="SELECT i_id, 'INDI' FROM {$TBLPREFIX}individuals WHERE i_file=?";
$vars[]=PGV_GED_ID;
break;
case 'FAM':
$sql[]="SELECT f_id, 'FAM' FROM {$TBLPREFIX}families WHERE f_file=?";
$vars[]=PGV_GED_ID;
break;
case 'SOUR':
$sql[]="SELECT s_id, 'SOUR' FROM {$TBLPREFIX}sources WHERE s_file=?";
$vars[]=PGV_GED_ID;
break;
case 'OBJE':
$sql[]="SELECT m_media, 'OBJE' FROM {$TBLPREFIX}media WHERE m_gedfile=?";
$vars[]=PGV_GED_ID;
break;
default:
$sql[]="SELECT o_id, ? FROM {$TBLPREFIX}other WHERE o_type=? AND o_file=?";
$vars[]=$type;
$vars[]=$type;
$vars[]=PGV_GED_ID;
break;
}
}
$this->all_xrefs=
PGV_DB::prepare(implode(' UNION ', $sql).' ORDER BY 1 ASC')
->execute($vars)
->fetchAssoc();
}
// Scan the plugin directory for a list of plugins
static function getPluginList() {
$array=array();
$dir=dirname(__FILE__).'/plugins/';
$dir_handle=opendir($dir);
while ($file=readdir($dir_handle)) {
if (substr($file, -4)=='.php') {
$array[]=basename($file, '.php');
}
}
closedir($dir_handle);
return $array;
}
// Javascript that gets included on every page
static function getJavascript() {
return
'<script type="text/javascript">'.
'function reset_reload() {'.
' var bu_form=document.getElementById("batch_update_form");'.
' bu_form.xref.value="";'.
' bu_form.action.value="";'.
' bu_form.data.value="";'.
' bu_form.submit();'.
'}</script>'
;
}
// Create a submit button for our form
static function createSubmitButton($text, $xref, $action='', $data='') {
return
'<input type="submit" value="'.$text.'" onclick="'.
'this.form.xref.value=\''.htmlspecialchars($xref).'\';'.
'this.form.action.value=\''.htmlspecialchars($action).'\';'.
'this.form.data.value=\''.htmlspecialchars($data).'\';'.
'return true;"'.
($xref ? '' : ' disabled').'>';
}
// Get the current view of a record, allowing for pending changes
static function getLatestRecord($xref, $type) {
global $GEDCOM, $pgv_changes;
$ged_id=get_id_from_gedcom($GEDCOM);
if (isset($pgv_changes[$xref.'_'.$GEDCOM])) {
return find_updated_record($xref, $ged_id);
} else {
switch ($type) {
case 'INDI': return find_person_record($xref, $ged_id);
case 'FAM': return find_family_record($xref, $ged_id);
case 'SOUR': return find_source_record($xref, $ged_id);
case 'OBJE': return find_media_record ($xref, $ged_id);
default: return find_other_record ($xref, $ged_id);
}
}
}
}
// Each plugin should extend the base_plugin class, and implement these
// two functions:
//
// bool doesRecordNeedUpdate($xref, $gedrec)
// string updateRecord($xref, $gedrec)
//
class base_plugin {
var $chan=false; // User option; update change record
// Default is to operate on INDI records
function getRecordTypesToUpdate() {
return array('INDI');
}
// Default option is just the "don't update CHAN record"
function getOptions() {
$this->chan=safe_GET_bool('chan');
}
// Default option is just the "don't update CHAN record"
function getOptionsForm() {
global $pgv_lang;
return
'<tr valign="top"><td class="list_label width20">'.$pgv_lang['bu_update_chan'].':</td>'.
'<td class="optionbox wrap"><select name="chan" onchange="this.form.submit();">'.
'<option value="no"' .($this->chan ? '' : ' selected="selected"').'>'.$pgv_lang['no'] .'</option>'.
'<option value="yes"'.($this->chan ? ' selected="selected"' : '').'>'.$pgv_lang['yes'].'</option>'.
'</select></td></tr>';
}
// Default buttons are update and update_all
function getActionButtons($xref) {
global $pgv_lang;
return array(
batch_update::createSubmitButton($pgv_lang['bu_button_update'], $xref, 'update'),
batch_update::createSubmitButton($pgv_lang['bu_button_update_all'], $xref, 'update_all')
);
}
// Default previewer for plugins with no custom preview.
function getActionPreview($xref, $gedrec) {
$old_lines=preg_split('/[\r\n]+/', $gedrec);
$new_lines=preg_split('/[\r\n]+/', $this->updateRecord($xref, $gedrec));
// Find matching lines using longest-common-subsequence algorithm.
$lcs=self::LCS($old_lines, $new_lines, 0, count($old_lines)-1, 0, count($new_lines)-1);
$diff_lines=array();
$last_old=-1;
$last_new=-1;
while ($lcs) {
list($old, $new)=array_shift($lcs);
while ($last_old<$old-1) {
$diff_lines[]=self::decorateDeletedText($old_lines[++$last_old]);
}
while ($last_new<$new-1) {
$diff_lines[]=self::decorateInsertedText($new_lines[++$last_new]);
}
$diff_lines[]=$new_lines[$new];
$last_old=$old;
$last_new=$new;
}
while ($last_old<count($old_lines)-1) {
$diff_lines[]=self::decorateDeletedText($old_lines[++$last_old]);
}
while ($last_new<count($new_lines)-1) {
$diff_lines[]=self::decorateInsertedText($new_lines[++$last_new]);
}
return '<pre>'.self::createEditLinks(implode("\n", $diff_lines)).'</pre>';
}
// Longest Common Subsequence.
static function LCS($X, $Y, $x1, $x2, $y1, $y2) {
if ($x2-$x1>=0 && $y2-$y1>=0) {
if ($X[$x1]==$Y[$y1]) {
// Match at start of sequence
$tmp=self::LCS($X, $Y, $x1+1, $x2, $y1+1, $y2);
array_unshift($tmp, array($x1, $y1));
return $tmp;
} elseif ($X[$x2]==$Y[$y2]) {
// Match at end of sequence
$tmp=self::LCS($X, $Y, $x1, $x2-1, $y1, $y2-1);
array_push($tmp, array($x2, $y2));
return $tmp;
} else {
// No match. Look for subsequences
$tmp1=self::LCS($X, $Y, $x1, $x2, $y1, $y2-1);
$tmp2=self::LCS($X, $Y, $x1, $x2-1, $y1, $y2);
return count($tmp1) > count($tmp2) ? $tmp1 : $tmp2;
}
} else {
// One array is empty - end recursion
return array();
}
}
// Default handler for plugin with no custom actions.
function performAction($xref, $gedrec, $action, $data) {
}
// Decorate inserted/deleted text
static function decorateInsertedText($text) {
return '<span class="search_hit">'.$text.'</span>';
}
static function decorateDeletedText($text) {
return '<span style="text-decoration:line-through;">'.$text.'</span>';
}
// Converted gedcom links into editable links
static function createEditLinks($gedrec) {
return preg_replace(
"/@([^#@\n]+)@/m",
'<a href="javascript:;" onclick="return edit_raw(\'\\1\');">@\\1@</a>',
$gedrec
);
}
}
?>