| 1 |
<?php
|
| 2 |
// $Id: EstimateDates.inc,v 1.4.2.1 2008/10/09 01:55:57 pyutaros Exp $
|
| 3 |
// EstimateDates.inc By Amnon Jonas 23-Apr-2006
|
| 4 |
|
| 5 |
function family_auto_privacy() {
|
| 6 |
$content = "<p>This will run an algorithm to automatically set privacy<br>".
|
| 7 |
"Generally, the algorithm sets living people and their generation as private. ".
|
| 8 |
"The algorithm tries to evaluate the generation of people, using their own data and data of their relatives. ".
|
| 9 |
"To protect privacy, errors are usually biased for protecting data.</p>".
|
| 10 |
"<p>Go to Administer Access control to set which roles may access public or private records</p>".
|
| 11 |
"<p>The results cannot be used to determine if a specific individual is actually living. ".
|
| 12 |
"If you have sensitive data, please check manually that the data is set as private. ".
|
| 13 |
"You should be aware that any database may be hacked and there is no guarantee for your data security</p>".
|
| 14 |
"<p>For large files this may take a few minutes.</p>".
|
| 15 |
"<p><a href=\"/admin/settings/family_privacy_done\">Start auto privacy</a></p>";
|
| 16 |
return $content;
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Decode GEDCOM formatted date field and store it as a range array
|
| 21 |
* Currently support only Gregorian dates for years 100-9999
|
| 22 |
*/
|
| 23 |
function family_decode_date($date) {
|
| 24 |
$range = array( 'MIN' => -99999,'MAX' => 99999,'AVG' => 2008,'VAR' => 9e9); //Default values
|
| 25 |
$date=explode('-',$date);
|
| 26 |
if($date[0]!='0000'){
|
| 27 |
$range['AVG']=$date[0];
|
| 28 |
// echo $date[0]."</br>";
|
| 29 |
$range['VAR']=0;
|
| 30 |
if($range['AVG']<=100){
|
| 31 |
$range['AVG']=2000;
|
| 32 |
}
|
| 33 |
return $range;
|
| 34 |
}
|
| 35 |
return $range;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Fix range array. Take care of min>max and use information from (min,max) to update (avg,var)
|
| 40 |
* and vice versa
|
| 41 |
*/
|
| 42 |
function family_fix_range($range) {
|
| 43 |
if ($range['VAR']==0){
|
| 44 |
$range['MIN']=$range['AVG'];
|
| 45 |
$range['MAX']=$range['AVG'];
|
| 46 |
}
|
| 47 |
if ($range['MIN'] > $range['MAX']) {
|
| 48 |
$range['VAR']=0;
|
| 49 |
$range['AVG']=($range['MIN'] + $range['MAX'])/2;
|
| 50 |
$range['MAX']=$range['AVG'];
|
| 51 |
$range['MIN']=$range['AVG'];
|
| 52 |
}
|
| 53 |
else {
|
| 54 |
if (pow(($range['MAX'] - $range['MIN'])/2,2)<$range['VAR']) {
|
| 55 |
$range['AVG']=($range['MIN'] + $range['MAX'])/2;
|
| 56 |
$range['VAR']=pow(($range['MAX'] - $range['MIN'])/2,2);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
return $range;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Add two range arrays
|
| 64 |
* Used to add offset range between two events to get range of one events, based on the other event
|
| 65 |
*/
|
| 66 |
function family_add_ranges($range1,$range2) {
|
| 67 |
$range['MIN']=$range1['MIN']+$range2['MIN'];
|
| 68 |
$range['MAX']=$range1['MAX']+$range2['MAX'];
|
| 69 |
$range['AVG']=$range1['AVG']+$range2['AVG'];
|
| 70 |
$range['VAR']=$range1['VAR']+$range2['VAR'];
|
| 71 |
$range = family_fix_range($range);
|
| 72 |
return $range;
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Merge two range arrays that refer to the same event. It selectss the more accurate data of the two.
|
| 77 |
*/
|
| 78 |
function family_merge_ranges($range1,$range2) {
|
| 79 |
$range['MIN']=max($range1['MIN'],$range2['MIN']);
|
| 80 |
$range['MAX']=min($range1['MAX'],$range2['MAX']);
|
| 81 |
if ($range1['VAR']==$range2['VAR']) {
|
| 82 |
$range['AVG']=($range1['AVG']+$range2['AVG'])/2;
|
| 83 |
$range['VAR']=$range1['VAR'];
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
if ($range1['VAR'] < $range2['VAR']) {
|
| 87 |
$range['AVG']=$range1['AVG'];
|
| 88 |
$range['VAR']=$range1['VAR'];
|
| 89 |
}
|
| 90 |
else {
|
| 91 |
$range['AVG']=$range2['AVG'];
|
| 92 |
$range['VAR']=$range2['VAR'];
|
| 93 |
}
|
| 94 |
}
|
| 95 |
$range = family_fix_range($range);
|
| 96 |
return $range;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Run an estimation algorithm that estimate dates ranges for birth and death events.
|
| 101 |
* This is used to determine which records should be private
|
| 102 |
*/
|
| 103 |
function family_auto_privacy_done($nid=0){
|
| 104 |
$birth2death = array('MIN' => 0, 'MAX' => 100,'AVG' => 60, 'VAR' => 1600);
|
| 105 |
$death2birth = array('MIN' => -100,'MAX' => 0,'AVG' => -60, 'VAR' => 1600);
|
| 106 |
$parentbirth2childbirth = array('MIN' => 16, 'MAX' => 56, 'AVG' => 32, 'VAR' => 256);
|
| 107 |
$parentdeath2childbirth = array('MIN' => -75, 'MAX' => 0, 'AVG' => -30, 'VAR' => 900);
|
| 108 |
$childbirth2parentbirth = array('MIN' => -56, 'MAX' => -16, 'AVG' => -32, 'VAR' => 256);
|
| 109 |
$childbirth2parentdeath = array('MIN' => 0, 'MAX' => 75, 'AVG' => 30, 'VAR' => 900);
|
| 110 |
$today = getdate();
|
| 111 |
$thisyear=$today['year'];
|
| 112 |
$content.="START</br>";
|
| 113 |
//Save the individuals and children queries results to save doing the same query each iteration
|
| 114 |
$individuals = db_query("SELECT * FROM {family_individual}");
|
| 115 |
// Collect Individuals Birth and Death data
|
| 116 |
while ($data = db_fetch_array($individuals)) {
|
| 117 |
$nid=$data['nid'];
|
| 118 |
$date_birth=explode('-',$data['birthdate']);
|
| 119 |
|
| 120 |
if ($date_birth[0]!='0000'){
|
| 121 |
$birthrange[$nid] = family_fix_range(family_decode_date($data['birthdate']));
|
| 122 |
$birthrange[$nid]['MAX'] = min($birthrange[$fid]['MAX'], $thisyear);
|
| 123 |
}
|
| 124 |
else {
|
| 125 |
$birthrange[$nid] = array('MIN' => -99999, 'MAX' => $thisyear, 'AVG' => 2000, 'VAR' => 9e9); //Default values
|
| 126 |
}
|
| 127 |
$date_death=explode('-',$data['deathdate']);
|
| 128 |
if ($date_death[0]!='0000'){
|
| 129 |
$deathrange[$nid] = family_fix_range(family_decode_date($data['deathdate']));
|
| 130 |
}
|
| 131 |
else {
|
| 132 |
$deathrange[$nid] = array('MIN' => -99999, 'MAX' => 99999, 'AVG' => 2000, 'VAR' => 9e9); //Default values
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
$number_iterations = 8;
|
| 137 |
for ($iterations = 0; $iterations < $number_iterations; $iterations++) {
|
| 138 |
//Compare Death and Birth
|
| 139 |
echo "<!-- -->"; //Just send an empy HTML comment to avoid client timeout while waiting
|
| 140 |
$individuals = db_query("SELECT nid FROM {family_individual}");
|
| 141 |
for ($i = 0; $i < db_num_rows($individuals); $i++) {
|
| 142 |
$nid=db_result($individuals,$i);
|
| 143 |
$deathrange[$nid] = family_merge_ranges($deathrange[$nid],family_add_ranges($birthrange[$nid],$birth2death));
|
| 144 |
$birthrange[$nid] = family_merge_ranges($birthrange[$nid],family_add_ranges($deathrange[$nid],$death2birth));
|
| 145 |
}
|
| 146 |
//Compare parents and children
|
| 147 |
$children = db_query("SELECT nid, ancestor_group FROM {family_individual} WHERE (ancestor_group<>'' AND ancestor_group<>'Unknown')");
|
| 148 |
while($child = db_fetch_array($children)) {
|
| 149 |
$nid1=$child['nid']; //Child fid
|
| 150 |
$fam=$child['ancestor_group']; //Family fid
|
| 151 |
$parents = db_fetch_array(db_query("SELECT parent1,parent2 FROM {family_group} WHERE nid=%d",$fam));
|
| 152 |
//father
|
| 153 |
$nid2=$parents['parent1']; //parent fid
|
| 154 |
$birthrange[$nid1] = family_merge_ranges($birthrange[$nid1],family_add_ranges($birthrange[$nid2],$parentbirth2childbirth));
|
| 155 |
$birthrange[$nid1] = family_merge_ranges($birthrange[$nid1],family_add_ranges($deathrange[$nid2],$parentdeath2childbirth));
|
| 156 |
$birthrange[$nid2] = family_merge_ranges($birthrange[$nid2],family_add_ranges($birthrange[$nid1],$childbirth2parentbirth));
|
| 157 |
$deathrange[$nid2] = family_merge_ranges($deathrange[$nid2],family_add_ranges($birthrange[$nid1],$childbirth2parentdeath));
|
| 158 |
|
| 159 |
//mother
|
| 160 |
$nid2=$parents['parent2']; //parent fid
|
| 161 |
|
| 162 |
$birthrange[$nid1] = family_merge_ranges($birthrange[$nid1],family_add_ranges($birthrange[$nid2],$parentbirth2childbirth));
|
| 163 |
$birthrange[$nid1] = family_merge_ranges($birthrange[$nid1],family_add_ranges($deathrange[$nid2],$parentdeath2childbirth));
|
| 164 |
$birthrange[$nid2] = family_merge_ranges($birthrange[$nid2],family_add_ranges($birthrange[$nid1],$childbirth2parentbirth));
|
| 165 |
$deathrange[$nid2] = family_merge_ranges($deathrange[$nid2],family_add_ranges($birthrange[$nid1],$childbirth2parentdeath));
|
| 166 |
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
// Update privacy information
|
| 171 |
// Privacy is stored in fact _PRV (family module defined tag)
|
| 172 |
// 0 or none: Auto (public until estimation is run), 1: Always public (overide auto), 2: Always private (overide auto)
|
| 173 |
// 3: Public (set by estimation algorithm), 4: Private (set by estimation algorithm)
|
| 174 |
$PrivacyLevel = array( 0 => "Auto", 1 => "Public (Manually)", 2=>"Private (Manually)", 3 => "Public (Auto)", 4=>"Private (Auto)");
|
| 175 |
$individuals = db_query("SELECT * FROM {family_individual}");
|
| 176 |
while ($data = db_fetch_array($individuals)) {
|
| 177 |
$nid=$data['nid'];
|
| 178 |
$privacy=0;
|
| 179 |
// if ($data['privacy']!=''){
|
| 180 |
// $privacy = $data['privacy'];
|
| 181 |
// }else{
|
| 182 |
// $privacy=0;
|
| 183 |
// }
|
| 184 |
//if (($privacy==0) || ($privacy==3) || ($privacy==4)) {
|
| 185 |
if ((($deathrange[$nid]['MAX']+1000)<=$thisyear) || (($deathrange[$nid]['AVG']+1000)+sqrt($deathrange[$nid]['VAR'])<=$thisyear)) {
|
| 186 |
$privacy=3;
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
$privacy=4;
|
| 190 |
}
|
| 191 |
db_query("UPDATE {family_individual} SET privacy='%d' WHERE nid='%d'",$privacy, $nid);
|
| 192 |
// $content.=$privacy."</br>";
|
| 193 |
//}
|
| 194 |
|
| 195 |
// Write Estimated dates in Biography, for testing
|
| 196 |
// $nid = db_result(db_query("SELECT nid FROM family_facts WHERE fid = %d", $fid));
|
| 197 |
// $node=node_load($nid);
|
| 198 |
// $content .= "<strong>Estimated Dates (calculated by Family Module)</strong>";
|
| 199 |
// $content.= "<ul>Birth: ".$birthrange[$nid]['MIN']."- ".$birthrange[$nid]['MAX']." ".round($birthrange[$nid]['AVG'])."±".round(sqrt($birthrange[$nid]['VAR']))."</ul>";
|
| 200 |
// $content.= "<ul>Death: ".$deathrange[$nid]['MIN']."- ".$deathrange[$nid]['MAX']." ".round($deathrange[$nid]['AVG'])."±".round(sqrt($deathrange[$nid]['VAR']))."</ul>";
|
| 201 |
// $content.="<p>Privacy status: ".$PrivacyLevel[$privacy]."</p>";
|
| 202 |
// $node->body = $content;
|
| 203 |
// node_save(&$node);
|
| 204 |
|
| 205 |
}
|
| 206 |
$content.="<p>Done</p>";
|
| 207 |
return $content;
|
| 208 |
}
|
| 209 |
|