-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessDocketSheets.php
More file actions
164 lines (141 loc) · 4.37 KB
/
processDocketSheets.php
File metadata and controls
164 lines (141 loc) · 4.37 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
<?php
require_once("config.php");
require_once("Docket.php");
require_once("ArrestSummary.php");
require_once("getDocketsGranular.php");
// foreach dir
// open dir
// for each file
//$file = $docketDir . "2010\\51\\CP\\1000\\CP-51-CR-0000011-2010.pdf";
//$file = $docketDir . "2009\\01\\CR\\1000\\CP-01-CR-0000012-2009.pdf";
$processDir = $GLOBALS['docketDir'];
$options = getopt("d::h::c::a::");
if (!empty($options["h"]))
{
print "Usage: php processDocketSheets.php [-d\"<directory name>\"] [-h]\n";
print "-d should be followed by a directory name where processing can start\n";
print "-c Means that you want continuous processing of the contDirectory";
print "-a Means that you want to process a directory with LOTS of files in it. This allows use of an iterator rather than reading everything into memory";
print "-h shows this message\n";
exit;
}
if (!empty($options["d"]))
getAndProcessFiles($options["d"]);;
if (!empty ($options["c"]))
processContinuous();
if (!empty ($options["a"]))
processLargeDir();
function getAndProcessFiles($dir)
{
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fullFileName = $dir . DIRECTORY_SEPARATOR . $file;
if(is_dir($fullFileName))
getAndProcessFiles($fullFileName);
else if (fnmatch("*pdf", $file))
processDocket($fullFileName);
}
}
}
}
function processLargeDir()
{
foreach (new DirectoryIterator($GLOBALS['contDocketDir']) as $fileInfo)
{
if ($fileInfo->isDot() || $fileInfo->isDir())
continue;
$file = $fileInfo->getPathname();
if (filesize($file) > 1)
{
processDocket($file);
// and then delete the file so that we don't reprocess it
unlink($file);
}
}
}
function processContinuous()
{
while ( $files = scandir($GLOBALS['contDocketDir']))
{
$processedfile = false;
foreach (range(3, count($files)) as $num)
{
$file = $GLOBALS['contDocketDir'] . DIRECTORY_SEPARATOR . $files[$num-1];
if (filesize($file) > 1)
{
processDocket($file);
// and then delete the file so that we don't reprocess it
unlink($file);
$processedfile = true;
}
else
print ".";
}
clearstatcache();
if (!$processedfile)
{
print ".";
sleep(5);
}
}
}
function processDocket($file)
{
$command = $GLOBALS['toolsDir'] . $GLOBALS['pdftotext'] . " -layout \"" . $file . "\" \"" . $GLOBALS['tempFile'] . "\"";
system($command, $ret);
if($GLOBALS['debug'])
print "\nThe pdftotext command: $command \n";
if ($ret == 0)
{
print "\n**************" . $file;
$thisDocket = file($GLOBALS['tempFile']);
$docket = new Docket();
$personID = null;
if ($docket->isDocketSheet($thisDocket[1]))
{
// if this is a regular docket sheet, use the regular parsing function
$docket->readArrestRecord($thisDocket);
$defendantID = null;
if (strpos($file, "#")!==FALSE)
{
$aDefendantID = explode("#", basename($file));
$defendantID = $aDefendantID[0];
}
$docket->writeDocketToDatabase($GLOBALS['db'], $defendantID);
if($GLOBALS['debug'])
$docket->simplePrint();
else
print "\nProcessing " . $docket->getDocketNumber();
}
// otherwise check if this is a summary docketSheet
if (ArrestSummary::isArrestSummaryFromLine($thisDocket[1]))
{
// and process accordingly
$arrestSummary = new ArrestSummary();
$arrestSummary->processArrestSummary($thisDocket);
// iterate over all the dockets; if any are archived dockets, download them so they can be later processed
// otherwise write the docket to the database
$ch = initConnection();
// we use the defendantID variable to store the db id of the defendant that we are adding to the DB so that the same
// defendant can be used over and over and over again.
$defendantID = null;
foreach($arrestSummary->getDockets() as $sDocket)
{
print "\nDefendantID: $defendantID";
// if we got all of the necessary information from the summary, just write to the DB
if (!$sDocket->getIsArchived())
$defendantID = $sDocket->writeDocketToDatabase($GLOBALS['db'], $defendantID);
// if not, then download the docket for later use
else
downloadDocketWithPrefix($ch, $sDocket->getDocketNumber(), $defendantID);
}
$defendantID = null; // null it out again, so that it doesn't infect later calls. This is probably unnecssary, but just in case
curl_close($ch);
}
}
}
?>