forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_trees_check.php
278 lines (248 loc) · 9.63 KB
/
admin_trees_check.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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2017 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;
use Fisharebest\Webtrees\Controller\PageController;
/** @global Tree $WT_TREE */
global $WT_TREE;
require 'includes/session.php';
$controller = new PageController;
$controller
->restrictAccess(Auth::isManager($WT_TREE))
->setPageTitle(I18N::translate('Check for errors') . ' — ' . $WT_TREE->getTitleHtml())
->pageHeader();
// We need to work with raw GEDCOM data, as we are looking for errors
// which may prevent the GedcomRecord objects from working.
$rows = Database::prepare(
"SELECT i_id AS xref, 'INDI' AS type, i_gedcom AS gedrec FROM `##individuals` WHERE i_file=?" .
" UNION " .
"SELECT f_id AS xref, 'FAM' AS type, f_gedcom AS gedrec FROM `##families` WHERE f_file=?" .
" UNION " .
"SELECT s_id AS xref, 'SOUR' AS type, s_gedcom AS gedrec FROM `##sources` WHERE s_file=?" .
" UNION " .
"SELECT m_id AS xref, 'OBJE' AS type, m_gedcom AS gedrec FROM `##media` WHERE m_file=?" .
" UNION " .
"SELECT o_id AS xref, o_type AS type, o_gedcom AS gedrec FROM `##other` WHERE o_file=? AND o_type NOT IN ('HEAD', 'TRLR')"
)->execute([$WT_TREE->getTreeId(), $WT_TREE->getTreeId(), $WT_TREE->getTreeId(), $WT_TREE->getTreeId(), $WT_TREE->getTreeId()])->fetchAll();
$records = [];
foreach ($rows as $row) {
$records[$row->xref] = $row;
}
// Need to merge pending new/changed/deleted records
$rows = Database::prepare(
"SELECT xref, SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(CASE WHEN old_gedcom='' THEN new_gedcom ELSE old_gedcom END, '\n', 1), ' ', 3), ' ', -1) AS type, new_gedcom AS gedrec" .
" FROM (" .
" SELECT MAX(change_id) AS change_id" .
" FROM `##change`" .
" WHERE gedcom_id=? AND status='pending'" .
" GROUP BY xref" .
" ) AS t1" .
" JOIN `##change` t2 USING (change_id)"
)->execute([$WT_TREE->getTreeId()])->fetchAll();
foreach ($rows as $row) {
if ($row->gedrec) {
// new/updated record
$records[$row->xref] = $row;
} else {
// deleted record
unset($records[$row->xref]);
}
}
// Keep a list of upper case XREFs, to detect mismatches.
$ukeys = [];
foreach (array_keys($records) as $key) {
$ukeys[strtoupper($key)] = $key;
}
// LOOK FOR BROKEN LINKS
$XREF_LINKS = [
'NOTE' => 'NOTE',
'SOUR' => 'SOUR',
'REPO' => 'REPO',
'OBJE' => 'OBJE',
'SUBM' => 'SUBM',
'FAMC' => 'FAM',
'FAMS' => 'FAM',
//'ADOP'=>'FAM', // Need to handle this case specially. We may have both ADOP and FAMC links to the same FAM, but only store one.
'HUSB' => 'INDI',
'WIFE' => 'INDI',
'CHIL' => 'INDI',
'ASSO' => 'INDI',
'_ASSO' => 'INDI', // A webtrees extension
'ALIA' => 'INDI',
'AUTH' => 'INDI', // A webtrees extension
'ANCI' => 'SUBM',
'DESI' => 'SUBM',
'_WT_OBJE_SORT' => 'OBJE',
'_LOC' => '_LOC',
];
$RECORD_LINKS = [
'INDI' => ['NOTE', 'OBJE', 'SOUR', 'SUBM', 'ASSO', '_ASSO', 'FAMC', 'FAMS', 'ALIA', '_WT_OBJE_SORT', '_LOC'],
'FAM' => ['NOTE', 'OBJE', 'SOUR', 'SUBM', 'ASSO', '_ASSO', 'HUSB', 'WIFE', 'CHIL', '_LOC'],
'SOUR' => ['NOTE', 'OBJE', 'REPO', 'AUTH'],
'REPO' => ['NOTE'],
'OBJE' => ['NOTE'], // The spec also allows SOUR, but we treat this as a warning
'NOTE' => [], // The spec also allows SOUR, but we treat this as a warning
'SUBM' => ['NOTE', 'OBJE'],
'SUBN' => ['SUBM'],
'_LOC' => ['SOUR', 'OBJE', '_LOC'],
];
$errors = false;
echo Bootstrap4::breadcrumbs([
'admin.php' => I18N::translate('Control panel'),
'admin_trees_manage.php' => I18N::translate('Manage family trees'),
], $controller->getPageTitle());
?>
<h1><?= $controller->getPageTitle() ?></h1>
<ul class="list-group">
<li class="list-group-item"><strong><?= I18N::translate('Types of error') ?></strong></li>
<li class="list-group-item list-group-item-danger"><?= I18N::translate('This may cause a problem for webtrees.') ?></li>
<li class="list-group-item list-group-item-warning"><?= I18N::translate('This may cause a problem for other applications.') ?></li>
<li class="list-group-item list-group-item-info"><?= I18N::translate('This may be a mistake in your data.') ?></li>
</ul>
<ul class="list-group">
<li class="list-group-item"><strong><?= I18N::translate('GEDCOM errors') ?></strong></li>
<?php
// Generate lists of all links
$all_links = [];
$upper_links = [];
foreach ($records as $record) {
$all_links[$record->xref] = [];
$upper_links[strtoupper($record->xref)] = $record->xref;
preg_match_all('/\n\d (' . WT_REGEX_TAG . ') @([^#@\n][^\n@]*)@/', $record->gedrec, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$all_links[$record->xref][$match[2]] = $match[1];
}
}
foreach ($all_links as $xref1 => $links) {
$type1 = $records[$xref1]->type;
foreach ($links as $xref2 => $type2) {
$type3 = isset($records[$xref2]) ? $records[$xref2]->type : '';
if (!array_key_exists($xref2, $all_links)) {
if (array_key_exists(strtoupper($xref2), $upper_links)) {
echo warning(
link_message($type1, $xref1, $type2, $xref2) . ' ' .
/* I18N: placeholders are GEDCOM XREFs, such as R123 */
I18N::translate('%1$s does not exist. Did you mean %2$s?', format_link($xref2), format_link($upper_links[strtoupper($xref2)]))
);
} else {
echo error(
link_message(
$type1, $xref1, $type2, $xref2) . ' ' .
/* I18N: placeholders are GEDCOM XREFs, such as R123 */
I18N::translate('%1$s does not exist.', format_link($xref2))
);
}
} elseif ($type2 === 'SOUR' && $type1 === 'NOTE') {
// Notes are intended to add explanations and comments to other records. They should not have their own sources.
} elseif ($type2 === 'SOUR' && $type1 === 'OBJE') {
// Media objects are intended to illustrate other records, facts, and source/citations. They should not have their own sources.
} elseif ($type2 === 'OBJE' && $type1 === 'REPO') {
echo warning(
link_message($type1, $xref1, $type2, $xref2) . ' ' . I18N::translate('This type of link is not allowed here.')
);
} elseif (!array_key_exists($type1, $RECORD_LINKS) || !in_array($type2, $RECORD_LINKS[$type1]) || !array_key_exists($type2, $XREF_LINKS)) {
echo error(
link_message($type1, $xref1, $type2, $xref2) . ' ' .
I18N::translate('This type of link is not allowed here.')
);
} elseif ($XREF_LINKS[$type2] !== $type3) {
// Target XREF does exist - but is invalid
echo error(
link_message($type1, $xref1, $type2, $xref2) . ' ' .
/* I18N: %1$s is an internal ID number such as R123. %2$s and %3$s are record types, such as INDI or SOUR */
I18N::translate('%1$s is a %2$s but a %3$s is expected.', format_link($xref2), format_type($type3), format_type($type2))
);
} elseif (
$type2 === 'FAMC' && (!array_key_exists($xref1, $all_links[$xref2]) || $all_links[$xref2][$xref1] !== 'CHIL') ||
$type2 === 'FAMS' && (!array_key_exists($xref1, $all_links[$xref2]) || $all_links[$xref2][$xref1] !== 'HUSB' && $all_links[$xref2][$xref1] !== 'WIFE') ||
$type2 === 'CHIL' && (!array_key_exists($xref1, $all_links[$xref2]) || $all_links[$xref2][$xref1] !== 'FAMC') ||
$type2 === 'HUSB' && (!array_key_exists($xref1, $all_links[$xref2]) || $all_links[$xref2][$xref1] !== 'FAMS') ||
$type2 === 'WIFE' && (!array_key_exists($xref1, $all_links[$xref2]) || $all_links[$xref2][$xref1] !== 'FAMS')
) {
echo error(
link_message($type1, $xref1, $type2, $xref2) . ' ' .
/* I18N: %1$s and %2$s are internal ID numbers such as R123 */
I18N::translate('%1$s does not have a link back to %2$s.', format_link($xref2), format_link($xref1))
);
}
}
}
if (!$errors) {
echo '<li class="list-group-item">', I18N::translate('No errors have been found.'), '</li>';
}
echo '</ul>';
/**
* Create a message linking one record to another.
*
* @param string $type1
* @param string $xref1
* @param string $type2
* @param string $xref2
*
* @return string
*/
function link_message($type1, $xref1, $type2, $xref2) {
return /* I18N: The placeholders are GEDCOM XREFs and tags. e.g. “INDI I123 contains a FAMC link to F234.” */ I18N::translate(
'%1$s %2$s has a %3$s link to %4$s.',
format_type($type1),
format_link($xref1),
format_type($type2),
format_link($xref2)
);
}
/**
* Format a link to a record.
*
* @param string $xref
*
* @return string
*/
function format_link($xref) {
return '<b><a href="gedrecord.php?pid=' . $xref . '">' . $xref . '</a></b>';
}
/**
* Format a record type.
*
* @param string $type
*
* @return string
*/
function format_type($type) {
return '<b title="' . strip_tags(GedcomTag::getLabel($type)) . '">' . $type . '</b>';
}
/**
* Format an error message.
*
* @param string $message
*
* @return string
*/
function error($message) {
global $errors;
$errors = true;
return '<li class="list-group-item list-group-item-danger">' . $message . '</li>';
}
/**
* Format a warning message.
*
* @param string $message
*
* @return string
*/
function warning($message) {
global $errors;
$errors = true;
return '<li class="list-group-item list-group-item-warning">' . $message . '</li>';
}