Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 403 lines (331 sloc) 12.251 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
<?php

/*
This doSearch page has three functions:
doSearch() hits our databases looking for partially matching suspect names
printResults() generates an HTML table containing the results of doSearch()
totalTime() calculates the execution time of this page for inclusion in printResults()
*/


//Calculate the total runtime of this page
function totalTime($start_time)
{
  echo ("<br>Total page execution time <font color=green size=+1>");
  $x = timeSince($start_time);
  print $x;
  echo ("</font> seconds.");
  
  print "<br><b><a href=\"./\">Back</a></b>";
  print "</b></center>";
  
  return $x;
}

//Print list of possible suspect matches to an HTML table
function printResults($output_type, $suspName, $suspSSN, $multiSearch, $possibles)
{
global $count_exact, $count_partial;

$count_exact = 0;
$count_partial = 0;

foreach($possibles as $row)
if($row->exactMatch == 1)
$count_exact++;
else
$count_partial++;

print "<br><table border=1 cellpadding=2 cellspacing=3 width=100%>\n";
if($output_type == "EXACT")
print "<td colspan=100%><center><font size=+1 color=blue>$count_exact</font>";
else
print "<td colspan=100%><center><font size=+1 color=blue>$count_partial</font>";
print " $output_type match(es) for ";
if(strlen($suspName))
print "'<font color=blue size=+1>$suspName</font>'\t";
if(strlen($suspSSN))
print "'<font color=blue size=+1>$suspSSN</font>'";
print "<center></font></td></tr>";
print "\t<tr><td colspan=100% bgcolor=EEAAAA><center>Matching elements listed in pink</center></td></tr>";

if(!$multiSearch)
{
   print "\t<tr bgcolor=DDDDDD>\n\t";
   print "<td>SSN<br>(master)</td><td>FNAME<br>(master)</td><td>LNAME<br>(master)</td>";
   print "<td>DOB<br>(master)</td><td>DLNO<br>(master)</td>";
   print "<td>HEIGHT<br>(master)</td><td>WEIGHT<br>(master)</td><td>RACE<br>(master)</td>";
   print "<td>STREETNO<br>(master)</td><td>STREETNAME<br>(master)</td><td>CITY<br>(master)</td><td>ZIP<br>(master)</td>";
   print "\n\t</tr>\n";
}
else
{
   print "\t<tr bgcolor=DDDDDD>\n\t";
   print "<td>SSN<br>(master)</td><td>FNAME<br>(master)</td><td>LNAME<br>(master)</td>";
   print "<td>DOB<br>(slaveone)</td><td>DLNO<br>(slaveone)</td>";
   print "<td>HEIGHT<br>(slavetwo)</td><td>WEIGHT<br>(slavetwo)</td><td>RACE<br>(slavetwo)</td>";
   print "<td>STREETNO<br>(slavethree)</td><td>STREETNAME<br>(slavethree)</td><td>CITY<br>(slavethree)</td><td>ZIP<br>(slavethree)</td>";
   print "\n\t</tr>\n";
}


//traverse result arrays for printing
foreach($possibles as $row)
{
if($output_type == "EXACT")
{
if($row->exactMatch == 1)
$row->printme();
}
  else
{
if($row->exactMatch != 1)
$row->printme();
}
}

print "</table>";

print "<br><hr>";

return $count_exact;
}

//Search across multiple databases to find possible suspect name matches.
//Uses Levenshtein distance, substring identification, and Soundex comparisons
// in order to identify potential matches. See paper for algorithm discussion.
function doSearch() //performs a search and prints results
{

//initialize timer
global $start_time;
$start_time = microtime();

//include timeSince()
include_once("timeSince.php");

//include Suspect class
include_once("class_suspect.php");

//include loadSuspects()
include_once("loadSuspects.php");

//include flagPossibles()
include_once("flagPossibles.php");

//import DB connection info
include("include/dbinfo.php");

//parse inputs
   $suspName = rtrim(strtoupper($_GET["suspName"]), "_"); //converts to all caps to match DB
$suspSSN = rtrim($_GET["suspSSN"], "_");

$doNameLev = $_GET["levName"];
$doSSNLev = $_GET["levSSN"];
$doSoundex = $_GET["soundex"];

//set shorter flags based on inputs for easier typing below
if($doNameLev == "true")
$doNameLev = 1;
else
$doNameLev = 0;
if($doSSNLev == "true")
$doSSNLev = 1;
else
$doSSNLev = 0;
if($doSoundex == "true")
$doSoundex = 1;
else
$doSoundex = 0;

if($_GET["subStr"] == "true")
$doSubStr = 1;
else
$doSubStr = 0;

//are we searching one DB or multiples?
if($_GET["searchType"] == single)
$multiSearch = 0;
else
$multiSearch = 1;

//set primary DB name - this one contains name/ssn info
if(!$multiSearch)
$masterDB = "single";
else
$masterDB = "suspects";

//open master DB conn
$master_link = mysql_connect("master",$user,$pass);
mysql_select_db($masterDB,$master_link);


//perform queries
//get ssn/fname/lname, store in array
//in all cases these are the first 3 columns of the master DB

//select all from $masterDB
$query = "SELECT SSN,FNAME,LNAME FROM suspects WHERE 1=1 ORDER BY SSN ASC";
// fname LIKE '$suspName' OR lname LIKE '$suspName' OR SSN LIKE '$suspSSN'";
   $result = mysql_query($query, $master_link);
  
   //empty query results into Suspect array
   $possibles = array();
   while($row = mysql_fetch_row($result))
   {
     $susp = new Suspect;
     $susp->setName($row[0],$row[1],$row[2]);
     array_push($possibles,$susp);
  }
 
//levenshtein threshholds
$name_thresh = strlen($suspName)/2;
$num_thresh = 2;

$possibles = flagPossibles($possibles, $suspSSN, $suspName, $doNameLev, $doSSNLev, $doSoundex, $multiSearch, $doSubStr, $name_thresh, $num_thresh);

$result = mysql_query("SELECT COUNT(*) FROM SUSPECTS", $master_link);
$num_records = mysql_fetch_row($result);

//set timer after possibles identified
global $extend_start;
$extend_start = microtime();

$count_possibles = count($possibles);

//print count of possible hits
print "<center>";
print "<font color=orange size=+1>";
print $count_possibles;
print "</font>";
print " potential match(es) identified out of <font color=orange size=+1>$num_records[0]</font> rows";
print " on SSN/Name database <font color=red size=+1>master</font> in ";
print "<font color=green size=+1>";
$time_possibles = timeSince($start_time);
print $time_possibles;

print "</font>";
print " seconds.<br>";

print "<br>Now loading extended information from database(s) <font color=red size=+1>";

// return;
   //sort ssn/name array? not needed if query results are ORDER BY ssn ASC

//get extended info on hits from other databases
if(!$multiSearch) // multiSearch is false
{
print "$masterDB</font>.\n<hr>";
//single: get the remaining columns from the master DB
//clear POSSIBLES table
//load list of $possible SSNs into table
$commit = array();
foreach($possibles as $key => $currSusp)
{
array_push($commit, $currSusp->ssn);
if(($key % 50) == 0 && $key > 0) //row index a nonzero multiple of 50
{
   //commit to possibles table
   loadSuspects($master_link, $commit);
   $commit = array();
}
}

//commit last <50 rows
   loadSuspects($master_link, $commit);
  
//do a join, return matches?
$result = mysql_query("SELECT S.* FROM SUSPECTS AS S, POSSIBLES AS P WHERE P.SSN = S.SSN ORDER BY S.SSN ASC", $master_link);

$x = 0;
     while($row = mysql_fetch_row($result))
{
if($possibles[$x]->ssn == $row[0])
{
$possibles[$x]->setName($row[0],$row[1],$row[2]);
$possibles[$x]->set_dob_dlno($row[3],$row[11]);
$possibles[$x]->set_vitals($row[4],$row[5],$row[6]);
$possibles[$x]->set_address($row[7],$row[8],$row[9],$row[10]);
}
$x++;
}

//clear possibles table
clearSuspects($master_link);

}
else // multiSearch is true
{
print " slaveone, slavetwo, and slavethree</font>.\n<hr>";
//multi:
//open slave DB conns
$slave1_link = mysql_connect("slaveone",$user,$pass);
$slave2_link = mysql_connect("slavetwo",$user,$pass);
$slave3_link = mysql_connect("slavethree",$user,$pass);

mysql_select_db($db,$slave1_link);
mysql_select_db($db,$slave2_link);
mysql_select_db($db,$slave3_link);

//load list of $possible SSNs into tables
$commit = array();
foreach($possibles as $key => $currSusp)
{
array_push($commit, $currSusp->ssn);
if(($key % 50) == 0 && $key > 0) //row index a nonzero multiple of 50
{
   //commit to possibles table
   loadSuspects($slave1_link, $commit);
   loadSuspects($slave2_link, $commit);
   loadSuspects($slave3_link, $commit);
   $commit = array();
}
}
//commit last <50 rows
   loadSuspects($slave1_link, $commit);
   loadSuspects($slave2_link, $commit);
   loadSuspects($slave3_link, $commit);
  
   $start_extend = microtime();
  
//do a join, return matches?
$result1 = mysql_query("SELECT S.* FROM SUSPECTS AS S, POSSIBLES AS P WHERE P.SSN = S.SSN ORDER BY S.SSN ASC", $slave1_link);
$result2 = mysql_query("SELECT S.* FROM SUSPECTS AS S, POSSIBLES AS P WHERE P.SSN = S.SSN ORDER BY S.SSN ASC", $slave2_link);
$result3 = mysql_query("SELECT S.* FROM SUSPECTS AS S, POSSIBLES AS P WHERE P.SSN = S.SSN ORDER BY S.SSN ASC", $slave3_link);

//load slave1 results into $possibles
$x = 0;
     while($row1 = mysql_fetch_row($result1))
{
if($possibles[$x]->ssn == $row1[0])
$possibles[$x]->set_dob_dlno($row1[1],$row1[2]);
$x++;
}

//load slave2 results into $possibles
$x = 0;
     while($row2 = mysql_fetch_row($result2))
{
if($possibles[$x]->ssn == $row2[0])
$possibles[$x]->set_address($row2[1],$row2[2],$row2[3],$row2[4]);
$x++;
}

//load slave3 results into $possibles
$x = 0;
     while($row3 = mysql_fetch_row($result3))
{
if($possibles[$x]->ssn == $row3[0])
$possibles[$x]->set_vitals($row3[1],$row3[2],$row3[3]);
$x++;
}

//clear possibles tables
clearSuspects($slave1_link);
clearSuspects($slave2_link);
clearSuspects($slave3_link);
}

//close DB conns
mysql_close($master_link);
if($multiSearch)
{
   mysql_close($slave1_link);
   mysql_close($slave2_link);
   mysql_close($slave3_link);
}

// $end_extend = timeSince($start_extend);

//print exact matches
$count_exact = printResults("EXACT", $suspName, $suspSSN, $multiSearch, $possibles);

//print extended results
printResults("PARTIAL", $suspName, $suspSSN, $multiSearch, $possibles);

    echo ("Extended information retrieved in <font color=green size=+1>");
    print timeSince($extend_start);
    echo ("</font> seconds.");
    
    $total_time = totalTime($start_time);
    
    
    
   // print "<hr><center>";
//print "EXACT_MATCHES: $count_exact<br>";
//print "PARTIAL_MATCHES: ";
$count_partial = $count_possibles - $count_exact;
// print "$count_partial<br>";
// print "POSSIBLESTIME: $time_possibles<br>";
// print "TOTALTIME: $total_time<br></center>";


//build log string of
//SUSP_NAME,SUSP_SSN,MULTI_SEARCH,LEV_NAME,SOUNDEX,LEVSSN,
//DO_SUBSTR,EXACT_MATCHES,PARTIAL_MATCHES,POSSIBLES_TIME,TOTAL_TIME

$logstring = "";
$logstring .= "$suspName,";
$logstring .= "$suspSSN,";
$logstring .= "$multiSearch,";
$logstring .= "$doNameLev,";
$logstring .= "$doSoundex,";
$logstring .= "$doSSNLev,";
$logstring .= "$doSubStr,";
$logstring .= "$count_exact,";
$logstring .= "$count_partial,";
$logstring .= "$time_possibles,";
$time_extend = $total_time - $time_possibles;
$logstring .="$time_extend,";
$logstring .= "$total_time\r\n";


$handle = fopen("logfile.csv", "a+");
fwrite($handle, $logstring);
// print "<hr>$handle";
// print "<hr>$logstring";
fclose($handle);

/*
//log results
$master_link = mysql_connect("master",$user,$pass);
mysql_select_db("suspects",$master_link);
$query = "INSERT INTO RESULTS VALUES (" . $logstring . ")";
$result = mysql_query($query, $master_link);
mysql_close($master_link);
//print "<hr>$query<hr>";
//var_dump($result);
//exit function
*/
return $logstring;
}
?>
Something went wrong with that request. Please try again.