-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.Chichester.inc.php
537 lines (457 loc) · 12 KB
/
class.Chichester.inc.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
class Chichester
{
/**
* Fetch the HTML for a given URL.
*
* @requires $this->url
* @returns true or false
* @sets $this->html
*/
function fetch_html()
{
if (!isset($this->url))
{
throw new Exception('No URL has been provided.');
return FALSE;
}
if (filter_var($this->url, FILTER_VALIDATE_URL) === FALSE)
{
throw new Exception('The URL ' . $this->url . ' is invalid.');
return FALSE;
}
/*
* Via cURL, retrieve the contents of the URL.
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$allowed_protocols = CURLPROTO_HTTP | CURLPROTO_HTTPS;
curl_setopt($ch, CURLOPT_PROTOCOLS, $allowed_protocols);
curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $allowed_protocols & ~(CURLPROTO_FILE | CURLPROTO_SCP));
$this->html = curl_exec($ch);
/*
* If our query failed, then we can't continue.
*/
if ($this->html === FALSE)
{
throw new Exception('cURL could not retrieve content for ' . $this->url . ', with the '
.'following error: ' . curl_error($ch));
}
curl_close($ch);
/*
* This HTML is invalid. Clean it up with HTML Tidy.
*/
if (class_exists('tidy', FALSE))
{
$tidy = new tidy;
$tidy->parseString($this->html);
$tidy->cleanRepair();
$this->html = $tidy;
}
elseif (exec('which tidy'))
{
$filename = '/tmp/' . $period_id .'.tmp';
file_put_contents($filename, $this->html);
exec('tidy --show-errors 0 --show-warnings no -q -m ' . $filename);
$this->html = file_get_contents($filename);
unlink($filename);
}
return TRUE;
}
/*
* Turn HTML into a DOM-based object
*
* @requires $this->html
* @returns TRUE or FALSE
* @sets $this->dom
*/
function html_to_object()
{
if (!isset($this->html))
{
throw new Exception('No HTML has been provided.');
return FALSE;
}
/*
* Render this as an object with PHP Simple HTML DOM Parser.
*/
try
{
$this->dom = str_get_html($this->html);
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
return FALSE;
}
/*
* If this can't be rendered, then there's a serious HTML error.
*/
if ($this->dom === FALSE)
{
throw new Exception('Invalid HTML.');
return FALSE;
}
return TRUE;
}
/**
* Fetch and structure the table of contents for the entire Administrative Code.
*
* @requires nothing
* @returns TRUE or FALSE
* @sets $this->agencies
* @todo Intelligently title-case agency names.
*/
function parse_toc()
{
/*
* The base URL for each title's table of contents.
*/
$this->master_url = 'http://law.lis.virginia.gov/admincode/title';
/*
* There are 24 Titles. Iterate through them.
*/
for ($title=1; $title<=24; $title++)
{
/*
* The URL is the master URL, plus the title number.
*/
$this->url = $this->master_url .= $title;
try
{
$this->fetch_html();
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
return FALSE;
}
/*
* Convert the HTML to an object.
*/
try
{
$this->html_to_object();
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
return FALSE;
}
/*
* Iterate through the definitions. Each dt/dd pair is an agency.
*/
$i=0;
foreach ($this->dom->find('dt') as $agency)
{
/*
* Save each field.
*/
$this->agencies->{$i}->toc_id = str_pad($title, 2, '0', STR_PAD_LEFT)
. str_pad(str_replace('Agency ', '', $agency->plaintext), 2, '0', STR_PAD_LEFT);
$this->agencies->{$i}->number = trim(str_replace('Agency ', '', $agency->plaintext));
$this->agencies->{$i}->name = trim($agency->next_sibling);
/*
* Determine if this agency has been abolished. If it has been, strip that flag out of
* the agency title, with which it's comingled. Save that flag as its own variable.
*/
if (stristr($this->agencies->{$i}->name, '(Abolished)') !== FALSE)
{
$this->agencies->{$i}->abolished = TRUE;
$this->agencies->{$i}->name = trim(str_ireplace('(Abolished)', '', $this->agencies->{$i}->name));
}
else
{
$this->a
$i++;
}
}
return TRUE;
}
/**
* Fetch and structure the table of contents for a single agency.
*
* @requires $this->agency_id
* @returns TRUE or FALSE
* @sets
*/
function parse_agency()
{
/*
* We need the agency ID, as found in the URL.
*/
if (!isset($this->agency_id))
{
throw new Exception('No agency ID has been provided');
return FALSE;
}
/*
* Get the HTML of the page.
*/
$this->url = 'http://leg1.state.va.us/cgi-bin/legp504.exe?000+reg+TOC' . $this->agency_id;
$this->fetch_html();
/*
* Convert the HTML to an object.
*/
try
{
$this->html_to_object();
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
return FALSE;
}
/*
* Fetch each table row.
*/
$i=0;
foreach ($this->dom->find('tr') as $section)
{
/*
* If this cell's contents are suspiciously long, then we're looking at a table row used
* for page layout. Skip it.
*/
if (strlen($section->find('td', 0)->innertext) > 100 )
{
continue;
}
/*
* See if this is a table row that we actually want. We look at the first cell to
* determine this, and see if it's a section, a collection of forms, or a "DIBR"—a
* list of documents included by reference.
*/
$td_1 = $section->find('td', 0)->plaintext;
/*
* Is this a section, a collection of forms, or documents included by reference? If
* it's none of those things, then skip it.
*/
if (stristr($td_1, 'section') !== FALSE)
{
$entry_type = 'section';
}
elseif (stristr($td_1, 'forms') !== FALSE)
{
$entry_type = 'forms';
}
elseif (stristr($td_1, 'dibr') !== FALSE)
{
$entry_type = 'dibr';
}
else
{
continue;
}
/*
* Save the type of entry that this is.
*/
$this->sections->{$i}->type = $entry_type;
/*
* Save the official URL for this section.
*/
$this->sections->{$i}->official_url = 'http://leg1.state.va.us'
. $section->find('td', 0)->find('a', 0)->href;
/*
* Save the section number (e.g., "2VAC5-585-2500"). We extract this from the URL.
*/
$this->sections->{$i}->section_number = str_replace('http://leg1.state.va.us/cgi-bin/legp504.exe?000+reg+',
'', $this->sections->{$i}->official_url);
/*
* Save the section catch line.
*/
$this->sections->{$i}->catch_line = trim($section->find('td', 1)->plaintext);
/*
* If the catch line contains "[Repealed]" then strip that text out of the catch line
* and mark it as repealed.
*/
if (stristr($this->sections->{$i}->catch_line, '[Repealed]') !== FALSE)
{
$this->sections->{$i}->catch_line = trim(str_replace('[Repealed]', '', $this->sections->{$i}->catch_line));
$this->sections->{$i}->repealed = TRUE;
$this->sections->{$i}->removed = FALSE;
}
/*
* And if the catch line
*/
elseif ($this->sections->{$i}->catch_line == '[Removed]')
{
unset($this->sections->{$i}->catch_line);
$this->sections->{$i}->removed = TRUE;
$this->sections->{$i}->repealed = FALSE;
}
else
{
$this->sections->{$i}->repealed = FALSE;
$this->sections->{$i}->removed = FALSE;
}
$i++;
}
return TRUE;
}
/**
* Fetch and structure the contents of a single section.
*
* @requires $this->html
* @returns TRUE or FALSE
* @sets TBD
* @todo // use <https://github.com/statedecoded/subsection-identifier>
*/
function parse_section()
{
/*
* Convert the HTML to an object.
*/
try
{
$this->html_to_object();
}
catch (Exception $e)
{
throw new Exception($e->getMessage());
return FALSE;
}
/*
* Save the section number and the catch line.
*
* For example:
* <p class=vacno>16VAC30-12-40. Information to be sent to persons on the list.</p>
*/
$tmp = $this->dom->find('p.vacno', 0);
$tmp = $tmp->plaintext;
/*
* If the section number and catch line are found in the HTML, extract it.
*/
if (!empty($tmp))
{
$pos = strpos($tmp, '. ');
$this->section->section_number = substr($tmp, 0, $pos);
$this->section->catch_line = html_entity_decode(substr($tmp, ($pos+2)));
}
/*
* But if the section number and catch line aren't found in the HTML, then this is a form
* list or a DIBR list. Get the pseudo-section-number from the URL.
*/
else
{
$components = explode('+', $this->url);
$this->section->section_number = $components[2];
}
/*
* Break the section number into its constitutents, to extract its ancestry (title, agency,
* and chapter).
*/
preg_match('/([0-9]{1,2})VAC([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,4})/', $this->section->section_number, $parts);
/*
* Produce an object containing all of our structural data.
*/
$structure = array(
1 => 'title',
2 => 'agency',
3 => 'chapter',
4 => 'section');
$this->section->structure = new stdClass();
$i=1;
foreach ($structure as $number => $label)
{
$tmp = new stdClass();
$tmp->label = $label;
$tmp->identifier = $parts[$number];
$tmp->TO_BE_NAMED = 'TO_BE_DEFINED';
$tmp->level = $parts[$i];
$this->section->structure->unit[] = $tmp;
$i++;
}
/*
* Find all "parts." I have no idea what this means.
*/
$this->dom->find('p.part');
/*
* Save the statutory authority under which this regulation was established. This may be
* multiple paragraphs, so we build it up with a loop.
*/
$this->section->authority = '';
foreach ($this->dom->find('p.auth') as $auth)
{
/*
* Skip the section header (which is just a P tag).
*/
if ($auth->plaintext == 'Statutory Authority')
{
continue;
}
$this->section->authority .= $auth->plaintext;
}
/*
* Convert HTML entities to Unicode.
*/
$this->section->authority = html_entity_decode($this->section->authority);
/*
* Save the actual text of this regulation. We do this by getting a list of all paragraphs
* with a class that starts with "sect," since we know that sections of text can have the
* class "sectind" or "sectbi", which means it's possible that othe classes are lurking out
* there somewhere.
*/
$this->section->text = '';
foreach ($this->dom->find('p[class^=sect]') as $text)
{
$this->section->text .= $text->innertext . "\n\n";
}
/*
* If we didn't get any text, eliminate the variable.
*/
$this->section->text = trim($this->section->text);
if (empty($this->section->text))
{
unset($this->section->text);
}
/*
* If we got any text.
*/
if (!empty($this->section->text))
{
/*
* Convert HTML entities to Unicode.
*/
$this->section->text = html_entity_decode($this->section->text);
/*
* Take our unstructured text and give it structure.
*/
$structurer = new SubsectionIdentifier();
$structurer->text = $this->section->text;
$structurer->parse();
$this->section->text = $structurer->structured;
}
/*
* Save the history of the establishment of and modifications to this regulation.
*/
$this->section->history = '';
foreach ($this->dom->find('p[class^=history]') as $history)
{
/*
* Skip the section header (which is just a P tag).
*/
if ($history->innertext == 'Historical Notes')
{
continue;
}
$this->section->history .= $history->innertext . "\n\n";
}
/*
* Convert HTML entities to Unicode.
*/
$this->section->history = html_entity_decode($this->section->history);
/*
* If we didn't get any history data, eliminate the variable.
*/
$this->section->history = trim($this->section->history);
if (empty($this->section->history))
{
unset($this->section->history);
}
return TRUE;
}
}