Overview

Packages

  • LegiScan
    • API
    • Cache
    • Common
    • Exceptions
    • Utility
  • PHP

Classes

  • LegiScan
  • LegiScan_Bulk
  • LegiScan_Cache_File
  • LegiScan_Cache_Memory
  • LegiScan_CommandLine
  • LegiScan_Endpoint
  • LegiScan_Import
  • LegiScan_Process
  • LegiScan_Pull
  • LegiScan_Push
  • LegiScan_WebTest
  • LegiScan_Worker
  • Overview
  • Package
  • Class
   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:  538:  539:  540:  541:  542:  543:  544:  545:  546:  547:  548:  549:  550:  551:  552:  553:  554:  555:  556:  557:  558:  559:  560:  561:  562:  563:  564:  565:  566:  567:  568:  569:  570:  571:  572:  573:  574:  575:  576:  577:  578:  579:  580:  581:  582:  583:  584:  585:  586:  587:  588:  589:  590:  591:  592:  593:  594:  595:  596:  597:  598:  599:  600:  601:  602:  603:  604:  605:  606:  607:  608:  609:  610:  611:  612:  613:  614:  615:  616:  617:  618:  619:  620:  621:  622:  623:  624:  625:  626:  627:  628:  629:  630:  631:  632:  633:  634:  635:  636:  637:  638:  639:  640:  641:  642:  643:  644:  645:  646:  647:  648:  649:  650:  651:  652:  653:  654:  655:  656:  657:  658:  659:  660:  661:  662:  663:  664:  665:  666:  667:  668:  669:  670:  671:  672:  673:  674:  675:  676:  677:  678:  679:  680:  681:  682:  683:  684:  685:  686:  687:  688:  689:  690:  691:  692:  693:  694:  695:  696:  697:  698:  699:  700:  701:  702:  703:  704:  705:  706:  707:  708:  709:  710:  711:  712:  713:  714:  715:  716:  717:  718:  719:  720:  721:  722:  723:  724:  725:  726:  727:  728:  729:  730:  731:  732:  733:  734:  735:  736:  737:  738:  739:  740:  741:  742:  743:  744:  745:  746:  747:  748:  749:  750:  751:  752:  753:  754:  755:  756:  757:  758:  759:  760:  761:  762:  763:  764:  765:  766:  767:  768:  769:  770:  771:  772:  773:  774:  775:  776:  777:  778:  779:  780:  781:  782:  783:  784:  785:  786:  787:  788:  789:  790:  791:  792:  793:  794:  795:  796:  797:  798:  799:  800:  801:  802:  803:  804:  805:  806:  807:  808:  809:  810:  811:  812:  813:  814:  815:  816:  817:  818:  819:  820:  821:  822:  823:  824:  825:  826:  827:  828:  829:  830:  831:  832:  833:  834:  835:  836:  837:  838:  839:  840:  841:  842:  843:  844:  845:  846:  847:  848:  849:  850:  851:  852:  853:  854:  855:  856:  857:  858:  859:  860:  861:  862:  863:  864:  865:  866:  867:  868:  869:  870:  871:  872:  873:  874:  875:  876:  877:  878:  879:  880:  881:  882:  883:  884:  885:  886:  887:  888:  889:  890:  891:  892:  893:  894:  895:  896:  897:  898:  899:  900:  901:  902:  903:  904:  905:  906:  907:  908:  909:  910:  911:  912:  913:  914:  915:  916:  917:  918:  919:  920:  921:  922:  923:  924:  925:  926:  927:  928:  929:  930:  931:  932:  933:  934:  935:  936:  937:  938:  939:  940:  941:  942:  943:  944:  945:  946:  947:  948:  949:  950:  951:  952:  953:  954:  955:  956:  957:  958:  959:  960:  961:  962:  963:  964:  965:  966:  967:  968:  969:  970:  971:  972:  973:  974:  975:  976:  977:  978:  979:  980:  981:  982:  983:  984:  985:  986:  987:  988:  989:  990:  991:  992:  993:  994:  995:  996:  997:  998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198: 1199: 1200: 1201: 1202: 1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286: 1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317: 1318: 1319: 1320: 1321: 1322: 1323: 1324: 1325: 1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360: 1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370: 1371: 1372: 1373: 1374: 1375: 1376: 1377: 1378: 1379: 1380: 1381: 1382: 1383: 1384: 1385: 1386: 1387: 1388: 1389: 1390: 1391: 1392: 1393: 1394: 1395: 1396: 1397: 1398: 1399: 1400: 1401: 1402: 1403: 1404: 1405: 1406: 1407: 1408: 1409: 1410: 1411: 1412: 1413: 1414: 1415: 1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423: 1424: 1425: 1426: 1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434: 1435: 1436: 1437: 1438: 1439: 1440: 1441: 1442: 1443: 1444: 1445: 1446: 1447: 1448: 1449: 1450: 1451: 1452: 1453: 1454: 1455: 1456: 1457: 1458: 1459: 1460: 1461: 1462: 1463: 1464: 1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474: 1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482: 1483: 1484: 1485: 1486: 1487: 1488: 1489: 1490: 1491: 1492: 1493: 1494: 1495: 1496: 1497: 1498: 1499: 1500: 1501: 1502: 1503: 1504: 1505: 1506: 1507: 1508: 1509: 1510: 1511: 1512: 1513: 1514: 1515: 1516: 1517: 1518: 1519: 1520: 1521: 1522: 1523: 1524: 1525: 1526: 1527: 1528: 1529: 1530: 1531: 1532: 1533: 1534: 1535: 1536: 1537: 1538: 1539: 1540: 1541: 1542: 1543: 1544: 1545: 1546: 1547: 1548: 1549: 1550: 1551: 1552: 1553: 1554: 1555: 1556: 1557: 1558: 1559: 1560: 1561: 1562: 1563: 1564: 1565: 1566: 1567: 1568: 1569: 1570: 1571: 1572: 1573: 1574: 1575: 1576: 1577: 1578: 1579: 1580: 1581: 1582: 1583: 1584: 1585: 1586: 1587: 1588: 1589: 1590: 1591: 1592: 1593: 1594: 1595: 1596: 1597: 1598: 1599: 1600: 1601: 1602: 1603: 1604: 1605: 1606: 1607: 1608: 1609: 1610: 1611: 1612: 1613: 1614: 1615: 1616: 1617: 1618: 1619: 1620: 1621: 1622: 1623: 1624: 1625: 1626: 1627: 1628: 1629: 1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640: 1641: 1642: 1643: 1644: 1645: 1646: 1647: 1648: 1649: 1650: 1651: 1652: 1653: 1654: 1655: 1656: 1657: 1658: 1659: 1660: 1661: 1662: 1663: 1664: 1665: 1666: 1667: 1668: 1669: 1670: 1671: 1672: 1673: 1674: 1675: 1676: 1677: 1678: 1679: 1680: 1681: 1682: 1683: 1684: 1685: 1686: 1687: 1688: 1689: 1690: 1691: 1692: 1693: 1694: 1695: 1696: 1697: 1698: 1699: 1700: 1701: 1702: 1703: 1704: 1705: 1706: 1707: 1708: 1709: 1710: 1711: 1712: 1713: 1714: 1715: 1716: 1717: 1718: 1719: 1720: 1721: 1722: 1723: 1724: 1725: 1726: 1727: 1728: 1729: 1730: 1731: 1732: 1733: 1734: 1735: 1736: 1737: 1738: 1739: 1740: 1741: 1742: 1743: 1744: 1745: 1746: 1747: 1748: 1749: 1750: 1751: 1752: 1753: 1754: 1755: 1756: 1757: 1758: 1759: 1760: 1761: 1762: 1763: 1764: 1765: 1766: 1767: 1768: 1769: 1770: 1771: 1772: 1773: 1774: 1775: 1776: 1777: 1778: 1779: 1780: 1781: 1782: 1783: 1784: 1785: 1786: 1787: 1788: 1789: 1790: 1791: 1792: 1793: 1794: 1795: 1796: 1797: 1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805: 1806: 1807: 1808: 1809: 1810: 1811: 1812: 1813: 1814: 1815: 1816: 1817: 1818: 1819: 1820: 1821: 1822: 1823: 1824: 1825: 1826: 1827: 1828: 1829: 1830: 1831: 1832: 1833: 1834: 1835: 1836: 1837: 1838: 1839: 1840: 1841: 1842: 1843: 1844: 1845: 1846: 1847: 1848: 1849: 1850: 1851: 1852: 1853: 1854: 1855: 1856: 1857: 1858: 1859: 1860: 1861: 1862: 1863: 1864: 1865: 1866: 1867: 1868: 1869: 1870: 1871: 1872: 1873: 1874: 1875: 1876: 1877: 1878: 1879: 1880: 1881: 1882: 1883: 1884: 1885: 1886: 1887: 1888: 1889: 1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897: 1898: 1899: 1900: 1901: 1902: 1903: 1904: 1905: 1906: 1907: 1908: 1909: 1910: 1911: 1912: 1913: 1914: 1915: 1916: 1917: 1918: 1919: 1920: 1921: 1922: 1923: 1924: 1925: 1926: 1927: 1928: 1929: 1930: 1931: 1932: 1933: 1934: 1935: 1936: 1937: 1938: 1939: 1940: 1941: 1942: 1943: 1944: 1945: 1946: 1947: 1948: 1949: 1950: 1951: 1952: 1953: 1954: 1955: 1956: 1957: 1958: 1959: 1960: 1961: 1962: 1963: 1964: 1965: 1966: 1967: 1968: 1969: 1970: 1971: 1972: 1973: 1974: 1975: 1976: 1977: 1978: 1979: 1980: 1981: 1982: 1983: 1984: 1985: 1986: 1987: 1988: 1989: 1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003: 2004: 2005: 2006: 2007: 2008: 2009: 2010: 2011: 2012: 2013: 2014: 2015: 2016: 2017: 2018: 2019: 2020: 2021: 2022: 2023: 2024: 2025: 2026: 2027: 2028: 2029: 2030: 2031: 2032: 2033: 2034: 2035: 2036: 2037: 2038: 2039: 2040: 2041: 2042: 2043: 2044: 2045: 2046: 2047: 2048: 2049: 2050: 2051: 2052: 2053: 2054: 2055: 2056: 2057: 2058: 2059: 2060: 2061: 2062: 2063: 2064: 2065: 2066: 2067: 2068: 2069: 2070: 2071: 2072: 2073: 2074: 2075: 2076: 2077: 2078: 2079: 2080: 2081: 2082: 2083: 2084: 2085: 2086: 2087: 2088: 2089: 2090: 2091: 2092: 2093: 2094: 2095: 2096: 2097: 2098: 2099: 2100: 2101: 2102: 2103: 2104: 2105: 2106: 2107: 2108: 2109: 2110: 2111: 2112: 2113: 2114: 2115: 2116: 2117: 2118: 2119: 2120: 2121: 2122: 2123: 2124: 2125: 2126: 2127: 2128: 2129: 2130: 2131: 2132: 2133: 2134: 2135: 2136: 2137: 2138: 2139: 2140: 2141: 2142: 2143: 2144: 2145: 2146: 2147: 2148: 2149: 2150: 2151: 2152: 2153: 2154: 2155: 2156: 2157: 2158: 2159: 2160: 2161: 2162: 2163: 2164: 2165: 2166: 2167: 2168: 2169: 2170: 2171: 2172: 2173: 2174: 2175: 2176: 2177: 2178: 2179: 2180: 2181: 2182: 2183: 2184: 2185: 2186: 2187: 2188: 2189: 2190: 2191: 2192: 2193: 2194: 2195: 2196: 2197: 2198: 2199: 2200: 2201: 2202: 2203: 2204: 2205: 2206: 2207: 2208: 2209: 2210: 2211: 2212: 2213: 2214: 2215: 2216: 2217: 2218: 2219: 2220: 2221: 2222: 2223: 2224: 2225: 2226: 2227: 2228: 2229: 2230: 2231: 2232: 2233: 2234: 2235: 2236: 2237: 2238: 2239: 2240: 2241: 2242: 2243: 2244: 2245: 2246: 2247: 2248: 2249: 2250: 2251: 2252: 2253: 2254: 2255: 2256: 2257: 2258: 2259: 2260: 2261: 2262: 2263: 2264: 2265: 2266: 2267: 2268: 2269: 2270: 2271: 2272: 2273: 2274: 2275: 2276: 2277: 2278: 2279: 2280: 2281: 2282: 2283: 2284: 2285: 2286: 2287: 2288: 2289: 2290: 2291: 2292: 2293: 2294: 2295: 2296: 2297: 2298: 2299: 2300: 2301: 2302: 2303: 2304: 2305: 2306: 2307: 2308: 2309: 2310: 2311: 2312: 2313: 2314: 2315: 2316: 2317: 2318: 2319: 2320: 2321: 2322: 2323: 2324: 2325: 2326: 2327: 2328: 2329: 2330: 2331: 2332: 2333: 2334: 2335: 2336: 2337: 2338: 2339: 2340: 2341: 2342: 2343: 2344: 2345: 2346: 2347: 2348: 2349: 2350: 2351: 2352: 2353: 2354: 2355: 2356: 2357: 2358: 2359: 2360: 2361: 2362: 2363: 2364: 2365: 2366: 2367: 2368: 2369: 2370: 2371: 2372: 2373: 2374: 2375: 2376: 2377: 2378: 2379: 2380: 2381: 2382: 2383: 2384: 2385: 2386: 2387: 2388: 2389: 2390: 2391: 2392: 2393: 2394: 2395: 2396: 2397: 2398: 2399: 2400: 2401: 2402: 2403: 2404: 2405: 2406: 2407: 2408: 2409: 2410: 2411: 2412: 2413: 2414: 2415: 2416: 2417: 2418: 2419: 2420: 2421: 2422: 2423: 2424: 2425: 2426: 2427: 2428: 2429: 2430: 2431: 2432: 2433: 2434: 2435: 2436: 2437: 2438: 2439: 2440: 2441: 2442: 2443: 2444: 2445: 2446: 2447: 2448: 2449: 2450: 2451: 2452: 2453: 2454: 2455: 2456: 2457: 2458: 2459: 2460: 2461: 2462: 2463: 2464: 2465: 2466: 2467: 2468: 2469: 2470: 2471: 2472: 2473: 2474: 2475: 2476: 2477: 2478: 2479: 2480: 2481: 2482: 2483: 2484: 2485: 2486: 2487: 2488: 2489: 2490: 2491: 2492: 2493: 2494: 2495: 2496: 2497: 2498: 2499: 2500: 2501: 2502: 2503: 2504: 2505: 2506: 2507: 2508: 2509: 2510: 2511: 2512: 2513: 2514: 2515: 2516: 2517: 2518: 2519: 2520: 2521: 2522: 2523: 2524: 2525: 2526: 2527: 2528: 2529: 2530: 2531: 2532: 2533: 2534: 2535: 2536: 2537: 2538: 2539: 2540: 2541: 2542: 2543: 2544: 2545: 2546: 2547: 2548: 2549: 2550: 2551: 2552: 2553: 2554: 2555: 2556: 2557: 2558: 2559: 2560: 2561: 2562: 2563: 2564: 2565: 2566: 2567: 2568: 2569: 2570: 2571: 2572: 2573: 2574: 2575: 2576: 2577: 2578: 2579: 2580: 2581: 2582: 2583: 2584: 2585: 2586: 2587: 2588: 2589: 2590: 2591: 2592: 2593: 2594: 2595: 2596: 2597: 2598: 2599: 2600: 2601: 2602: 2603: 2604: 2605: 2606: 2607: 2608: 2609: 2610: 2611: 2612: 2613: 2614: 2615: 2616: 2617: 2618: 2619: 2620: 2621: 2622: 2623: 2624: 2625: 2626: 2627: 2628: 2629: 2630: 2631: 2632: 2633: 2634: 2635: 2636: 2637: 2638: 2639: 2640: 2641: 2642: 2643: 2644: 2645: 2646: 2647: 2648: 2649: 2650: 2651: 2652: 2653: 2654: 2655: 2656: 2657: 2658: 2659: 2660: 2661: 2662: 2663: 2664: 2665: 2666: 2667: 2668: 2669: 2670: 2671: 2672: 2673: 2674: 2675: 2676: 2677: 2678: 2679: 2680: 2681: 2682: 2683: 2684: 2685: 2686: 2687: 2688: 2689: 2690: 2691: 2692: 2693: 2694: 2695: 2696: 2697: 2698: 2699: 2700: 2701: 2702: 2703: 2704: 2705: 2706: 2707: 2708: 2709: 2710: 2711: 2712: 2713: 2714: 2715: 2716: 2717: 2718: 2719: 2720: 2721: 2722: 2723: 2724: 2725: 2726: 2727: 2728: 2729: 2730: 2731: 2732: 2733: 2734: 2735: 2736: 2737: 2738: 2739: 2740: 2741: 2742: 2743: 2744: 2745: 2746: 2747: 2748: 2749: 2750: 2751: 2752: 2753: 2754: 2755: 2756: 2757: 2758: 2759: 2760: 2761: 2762: 2763: 2764: 2765: 2766: 2767: 2768: 2769: 2770: 2771: 2772: 2773: 2774: 2775: 2776: 2777: 2778: 2779: 2780: 2781: 2782: 2783: 2784: 2785: 2786: 2787: 2788: 2789: 2790: 2791: 2792: 2793: 2794: 2795: 2796: 2797: 2798: 2799: 2800: 2801: 2802: 2803: 2804: 2805: 2806: 2807: 2808: 2809: 2810: 2811: 2812: 2813: 2814: 2815: 2816: 2817: 2818: 2819: 2820: 2821: 2822: 2823: 2824: 2825: 2826: 2827: 2828: 2829: 2830: 2831: 2832: 2833: 2834: 2835: 2836: 2837: 2838: 2839: 2840: 2841: 2842: 2843: 2844: 2845: 2846: 2847: 2848: 2849: 2850: 2851: 2852: 2853: 2854: 2855: 2856: 2857: 2858: 2859: 2860: 2861: 2862: 2863: 2864: 2865: 2866: 2867: 2868: 2869: 2870: 2871: 2872: 2873: 2874: 2875: 2876: 2877: 2878: 2879: 2880: 2881: 2882: 2883: 2884: 2885: 2886: 2887: 2888: 2889: 2890: 2891: 2892: 2893: 2894: 2895: 2896: 2897: 2898: 2899: 2900: 2901: 2902: 2903: 2904: 2905: 2906: 2907: 2908: 2909: 2910: 2911: 2912: 2913: 2914: 2915: 2916: 2917: 2918: 2919: 2920: 2921: 2922: 2923: 2924: 2925: 2926: 2927: 2928: 2929: 2930: 2931: 2932: 2933: 2934: 2935: 2936: 2937: 2938: 2939: 2940: 2941: 2942: 2943: 2944: 2945: 2946: 2947: 2948: 2949: 2950: 2951: 2952: 2953: 2954: 2955: 2956: 2957: 2958: 2959: 2960: 2961: 2962: 2963: 2964: 2965: 2966: 2967: 2968: 2969: 2970: 2971: 2972: 2973: 2974: 2975: 2976: 2977: 2978: 2979: 2980: 2981: 2982: 2983: 2984: 2985: 2986: 2987: 2988: 2989: 2990: 2991: 2992: 2993: 2994: 2995: 2996: 2997: 2998: 2999: 3000: 3001: 3002: 3003: 3004: 3005: 3006: 3007: 3008: 3009: 3010: 3011: 3012: 3013: 3014: 3015: 3016: 3017: 3018: 3019: 3020: 3021: 3022: 3023: 3024: 3025: 3026: 3027: 3028: 3029: 3030: 3031: 3032: 3033: 3034: 3035: 3036: 3037: 3038: 3039: 3040: 3041: 3042: 3043: 3044: 3045: 3046: 3047: 3048: 3049: 3050: 3051: 3052: 3053: 3054: 3055: 3056: 3057: 3058: 3059: 3060: 3061: 3062: 3063: 3064: 3065: 3066: 3067: 3068: 3069: 3070: 3071: 3072: 3073: 3074: 3075: 3076: 3077: 3078: 3079: 3080: 3081: 3082: 3083: 3084: 3085: 3086: 3087: 3088: 3089: 3090: 3091: 3092: 3093: 3094: 3095: 3096: 3097: 3098: 3099: 3100: 3101: 3102: 3103: 3104: 3105: 3106: 3107: 3108: 3109: 3110: 3111: 3112: 3113: 3114: 3115: 3116: 3117: 3118: 3119: 3120: 3121: 3122: 3123: 3124: 3125: 3126: 3127: 3128: 3129: 3130: 3131: 3132: 3133: 3134: 3135: 3136: 3137: 3138: 3139: 3140: 3141: 3142: 3143: 3144: 3145: 3146: 3147: 3148: 3149: 3150: 3151: 3152: 3153: 3154: 3155: 3156: 3157: 3158: 3159: 3160: 3161: 3162: 3163: 3164: 3165: 3166: 3167: 3168: 3169: 3170: 3171: 3172: 3173: 3174: 3175: 3176: 3177: 3178: 3179: 3180: 3181: 3182: 3183: 3184: 3185: 3186: 3187: 3188: 3189: 3190: 3191: 3192: 3193: 3194: 3195: 3196: 3197: 3198: 3199: 3200: 3201: 3202: 3203: 3204: 3205: 3206: 3207: 3208: 3209: 3210: 3211: 3212: 3213: 3214: 3215: 3216: 3217: 3218: 3219: 3220: 3221: 3222: 3223: 3224: 3225: 3226: 3227: 3228: 3229: 3230: 3231: 3232: 3233: 3234: 3235: 3236: 3237: 3238: 3239: 3240: 3241: 3242: 3243: 3244: 3245: 3246: 3247: 3248: 3249: 3250: 3251: 3252: 3253: 3254: 3255: 3256: 3257: 3258: 3259: 3260: 3261: 3262: 3263: 3264: 3265: 3266: 3267: 3268: 3269: 3270: 3271: 3272: 3273: 3274: 3275: 3276: 3277: 3278: 3279: 3280: 3281: 3282: 3283: 3284: 3285: 3286: 3287: 3288: 3289: 3290: 3291: 3292: 3293: 3294: 3295: 3296: 3297: 3298: 3299: 3300: 3301: 3302: 3303: 3304: 3305: 3306: 3307: 3308: 3309: 3310: 3311: 3312: 3313: 3314: 3315: 3316: 3317: 3318: 3319: 3320: 3321: 3322: 3323: 3324: 3325: 3326: 3327: 3328: 3329: 3330: 3331: 3332: 3333: 3334: 3335: 3336: 3337: 3338: 3339: 3340: 3341: 3342: 3343: 3344: 3345: 3346: 3347: 3348: 3349: 3350: 3351: 3352: 3353: 3354: 3355: 3356: 3357: 3358: 3359: 3360: 3361: 3362: 3363: 3364: 3365: 3366: 3367: 3368: 3369: 3370: 3371: 3372: 3373: 3374: 3375: 3376: 3377: 3378: 3379: 3380: 3381: 3382: 3383: 3384: 3385: 3386: 3387: 3388: 3389: 3390: 3391: 3392: 3393: 3394: 3395: 3396: 3397: 3398: 3399: 3400: 3401: 3402: 3403: 3404: 3405: 3406: 3407: 3408: 3409: 3410: 3411: 3412: 3413: 3414: 3415: 3416: 3417: 3418: 3419: 3420: 3421: 3422: 3423: 3424: 3425: 3426: 3427: 3428: 3429: 3430: 3431: 3432: 3433: 3434: 3435: 3436: 3437: 3438: 3439: 3440: 3441: 3442: 3443: 3444: 3445: 3446: 3447: 3448: 3449: 3450: 3451: 3452: 3453: 3454: 3455: 3456: 3457: 3458: 3459: 3460: 3461: 3462: 3463: 3464: 3465: 3466: 3467: 3468: 3469: 3470: 3471: 3472: 3473: 3474: 3475: 3476: 3477: 3478: 3479: 3480: 3481: 3482: 3483: 3484: 3485: 3486: 3487: 3488: 3489: 3490: 3491: 3492: 3493: 3494: 3495: 3496: 3497: 3498: 3499: 3500: 3501: 3502: 3503: 3504: 3505: 3506: 3507: 3508: 3509: 3510: 3511: 3512: 3513: 3514: 3515: 3516: 3517: 3518: 3519: 3520: 3521: 3522: 3523: 3524: 3525: 3526: 3527: 3528: 3529: 3530: 3531: 3532: 3533: 3534: 3535: 3536: 3537: 3538: 3539: 3540: 3541: 3542: 3543: 3544: 3545: 3546: 3547: 3548: 3549: 3550: 3551: 3552: 3553: 3554: 3555: 3556: 3557: 3558: 3559: 3560: 3561: 3562: 3563: 3564: 3565: 3566: 3567: 3568: 3569: 3570: 3571: 3572: 3573: 3574: 3575: 3576: 3577: 3578: 3579: 3580: 3581: 3582: 3583: 3584: 3585: 3586: 3587: 3588: 3589: 3590: 3591: 3592: 3593: 3594: 3595: 3596: 3597: 3598: 3599: 3600: 3601: 3602: 3603: 3604: 3605: 3606: 3607: 3608: 3609: 3610: 3611: 3612: 3613: 3614: 3615: 3616: 3617: 3618: 3619: 3620: 3621: 3622: 3623: 3624: 3625: 3626: 3627: 3628: 3629: 3630: 3631: 3632: 3633: 3634: 3635: 3636: 3637: 3638: 3639: 3640: 3641: 3642: 3643: 3644: 3645: 3646: 3647: 3648: 3649: 3650: 3651: 3652: 3653: 3654: 3655: 3656: 3657: 3658: 3659: 3660: 3661: 3662: 3663: 3664: 3665: 3666: 3667: 3668: 3669: 3670: 3671: 3672: 3673: 3674: 3675: 3676: 3677: 3678: 3679: 3680: 3681: 3682: 3683: 3684: 3685: 3686: 3687: 3688: 3689: 3690: 3691: 3692: 3693: 3694: 3695: 3696: 3697: 3698: 3699: 3700: 3701: 3702: 3703: 3704: 3705: 3706: 3707: 3708: 3709: 3710: 3711: 3712: 3713: 3714: 3715: 3716: 3717: 3718: 3719: 3720: 3721: 3722: 3723: 3724: 3725: 3726: 3727: 3728: 3729: 3730: 3731: 3732: 3733: 3734: 3735: 3736: 3737: 3738: 3739: 3740: 3741: 3742: 3743: 3744: 3745: 3746: 3747: 3748: 3749: 3750: 3751: 3752: 3753: 3754: 3755: 3756: 3757: 3758: 3759: 3760: 3761: 3762: 3763: 3764: 3765: 3766: 3767: 3768: 3769: 3770: 3771: 3772: 3773: 3774: 3775: 3776: 3777: 3778: 3779: 3780: 3781: 3782: 3783: 3784: 3785: 3786: 3787: 3788: 3789: 3790: 3791: 3792: 3793: 3794: 3795: 3796: 3797: 3798: 3799: 3800: 3801: 3802: 3803: 3804: 3805: 3806: 3807: 3808: 3809: 3810: 3811: 3812: 3813: 3814: 3815: 3816: 3817: 3818: 3819: 3820: 3821: 3822: 3823: 3824: 3825: 3826: 3827: 3828: 3829: 3830: 3831: 3832: 3833: 3834: 3835: 3836: 3837: 3838: 3839: 3840: 3841: 3842: 3843: 3844: 3845: 3846: 3847: 3848: 3849: 3850: 3851: 3852: 3853: 3854: 3855: 3856: 3857: 3858: 3859: 3860: 3861: 3862: 3863: 3864: 3865: 3866: 3867: 3868: 3869: 3870: 3871: 3872: 3873: 3874: 3875: 3876: 3877: 3878: 3879: 3880: 3881: 3882: 3883: 3884: 3885: 3886: 3887: 3888: 3889: 3890: 3891: 3892: 3893: 3894: 3895: 3896: 3897: 3898: 3899: 3900: 3901: 3902: 3903: 3904: 3905: 3906: 3907: 3908: 3909: 3910: 3911: 3912: 3913: 3914: 3915: 3916: 3917: 3918: 3919: 3920: 3921: 3922: 3923: 3924: 3925: 3926: 3927: 3928: 3929: 3930: 3931: 3932: 3933: 3934: 3935: 3936: 3937: 3938: 3939: 3940: 3941: 3942: 3943: 3944: 3945: 3946: 3947: 3948: 3949: 3950: 3951: 3952: 3953: 3954: 3955: 3956: 3957: 3958: 3959: 3960: 3961: 3962: 3963: 3964: 3965: 3966: 3967: 3968: 3969: 3970: 3971: 3972: 3973: 3974: 3975: 3976: 3977: 3978: 3979: 3980: 3981: 3982: 3983: 3984: 3985: 3986: 3987: 3988: 3989: 3990: 3991: 3992: 3993: 3994: 3995: 3996: 3997: 3998: 3999: 4000: 4001: 4002: 4003: 4004: 4005: 4006: 4007: 4008: 4009: 4010: 4011: 4012: 4013: 4014: 4015: 4016: 4017: 4018: 4019: 4020: 4021: 4022: 4023: 4024: 4025: 4026: 4027: 4028: 4029: 4030: 4031: 4032: 4033: 4034: 4035: 4036: 4037: 4038: 4039: 4040: 4041: 4042: 4043: 4044: 4045: 4046: 4047: 4048: 4049: 4050: 4051: 4052: 4053: 4054: 4055: 4056: 4057: 4058: 4059: 4060: 4061: 4062: 4063: 4064: 4065: 4066: 4067: 4068: 4069: 4070: 4071: 4072: 4073: 4074: 4075: 4076: 4077: 4078: 4079: 4080: 4081: 4082: 4083: 4084: 4085: 4086: 4087: 4088: 4089: 4090: 4091: 4092: 4093: 4094: 4095: 4096: 4097: 4098: 4099: 4100: 4101: 4102: 4103: 4104: 4105: 4106: 4107: 4108: 4109: 4110: 4111: 4112: 4113: 4114: 4115: 4116: 4117: 4118: 4119: 4120: 4121: 4122: 4123: 4124: 4125: 4126: 4127: 4128: 4129: 4130: 4131: 4132: 4133: 4134: 4135: 4136: 4137: 4138: 4139: 4140: 4141: 4142: 4143: 4144: 4145: 4146: 4147: 4148: 4149: 4150: 4151: 4152: 4153: 4154: 4155: 4156: 4157: 4158: 4159: 4160: 4161: 4162: 4163: 4164: 4165: 4166: 4167: 4168: 4169: 4170: 4171: 4172: 4173: 4174: 4175: 4176: 4177: 4178: 4179: 4180: 4181: 4182: 4183: 4184: 4185: 4186: 4187: 4188: 4189: 4190: 4191: 4192: 4193: 4194: 4195: 4196: 4197: 4198: 4199: 4200: 4201: 4202: 4203: 4204: 4205: 4206: 4207: 4208: 4209: 4210: 4211: 4212: 4213: 4214: 4215: 4216: 4217: 4218: 4219: 4220: 4221: 4222: 4223: 4224: 4225: 4226: 4227: 4228: 4229: 4230: 4231: 4232: 4233: 4234: 4235: 4236: 4237: 4238: 4239: 4240: 4241: 4242: 4243: 4244: 4245: 4246: 4247: 4248: 4249: 4250: 4251: 4252: 4253: 4254: 4255: 4256: 4257: 4258: 4259: 4260: 4261: 4262: 4263: 4264: 4265: 4266: 4267: 4268: 4269: 4270: 4271: 4272: 4273: 4274: 4275: 4276: 4277: 4278: 4279: 4280: 4281: 4282: 4283: 4284: 4285: 4286: 4287: 4288: 4289: 4290: 4291: 4292: 4293: 4294: 4295: 4296: 4297: 4298: 4299: 4300: 4301: 4302: 4303: 4304: 4305: 4306: 4307: 4308: 4309: 4310: 4311: 4312: 4313: 4314: 4315: 4316: 4317: 4318: 4319: 4320: 4321: 4322: 4323: 4324: 4325: 4326: 4327: 4328: 4329: 4330: 4331: 4332: 4333: 4334: 4335: 4336: 4337: 4338: 4339: 4340: 4341: 4342: 4343: 4344: 4345: 4346: 4347: 4348: 4349: 4350: 4351: 4352: 4353: 4354: 4355: 4356: 4357: 4358: 4359: 4360: 4361: 4362: 4363: 4364: 4365: 4366: 4367: 4368: 4369: 4370: 4371: 4372: 4373: 4374: 4375: 4376: 4377: 4378: 4379: 4380: 4381: 4382: 4383: 4384: 4385: 4386: 4387: 4388: 4389: 4390: 4391: 4392: 4393: 4394: 4395: 4396: 4397: 4398: 4399: 4400: 4401: 4402: 4403: 4404: 4405: 4406: 4407: 4408: 4409: 4410: 4411: 4412: 4413: 4414: 4415: 4416: 4417: 4418: 4419: 4420: 4421: 4422: 4423: 4424: 4425: 4426: 4427: 4428: 4429: 4430: 4431: 4432: 4433: 4434: 4435: 4436: 4437: 4438: 4439: 4440: 4441: 4442: 4443: 4444: 4445: 4446: 4447: 4448: 4449: 4450: 4451: 4452: 4453: 4454: 4455: 4456: 4457: 4458: 4459: 4460: 4461: 4462: 4463: 4464: 4465: 4466: 4467: 4468: 4469: 4470: 4471: 4472: 4473: 4474: 4475: 4476: 4477: 4478: 4479: 4480: 4481: 4482: 4483: 4484: 4485: 4486: 4487: 4488: 4489: 4490: 4491: 4492: 4493: 4494: 4495: 4496: 4497: 4498: 4499: 4500: 4501: 4502: 4503: 4504: 4505: 4506: 4507: 4508: 4509: 4510: 4511: 4512: 4513: 4514: 4515: 4516: 4517: 4518: 4519: 4520: 4521: 4522: 4523: 4524: 4525: 4526: 4527: 4528: 4529: 4530: 4531: 4532: 4533: 4534: 4535: 4536: 4537: 4538: 4539: 4540: 4541: 4542: 4543: 4544: 4545: 4546: 4547: 4548: 4549: 4550: 4551: 4552: 4553: 4554: 4555: 4556: 4557: 4558: 4559: 4560: 4561: 4562: 4563: 4564: 4565: 4566: 4567: 4568: 4569: 4570: 4571: 4572: 4573: 4574: 4575: 4576: 4577: 4578: 4579: 4580: 4581: 4582: 4583: 4584: 4585: 4586: 4587: 4588: 4589: 4590: 4591: 4592: 4593: 4594: 4595: 4596: 4597: 4598: 4599: 4600: 4601: 4602: 4603: 4604: 4605: 4606: 4607: 4608: 4609: 4610: 4611: 4612: 4613: 4614: 4615: 4616: 4617: 4618: 4619: 4620: 4621: 4622: 4623: 4624: 4625: 4626: 4627: 4628: 4629: 4630: 4631: 4632: 4633: 4634: 4635: 4636: 4637: 4638: 4639: 4640: 4641: 4642: 4643: 4644: 4645: 4646: 4647: 4648: 4649: 4650: 4651: 4652: 4653: 4654: 4655: 4656: 4657: 4658: 4659: 4660: 4661: 4662: 4663: 4664: 4665: 4666: 4667: 4668: 4669: 4670: 4671: 4672: 4673: 4674: 4675: 4676: 4677: 4678: 4679: 4680: 4681: 4682: 4683: 4684: 4685: 4686: 4687: 4688: 4689: 4690: 4691: 4692: 4693: 4694: 4695: 4696: 4697: 4698: 4699: 4700: 4701: 4702: 4703: 4704: 4705: 4706: 4707: 4708: 4709: 4710: 4711: 4712: 4713: 4714: 4715: 4716: 4717: 4718: 4719: 4720: 4721: 4722: 4723: 4724: 4725: 4726: 4727: 4728: 4729: 4730: 4731: 4732: 4733: 4734: 4735: 4736: 4737: 4738: 4739: 4740: 4741: 4742: 4743: 4744: 4745: 4746: 4747: 4748: 4749: 4750: 4751: 4752: 4753: 4754: 4755: 4756: 4757: 4758: 4759: 4760: 4761: 4762: 4763: 4764: 4765: 4766: 4767: 4768: 4769: 4770: 4771: 4772: 4773: 4774: 4775: 4776: 4777: 4778: 4779: 4780: 4781: 4782: 4783: 4784: 4785: 4786: 4787: 4788: 4789: 4790: 4791: 4792: 4793: 4794: 4795: 4796: 4797: 4798: 4799: 4800: 4801: 4802: 4803: 4804: 4805: 4806: 4807: 4808: 4809: 4810: 4811: 4812: 4813: 4814: 4815: 4816: 4817: 4818: 4819: 4820: 4821: 4822: 4823: 4824: 4825: 4826: 4827: 4828: 4829: 4830: 4831: 4832: 4833: 4834: 4835: 4836: 4837: 4838: 4839: 4840: 4841: 4842: 4843: 4844: 4845: 4846: 4847: 4848: 4849: 4850: 4851: 4852: 4853: 4854: 4855: 4856: 4857: 4858: 4859: 4860: 4861: 4862: 4863: 4864: 4865: 4866: 4867: 4868: 4869: 4870: 4871: 4872: 4873: 4874: 4875: 4876: 4877: 4878: 4879: 4880: 4881: 4882: 4883: 4884: 4885: 4886: 4887: 4888: 4889: 4890: 4891: 4892: 4893: 4894: 4895: 4896: 4897: 4898: 4899: 4900: 4901: 4902: 4903: 4904: 4905: 4906: 4907: 4908: 4909: 4910: 4911: 4912: 4913: 4914: 4915: 4916: 4917: 4918: 4919: 4920: 4921: 4922: 4923: 4924: 4925: 4926: 4927: 4928: 4929: 4930: 4931: 4932: 4933: 4934: 4935: 4936: 4937: 4938: 4939: 4940: 4941: 4942: 4943: 4944: 4945: 4946: 4947: 4948: 4949: 4950: 4951: 4952: 4953: 4954: 4955: 4956: 4957: 4958: 4959: 4960: 4961: 4962: 4963: 4964: 4965: 4966: 4967: 4968: 4969: 4970: 4971: 4972: 4973: 4974: 4975: 4976: 4977: 4978: 4979: 4980: 4981: 4982: 4983: 4984: 4985: 4986: 4987: 4988: 4989: 4990: 4991: 4992: 4993: 4994: 4995: 4996: 4997: 4998: 4999: 5000: 5001: 5002: 5003: 5004: 5005: 5006: 5007: 5008: 5009: 5010: 5011: 5012: 5013: 5014: 5015: 5016: 5017: 5018: 5019: 5020: 5021: 5022: 5023: 5024: 5025: 5026: 5027: 5028: 5029: 5030: 5031: 5032: 5033: 5034: 5035: 5036: 5037: 5038: 5039: 5040: 5041: 5042: 5043: 5044: 5045: 5046: 5047: 5048: 5049: 5050: 5051: 5052: 5053: 5054: 5055: 5056: 5057: 5058: 5059: 5060: 5061: 5062: 5063: 5064: 5065: 5066: 5067: 5068: 5069: 5070: 5071: 5072: 5073: 5074: 5075: 5076: 5077: 5078: 5079: 5080: 5081: 5082: 5083: 5084: 5085: 5086: 5087: 5088: 5089: 5090: 5091: 5092: 5093: 5094: 5095: 5096: 5097: 5098: 5099: 5100: 5101: 5102: 5103: 5104: 5105: 5106: 5107: 5108: 5109: 5110: 5111: 5112: 5113: 5114: 5115: 5116: 5117: 5118: 5119: 5120: 5121: 5122: 5123: 5124: 5125: 5126: 5127: 5128: 
<?php
/**
 * LegiScan Client API
 * 
 * A simple implementation of the LegiScan API Pull, Push and Bulk classes
 *
 * @package LegiScan
 * @author LegiScan API Team <[email protected]>
 * @license https://opensource.org/licenses/BSD-2-Clause
 * @copyright 2010-2020 LegiScan LLC
 * 
 */

require_once(__DIR__ . '/' . 'common.php');

// {{{ Exceptions
/**
 * Exception class for API general errors
 * @package LegiScan\Exceptions
 */
class APIException extends Exception { }

/**
 * Exception class for API missing file / bad permissions
 * @package LegiScan\Exceptions
 */
class APIAccessException extends Exception { }

/**
 * Exception class for API status errors
 * @package LegiScan\Exceptions
 */
class APIStatusException extends Exception { }

/**
 * Exception class for API authorization token errors
 * @package LegiScan\Exceptions
 */
class APIAuthTokenException extends Exception { }
// }}}

// {{{ LegiScan Class
/**
 * A generic holder for some core static functions
 *
 * Non-inherited class primarily for static calls to pull configuration values, write log messages,
 * send mail alerts and hold constants. You will likely find {@see LegiScan_Pull} and
 * {@see LegiScan_Process} of much greater interest.
 *
 * @package LegiScan\Utility
 * @link https://api.legiscan.com/dl/
 * @link https://legiscan.com/legiscan
 *
 */
class LegiScan
{
    // {{{ Class Constants
    /**
     * Version number of the LegiScan API Client
     */
    const VERSION = '1.4.1';

    /**
     * Version number of the LegiScan API database schema
     */
    const SCHEMA_VERSION = 9;

    /**
     * Response string for a successful response
     */
    const API_OK = 'OK';

    /**
     * Response string for a failure response
     */
    const API_ERROR = 'ERROR';

    /**
     * MasterList/Search import control, do nothing
     */
    const IMPORT_NONE = 0;

    /**
     * MasterList/Search import control, only new bills
     */
    const IMPORT_NEW = 1;

    /**
     * MasterList/Search import control, only changed bills
     */
    const IMPORT_CHANGED = 2;

    /**
     * MasterList/Search import control, all bills
     */
    const IMPORT_ALL = 3;

    /**
     * setMonitor stance, watch bill
     */
    const STANCE_WATCH = 0;

    /**
     * setMonitor stance, support bill
     */
    const STANCE_SUPPORT = 1;

    /**
     * setMonitor stance, oppose bill
     */
    const STANCE_OPPOSE = 2;
    // }}}

    // {{{ getConfig()
    /**
     * Get the configuation array or a specific key from it
     *
     * @param string|null $key
     *   (**OPTIONAL**) Return `$key` from config array
     *
     * @param mixed|null $default
     *   (**OPTIONAL**) If `$key` does not exist return `$default`
     *
     * @return mixed
     *   Either the configuration array or a specific `$key` value
     */
    static function getConfig($key = null, $default = null)
    {
        static $config = array();

        if (empty($config))
            $config = parse_ini_file(realpath(__DIR__ . '/' . 'config.php'));

        if ($key !== null)
        {
            // Look for a specific key
            if (isset($config[$key]))
            {
                return $config[$key];
            }
            else
            {
                if ($default !== null)
                    return $default;
                else
                    return null;
            }
        }
        else
        {
            // Return the entire config array
            return $config;
        }
    }
    // }}}

    // {{{ fileLog()
    /**
     * Write log message to file located at {log_dir}/legiscan.log
     *
     * @throws APIAccessException
     *
     * @param string $msg
     *   The log message to be written
     *
     * @param string $file
     *   (**OPTIONAL**) If specified write log to {log_dir}/$file
     *
     */
    static function fileLog($msg, $file = null)
    {
        static $log_dir;

        if (!isset($log_dir))
        {
            $log_dir = LegiScan::getConfig('log_dir');
            if ($log_dir[0] != '/' || $log_dir[1] != ':')
                $log_dir = realpath(__DIR__ . '/' . $log_dir);
            if (!is_dir($log_dir))
                throw new APIAccessException("Log directory does not exist: " . LegiScan::getConfig('log_dir'));
        }

        $log_file = $log_dir . '/' . 'legiscan.log';
        if ($file !== null)
            $log_file = $log_dir . '/' . $file;

        if (($fd = @fopen($log_file, 'a+')) !== false)
        {
            $msg = rtrim($msg);
            $ts = strftime('[%Y/%m/%d %H:%M:%S]');
            fwrite($fd, $ts . ' ' . $msg . "\n");
            fclose($fd);
        }
        else
        {
            throw new APIAccessException("Cannot open log file for writing: $log_file");
        }
    }
    // }}}

    // {{{ sendMail()
    /**
     * Send an email to the address specified in config.php, throttled
     * to 1 msg/5 min (overflow sent to fileLog)
     *
     * @param string $subject
     *   The email subject
     *
     * @param string $body
     *   The email body
     *
     */
    static function sendMail($subject, $body)
    {
        static $last_sent;
        static $throttle_warning = false;

        if (!isset($last_sent))
        {
            $last_sent = time();
            $throttled = false;
        }
        else
        {
            $throttled = (bool) ((time() - $last_sent) <= 300);
        }

        $email = LegiScan::getConfig('email');
        $header = "From: LegiScan API <$email>";

        if ($email && $email === filter_var($email, FILTER_VALIDATE_EMAIL) && !$throttled)
        {
            mail($email, $subject, $body, $header);
            $last_sent = time();
            $throttle_warning = false;
        }
        elseif ($throttled)
        {
            if (!$throttle_warning)
            {
                $body = "Mail has been throttled for the next 5 minutes, please see legiscan.log for more messages\n\n$body";
                mail($email, "Throttled: $subject", $body, $header);
                $throttle_warning = true;
            }
            else
            {
                LegiScan::fileLog("Mail throttled [$subject] " . str_replace("\n", '\\n', $body));
            }
        }
    }
    // }}}

    // {{{ getVersion()
    /**
     * Get the LegiScan version number
     *
     * @return string Version number
     */
    static function getVersion()
    {
        return self::VERSION;
    }
    // }}}
}
// }}}

// {{{ LegiScan_Cache_File Class
/**
 * LegiScan API File Cache Class
 *
 * Basic file caching with expirations to cache API responses and
 * build permanent local bill text / document storage. To generate the
 * path fragment under each respective cache directories use 
 * {@link LegiScan_Pull::getCacheFilename()} or
 * {@link LegiScan_Process::getCacheFilename()}.
 *
 * ## API cache
 *
 * As a dynamic API cache this stores pull API requests and search
 * results in a local directory structure if there are no errors and
 * subsequent calls to the same API hook will replay.
 *
 * Directory structure where `{api_dir}` is from `LegiScan::getConfig('api_dir')`
 *
 * * `{api_dir}/amendment/AMENDMENT_ID.json`
 * * `{api_dir}/bill/BILL_ID.json`
 * * `{api_dir}/masterlist/SESSION_ID.json`
 * * `{api_dir}/people/PEOPLE_ID.json`
 * * `{api_dir}/rollcall/ROLL_CALL_ID.json`
 * * `{api_dir}/search/SEARCH_GUID.json`
 * * `{api_dir}/sessionlist/STATE.json`
 * * `{api_dir}/supplement/SUPPLEMENT_ID.json`
 * * `{api_dir}/text/TEXT_ID.json`
 *
 * ## Document cache
 *
 * As a static document cache this is the basis for local storage of
 * related documents (bill texts, amendments, supplements) as a well
 * structured archive that can be easily accessed by humans and automation.
 * 
 * Directory structure `TYPE/STATE/SESSION_ID/BILL_NUMBER/ID/MIME_EXT`
 * where `{doc_dir}` is from `LegiScan::getConfig('doc_dir')`
 *
 * * `{doc_dir}/amendment/tx/1478/hb1/3928402/pdf`
 * * `{doc_dir}/supplement/md/1412/sb493/56768/pdf`
 * * `{doc_dir}/text/ca/1400/sb1/1439083/html`
 *
 * @package LegiScan\Cache
 * @see LegiScan_Pull::getCacheFilename
 * @see LegiScan_Process::getCacheFilename
 * @link https://api.legiscan.com/dl/
 *
 */
class LegiScan_Cache_File
{
    // {{{ Class Constants
    /**
     * Type - API (expiration)
     */
    const TYPE_API = 1;
    /**
     * Type - Document (permanent)
     */
    const TYPE_DOC = 2;
    /**
     * Default lifetime of API cache objects in seconds (60 minutes)
     */
    const LIFETIME = 3600;
    // }}}

    // {{{ Class Variables
    /**
     * Lifetime of API cache objects
     */
    private $lifetime;
    /**
     * Type of cache
     */
    private $cache_type;
    /**
     * Base directory for cached files
     */
    private $cache_dir;
    // }}}

    // {{{ __construct()
    /**
     * Class constructor
     *
     * @throws APIAccessException
     *
     * @param string $type Type of cache {api, doc}
     * @param integer $lifetime Time in seconds for api cache lifetime
     */
    function __construct($type, $lifetime = self::LIFETIME)
    {
        switch ($type)
        {
            case 'api':
                $api_dir = LegiScan::getConfig('api_cache');
                if ($api_dir[0] != '/' && $api_dir[1] != ':')
                    $api_dir = realpath(__DIR__ . '/' . $api_dir);
                $this->cache_dir = $api_dir;
                if (!file_exists($this->cache_dir) || !is_dir($this->cache_dir))
                    throw new APIAccessException("LegiScan_Cache(): Invalid api cache directory: " . $this->cache_dir);
                $this->cache_type = self::TYPE_API;
                $this->lifetime = $lifetime;
                break;
            case 'doc':
                $doc_dir = LegiScan::getConfig('doc_cache');
                if ($doc_dir[0] != '/' && $doc_dir[1] != ':')
                    $doc_dir = realpath(__DIR__ . '/' . $doc_dir);
                $this->cache_dir = $doc_dir;
                if (!file_exists($this->cache_dir) || !is_dir($this->cache_dir))
                    throw new APIAccessException("LegiScan_Cache(): Invalid document cache directory: " . $this->cache_dir);
                $this->cache_type = self::TYPE_DOC;
                $this->lifetime = null;
                break;
        }
    }
    // }}}

    // {{{ get()
    /**
     * Get an entry from the cache, if operating as API cache also enfore expiration lifetime,
     * otherwise treat as a permanent document archive
     *
     * @param string $cache_file
     *   File location to retrieve, this may include additional directories under _cache\_dir_
     *
     * @param integer $lifetime
     *   Max age in seconds that a cached file is valid
     *
     * @return mixed
     *   The contents of the cache location or FALSE for missing/stale
     */
    public function get($file, $lifetime = null)
    {
        $filename = $this->cache_dir . '/' . $file;

        if (!file_exists($filename))
            return false;

        if ($this->cache_type == self::TYPE_API)
        {
            if ($lifetime === null)
                $lifetime = $this->lifetime;

            if (filemtime($filename) > (time() - $lifetime))
                $data = file_get_contents($filename);
            else
                $data = false;
        }
        else
        {
            $data = file_get_contents($filename);
        }

        return $data;
    }
    // }}}

    // {{{ set()
    /**
     * Put an entry into the cache, creating any nested directories as required
     *
     * @throws APIAccessException
     *
     * @param string $file
     *   File location to store, this may include additional directories under _cache\_dir_
     *
     * @param string $data
     *   Data to store in cache location
     *
     * @return boolean
     *   Indicating the success/failure of storing the data
     *
     */
    public function set($file, $data)
    {
        $filename = $this->cache_dir . '/' . $file;
        $dirname = dirname($filename);

        // Check if the directory path exists
        if (!file_exists($dirname))
        {
            // Create it recursively
            if (!@mkdir($dirname, 0777, true))
                throw new APIAccessException("Cannot create directory $dirname");
        }

        if (@file_put_contents($filename, $data) !== false)
            return true;
        else
            throw new APIAccessException("Cannot write $filename");
    }
    // }}}

    // {{{ clean()
    /**
     * Clean the API cache of stale entries
     *
     * @throws APIAccessException
     *
     * @return boolean
     *   Indicating the success/failure of the cleanup
     *
     */
    public function clean($verbose = false)
    {
        $result = true;

        /**
         * Directories under API cache root that we own 
         * to avoid stepping on toes
         *
         * @var $subdirs
         */
        $subdirs = array(
            'amendment',
            'datasetlist',
            'bill',
            'masterlist',
            'people',
            'rollcall',
            'search',
            'sessionlist',
            'supplement',
            'text',
        );

        $size = 0;
        $count = 0;

        if ($this->cache_type == self::TYPE_API)
        {
            $dir = $this->cache_dir;
            if (!($dh = opendir($dir))) {
                throw new Exception("clean(): Unable to open API cache directory $dir");
            }

            while (($file = readdir($dh)) !== false)
            {
                if ($file != '.' && $file != '..' && in_array($file, $subdirs))
                {
                    $dir2 = $dir . '/' . $file;
                    if (is_dir($dir2))
                    {
                        if (!($dh2 = opendir($dir2)))
                            throw new APIAccessException("clean(): Unable to open API cache directory $dir2");

                        while (($file2 = readdir($dh2)) !== false)
                        {
                            if ($file2 != '.' && $file2 != '..' && substr($file2, -4) == 'json')
                            {
                                $file2 = $dir2 . '/' . $file2;
                                // files older than lifetime get deleted from cache
                                if ((time() - @filemtime($file2)) > $this->lifetime)
                                {
                                    if ($verbose) echo "Unlink $file2\n";
                                    $stat = stat($file2);
                                    $size += $stat['size'];
                                    $result = ($result and (@unlink($file2)));
                                    $count++;
                                }
                            }
                        }
                    }
                    closedir($dh2);
                }
            }
            closedir($dh);
        }

        if ($verbose)
            echo number_format($size) . ' bytes cleaned in ' . number_format($count) . " files\n";

        return $result;
    }
    // }}}
}
// }}}

// {{{ LegiScan_Cache_Memory Class
/**
 * LegiScan API Memory Cache Class
 *
 * Basic singleton memory cache that offers a simple non-stateful memory cache or
 * interfaces with an external memcache server, either via Memcache or Memcached.
 *
 * Underutilized in this application, it currently caches simple `exists(ID)` style
 * lookups. Internally at LegiScan this saves 100-125 qps in production, though in 
 * this isolated API Client implementation will be around 5-10 qps.
 *
 * <code>
 * $memcache = LegiScan_Cache_Memory::getInstance();
 *
 * $key = "key-sample-1";
 *
 * $memcache->set($key, 12345);
 * $value = $memcache->get($key);
 *
 * var_dup($value);
 * </code>
 *
 * @package LegiScan\Cache
 * @see LegiScan_Process::checkExists
 * @link https://memcached.org/
 * @link https://api.legiscan.com/dl/
 *
 */
class LegiScan_Cache_Memory
{
    // {{{ Class Constants
    /**
     * Default lifetime of API cache objects in seconds (60 minutes)
     */
    const LIFETIME = 3600;
    // }}}

    // {{{ Class Variables
    /**
     * Lifetime of cache objects
     * @var integer
     */
    private static $lifetime;
    /**
     * Our cache array if no memcached server
     * @var array
     */
    private static $cache;
    /**
     * Singleton class instance
     * @var object
     */
    private static $instance;
    // }}}

    // {{{ __construct()
    /**
     * This is not the contructor you are looking for... Static Singleton
     *
     * @param integer $lifetime
     *   Time in seconds for api cache lifetime
     *
     */
    private function __construct($lifetime)
    {
        self::$cache = array();
        self::$lifetime = $lifetime;
    }
    // }}}

    // {{{ getInstance()
    /**
     * Get a instance of either internal memory cache or external memcached
     *
     * @param integer $lifetime
     *   Lifetime of a cached value
     *
     */
    public static function getInstance($lifetime = self::LIFETIME)
    {
        if (!isset(self::$instance))
        {
            $use_memcache = strtolower(LegiScan::getConfig('use_memcached'));
            if ($use_memcache)
            {
                $memcache_host = LegiScan::getConfig('memcache_host');
                $memcache_port = LegiScan::getConfig('memcache_port');

                if (class_exists('Memcache', false))
                {
                    self::$instance = new Memcache();
                    if (!@self::$instance->connect($memcache_host, $memcache_port))
                        throw new APIException('Could not connect via Memcache to ' . $memcache_host . ':' . $memcache_port);
                }
                elseif (class_exists('Memcached', false))
                {
                    self::$instance = new Memcached();
                    self::$instance->addServer($memcache_host, $memcache_port);

                    // Validate since Memcached will not connect from pool until needed
                    if (empty(self::$instance->getVersion()))
                        throw new APIException('Could not connect via Memcached to ' . $memcache_host . ':' . $memcache_port);
                }
                else
                {
                    throw new APIException('Missing Memcache/Memcached PHP extension for use_memory_cache = ' . $use_memcache);
                }
            }
            else
            {
                self::$instance = new LegiScan_Cache_Memory($lifetime);
            }
        }

        return self::$instance;
    }
    // }}}

    // {{{ get()
    /**
     * Get an entry from the cache
     *
     * @param string $key
     *   Cache key to retrieve
     *
     * @return mixed
     *   The contents of the file or FALSE for missing/stale
     */
    public function get($key)
    {
        $data = false;

        if (isset(self::$cache[$key]) && self::$cache[$key]['expires'] > time())
        {
            $data = self::$cache[$key]['data'];
        }

        return $data;
    }
    // }}}

    // {{{ set()
    /**
     * Store an entry in the cache
     *
     * @param string $key
     *   Cache key value
     *
     * @param string $data
     *   Data to store under cache key
     *
     * @param string $lifetime
     *   How long until the data expires, in seconds
     *
     */
    public function set($key, $data, $lifetime = self::LIFETIME)
    {
        self::$cache[$key] = array(
            'expires' => time() + $lifetime,
            'lifetime' => $lifetime,
            'data' => $data,
        );
    }
    // }}}

    // {{{ flush()
    /**
     * Clear all items from cache
     *
     * @return boolean
     *   Indicating the success/failure of the cleanup
     *
     */
    public function flush()
    {
        self::$cache = array();

        return true;
    }
    // }}}
}
// }}}

// {{{ LegiScan_Push Class
/**
 * LegiScan API Push Interface Class
 *
 * Framework to process and decode incoming payloads as the endpoint listener
 * in a Push API interface.
 *
 * @package LegiScan\API
 * @see LegiScan_Endpoint
 * @see LegiScan_Process
 * @link https://api.legiscan.com/dl/
 * @link https://legiscan.com/legiscan
 *
 */
class LegiScan_Push
{
    // {{{ Class Variables
    /**
     * The decoded payload that was last processed through {@link processPush()}
     *
     * @var array
     */
    protected $payload;

    /**
     * The type of payload that was last processed through {@link processPush()}
     *
     * @var string
     */
    protected $payload_type;

    /**
     * Form variable name in the case of cooked x-www-form-urlencoded payload
     *
     * @var string
     */
    protected $form_var;
    // }}}

    // {{{ __construct()
    /** 
     * Class constructor
     *
     */
    function __construct()
    {
        $this->form_var = LegiScan::getConfig('push_form_var', 'UNKNOWN');
    }
    // }}}

    // {{{ processPush()
    /** 
     * Processes an incoming LegiScan API JSON payload into an associative array
     *
     * @throws APIException
     *
     * @return boolean
     *   Success of payload processing
     *
     */
    public function processPush()
    {
        // Check to see if the incoming payload is raw or cooked
        if ($_SERVER['CONTENT_TYPE'] != 'application/x-www-form-urlencoded') 
        {
            // Raw response waiting on STDIN
            $payload = file_get_contents('php://input');
        }
        else
        {
            // Cooked into a POST variable
            if (isset($_POST[$this->form_var]))
                $payload = $_POST[$this->form_var];
            else
                throw new APIException('processPush expected POST variable [' . $this->form_var . '] is not set');
        }

        // Decode payload into associative array instead of object
        $this->payload = json_decode($payload, true);
        if ($this->payload === false)
            throw new APIException('processPush could not decode API payload');

        // Look for root markers in payload to set type
        if (isset($this->payload['bill']))
            $this->payload_type = 'bill';
        elseif (isset($this->payload['text']))
            $this->payload_type = 'text';
        elseif (isset($this->payload['roll_call']))
            $this->payload_type = 'roll_call';
        elseif (isset($this->payload['person']))
            $this->payload_type = 'person';
        elseif (isset($this->payload['amendment']))
            $this->payload_type = 'amendment';
        elseif (isset($this->payload['supplement']))
            $this->payload_type = 'supplement';
        elseif (isset($this->payload['session']))
            $this->payload_type = 'session';
        else
            throw new APIException('processPush could not determine payload type [' . array_keys($this->payload)[0] . ']');

        return true;
    }
    // }}}

    // {{{ getPayload()
    /** 
     * Returns the decoded payload associative array for the most recently processed
     * record
     *
     * @return mixed[] The decoded payload array
     *
     */
    public function getPayload()
    {
        return $this->payload;
    }
    // }}}

    // {{{ getPayloadType()
    /** 
     * Returns the type of payload that was most recently processed
     *
     * @return string The type of payload `[bill, text, amendment, supplement, roll_call, person, session]`
     *
     */
    public function getPayloadType()
    {
        return $this->payload_type;
    }
    // }}}

    // {{{ respondOk()
    /** 
     * Wrapper function around respond() that creates a valid OK response
     *
     * @param array $missing
     *   Optional array listing missing objects to request in the response
     *
     */
    public function respondOk($missing = array())
    {
        $response = array('status'=>LegiScan::API_OK);
        if (is_array($missing) && !empty($missing))
            $response['missing'] = $missing;
        $this->respond($response);
    }
    // }}}

    // {{{ respondError()
    /** 
     * Wrapper function around respond() that creates a valid ERROR response
     *
     * @param string $error_message
     *   A descriptive user-defined error message to explain the error response
     *
     */
    public function respondError($error_message)
    {
        $response = array(
            'status' => LegiScan::API_ERROR,
            'alert' => array(
                'message'=>$error_message
            ),
        );
        $this->respond($response);
    }
    // }}}

    // {{{ respond()
    /** 
     * Encode and send a valid API response back to LegiScan
     *
     * @param array $response
     *   Contains the push endpoint server response structure
     *
     */
    private function respond($response)
    {
        header("Content-Type: application/json");

        $msg = json_encode($response);

        echo $msg;

        exit(0);
    }
    // }}}
}
// }}}

// {{{ LegiScan_Pull Class
/**
 * LegiScan API Pull Interface Class
 *
 * Basic pull class to generate API requests and decode the JSON responses. Doing
 * something interesting with the results is the job of {@link LegiScan_Process}.
 * Each `get*()` function maps to the corresponding API operation, preparing the
 * request array and processing through {@link LegiScan_Pull::apiRequest()}.
 *
 * @package LegiScan\API
 * @see LegiScan_CommandLine
 * @see LegiScan_Process
 * @see LegiScan_Worker
 * @link https://api.legiscan.com/dl/
 * @link https://legiscan.com/legiscan
 *
 */
class LegiScan_Pull
{
    // {{{ Class Variables

    /**
     * LegiScan API key for requests from `config.php`
     *
     * @var string Assigned 32 character API hash
     * @access protected
     */
    protected $api_key;

    /**
     * Raw response from an API call
     *
     * @var string
     * @access protected
     */
    protected $response;

    /**
     * Decoded associative array representing the API response
     *
     * @var mixed[]
     * @access protected
     */
    protected $payload;

    /**
     * Cache layer interface
     *
     * @var LegiScan_Cache_File
     * @access protected
     */
    protected $cache;

    /**
     * Most recent Pull API request URL
     *
     * @var string
     * @access protected
     */
    protected $request_url;

    // }}}

    // {{{ __construct()
    /** 
     * Class constructor that nominally validates the API key
     *
     * @throws APIException
     *
     * @param string $api_key
     *   (**OPTIONAL**) Override the api_key from LegiScan::getConfig()
     *
     */
    function __construct()
    {
        $this->cache = new LegiScan_Cache_File('api');

        $this->api_key = LegiScan::getConfig('api_key');

        // Sanity checks to avoid sending junk requests
        if (!preg_match('/^[0-9a-f]{32}$/i', $this->api_key))
            throw new APIException('Invalid API key');
    }
    // }}}

    // {{{ getSessionList()
    /** 
     * Get a session list for a specified state
     *
     * @param string $state
     *   State to pull the session list for
     * 
     * @return array
     *   The list of state sessions and their corresponding session_id numbers
     */
    public function getSessionList($state)
    {
        $params = array('state'=>strtoupper($state));

        return $this->apiRequest('getSessionList', $params);
    }
    // }}}

    // {{{ getMasterList()
    /** 
     * Get a master list of bills for a specific session_id
     *
     * @param integer $session_id
     *   LegiScan session_id to pull the master list for
     * 
     * @return array
     *   The master list of current bills with their corresponding bill_id numbers
     */
    public function getMasterList($session_id)
    {
        $params = array('id'=>$session_id);

        // Preferred method is using session_id, but support the 
        // state abbr shortcut call mostly for legiscan-cli.php
        if (preg_match('#^[A-Z]{2}#i', $session_id))
            $params = array('state'=>$session_id);

        return $this->apiRequest('getMasterList', $params);
    }
    // }}}

    // {{{ getMasterListRaw()
    /** 
     * Get a master list of bills for a specific session_id optimized for
     * `change_hash` detection
     *
     * @param integer $session_id
     *   LegiScan session_id to pull the master list for
     * 
     * @return array
     *   The master list of current bills with their corresponding bill_id numbers
     */
    public function getMasterListRaw($session_id)
    {
        $params = array('id'=>$session_id);

        // Preferred method is using session_id, but support the 
        // state abbr shortcut call mostly for legiscan-cli.php
        if (preg_match('#^[A-Z]{2}#i', $session_id))
            $params = array('state'=>$session_id);

        return $this->apiRequest('getMasterListRaw', $params);
    }
    // }}}

    // {{{ getBill()
    /** 
     * Get the bill detail record for a specified bill
     *
     * @param integer $bill_id
     *   The internal bill id for the requested bill
     * 
     * @return array
     *   The record containing a summary of the bill detail information
     */
    public function getBill($bill_id)
    {
        $params = array('id'=>$bill_id);

        return $this->apiRequest('getBill', $params);
    }
    // }}}

    // {{{ getBillText()
    /** 
     * Get a copy of the bill text for the specified document id
     *
     * (<b>Note</b>: doc is base64 encoded)
     *
     * @param integer $text_id
     *   The internal bill id for the requested bill text
     * 
     * @return array
     *   The bill text record with the actual text and meta type information
     */
    public function getBillText($text_id)
    {
        $params = array('id'=>$text_id);

        if (LegiScan::getConfig('prefer_pdf'))
            $params['prefer'] = 'pdf';

        return $this->apiRequest('getBillText', $params);
    }
    // }}}

    // {{{ getAmendment()
    /** 
     * Get a copy of the bill amendment for the specified amendment id
     *
     * (<b>Note</b>: doc is base64 encoded)
     *
     * @param integer $amendment_id
     *   The internal amendment id for the requested bill amendment
     * 
     * @return array
     *   The bill amendment record with the actual amendment and meta type information
     */
    public function getAmendment($amendment_id)
    {
        $params = array('id'=>$amendment_id);

        if (LegiScan::getConfig('prefer_pdf'))
            $params['prefer'] = 'pdf';

        return $this->apiRequest('getAmendment', $params);
    }
    // }}}

    // {{{ getSupplement()
    /** 
     * Get a copy of the bill supplement for the specified supplement id
     *
     * (<b>Note</b>: doc is base64 encoded)
     *
     * @param integer $supplement_id
     *   The internal supplement id for the requested bill supplement
     * 
     * @return array
     *   The bill supplement record with the actual supplement and meta type information
     */
    public function getSupplement($supplement_id)
    {
        $params = array('id'=>$supplement_id);

        if (LegiScan::getConfig('prefer_pdf'))
            $params['prefer'] = 'pdf';

        return $this->apiRequest('getSupplement', $params);
    }
    // }}}

    // {{{ getRollCall()
    /** 
     * Get the roll call record for a specified bill with vote summary and
     * individual legislator vote information
     *
     * @param integer $roll_call_id
     *   The internal LegiScan roll_call_id for the requested vote
     * 
     * @return array
     *   A detailed roll call record with individual vote data
     */
    public function getRollCall($roll_call_id)
    {
        $params = array('id'=>$roll_call_id);

        return $this->apiRequest('getRollCall', $params);
    }
    // }}}

    // {{{ getPerson()
    /** 
     * Get a basic people record for the specified id
     *
     * @param integer $people_id
     *   The internal LegiScan people_id to be retrieved {@link apiRequest()}
     * 
     * @return array
     *   The basic sponsor information for the given people_id
     */
    public function getPerson($people_id)
    {
        $params = array('id'=>$people_id);

        return $this->apiRequest('getPerson', $params);
    }
    // }}}

    // {{{ getSearch()
    /** 
     * Performs a search against the LegiScan national legislative database
     *
     * @throws APIException
     *
     * @param array $search
     *   An associative array with the search parameters: `state`, `bill`,
     *   `query`, `year`
     * 
     * @return array
     *   A page of search results for the given query parameters
     */
    public function getSearch($search)
    {
        // Check and build the search query parameters
        $params = array();

        // Limit to a state (abbreviation) or ALL, defaults to all
        if (isset($search['state']))
            $params['state'] = $search['state'];
        // Exact bill number search
        if (isset($search['bill']))
            $params['bill'] = $search['bill'];
        // Full text search query string
        if (isset($search['query']))
            $params['query'] = $search['query'];
        // Search year: 1=all,2=current,3=recent,4=prior,specific year otherwise
        if (isset($search['year']))
            $params['year'] = $search['year'];
        // Page of results to fetch, defaults to page 1
        if (isset($search['page']))
            $params['page'] = $search['page'];
        // Use a raw search (2000 count page size but only bill_id, score and change_hash)
        if (isset($search['raw']))
            $params['raw'] = 1;

        // Make sure at least something useful is being searched for
        if (!isset($search['bill']) && !isset($search['query']))
            throw new APIException('Missing required search parameters');

        return $this->apiRequest('getSearch', $params);
    }
    // }}}

    // {{{ getSearchRaw()
    /** 
     * Performs a raw search against the LegiScan national legislative database
     *
     * @throws APIException
     *
     * @param array $search
     *   An associative array with the search parameters: `state`, `bill`,
     *   `query`, `year`
     * 
     * @return array
     *   A page of search results for the given query parameters
     */
    public function getSearchRaw($search)
    {
        // Check and build the search query parameters
        $params = array();

        // Limit to a state (abbreviation) or ALL, defaults to all
        if (isset($search['state']))
            $params['state'] = $search['state'];
        // Exact bill number search
        if (isset($search['bill']))
            $params['bill'] = $search['bill'];
        // Full text search query string
        if (isset($search['query']))
            $params['query'] = $search['query'];
        // Search year: 1=all,2=current,3=recent,4=prior,specific year otherwise
        if (isset($search['year']))
            $params['year'] = $search['year'];
        // Page of results to fetch, defaults to page 1
        if (isset($search['page']))
            $params['page'] = $search['page'];

        // Make sure at least something useful is being searched for
        if (!isset($search['bill']) && !isset($search['query']))
            throw new APIException('Missing required search parameters');

        return $this->apiRequest('getSearchRaw', $params);
    }
    // }}}

    // {{{ getDatasetList()
    /** 
     * Get a list of available datasets with optional filters
     *
     * @param array $filter
     *   Optional state and year filters
     * 
     * @return array
     *   The list of datasets available and their corresponding access keys
     */
    public function getDatasetList($filter)
    {
        $params = array();
        if (isset($filter['state']))
            $params['state'] = $filter['state'];
        if (isset($filter['year']))
            $params['year'] = $filter['year'];

        return $this->apiRequest('getDatasetList', $params);
    }
    // }}}

    // {{{ getDataset()
    /** 
     * Get a dataset archive payload for a specific session id
     *
     * @param integer $session_id
     *   The internal LegiScan session_id to be retrieved
     * 
     * @param string $access_key
     *   A valid access key provided in getDatasetList
     * 
     * @return array
     *   The dataset payload with the actual zip file and meta information
     */
    public function getDataset($session_id, $access_key)
    {
        $params = array('id'=>$session_id,'access_key'=>$access_key);

        return $this->apiRequest('getDataset', $params);
    }
    // }}}

    // {{{ getSessionPeople()
    /** 
     * Retrieve a list of people records active in a specific session id
     *
     * @param integer $session_id
     *   The internal LegiScan session_id to scan
     * 
     * @return array
     *   Collection of {@link getPerson()} equivalent records of those active in session
     */
    public function getSessionPeople($session_id)
    {
        $params = array('id'=>$session_id);

        return $this->apiRequest('getSessionPeople', $params);
    }
    // }}}

    // {{{ getSponsoredList()
    /** 
     * Retrieve a list of bills sponsored by an individual legislator 
     *
     * @param integer $people
     *   The internal LegiScan people_id to look for
     * 
     * @return array
     *   Information on the list of sponsored bills
     */
    public function getSponsoredList($people_id)
    {
        $params = array('id'=>$people_id);

        return $this->apiRequest('getSponsoredList', $params);
    }
    // }}}

    // {{{ getMonitorList()
    /** 
     * Retrieve a list of bills monitored in the GAITS account associated with the
     * API key
     *
     * @param array $filter
     *   An associative array with the filter parameters: `record`
     *
     * @return array
     *   Information on the list of monitored bills
     */
    public function getMonitorList($filter)
    {
        $params = array();
        if (isset($filter['record']))
            $params['record'] = $filter['record'];
        else
            $params['recrod'] = 'current';

        return $this->apiRequest('getMonitorList', $params);
    }
    // }}}

    // {{{ getMonitorListRaw()
    /** 
     * Retrieve a list of bills monitored in the associated GAITS account, optimized for
     * `change_hash` detection
     *
     * @param array $filter
     *   An associative array with the filter parameters: `record`
     *
     * @return array
     *   Information on the list of monitored bills
     */
    public function getMonitorListRaw($filter)
    {
        $params = array();
        if (isset($filter['record']))
            $params['record'] = $filter['record'];
        else
            $params['record'] = 'current';

        return $this->apiRequest('getMonitorListRaw', $params);
    }
    // }}}

    // {{{ setMonitor()
    /** 
     * Set the monitoring status of one or more `bill_id` in the GAITS account
     * monitoring or ignore list
     *
     * @param array $monitor 
     *   An associative array with the monitor parameters:
     *   `action` - action to take: monitor, ignore, remove
     *   `list` - bill_id list to operate on
     *   `stance` - position on bill: watch, support, oppose
     *
     * @return array
     *   Information on the list of monitored bills
     */
    public function setMonitor($monitor)
    {
        $params = array();

        if (isset($monitor['action']))
            $params['action'] = $monitor['action'];
        if (isset($monitor['stance']))
            $params['stance'] = $monitor['stance'];

        $params['list'] = implode(',', $monitor['list']);

        return $this->apiRequest('setMonitor', $params);
    }
    // }}}

    // {{{ apiRequest()
    /** 
     * Makes the actual request to the LegiScan API server via cURL
     *
     * @throws APIException
     *
     * @param string $op
     *   The API operation to actually perform
     * 
     * @param array $params
     *   An associative array of the required parameters to perform the
     *   needed API operation
     * 
     * @return mixed[]
     *   An associative array representing the API response
     */
    protected function apiRequest($op, $params)
    {
        // Merge in the base parameters
        $query = array_merge(array('key'=>$this->api_key,'op'=>$op), $params);

        $query_string = http_build_query($query);

        $this->request_url = 'https://api.legiscan.com/?' . $query_string;

        // Use cache time guidelines per API Operations p.7 API User Manual
        $cache_times = array(
            'getsessionlist'    => 86400,   // Daily
            'getmasterlist'     => 3600,    // 1 hour
            'getmasterlistraw'  => 3600,    // 1 hour
            'getbill'           => 10800,   // 3 hours
            'getbilltext'       => 2592000, // Static (30 days)
            'getamendment'      => 2592000, // Static (30 days)
            'getsupplement'     => 2592000, // Static (30 days)
            'getrollcall'       => 2592000, // Static (30 days)
            'getperson'         => 604800,  // Weekly
            'getsearch'         => 3600,    // 1 hour
            'getsearchraw'      => 3600,    // 1 hour
            'search'            => 3600,    // 1 hour
            'searchraw'         => 3600,    // 1 hour
            'getdatasetlist'    => 86400,   // Weekly (1 day)
            'getdataset'        => 86400,   // Weekly (1 day)
            'getsessionpeople'  => 86400,   // Weekly (1 day)
            'getsponsoredlist'  => 86400,   // Daily
            'getmonitorlist'    => 0,       // Live hook
            'getmonitorlistraw' => 0,       // Live hook
            'setmonitor'        => 0,       // Live hook
        );

        // Default 1 hour cache and check for guideline
        $lifetime = 3600;
        $nop = strtolower($op);
        if (isset($cache_times[$nop]))
            $lifetime = $cache_times[$nop];

        // Always check local cache first to save an actual API call
        $cache_file = $this->getCacheFilename($op, $params);
        $this->response = $this->cache->get($cache_file, $lifetime);

        // Flag if the cache needs updating
        $update_cache = false;

        if (!$this->response)
        {
            // Initialize curl and make the real request
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->request_url);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_BUFFERSIZE, 64000);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, "LegiScan API Client " . LegiScan::VERSION);

            $this->response = curl_exec($ch);

            // See if something drastic happened
            if ($this->response === false)
                throw new APIException('Could not get response from LegiScan API server (cURL ' . curl_errno($ch) . ': ' . curl_error($ch) . ')');

            // Fresh data so update cache
            $update_cache = true;   
        }

        // Decode the response and check for error status
        $this->payload = json_decode($this->response, true);
        if ($this->payload === false)
            throw new APIException('Could not decode JSON API response');

        // If the API returned and error pitch it up the chain with an exception
        if ($this->payload['status'] == LegiScan::API_ERROR)
            throw new APIException($this->payload['alert']['message']);

        // Save to cache if needed and we get here without errors
        if ($update_cache)
            $this->cache->set($cache_file, $this->response);

        return $this->payload;
    }
    // }}}

    // {{{ importBillList()
    /**
     * Process a bill_id list processing and requesting any necessary missing child objects
     *
     * @param array $payload
     *   List of the LegiScan bill_ids that should be imported
     *
     * @param LegiScan_Process $logic
     *   An instance of {@link LegiScan_Process} attached to its database
     * 
     * @return boolean
     *   Indicating the success/failure of bill payload processing
     */
    function importBillList($bills, $logic)
    {
        $n = count($bills) - 1;
        foreach ($bills as $i => $bill_id)
        {
            LegiScan::fileLog("importBillList requesting bill $bill_id ($i / $n)");

            $resp = $this->getBill($bill_id);
            if ($resp)
            {
                $missing = $logic->processBill($resp);

                if (!empty($missing))
                {
                    if (isset($missing['texts']))
                    {
                        foreach ($missing['texts'] as $text_id)
                        {
                            LegiScan::fileLog("importBillList requesting text $text_id");
                            $resp = $this->getBillText($text_id);
                            if ($resp)
                                $logic->processBillText($resp);
                        }
                    }
                    if (isset($missing['amendments']))
                    {
                        foreach ($missing['amendments'] as $amendment_id)
                        {
                            LegiScan::fileLog("importBillList requesting amendment $amendment_id");
                            $resp = $this->getAmendment($amendment_id);
                            if ($resp)
                                $logic->processAmendment($resp);
                        }
                    }
                    if (isset($missing['supplements']))
                    {
                        foreach ($missing['supplements'] as $supplement_id)
                        {
                            LegiScan::fileLog("importBillList requesting supplement $supplement_id");
                            $resp = $this->getSupplement($supplement_id);
                            if ($resp)
                                $logic->processSupplement($resp);
                        }
                    }
                    if (isset($missing['sponsors']))
                    {
                        foreach ($missing['sponsors'] as $people_id)
                        {
                            LegiScan::fileLog("importBillList requesting person $people_id");
                            $resp = $this->getPerson($people_id);
                            if ($resp)
                                $logic->processPerson($resp);
                        }
                    }
                    if (isset($missing['votes']))
                    {
                        foreach ($missing['votes'] as $roll_call_id)
                        {
                            LegiScan::fileLog("importBillList requesting roll_call $roll_call_id");
                            $resp = $this->getRollCall($roll_call_id);
                            if ($resp)
                            {
                                $people_list = $logic->processRollCall($resp);
                                foreach ($people_list as $people_id)
                                {
                                    LegiScan::fileLog("importBillList requesting person $people_id");
                                    $resp = $this->getPerson($people_id);
                                    if ($resp)
                                        $logic->processPerson($resp);
                                }
                            }
                        }
                    }
                }
            }

            // May have hammered for missing items, plus just be nice =)
            if ($i != $n)
                sleep(1);
        }

        return true;
    }
    // }}}

    // {{{ getURL()
    /** 
     * Return the most recent Pull API request URL generated by {@see apiRequest()}
     *
     * @return string
     *   The full request URL
     */
    function getURL()
    {
        return $this->request_url;
    }
    // }}}

    // {{{ getRawResponse()
    /** 
     * Return the JSON payload of the most recent Pull API request by {@see apiRequest()}
     *
     * @return string
     *   The JSON data structure
     */
    function getRawResponse()
    {
        return $this->response;
    }
    // }}}

    // {{{ getCacheFilename()
    /**
     * Generate an API cache filename 
     *
     * @param string $op
     *   The API operation hook name
     *
     * @param array $params
     *   The API paramaters that were part of the call
     * 
     * @return string
     *   Filename path fragment for the cache location under cache root
     */
    function getCacheFilename($op, $params)
    {
        $op = strtolower($op);

        switch ($op)
        {
            case 'getbill':
                $filename = 'bill/' . $params['id'] . '.json';
                break;
            case 'getperson':
                $filename = 'people/' . $params['id'] . '.json';
                break;
            case 'getrollcall':
                $filename = 'rollcall/' . $params['id'] . '.json';
                break;
            case 'getbilltext':
                $filename = 'text/' . $params['id'] . '.json';
                break;
            case 'getamendment':
                $filename = 'amendment/' . $params['id'] . '.json';
                break;
            case 'getsupplement':
                $filename = 'supplement/' . $params['id'] . '.json';
                break;
            case 'getsessionlist':
                $filename = 'sessionlist/' . $params['state'] . '.json';
                break;
            case 'getmasterlist':
            case 'getmasterlistraw':
                $raw = '';
                if (isset($params['raw']) || $op == 'getmasterlistraw')
                    $raw = '_raw';
                // If state is present prefer that
                if (isset($params['state']))
                    $filename = 'masterlist/' . $params['state'] . $raw . '.json';
                else
                    $filename = 'masterlist/' . $params['id'] . $raw . '.json';
                break;
            case 'search':
            case 'searchraw':
            case 'getsearch':
            case 'getsearchraw':
                $chunks = array();
                if (isset($params['state']))
                    $chunks[] = $params['state'];
                if (isset($params['raw']) || $op == 'searchraw')
                    $chunks[] = 'raw';
                if (isset($params['bill']))
                    $chunks[] = $params['bill'];
                if (isset($params['year']))
                    $chunks[] = 'y' . $params['year'];
                if (isset($params['page']))
                    $chunks[] = 'p' . $params['page'];
                if (isset($params['query']))
                    $chunks[] = $params['query'];

                $file_chunk = strtolower(implode('_', $chunks));

                // NOTE: To maintain some sort of readability to the file name
                // do a quick transformation.
                $file_chunk = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $file_chunk);
                $file_chunk = mb_ereg_replace("([\.]{2,})", '', $file_chunk);
                // Very long searches will break max filename length, check and adjust
                // accounting for extension
                if ((strlen($file_chunk) + 5) > 255)
                    $file_chunk = substr($file_chunk, 0, 250);

                $filename = 'search/' . $file_chunk . '.json';
                break;
            case 'getdatasetlist':
                $chunks = array();
                $chunks[] = 'list';
                if (isset($params['state']))
                    $chunks[] = $params['state'];
                if (isset($params['year']))
                    $chunks[] = 'y' . $params['year'];

                $file_chunk = strtolower(implode('_', $chunks));

                $filename = 'datasetlist/' . $file_chunk . '.json';
                break;
            case 'getdataset':
                $filename = 'dataset/' . $params['id'] . '.json';
                break;
            case 'getsessionpeople':
                $filename = 'sessionpeople/' . $params['id'] . '.json';
                break;
            case 'getsponsoredlist':
                $filename = 'sponsoredlist/' . $params['id'] . '.json';
                break;
            case 'getmonitorlist':
            case 'getmonitorlistraw':
                $raw = '';
                if ($op == 'getmonitorlistraw')
                    $raw = '_raw';
                $filename = 'monitorlist/' . $params['record'] . $raw . '.json';
                break;
            case 'setmonitor':
                $filename = 'monitorlist/' . $params['action'] . '.json';
                break;
            default:
                throw new APIException("Cannot determine Pull API cache file for $op");
                break;
        }

        return $filename;
    }
    // }}}
}
// }}}

// {{{ LegiScan_Bulk Class
/**
 * LegiScan API Bulk Dataset Class
 *
 * Worker class that processes a LegiScan dataset archive and imports / updates
 * the local database, passing the individual JSON payloads to their corresponding
 * handlers in {@see LegiScan_Process}. Used by {@see LegiScan_Import} in the
 * `legiscan-bulk.php` utility.
 *
 * @package LegiScan\API
 * @see LegiScan_Import
 * @see LegiScan_Process
 * @link https://api.legiscan.com/dl/
 * @link https://legiscan.com/datasets
 *
 */
class LegiScan_Bulk
{
    // {{{ Class Variables
    /**
     * Instance of {@see LegiScan_Process}
     *
     * @var LegiScan_Process
     */ 
    protected $logic;
    // }}}

    // {{{ __construct()
    /** 
     * Class constructor, initilizes necessary {@see LegiScan_Process} instances
     *
     */
    function __construct()
    {
        $this->logic = new LegiScan_Process();
    }
    // }}}

    // {{{ importDataset()
    /**
     * Process LegiScan session dataset ZIP archive and import into database {@see LegiScan_Import}
     *
     * @param string $zipfile
     *   Path to a zip archive from LegiScan datasets or subscriber snapshot
     *
     * @param mixed[] $params
     *   Additional parameters to control behavior and output
     *
     * @return mixed[]
     *   Array with hash and counts for the number of objects imported
     */
    function importDataset($zipfile, $params)
    {
        $dry_run = isset($params['dry_run']) ? $params['dry_run'] : false;
        $verbose = isset($params['verbose']) ? $params['verbose'] : false;
        $debug = isset($params['debug']) ? $params['debug'] : false;
        $expected_hash = isset($params['expected_hash']) ? $params['expected_hash'] : '';

        $hash = '';
        $import_stats = array();

        $timer_start = microtime(true);

        try {
            $zip = new ZipArchive();

            // Everybody likes numbers
            $import_stats = array(
                'bill' => 0,
                'people' => 0,
                'vote' => 0,
                'text' => 0,
                'amendment' => 0,
                'supplement' => 0,
            );

            $zip->open($zipfile);

            // Last file is always hash.md5
            $import_hash = $zip->getFromIndex($zip->numFiles - 1);
            $import_date = strftime('%Y-%m-%d', $zip->statIndex($zip->numFiles - 1)['mtime']);
            $import_session_id = 0;

            // If there is an expected hash, check it
            if ($expected_hash && $import_hash != $expected_hash)
            {
                throw new APIException("processDataset hash mismatch");
            }

            for ($i = 0; $i < $zip->numFiles; $i++)
            {
                $name = $zip->statIndex($i)['name'];
                $basename = basename($name);

                if (stripos($basename, '.json') !== false)
                {
                    // Decode the payload into an associative array
                    $payload = json_decode($zip->getFromIndex($i), true);

                    if ($payload)
                    {
                        // Use path information to determine payload type

                        if (stripos($name, '/bill/') !== false)
                        {
                            if ($debug) echo "\tbill {$payload['bill']['bill_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processBill($payload);
                            $import_stats['bill']++;
                            if (!$import_session_id)
                                $import_session_id = $payload['bill']['session_id'];
                        }
                        elseif (stripos($name, '/people/') !== false)
                        {
                            if ($debug) echo "\tperson {$payload['person']['people_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processPerson($payload);
                            $import_stats['people']++;
                        }
                        elseif (stripos($name, '/vote/') !== false)
                        {
                            if ($debug) echo "\troll_call {$payload['roll_call']['roll_call_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processRollCall($payload);
                            $import_stats['vote']++;
                        }
                        elseif (stripos($name, '/text/') !== false)
                        {
                            if ($debug) echo "\ttext {$payload['text']['doc_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processBillText($payload);
                            $import_stats['text']++;
                        }
                        elseif (stripos($name, '/amendment/') !== false)
                        {
                            if ($debug) echo "\tamendment {$payload['amendment']['amendment_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processAmendment($payload);
                            $import_stats['amendment']++;
                        }
                        elseif (stripos($name, '/supplement/') !== false)
                        {
                            if ($debug) echo "\tsupplement {$payload['supplement']['supplement_id']} from $basename\n";
                            if (!$dry_run) $this->logic->processSupplement($payload);
                            $import_stats['supplement']++;
                        }
                    }
                    else
                    {
                        throw new APIException("import could not decode API payload $name");
                    }
                }
            }

            $zip->close();

            $timer_end = microtime(true);
            $time_elapsed = $timer_end - $timer_start;
            $time = sec2hms(round($time_elapsed));

            $db = $this->logic->getDB();
            $sql = 'UPDATE ls_session SET import_date = :import_date, import_hash = :import_hash WHERE session_id = :session_id';
            $stmt = $db->prepare($sql);
            $stmt->bindValue(':session_id', $import_session_id, PDO::PARAM_INT);
            $stmt->bindValue(':import_hash', $import_hash, PDO::PARAM_STR);
            $stmt->bindValue(':import_date', $import_date, PDO::PARAM_STR);
            $stmt->execute();

            $stats = array();
            $stats[] = '[' . $import_session_id . ']';
            $stats[] = basename($zipfile);
            foreach ($import_stats as $k => $v)
            {
                if ($v)
                {
                    if ($verbose) echo sprintf("%10d %s\n", $v, $k);
                    $stats[] = $v . ' ' . $k;
                }
            }
            if ($verbose) echo sprintf("%10s elapsed time\n", $time);
            $stats[] = 'in ' . $time;

            $log_msg = "LegiScan Import Complete: " . implode(' ', $stats);
            LegiScan::fileLog($log_msg);

        } catch (Exception $e) {
            // Catch any other errors and push back an API error message
            $error_msg = "LegiScan Import ERROR: " . $e->getMessage() . ' in ' . basename($e->getFile()) . ' on line ' . $e->getLine();
            LegiScan::fileLog($error_msg);
            echo "$error_msg\n";
        }

        $response = array(
            'hash' => $import_hash,
            'stats' => $import_stats,
        );

        return $response;
    }
    // }}}
}
// }}}

// {{{ LegiScan_Process Class
/**
 * LegiScan API Database Process Class
 *
 * Class that processes decoded API payloads from {@link LegiScan_Bulk},
 * {@link LegiScan_Pull} or {@link LegiScan_Push} into the database. Each
 * `process*()` function maps to the corresponding API response to INSERT
 * or UPDATE the necessary data.
 *
 * @package LegiScan\API
 * @see LegiScan_CommandLine
 * @see LegiScan_Import
 * @see LegiScan_Endpoint
 * @see LegiScan_Worker
 * @link https://api.legiscan.com/dl/
 * @link https://legiscan.com/legiscan
 *
 */
class LegiScan_Process
{

    // {{{ Class Variables
    /**
     * Array of object ids that are missing and should be requested
     *
     * @var array[]
     */
    protected $missing;

    /**
     * PDO Database object handle
     *
     * @var PDO
     */
    protected $db;

    /**
     * LegiScan document cache object handle
     *
     * @var LegiScan_Cache_File
     */
    protected $cache;

    /**
     * LegiScan memory cache object handle
     *
     * @var LegiScan_Cache_Memory
     */
    protected $memcache;

    /**
     * Should dates be massaged for PostgreSQL in case of '0000-00-00'
     *
     * @var boolean
     */
    private $massage;
    // }}}

    // {{{ __construct()
    /** 
     * Class constructor, connects to database and caching layers 
     *
     * @throws PDOException
     *
     */
    function __construct()
    {
        // Setup the document cache handle
        $this->cache = new LegiScan_Cache_File('doc');

        // Grab the memory cache instance
        $this->memcache = LegiScan_Cache_Memory::getInstance();

        // Initialize the missing array
        $this->resetMissing();

        $dsn = LegiScan::getConfig('dsn');
        $username = LegiScan::getConfig('db_user');
        $password = LegiScan::getConfig('db_pass');
        $pdo_options = array(
            PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES      => false,
        );

        // For MySQL force add charset to dsn, preferred over SET NAMES
        if (stripos($dsn, 'mysql') === 0 && stripos($dsn, 'charset=utf8') === false)
            $dsn .= ';charset=utf8';

        $this->massage = (bool) LegiScan::getConfig('massage_dates', false);
        
        // PostgreSQL will need the '0000-00-00' date massaged to NULL
        if (stripos($dsn, 'pgsql') !== false)
            $this->massage = true;

        // For MSSQL force the issue
        if (stripos($dsn, 'sqlsrv') !== false)
            $this->massage = true;

        $this->db = new PDO($dsn, $username, $password, $pdo_options);
    }
    // }}}

    // {{{ processSessionList()
    /** 
     * Process a session list payload
     *
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a session list
     * 
     * @return boolean
     *   Indicating the success/failure of session list payload processing
     */
    public function processSessionList($payload)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processSessionList payload status = "' . $payload['status'] . '"');

        $sessions = $payload['sessions'];

        try {
            foreach ($sessions as $session)
            {
                if ($this->checkExists('session', $session['session_id']))
                    $sql = "UPDATE ls_session
                            SET state_id = :state_id, year_start = :year_start, year_end = :year_end,
                                prefile = :prefile, prior = :prior, sine_die = :sine_die,
                                special = :special, session_title = :session_title,
                                session_name = :session_name, session_tag = :session_tag
                            WHERE session_id = :session_id";
                else
                    $sql = "INSERT INTO ls_session (
                                session_id, state_id, year_start, year_end, prefile, prior, special,
                                sine_id, session_title, session_name, session_tag
                            ) VALUES (
                                :session_id, :state_id, :year_start, :year_end, :prefile, :prior,
                                :sine_die, :special, :session_title, :session_name, :session_tag
                            )";

                $this->db->beginTransaction();

                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(':session_id', $session['session_id'], PDO::PARAM_INT);
                $stmt->bindValue(':state_id', $session['state_id'], PDO::PARAM_INT);
                $stmt->bindValue(':year_start', $session['year_start'], PDO::PARAM_INT);
                $stmt->bindValue(':year_end', $session['year_end'], PDO::PARAM_INT);
                $stmt->bindValue(':special', $session['special'], PDO::PARAM_INT);
                $stmt->bindValue(':prefile', $session['prefile'], PDO::PARAM_INT);
                $stmt->bindValue(':sine_die', $session['sine_die'], PDO::PARAM_INT);
                $stmt->bindValue(':prior', $session['prior'], PDO::PARAM_INT);
                $stmt->bindValue(':session_title', $session['session_title'], PDO::PARAM_STR);
                $stmt->bindValue(':session_name', $session['session_name'], PDO::PARAM_STR);
                $stmt->bindValue(':session_tag', $session['session_name'], PDO::PARAM_STR);
                $stmt->execute();

                $this->db->commit();

            }
        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processSession()
    /** 
     * Process a single session payload
     *
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a session
     * 
     * @return boolean
     *   Indicating the success/failure of session payload processing
     *
     */
    public function processSession($payload)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processSession payload status = "' . $payload['status'] . '"');

        $session = $payload['session'];

        try {
            if ($this->checkExists('session', $session['session_id']))
                $sql = "UPDATE ls_session
                        SET state_id = :state_id, year_start = :year_start, year_end = :year_end,
                            prefile = :prefile, sine_die = :sine_die, prior = :prior,
                            special = :special, session_title = :session_title,
                            session_name = :session_name, session_tag = :session_tag
                        WHERE session_id = :session_id";
            else
                $sql = "INSERT INTO ls_session (
                            session_id, state_id, year_start, year_end, prefile, sine_die, prior,
                            special, session_title, session_name, session_tag
                        ) VALUES (
                            :session_id, :state_id, :year_start, :year_end, :prefile, :sine_die, :prior,
                            :special, :session_title, :session_name, :session_tag
                        )";

            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':session_id', $session['session_id'], PDO::PARAM_INT);
            $stmt->bindValue(':state_id', $session['state_id'], PDO::PARAM_INT);
            $stmt->bindValue(':year_start', $session['year_start'], PDO::PARAM_INT);
            $stmt->bindValue(':year_end', $session['year_end'], PDO::PARAM_INT);
            $stmt->bindValue(':prefile', $session['prefile'], PDO::PARAM_INT);
            $stmt->bindValue(':prior', $session['prior'], PDO::PARAM_INT);
            $stmt->bindValue(':sine_die', $session['sine_die'], PDO::PARAM_INT);
            $stmt->bindValue(':special', $session['special'], PDO::PARAM_INT);
            $stmt->bindValue(':session_title', $session['session_title'], PDO::PARAM_STR);
            $stmt->bindValue(':session_name', $session['session_name'], PDO::PARAM_STR);
            $stmt->bindValue(':session_tag', $session['session_tag'], PDO::PARAM_STR);
            $stmt->execute();

            $this->db->commit();

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processMasterList()
    /** 
     * Process a master list payload, adding any necessary missing child objects to 
     * the missing list
     *
     * @throws APIStatusException
     *
     * @param array $payload
     *   The decoded internal payload representing a master list
     *
     * @param integer $import_type 
     *   The `LegiScan::IMPORT_*` to control how bills are selected for import
     * 
     * @return integer[]
     *   Returns an array of missing/changed bill_id or FALSE on processing errors
     */
    public function processMasterList($payload, $import_type = LegiScan::IMPORT_ALL)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processMasterList payload status = "' . $payload['status'] . '"');

        $session = array_shift($payload['masterlist']);

        foreach ($payload['masterlist'] as $bill)
        {
            switch ($import_type)
            {
                case LegiScan::IMPORT_NEW:
                    if (!$this->checkExists('bill', $bill['bill_id']))
                        $this->request('bills', $bill['bill_id']);
                    break;

                case LegiScan::IMPORT_CHANGED:
                case LegiScan::IMPORT_ALL:
                    $sql = "SELECT bill_id
                            FROM ls_bill
                            WHERE bill_id = :bill_id AND change_hash = :change_hash";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':change_hash', $bill['change_hash'], PDO::PARAM_STR);
                    $stmt->execute();
                    $exists = $stmt->fetchColumn();

                    if (!$exists)
                        $this->request('bills', $bill['bill_id']);
                    break;
            }
        }

        if (isset($this->missing['bills']))
            return $this->missing['bills'];
        else
            return array();
    }
    // }}}

    // {{{ processBill()
    /**
     * Process a bill payload, adding any necessary missing child objects to 
     * the missing list
     *
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a bill
     * 
     * @return mixed[]
     *   Array of objects : ids that are missing and need to be requested
     *
     */
    public function processBill($payload)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processBill payload status = "' . $payload['status'] . '"');

        $bill = $payload['bill'];
        $bill_id = $bill['bill_id'];

        $now = date('Y-m-d H:i:s');

        try {
            $exists_id = $this->checkExists('bill', $bill['bill_id']);

            $this->db->beginTransaction();

            if (!$exists_id)
            {
                // New bill inserts are easy...

                // {{{ New Bill

                // This could use makeSQLStatement like the existing bill path,
                // instead stay verbose for clarity to familiarize users with
                // structure

                // {{{ Session
                if (!$this->checkExists('session', $bill['session_id']))
                {
                    $sql = "INSERT INTO ls_session (
                                session_id, state_id, year_start, year_end, prefile, sine_die, prior,
                                special, session_name, session_title, session_tag
                            ) VALUES (
                                :session_id, :state_id, :year_start, :year_end, :prefile, :sine_die, :prior,
                                :special, :session_name, :session_title, :session_tag
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':session_id', $bill['session_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':year_start', $bill['session']['year_start'], PDO::PARAM_INT);
                    $stmt->bindValue(':year_end', $bill['session']['year_end'], PDO::PARAM_INT);
                    $stmt->bindValue(':special', $bill['session']['special'], PDO::PARAM_INT);
                    $stmt->bindValue(':session_name', $bill['session']['session_name'], PDO::PARAM_STR);
                    $stmt->bindValue(':session_title', $bill['session']['session_title'], PDO::PARAM_STR);
                    $stmt->bindValue(':session_tag', $bill['session']['session_tag'], PDO::PARAM_STR);
                    $stmt->bindValue(':prefile', $bill['session']['prefile'], PDO::PARAM_INT);
                    $stmt->bindValue(':sine_die', $bill['session']['sine_die'], PDO::PARAM_INT);
                    $stmt->bindValue(':prior', $bill['session']['prior'], PDO::PARAM_INT);
                    $stmt->execute();
                    
                    $this->middlewareSignal('session', $bill['session_id']);
                }
                // }}}

                // {{{ Base Bill
                $sql = "INSERT INTO ls_bill (
                            bill_id, state_id, session_id, body_id, current_body_id,
                            bill_type_id, bill_number, pending_committee_id,
                            status_id, status_date, title, description,
                            legiscan_url, state_url, change_hash,
                            updated, created
                        ) VALUES (
                            :bill_id, :state_id, :session_id, :body_id, :current_body_id,
                            :bill_type_id, :bill_number, :pending_committee_id,
                            :status_id, :status_date, :title, :description,
                            :legiscan_url, :state_url, :change_hash,
                            :updated, :created
                        )";

                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                $stmt->bindValue(':session_id', $bill['session']['session_id'], PDO::PARAM_INT);
                $stmt->bindValue(':body_id', $bill['body_id'], PDO::PARAM_INT);
                $stmt->bindValue(':current_body_id', $bill['current_body_id'], PDO::PARAM_INT);
                $stmt->bindValue(':bill_type_id', $bill['bill_type_id'], PDO::PARAM_INT);
                $stmt->bindValue(':bill_number', $bill['bill_number'], PDO::PARAM_STR);
                $stmt->bindValue(':pending_committee_id', $bill['pending_committee_id'], PDO::PARAM_INT);
                $stmt->bindValue(':status_id', $bill['status'], PDO::PARAM_INT);
                $stmt->bindValue(':status_date', $this->dbDate($bill['status_date']), PDO::PARAM_STR);
                $stmt->bindValue(':title', $bill['title'], PDO::PARAM_STR);
                $stmt->bindValue(':description', $bill['description'], PDO::PARAM_STR);
                $stmt->bindValue(':legiscan_url', $bill['url'], PDO::PARAM_STR);
                $stmt->bindValue(':state_url', $bill['state_link'], PDO::PARAM_STR);
                $stmt->bindValue(':change_hash', $bill['change_hash'], PDO::PARAM_STR);
                $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                $stmt->execute();
                // }}}

                // {{{ New Committee Check
                if ($bill['pending_committee_id'] && !$this->checkExists('committee', $bill['pending_committee_id']))
                {
                    $sql = "INSERT INTO ls_committee (
                                committee_id, committee_body_id, committee_name
                            ) VALUES (
                                :committee_id, :committee_body_id, :committee_name
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':committee_id', $bill['committee']['committee_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':committee_body_id', $bill['committee']['chamber_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':committee_name', $bill['committee']['name'], PDO::PARAM_STR);
                    $stmt->execute();
                }
                // }}}

                // {{{ Referral Chain
                if (!empty($bill['referrals']))
                {
                    foreach ($bill['referrals'] as $idx => $link)
                    {
                        if (!$this->checkExists('committee', $link['committee_id']))
                        {
                            $sql = "INSERT INTO ls_committee (
                                committee_id, committee_body_id, committee_name
                            ) VALUES (
                                :committee_id, :committee_body_id, :committee_name
                            )";

                            $stmt = $this->db->prepare($sql);
                            $stmt->bindValue(':committee_id', $link['committee_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':committee_body_id', $link['chamber_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':committee_name', $link['name'], PDO::PARAM_STR);
                            $stmt->execute();
                        }

                        $step = $idx + 1; // Add 1 to 0 based index

                        $sql = "INSERT INTO ls_bill_referral (
                            bill_id, referral_step, referral_date, committee_id
                        ) VALUES (
                            :bill_id, :referral_step, :referral_date, :committee_id
                        )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':referral_step', $step, PDO::PARAM_INT);
                        $stmt->bindValue(':referral_date', $this->dbDate($link['date']), PDO::PARAM_STR);
                        $stmt->bindValue(':committee_id', $link['committee_id'], PDO::PARAM_INT);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Reasons
                if (isset($bill['reasons']))
                {
                    foreach ($bill['reasons'] as $reason_id => $reason)
                    {
                        $sql = "INSERT INTO ls_bill_reason (
                                    bill_id, reason_id, created
                                ) VALUES (
                                    :bill_id, :reason_id, :created
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':reason_id', $reason_id, PDO::PARAM_INT);
                        $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Progress
                foreach ($bill['progress'] as $step => $progress)
                {
                    $step += 1; // Add 1 to 0 based index

                    $sql = "INSERT INTO ls_bill_progress (
                                bill_id, progress_step, progress_date,
                                progress_event_id
                            ) VALUES (
                                :bill_id, :progress_step, :progress_date,
                                :progress_event_id
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':progress_step', $step, PDO::PARAM_INT);
                    $stmt->bindValue(':progress_date', $this->dbDate($progress['date']), PDO::PARAM_STR);
                    $stmt->bindValue(':progress_event_id', $progress['event'], PDO::PARAM_INT);
                    $stmt->execute();
                }
                // }}}

                // {{{ History
                foreach ($bill['history'] as $step => $history)
                {
                    $step += 1; // Add 1 to 0 based index

                    $sql = "INSERT INTO ls_bill_history (
                                bill_id, history_step, history_major,
                                history_body_id, history_date, history_action
                            ) VALUES (
                                :bill_id, :history_step, :history_major,
                                :history_body_id, :history_date, :history_action
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':history_step', $step, PDO::PARAM_INT);
                    $stmt->bindValue(':history_major', $history['importance'], PDO::PARAM_INT);
                    $stmt->bindValue(':history_body_id', $history['chamber_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':history_date', $this->dbDate($history['date']), PDO::PARAM_STR);
                    $stmt->bindValue(':history_action', $history['action'], PDO::PARAM_STR);
                    $stmt->execute();
                }
                // }}}

                // {{{ Sponsors
                foreach ($bill['sponsors'] as $person)
                {
                    $sql = "INSERT INTO ls_bill_sponsor (
                                bill_id, people_id, sponsor_order, sponsor_type_id
                            ) VALUES (
                                :bill_id, :people_id, :sponsor_order, :sponsor_type_id
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':people_id', $person['people_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':sponsor_order', $person['sponsor_order'], PDO::PARAM_INT);
                    $stmt->bindValue(':sponsor_type_id', $person['sponsor_type_id'], PDO::PARAM_INT);
                    $stmt->execute();

                    if (($people_exists_id = $this->checkExists('people', $person['people_id'])))
                        $sql = "UPDATE ls_people
                                SET state_id = :state_id, role_id = :role_id, party_id = :party_id,
                                    name = :name, first_name = :first_name, middle_name = :middle_name,
                                    last_name = :last_name, suffix = :suffix, nickname = :nickname,
                                    district = :district, committee_sponsor_id = :committee_id,
                                    votesmart_id = :votesmart_id, followthemoney_eid = :followthemoney_eid,
                                    opensecrets_id = :opensecrets_id, ballotpedia = :ballotpedia,
                                    knowwho_pid = :knowwho_pid, person_hash = :person_hash,
                                    updated = :updated
                                WHERE people_id = :people_id";
                    else
                        $sql = "INSERT INTO ls_people (
                                    people_id, state_id, role_id, party_id, name, first_name,
                                    middle_name, last_name, suffix, nickname, district,
                                    committee_sponsor_id, votesmart_id, followthemoney_eid,
                                    opensecrets_id, ballotpedia, knowwho_pid, person_hash,
                                    updated, created
                                ) VALUES (
                                    :people_id, :state_id, :role_id, :party_id, :name, :first_name,
                                    :middle_name, :last_name, :suffix, :nickname, :district,
                                    :committee_id, :votesmart_id, :followthemoney_eid,
                                    :opensecrets_id, :ballotpedia, :knowwho_pid, :person_hash,
                                    :updated, :created
                                )";

                    if ($people_exists_id)
                        $p = $this->db->query("SELECT person_hash FROM ls_people WHERE people_id = {$people_exists_id}")->fetch();

                    if (!$people_exists_id || $p['person_hash'] != $person['person_hash'])
                    {
                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':people_id', $person['people_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':role_id', $person['role_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':party_id', $person['party_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':name', $person['name'], PDO::PARAM_STR);
                        $stmt->bindValue(':first_name', $person['first_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':middle_name', $person['middle_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':last_name', $person['last_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':suffix', $person['suffix'], PDO::PARAM_STR);
                        $stmt->bindValue(':nickname', $person['nickname'], PDO::PARAM_STR);
                        $stmt->bindValue(':district', $person['district'], PDO::PARAM_STR);
                        $stmt->bindValue(':committee_id', $person['committee_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':ballotpedia', $person['ballotpedia'], PDO::PARAM_STR);
                        $stmt->bindValue(':followthemoney_eid', $person['ftm_eid'], PDO::PARAM_INT);
                        $stmt->bindValue(':votesmart_id', $person['votesmart_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':knowwho_pid', $person['knowwho_pid'], PDO::PARAM_INT);
                        $stmt->bindValue(':opensecrets_id', $person['opensecrets_id'], PDO::PARAM_STR);
                        $stmt->bindValue(':person_hash', $person['person_hash'], PDO::PARAM_STR);
                        $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                        if (!$people_exists_id)
                            $stmt->bindValue(':created', $now, PDO::PARAM_STR);

                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Votes
                foreach ($bill['votes'] as $vote)
                {
                    $sql = "INSERT INTO ls_bill_vote (
                                roll_call_id, bill_id, roll_call_body_id, roll_call_date,
                                roll_call_desc, yea, nay, nv, absent, total, passed,
                                legiscan_url, state_url, updated, created
                            ) VALUES (
                                :roll_call_id, :bill_id, :roll_call_body_id, :roll_call_date,
                                :roll_call_desc, :yea, :nay, :nv, :absent, :total, :passed,
                                :legiscan_url, :state_url, :updated, :created
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':roll_call_id', $vote['roll_call_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':roll_call_body_id', $vote['chamber_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':roll_call_date', $this->dbDate($vote['date']), PDO::PARAM_STR);
                    $stmt->bindValue(':roll_call_desc', $vote['desc'], PDO::PARAM_STR);
                    $stmt->bindValue(':yea', $vote['yea'], PDO::PARAM_INT);
                    $stmt->bindValue(':nay', $vote['nay'], PDO::PARAM_INT);
                    $stmt->bindValue(':nv', $vote['nv'], PDO::PARAM_INT);
                    $stmt->bindValue(':absent', $vote['absent'], PDO::PARAM_INT);
                    $stmt->bindValue(':total', $vote['total'], PDO::PARAM_INT);
                    $stmt->bindValue(':passed', $vote['passed'], PDO::PARAM_INT);
                    $stmt->bindValue(':legiscan_url', $vote['url'], PDO::PARAM_STR);
                    $stmt->bindValue(':state_url', $vote['state_link'], PDO::PARAM_STR);
                    $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                    $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                    $stmt->execute();

                    if (LegiScan::getConfig('want_vote_details'))
                        $this->request('votes', $vote['roll_call_id']);
                }
                // }}}

                // {{{ Texts
                foreach ($bill['texts'] as $text)
                {
                    $sql = "INSERT INTO ls_bill_text (
                                text_id, bill_id, local_copy, bill_text_type_id,
                                bill_text_mime_id, bill_text_date,
                                legiscan_url, state_url, bill_text_size, bill_text_hash,
                                updated, created
                            ) VALUES (
                                :text_id, :bill_id, :local_copy, :bill_text_type_id,
                                :bill_text_mime_id, :bill_text_date,
                                :legiscan_url, :state_url, :bill_text_size, :bill_text_hash,
                                :updated, :created
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':text_id', $text['doc_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
                    $stmt->bindValue(':bill_text_size', $text['text_size'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_text_hash', $text['text_hash'], PDO::PARAM_STR);
                    $stmt->bindValue(':bill_text_type_id', $text['type_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_text_mime_id', $text['mime_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_text_date', $this->dbDate($text['date']), PDO::PARAM_STR);
                    $stmt->bindValue(':legiscan_url', $text['url'], PDO::PARAM_STR);
                    $stmt->bindValue(':state_url', $text['state_link'], PDO::PARAM_STR);
                    $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                    $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                    $stmt->execute();

                    if (LegiScan::getConfig('want_bill_text'))
                        $this->request('texts', $text['doc_id']);
                }
                // }}}

                // {{{ Amendments
                foreach ($bill['amendments'] as $amendment)
                {
                    $sql = "INSERT INTO ls_bill_amendment (
                                amendment_id, bill_id, local_copy, adopted,
                                amendment_body_id, amendment_mime_id,
                                amendment_date, amendment_title, amendment_desc,
                                legiscan_url, state_url, amendment_size, amendment_hash,
                                updated, created
                            ) VALUES (
                                :amendment_id, :bill_id, :local_copy, :adopted,
                                :amendment_body_id, :amendment_mime_id,
                                :amendment_date, :amendment_title, :amendment_desc,
                                :legiscan_url, :state_url, :amendment_size, :amendment_hash,
                                :updated, :created
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':amendment_id', $amendment['amendment_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
                    $stmt->bindValue(':adopted', $amendment['adopted'], PDO::PARAM_INT);
                    $stmt->bindValue(':amendment_size', $amendment['amendment_size'], PDO::PARAM_INT);
                    $stmt->bindValue(':amendment_hash', $amendment['amendment_hash'], PDO::PARAM_STR);
                    $stmt->bindValue(':amendment_body_id', $amendment['chamber_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':amendment_mime_id', $amendment['mime_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':amendment_date', $this->dbDate($amendment['date']), PDO::PARAM_STR);
                    $stmt->bindValue(':amendment_title', $amendment['title'], PDO::PARAM_STR);
                    $stmt->bindValue(':amendment_desc', $amendment['description'], PDO::PARAM_STR);
                    $stmt->bindValue(':legiscan_url', $amendment['url'], PDO::PARAM_STR);
                    $stmt->bindValue(':state_url', $amendment['state_link'], PDO::PARAM_STR);
                    $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                    $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                    $stmt->execute();

                    if (LegiScan::getConfig('want_amendment'))
                        $this->request('amendments', $amendment['amendment_id']);
                }
                // }}}

                // {{{ Supplements
                foreach ($bill['supplements'] as $supplement)
                {
                    $sql = "INSERT INTO ls_bill_supplement (
                                supplement_id, bill_id, local_copy,
                                supplement_type_id, supplement_mime_id,
                                supplement_date, supplement_title, supplement_desc,
                                legiscan_url, state_url, supplement_size, supplement_hash,
                                updated, created
                            ) VALUES (
                                :supplement_id, :bill_id, :local_copy,
                                :supplement_type_id, :supplement_mime_id,
                                :supplement_date, :supplement_title, :supplement_desc,
                                :legiscan_url, :state_url, :supplement_size, :supplement_hash,
                                :updated, :created
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':supplement_id', $supplement['supplement_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_size', $supplement['supplement_size'], PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_hash', $supplement['supplement_hash'], PDO::PARAM_STR);
                    $stmt->bindValue(':supplement_type_id', $supplement['type_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_mime_id', $supplement['mime_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_date', $this->dbDate($supplement['date']), PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_title', $supplement['title'], PDO::PARAM_INT);
                    $stmt->bindValue(':supplement_desc', $supplement['description'], PDO::PARAM_INT);
                    $stmt->bindValue(':legiscan_url', $supplement['url'], PDO::PARAM_STR);
                    $stmt->bindValue(':state_url', $supplement['state_link'], PDO::PARAM_STR);
                    $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                    $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                    $stmt->execute();

                    if (LegiScan::getConfig('want_supplement'))
                        $this->request('supplements', $supplement['supplement_id']);
                }
                // }}}

                // {{{ Same As / Similar To
                foreach ($bill['sasts'] as $sast)
                {
                    $sql = "INSERT INTO ls_bill_sast (
                                bill_id, sast_type_id, sast_bill_id, sast_bill_number
                            ) VALUES (
                                :bill_id, :sast_type_id, :sast_bill_id, :sast_bill_number
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':sast_type_id', $sast['type_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':sast_bill_id', $sast['sast_bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':sast_bill_number', $sast['sast_bill_number'], PDO::PARAM_STR);
                    $stmt->execute();
                }
                // }}}

                // {{{ Subjects
                foreach ($bill['subjects'] as $subject)
                {
                    $sql = "INSERT INTO ls_bill_subject (
                                bill_id, subject_id
                            ) VALUES (
                                :bill_id, :subject_id
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':subject_id', $subject['subject_id'], PDO::PARAM_INT);
                    $stmt->execute();

                    $subject_exists_id = $this->checkExists('subject', $subject['subject_id']);
                    if (!$subject_exists_id)
                    {
                        $sql = "INSERT INTO ls_subject (
                                    subject_id, state_id, subject_name
                                ) VALUES (
                                    :subject_id, :state_id, :subject_name
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':subject_id', $subject['subject_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':subject_name', $subject['subject_name'], PDO::PARAM_STR);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Calendar
                foreach ($bill['calendar'] as $calendar)
                {
                    $event_hash = md5(strtolower("{$calendar['type']}-{$calendar['date']}-{$calendar['time']}-{$calendar['location']}-{$calendar['description']}"));
                    $event_hash = substr($event_hash, 0, 8);

                    $sql = "SELECT 1 FROM ls_bill_calendar WHERE bill_id = :bill_id AND event_hash = :event_hash";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':event_hash', $event_hash, PDO::PARAM_STR);
                    $stmt->execute();
                    if (!$stmt->fetchColumn())
                    {
                        $sql = "INSERT INTO ls_bill_calendar (
                                    bill_id, event_hash, event_type_id, event_date,
                                    event_time, event_location, event_desc,
                                    updated, created
                                ) VALUES (
                                    :bill_id, :event_hash, :event_type_id, :event_date,
                                    :event_time, :event_location, :event_desc,
                                    :updated, :created
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':event_hash', $event_hash, PDO::PARAM_STR);
                        $stmt->bindValue(':event_type_id', $calendar['type_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':event_date', $this->dbDate($calendar['date']), PDO::PARAM_STR);
                        $stmt->bindValue(':event_time', $calendar['time'], PDO::PARAM_STR);
                        $stmt->bindValue(':event_location', $calendar['location'], PDO::PARAM_STR);
                        $stmt->bindValue(':event_desc', $calendar['description'], PDO::PARAM_STR);
                        $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                        $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                        $stmt->execute();
                    }
                }
                // }}}
                // }}}
            }
            else
            {
                // Updating bills, a little less so... AKA more if statements

                // {{{ Update Bill
                $bill_old = $this->loadBill($bill['bill_id']);

                // {{{ Session
                $updates = array();

                if ($bill['session']['prefile'] != $bill_old['session']['prefile'])
                    $updates['prefile'] = array($bill['session']['prefile'], PDO::PARAM_INT);
                if ($bill['session']['sine_die'] != $bill_old['session']['sine_die'])
                    $updates['sine_die'] = array($bill['session']['sine_die'], PDO::PARAM_INT);
                if ($bill['session']['prior'] != $bill_old['session']['prior'])
                    $updates['prior'] = array($bill['session']['prior'], PDO::PARAM_INT);
    
                if ($updates)
                {
                    $key = array(
                        'session_id'=>array($bill['session']['session_id'], PDO::PARAM_INT),
                    );
                    $stmt = $this->makeSQLStatement('update', 'ls_session', $updates, $key);
                    $stmt->execute();
                    
                    $this->middlewareSignal('session', $bill['session_id']);
                }
                // }}}

                // {{{ Base Bill
                $updates = array();

                if ($bill['bill_type_id'] != $bill_old['bill_type_id'])
                    $updates['bill_type_id'] = array($bill['bill_type_id'], PDO::PARAM_INT);
                if ($bill['current_body_id'] != $bill_old['current_body_id'])
                    $updates['current_body_id'] = array($bill['current_body_id'], PDO::PARAM_INT);
                if ($bill['status'] != $bill_old['status'])
                    $updates['status_id'] = array($bill['status'], PDO::PARAM_INT);
                if ($bill['status_date'] != $bill_old['status_date'])
                    $updates['status_date'] = array($this->dbDate($bill['status_date']), PDO::PARAM_STR);
                if ($bill['title'] != $bill_old['title'])
                    $updates['title'] = array($bill['title'], PDO::PARAM_STR);
                if ($bill['description'] != $bill_old['description'])
                    $updates['description'] = array($bill['description'], PDO::PARAM_STR);
                if ($bill['pending_committee_id'] != $bill_old['pending_committee_id'])
                    $updates['pending_committee_id'] = array($bill['pending_committee_id'], PDO::PARAM_INT);
                if ($bill['change_hash'] != $bill_old['change_hash'])
                    $updates['change_hash'] = array($bill['change_hash'], PDO::PARAM_STR);

                if ($updates)
                {
                    $key = array(
                        'bill_id'=>array($bill['bill_id'], PDO::PARAM_INT),
                    );
                    $stmt = $this->makeSQLStatement('update', 'ls_bill', $updates, $key);
                    $stmt->execute();
                }
                // }}}

                // {{{ New Committee Check
                if ($bill['pending_committee_id'] && !$this->checkExists('committee', $bill['pending_committee_id']))
                {
                    $sql = "INSERT INTO ls_committee (
                                committee_id, committee_body_id, committee_name
                            ) VALUES (
                                :committee_id, :committee_body_id, :committee_name
                            )";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':committee_id', $bill['committee']['committee_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':committee_body_id', $bill['committee']['chamber_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':committee_name', $bill['committee']['name'], PDO::PARAM_STR);
                    $stmt->execute();
                }
                // }}}

                // {{{ Referral Chain
                if (!empty($bill['referrals']))
                {
                    foreach ($bill['referrals'] as $idx => $link)
                    {
                        if (!$this->checkExists('committee', $link['committee_id']))
                        {
                            $sql = "INSERT INTO ls_committee (
                                committee_id, committee_body_id, committee_name
                            ) VALUES (
                                :committee_id, :committee_body_id, :committee_name
                            )";

                            $stmt = $this->db->prepare($sql);
                            $stmt->bindValue(':committee_id', $link['committee_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':committee_body_id', $link['chamber_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':committee_name', $link['name'], PDO::PARAM_STR);
                            $stmt->execute();
                        }

                        $step = $idx + 1;
                        $updates = array();
                        $key = array(
                            'bill_id' => array($bill['bill_id'], PDO::PARAM_INT),
                            'referral_step' => array($step, PDO::PARAM_INT),
                        );

                        if (isset($bill_old['referrals'][$idx]))
                        {
                            if ($link['date'] != $bill_old['referrals'][$idx]['date'])
                                $updates['referral_date'] = array($this->dbDate($link['date']), PDO::PARAM_STR);
                            if ($link['committee_id'] != $bill_old['referrals'][$idx]['committee_id'])
                                $updates['committee_id'] = array($link['committee_id'], PDO::PARAM_INT);
                            if ($updates)
                            {
                                $stmt = $this->makeSQLStatement('update', 'ls_bill_referral', $updates, $key);
                                $stmt->execute();
                            }
                        }
                        else
                        {
                            $updates['referral_date'] = array($this->dbDate($link['date']), PDO::PARAM_STR);
                            $updates['committee_id'] = array($link['committee_id'], PDO::PARAM_STR);
                            $stmt = $this->makeSQLStatement('insert', 'ls_bill_referral', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    // Check for shrinkage, nobody likes shrinkage
                    if (($ocnt = count($bill_old['referrals'])) > ($cnt = count($bill['referrals'])))
                    {
                        $step_cut = $ocnt - $cnt + 1;
                        $sql = 'DELETE FROM ls_bill_referral WHERE bill_id = :bill_id AND referral_step > :step_cut';
                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':step_cut', $step_cut, PDO::PARAM_INT);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Reasons
                if (isset($bill['reasons']))
                {
                    foreach ($bill['reasons'] as $reason_id => $reason)
                    {
                        $sql = "INSERT INTO ls_bill_reason (
                                    bill_id, reason_id, created
                                ) VALUES (
                                    :bill_id, :reason_id, :created
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':reason_id', $reason_id, PDO::PARAM_INT);
                        $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Progress
                foreach ($bill['progress'] as $idx => $event)
                {
                    $step = $idx + 1;
                    $updates = array();
                    $key = array(
                        'bill_id' => array($bill['bill_id'], PDO::PARAM_INT),
                        'progress_step' => array($step, PDO::PARAM_INT),
                    );
                    if (isset($bill_old['progress'][$idx]))
                    {
                        if ($event['date'] != $bill_old['progress'][$idx]['date'])
                            $updates['progress_date'] = array($this->dbDate($event['date']), PDO::PARAM_STR);
                        if ($event['event'] != $bill_old['progress'][$idx]['event'])
                            $updates['progress_event_id'] = array($event['event'], PDO::PARAM_INT);
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_progress', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        $updates['progress_date'] = array($this->dbDate($event['date']), PDO::PARAM_STR);
                        $updates['progress_event_id'] = array($event['event'], PDO::PARAM_INT);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_progress', $updates, $key);
                        $stmt->execute();
                    }
                }
                // Check for shrinkage, nobody likes shrinkage
                if (($ocnt = count($bill_old['progress'])) > ($cnt = count($bill['progress'])))
                {
                    $step_cut = $ocnt - $cnt + 1;
                    $sql = 'DELETE FROM ls_bill_progress WHERE bill_id = :bill_id AND progress_step > :step_cut';
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':step_cut', $step_cut, PDO::PARAM_INT);
                    $stmt->execute();
                }
                // }}}

                // {{{ History
                foreach ($bill['history'] as $idx => $action)
                {
                    $step = $idx + 1;
                    $updates = array();
                    $key = array(
                        'bill_id' => array($bill['bill_id'], PDO::PARAM_INT),
                        'history_step' => array($step, PDO::PARAM_INT),
                    );
                    if (isset($bill_old['history'][$idx]))
                    {
                        if ($action['importance'] != $bill_old['history'][$idx]['importance'])
                            $updates['history_major'] = array($action['importance'], PDO::PARAM_INT);
                        if ($action['chamber_id'] != $bill_old['history'][$idx]['chamber_id'])
                            $updates['history_body_id'] = array($action['chamber_id'], PDO::PARAM_INT);
                        if ($action['date'] != $bill_old['history'][$idx]['date'])
                            $updates['history_date'] = array($this->dbDate($action['date']), PDO::PARAM_STR);
                        if ($action['action'] != $bill_old['history'][$idx]['action'])
                            $updates['history_action'] = array($action['action'], PDO::PARAM_STR);
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_history', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        $updates['history_major'] = array($action['importance'], PDO::PARAM_INT);
                        $updates['history_body_id'] = array($action['chamber_id'], PDO::PARAM_INT);
                        $updates['history_date'] = array($this->dbDate($action['date']), PDO::PARAM_STR);
                        $updates['history_action'] = array($action['action'], PDO::PARAM_STR);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_history', $updates, $key);
                        $stmt->execute();
                    }
                }
                // Check for shrinkage, nobody likes shrinkage
                if (($ocnt = count($bill_old['history'])) > ($cnt = count($bill['history'])))
                {
                    $step_cut = $ocnt - $cnt + 1;
                    $sql = 'DELETE FROM ls_bill_history WHERE bill_id = :bill_id AND history_step > :step_cut';
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':step_cut', $step_cut, PDO::PARAM_INT);
                    $stmt->execute();
                }
                // }}}

                // {{{ Sponsors
                foreach ($bill['sponsors'] as $idx => $person)
                {
                    $exists = false;
                    $updates = array();
                    $key = array(
                        'bill_id' => array($bill['bill_id'], PDO::PARAM_INT),
                        'people_id' => array($person['people_id'], PDO::PARAM_INT),
                    );

                    foreach ($bill_old['sponsors'] as $og_idx => $og)
                    {
                        if ($person['people_id'] == $og['people_id'])
                            $exists = true;
                    }

                    if (!$exists)
                    {
                        $updates['sponsor_order'] = array($person['sponsor_order'], PDO::PARAM_INT);
                        $updates['sponsor_type_id'] = array($person['sponsor_type_id'], PDO::PARAM_INT);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_sponsor', $updates, $key);
                        $stmt->execute();
                    }
                    else
                    {
                        if ($person['sponsor_order'] != $bill_old['sponsors'][$og_idx]['sponsor_order'])
                            $updates['sponsor_order'] = array($person['sponsor_order'], PDO::PARAM_INT);
                        if ($person['sponsor_type_id'] != $bill_old['sponsors'][$og_idx]['sponsor_type_id'])
                            $updates['sponsor_type_id'] = array($person['sponsor_type_id'], PDO::PARAM_INT);
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_sponsor', $updates, $key);
                            $stmt->execute();
                        }
                    }

                    // Keep people records updated as we see them
                    if (($people_exists_id = $this->checkExists('people', $person['people_id'])))
                        $sql = "UPDATE ls_people
                                SET state_id = :state_id, role_id = :role_id, party_id = :party_id,
                                    name = :name, first_name = :first_name, middle_name = :middle_name,
                                    last_name = :last_name, suffix = :suffix, nickname = :nickname,
                                    district = :district, committee_sponsor_id = :committee_id,
                                    votesmart_id = :votesmart_id, followthemoney_eid = :followthemoney_eid,
                                    opensecrets_id = :opensecrets_id, ballotpedia = :ballotpedia,
                                    knowwho_pid = :knowwho_pid, person_hash = :person_hash,
                                    updated = :updated
                                WHERE people_id = :people_id";
                    else
                        $sql = "INSERT INTO ls_people (
                                    people_id, state_id, role_id, party_id, name, first_name,
                                    middle_name, last_name, suffix, nickname, district,
                                    committee_sponsor_id, votesmart_id, followthemoney_eid,
                                    opensecrets_id, ballotpedia, knowwho_pid, person_hash,
                                    updated, created
                                ) VALUES (
                                    :people_id, :state_id, :role_id, :party_id, :name, :first_name,
                                    :middle_name, :last_name, :suffix, :nickname, :district,
                                    :committee_id, :votesmart_id, :followthemoney_eid,
                                    :opensecrets_id, :ballotpedia, :knowwho_pid, :person_hash,
                                    :updated, :created
                                )";

                    if ($people_exists_id)
                        $p = $this->db->query("SELECT person_hash FROM ls_people WHERE people_id = {$people_exists_id}")->fetch();

                    if (!$people_exists_id || $p['person_hash'] != $person['person_hash'])
                    {
                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':people_id', $person['people_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':role_id', $person['role_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':party_id', $person['party_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':name', $person['name'], PDO::PARAM_STR);
                        $stmt->bindValue(':first_name', $person['first_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':middle_name', $person['middle_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':last_name', $person['last_name'], PDO::PARAM_STR);
                        $stmt->bindValue(':suffix', $person['suffix'], PDO::PARAM_STR);
                        $stmt->bindValue(':nickname', $person['nickname'], PDO::PARAM_STR);
                        $stmt->bindValue(':district', $person['district'], PDO::PARAM_STR);
                        $stmt->bindValue(':committee_id', $person['committee_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':ballotpedia', $person['ballotpedia'], PDO::PARAM_STR);
                        $stmt->bindValue(':followthemoney_eid', $person['ftm_eid'], PDO::PARAM_INT);
                        $stmt->bindValue(':votesmart_id', $person['votesmart_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':knowwho_pid', $person['knowwho_pid'], PDO::PARAM_INT);
                        $stmt->bindValue(':opensecrets_id', $person['opensecrets_id'], PDO::PARAM_STR);
                        $stmt->bindValue(':person_hash', $person['person_hash'], PDO::PARAM_STR);
                        $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                        if (!$people_exists_id)
                            $stmt->bindValue(':created', $now, PDO::PARAM_STR);

                        $stmt->execute();
                    }
                }

                // Cleanup sponsors that may have been removed
                foreach ($bill_old['sponsors'] as $og)
                {
                    $exists = false;
                    foreach ($bill['sponsors'] as $person)
                    {
                        if ($og['people_id'] == $person['people_id'])
                            $exists = true;
                    }
                    if (!$exists)
                    {
                        $sql = 'DELETE FROM ls_bill_sponsor WHERE bill_id = :bill_id AND people_id = :people_id';
                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':people_id', $og['people_id'], PDO::PARAM_INT);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Votes
                foreach ($bill['votes'] as $vote)
                {
                    $updates = array();
                    $key = array(
                        'roll_call_id' => array($vote['roll_call_id'], PDO::PARAM_INT),
                    );
                    if (($exists_id = $this->checkExists('bill_vote', $vote['roll_call_id'])))
                    {
                        // See if anything has changed with an existing record
                        foreach ($bill_old['votes'] as $old_vote)
                        {
                            // Found our match
                            if ($vote['roll_call_id'] == $old_vote['roll_call_id'])
                            {
                                if ($vote['chamber_id'] != $old_vote['chamber_id'])
                                    $updates['roll_call_body_id'] = array($vote['chamber_id'], PDO::PARAM_INT);
                                if ($vote['date'] != $old_vote['date'])
                                    $updates['roll_call_date'] = array($this->dbDate($vote['date']), PDO::PARAM_STR);
                                if ($vote['desc'] != $old_vote['desc'])
                                    $updates['roll_call_desc'] = array($vote['desc'], PDO::PARAM_STR);
                                if ($vote['yea'] != $old_vote['yea'])
                                    $updates['yea'] = array($vote['yea'], PDO::PARAM_INT);
                                if ($vote['nay'] != $old_vote['nay'])
                                    $updates['nay'] = array($vote['nay'], PDO::PARAM_INT);
                                if ($vote['nv'] != $old_vote['nv'])
                                    $updates['nv'] = array($vote['nv'], PDO::PARAM_INT);
                                if ($vote['absent'] != $old_vote['absent'])
                                    $updates['absent'] = array($vote['absent'], PDO::PARAM_INT);
                                if ($vote['total'] != $old_vote['total'])
                                    $updates['total'] = array($vote['total'], PDO::PARAM_INT);
                                if ($vote['passed'] != $old_vote['passed'])
                                    $updates['passed'] = array($vote['passed'], PDO::PARAM_INT);
                                if ($vote['url'] != $old_vote['url'])
                                    $updates['legiscan_url'] = array($vote['url'], PDO::PARAM_STR);
                                if ($vote['state_link'] != $old_vote['state_link'])
                                    $updates['state_url'] = array($vote['state_link'], PDO::PARAM_STR);
                                break;
                            }
                        }
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_vote', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        // New roll call
                        $updates['roll_call_id'] = array($vote['roll_call_id'], PDO::PARAM_INT);
                        $updates['bill_id'] = array($bill['bill_id'], PDO::PARAM_INT);
                        $updates['roll_call_body_id'] = array($vote['chamber_id'], PDO::PARAM_INT);
                        $updates['roll_call_date'] = array($this->dbDate($vote['date']), PDO::PARAM_STR);
                        $updates['roll_call_desc'] = array($vote['desc'], PDO::PARAM_STR);
                        $updates['yea'] = array($vote['yea'], PDO::PARAM_INT);
                        $updates['nay'] = array($vote['nay'], PDO::PARAM_INT);
                        $updates['nv'] = array($vote['nv'], PDO::PARAM_INT);
                        $updates['absent'] = array($vote['absent'], PDO::PARAM_INT);
                        $updates['total'] = array($vote['total'], PDO::PARAM_INT);
                        $updates['passed'] = array($vote['passed'], PDO::PARAM_INT);
                        $updates['legiscan_url'] = array($vote['url'], PDO::PARAM_STR);
                        $updates['state_url'] = array($vote['state_link'], PDO::PARAM_STR);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_vote', $updates, $key);
                        $stmt->execute();

                        // If this is new and we want vote details, add to missing list
                        if (LegiScan::getConfig('want_vote_details'))
                            $this->request('votes', $vote['roll_call_id']);
                    }
                }
                // }}}

                // {{{ Texts
                foreach ($bill['texts'] as $text)
                {
                    $updates = array();
                    $key = array(
                        'text_id' => array($text['doc_id'], PDO::PARAM_INT),
                    );
                    if (($exists_id = $this->checkExists('bill_text', $text['doc_id'])))
                    {
                        // See if anything has changed with an existing record
                        foreach ($bill_old['texts'] as $old_text)
                        {
                            // Found our match
                            if ($text['doc_id'] == $old_text['doc_id'])
                            {
                                if ($text['date'] != $old_text['date'])
                                    $updates['bill_text_date'] = array($this->dbDate($text['date']), PDO::PARAM_STR);
                                if ($text['type_id'] != $old_text['type_id'])
                                    $updates['bill_text_type_id'] = array($text['type_id'], PDO::PARAM_INT);
                                if ($text['mime_id'] != $old_text['mime_id'])
                                    $updates['bill_text_mime_id'] = array($text['mime_id'], PDO::PARAM_INT);
                                if ($text['url'] != $old_text['url'])
                                    $updates['legiscan_url'] = array($text['url'], PDO::PARAM_STR);
                                if ($text['state_link'] != $old_text['state_link'])
                                    $updates['state_url'] = array($text['state_link'], PDO::PARAM_STR);
                                if ($text['text_size'] != $old_text['text_size'])
                                    $updates['bill_text_size'] = array($text['text_size'], PDO::PARAM_INT);
                                if ($text['text_hash'] != $old_text['text_hash'])
                                    $updates['bill_text_hash'] = array($text['text_hash'], PDO::PARAM_STR);
                                break;
                            }
                        }
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_text', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        // New bill text
                        $updates['text_id'] = array($text['doc_id'], PDO::PARAM_INT);
                        $updates['bill_id'] = array($bill['bill_id'], PDO::PARAM_INT);
                        $updates['local_copy'] = array(0, PDO::PARAM_INT);
                        $updates['bill_text_size'] = array($text['text_size'], PDO::PARAM_INT);
                        $updates['bill_text_hash'] = array($text['text_hash'], PDO::PARAM_STR);
                        $updates['bill_text_type_id'] = array($text['type_id'], PDO::PARAM_INT);
                        $updates['bill_text_mime_id'] = array($text['mime_id'], PDO::PARAM_INT);
                        $updates['bill_text_date'] = array($this->dbDate($text['date']), PDO::PARAM_STR);
                        $updates['legiscan_url'] = array($text['url'], PDO::PARAM_STR);
                        $updates['state_url'] = array($text['state_link'], PDO::PARAM_STR);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_text', $updates, $key);
                        $stmt->execute();

                        // If this is new and we want texts, add to missing list
                        if (LegiScan::getConfig('want_bill_text'))
                            $this->request('texts', $text['doc_id']);
                    }
                }
                // }}}

                // {{{ Amendments
                foreach ($bill['amendments'] as $amendment)
                {
                    $updates = array();
                    $key = array(
                        'amendment_id' => array($amendment['amendment_id'], PDO::PARAM_INT),
                    );
                    if (($exists_id = $this->checkExists('bill_amendment', $amendment['amendment_id'])))
                    {
                        // See if anything has changed with an existing record
                        foreach ($bill_old['amendments'] as $old_amendment)
                        {
                            // Found our match
                            if ($amendment['amendment_id'] == $old_amendment['amendment_id'])
                            {
                                if ($amendment['adopted'] != $old_amendment['adopted'])
                                    $updates['adopted'] = array($amendment['adopted'], PDO::PARAM_INT);
                                if ($amendment['chamber_id'] != $old_amendment['chamber_id'])
                                    $updates['amendment_body_id'] = array($amendment['chamber_id'], PDO::PARAM_INT);
                                if ($amendment['mime_id'] != $old_amendment['mime_id'])
                                    $updates['amendment_mime_id'] = array($amendment['mime_id'], PDO::PARAM_INT);
                                if ($amendment['date'] != $old_amendment['date'])
                                    $updates['amendment_date'] = array($this->dbDate($amendment['date']), PDO::PARAM_STR);
                                if ($amendment['title'] != $old_amendment['title'])
                                    $updates['amendment_title'] = array($amendment['title'], PDO::PARAM_STR);
                                if ($amendment['description'] != $old_amendment['description'])
                                    $updates['amendment_desc'] = array($amendment['description'], PDO::PARAM_STR);
                                if ($amendment['url'] != $old_amendment['url'])
                                    $updates['legiscan_url'] = array($amendment['url'], PDO::PARAM_STR);
                                if ($amendment['state_link'] != $old_amendment['state_link'])
                                    $updates['state_url'] = array($amendment['state_link'], PDO::PARAM_STR);
                                if ($amendment['amendment_size'] != $old_amendment['amendment_size'])
                                    $updates['amendment_size'] = array($amendment['amendment_size'], PDO::PARAM_INT);
                                if ($amendment['amendment_hash'] != $old_amendment['amendment_hash'])
                                    $updates['amendment_hash'] = array($amendment['amendment_hash'], PDO::PARAM_STR);
                                break;
                            }
                        }
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_amendment', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        // New bill amendment
                        $updates['amendment_id'] = array($amendment['amendment_id'], PDO::PARAM_INT);
                        $updates['bill_id'] = array($bill['bill_id'], PDO::PARAM_INT);
                        $updates['local_copy'] = array(0, PDO::PARAM_INT);
                        $updates['adopted'] = array($amendment['adopted'], PDO::PARAM_INT);
                        $updates['amendment_size'] = array($amendment['amendment_size'], PDO::PARAM_INT);
                        $updates['amendment_hash'] = array($amendment['amendment_hash'], PDO::PARAM_STR);
                        $updates['amendment_body_id'] = array($amendment['chamber_id'], PDO::PARAM_INT);
                        $updates['amendment_mime_id'] = array($amendment['mime_id'], PDO::PARAM_INT);
                        $updates['amendment_date'] = array($this->dbDate($amendment['date']), PDO::PARAM_STR);
                        $updates['amendment_title'] = array($amendment['title'], PDO::PARAM_STR);
                        $updates['amendment_desc'] = array($amendment['description'], PDO::PARAM_STR);
                        $updates['legiscan_url'] = array($amendment['url'], PDO::PARAM_STR);
                        $updates['state_url'] = array($amendment['state_link'], PDO::PARAM_STR);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_amendment', $updates, $key);
                        $stmt->execute();

                        // If this is new and we want amendments, add to missing list
                        if (LegiScan::getConfig('want_amendment'))
                            $this->request('amendments', $amendment['amendment_id']);
                    }
                }
                // }}}

                // {{{ Supplements
                foreach ($bill['supplements'] as $supplement)
                {
                    $updates = array();
                    $key = array(
                        'supplement_id' => array($supplement['supplement_id'], PDO::PARAM_INT),
                    );
                    if (($exists_id = $this->checkExists('bill_supplement', $supplement['supplement_id'])))
                    {
                        // See if anything has changed with an existing record
                        foreach ($bill_old['supplements'] as $old_supplement)
                        {
                            // Found our match
                            if ($supplement['supplement_id'] == $old_supplement['supplement_id'])
                            {
                                if ($supplement['type_id'] != $old_supplement['type_id'])
                                    $updates['supplement_type_id'] = array($supplement['type_id'], PDO::PARAM_INT);
                                if ($supplement['mime_id'] != $old_supplement['mime_id'])
                                    $updates['supplement_mime_id'] = array($supplement['mime_id'], PDO::PARAM_INT);
                                if ($supplement['date'] != $old_supplement['date'])
                                    $updates['supplement_date'] = array($this->dbDate($supplement['date']), PDO::PARAM_STR);
                                if ($supplement['title'] != $old_supplement['title'])
                                    $updates['supplement_title'] = array($supplement['title'], PDO::PARAM_STR);
                                if ($supplement['description'] != $old_supplement['description'])
                                    $updates['supplement_desc'] = array($supplement['description'], PDO::PARAM_STR);
                                if ($supplement['url'] != $old_supplement['url'])
                                    $updates['legiscan_url'] = array($supplement['url'], PDO::PARAM_STR);
                                if ($supplement['state_link'] != $old_supplement['state_link'])
                                    $updates['state_url'] = array($supplement['state_link'], PDO::PARAM_STR);
                                if ($supplement['supplement_size'] != $old_supplement['supplement_size'])
                                    $updates['supplement_size'] = array($supplement['supplement_size'], PDO::PARAM_INT);
                                if ($supplement['supplement_hash'] != $old_supplement['supplement_hash'])
                                    $updates['supplement_hash'] = array($supplement['supplement_hash'], PDO::PARAM_STR);
                                break;
                            }
                        }
                        if ($updates)
                        {
                            $stmt = $this->makeSQLStatement('update', 'ls_bill_supplement', $updates, $key);
                            $stmt->execute();
                        }
                    }
                    else
                    {
                        // New bill supplement
                        $updates['supplement_id'] = array($supplement['supplement_id'], PDO::PARAM_INT);
                        $updates['bill_id'] = array($bill['bill_id'], PDO::PARAM_INT);
                        $updates['local_copy'] = array(0, PDO::PARAM_INT);
                        $updates['supplement_size'] = array($supplement['supplement_size'], PDO::PARAM_INT);
                        $updates['supplement_hash'] = array($supplement['supplement_hash'], PDO::PARAM_STR);
                        $updates['supplement_type_id'] = array($supplement['type_id'], PDO::PARAM_INT);
                        $updates['supplement_mime_id'] = array($supplement['mime_id'], PDO::PARAM_INT);
                        $updates['supplement_date'] = array($this->dbDate($supplement['date']), PDO::PARAM_INT);
                        $updates['supplement_title'] = array($supplement['title'], PDO::PARAM_INT);
                        $updates['supplement_desc'] = array($supplement['description'], PDO::PARAM_STR);
                        $updates['legiscan_url'] = array($supplement['url'], PDO::PARAM_STR);
                        $updates['state_url'] = array($supplement['state_link'], PDO::PARAM_STR);
                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_supplement', $updates, $key);
                        $stmt->execute();

                        // If this is new and we want supplements, add to missing list
                        if (LegiScan::getConfig('want_supplement'))
                            $this->request('supplements', $supplement['supplement_id']);
                    }
                }
                // }}}

                // {{{ Same As / Similar To
                $sast_ids = $old_sast_ids = array();
                foreach ($bill['sasts'] as $sast)
                    $sast_ids[] = $sast['sast_bill_id'];
                foreach ($bill_old['sasts'] as $old_sast)
                    $old_sast_ids[] = $old_sast['sast_bill_id'];
                foreach ($bill['sasts'] as $sast)
                {
                    if (!in_array($sast['sast_bill_id'], $old_sast_ids))
                    {
                        $sql = "INSERT INTO ls_bill_sast (
                                    bill_id, sast_type_id, sast_bill_id, sast_bill_number
                                ) VALUES (
                                    :bill_id, :sast_type_id, :sast_bill_id, :sast_bill_number
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':sast_type_id', $sast['type_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':sast_bill_id', $sast['sast_bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':sast_bill_number', $sast['sast_bill_number'], PDO::PARAM_STR);
                        $stmt->execute();
                    }
                }
                foreach ($bill_old['sasts'] as $old_sast)
                {
                    if (!in_array($old_sast['sast_bill_id'], $sast_ids))
                    {
                        $sql = "DELETE FROM ls_bill_sast WHERE bill_id = :bill_id AND sast_bill_id = :sast_bill_id AND sast_type_id = :sast_type_id";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':sast_bill_id', $old_sast['sast_bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':sast_type_id', $old_sast['sast_type_id'], PDO::PARAM_INT);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Subject
                $subject_ids = $old_subject_ids = array();
                foreach ($bill['subjects'] as $subject)
                    $subject_ids[] = $subject['subject_id'];
                foreach ($bill_old['subjects'] as $old_subject)
                    $old_subject_ids[] = $old_subject['subject_id'];
                foreach ($bill['subjects'] as $subject)
                {
                    if (!in_array($subject['subject_id'], $old_subject_ids))
                    {
                        $sql = "INSERT INTO ls_bill_subject (
                                    bill_id, subject_id
                                ) VALUES (
                                    :bill_id, :subject_id
                                )";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':subject_id', $subject['subject_id'], PDO::PARAM_INT);
                        $stmt->execute();

                        $subject_exists_id = $this->checkExists('subject', $subject['subject_id']);
                        if (!$subject_exists_id)
                        {
                            $sql = "INSERT INTO ls_subject (
                                        subject_id, state_id, subject_name
                                    ) VALUES (
                                        :subject_id, :state_id, :subject_name
                                    )";

                            $stmt = $this->db->prepare($sql);
                            $stmt->bindValue(':subject_id', $subject['subject_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':state_id', $bill['state_id'], PDO::PARAM_INT);
                            $stmt->bindValue(':subject_name', $subject['subject_name'], PDO::PARAM_STR);
                            $stmt->execute();
                        }
                    }
                }
                foreach ($bill_old['subjects'] as $old_subject)
                {
                    if (!in_array($old_subject['subject_id'], $subject_ids))
                    {
                        $sql = "DELETE FROM ls_bill_subject WHERE bill_id = :bill_id AND subject_id = :subject_id";

                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':subject_id', $old_subject['subject_id'], PDO::PARAM_INT);
                        $stmt->execute();
                    }
                }
                // }}}

                // {{{ Calendar
                foreach ($bill['calendar'] as $cal)
                {
                    $event_hash = md5(strtolower("{$cal['type']}-{$cal['date']}-{$cal['time']}-{$cal['location']}-{$cal['description']}"));
                    $event_hash = substr($event_hash, 0, 8);

                    // Avoid a hash collision
                    $sql = "SELECT 1 FROM ls_bill_calendar WHERE bill_id = :bill_id AND event_hash = :event_hash";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':event_hash', $event_hash, PDO::PARAM_STR);
                    $stmt->execute();

                    if (!$stmt->fetchColumn())
                    {
                        $key = array(
                            'bill_id' => array($bill['bill_id'], PDO::PARAM_INT),
                            'event_hash' => array($event_hash, PDO::PARAM_STR),
                        );
                        
                        $updates = array();
                        $updates['event_type_id'] = array($cal['type_id'], PDO::PARAM_INT);
                        $updates['event_date'] = array($this->dbDate($cal['date']), PDO::PARAM_STR);
                        $updates['event_time'] = array($cal['time'], PDO::PARAM_STR);
                        $updates['event_location'] = array($cal['location'], PDO::PARAM_STR);
                        $updates['event_desc'] = array($cal['description'], PDO::PARAM_STR);

                        $stmt = $this->makeSQLStatement('insert', 'ls_bill_calendar', $updates, $key);
                        $stmt->execute();
                    }
                }
                // }}}
                // }}}
            }

            $this->db->commit();

            $this->middlewareSignal('bill', $bill['bill_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        }

        if (!empty($this->missing))
            return $this->missing;
        else
            return array();
    }
    // }}}

    // {{{ processBillText()
    /** 
     * Process a bill text payload
     *
     * @throws APIException
     * @throws APIAccessException
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a bill text
     * 
     * @return boolean
     *   Indicating the success/failure of bill text payload processing
     *
     */
    public function processBillText($payload)
    {
        $this->resetMissing(); // For consistency

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processBillText payload status = "' . $payload['status'] . '"');

        $text = $payload['text'];

        $now = date('Y-m-d H:i:s');
            
        try {
            if (($exists_id = $this->checkExists('bill_text', $text['doc_id'])))
                $sql = "UPDATE ls_bill_text
                        SET bill_id = :bill_id,
                            local_copy = :local_copy,
                            bill_text_type_id = :bill_text_type_id,
                            bill_text_mime_id = :bill_text_mime_id, 
                            bill_text_date = :bill_text_date,
                            bill_text_size = :bill_text_size,
                            bill_text_hash = :bill_text_hash,
                            updated = :updated 
                        WHERE text_id = :text_id";
            else
                $sql = "INSERT INTO ls_bill_text (
                            text_id, bill_id, local_copy, bill_text_type_id,
                            bill_text_mime_id, bill_text_date, bill_text_size, bill_text_hash,
                            updated, created
                        ) VALUES (
                            :text_id, :bill_id, :local_copy, :bill_text_type_id,
                            :bill_text_mime_id, :bill_text_date, :bill_text_size, :bill_text_hash,
                            :updated, :created
                        )";

            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':text_id', $text['doc_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_id', $text['bill_id'], PDO::PARAM_INT);
            $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
            $stmt->bindValue(':bill_text_size', $text['text_size'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_text_hash', $text['text_hash'], PDO::PARAM_STR);
            $stmt->bindValue(':bill_text_type_id', $text['type_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_text_mime_id', $text['mime_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_text_date', $this->dbDate($text['date']), PDO::PARAM_STR);
            $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
            if (!$exists_id)
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            if ($text['doc'] != '')
            {
                $blob = base64_decode($text['doc']);
                if ($blob !== false)
                {
                    $cache_file = $this->getCacheFilename('text', $text['doc_id']);
                    $this->cache->set($cache_file, $blob);
                    $sql = "UPDATE ls_bill_text
                            SET local_copy = :local_copy, local_fragment = :local_fragment
                            WHERE text_id = :text_id";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':local_copy', 1, PDO::PARAM_INT);
                    $stmt->bindValue(':local_fragment', $cache_file, PDO::PARAM_STR);
                    $stmt->bindValue(':text_id', $text['doc_id'], PDO::PARAM_INT);
                    $stmt->execute();   
                }
                else
                {
                    throw new APIException('Could not decode bill text blob');
                }
            }

            $this->db->commit();

            $this->middlewareSignal('text', $text['doc_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        } catch (APIAccessException $e) {
            $this->db->rollback();

            throw($e);
        } catch (APIException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processAmendment()
    /** 
     * Process an amendment payload
     *
     * @throws APIException
     * @throws APIAccessException
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing an amendment
     * 
     * @return boolean
     *   Indicating the success/failure of amendment payload processing
     *
     */
    public function processAmendment($payload)
    {
        $this->resetMissing(); // For consistency

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processAmendment payload status = "' . $payload['status'] . '"');

        $amendment = $payload['amendment'];

        try {
            if (($exists_id = $this->checkExists('bill_amendment', $amendment['amendment_id'])))
                $sql = "UPDATE ls_bill_amendment
                        SET bill_id = :bill_id,
                            local_copy = :local_copy,
                            adopted = :adopted,
                            amendment_body_id = :amendment_body_id,
                            amendment_mime_id = :amendment_mime_id,
                            amendment_date = :amendment_date,
                            amendment_title = :amendment_title,
                            amendment_desc = :amendment_desc,
                            amendment_size = :amendment_size,
                            amendment_hash = :amendment_hash,
                            updated = :updated
                        WHERE amendment_id = :amendment_id";
            else
                $sql = "INSERT INTO ls_bill_amendment (
                            amendment_id, bill_id, local_copy, adopted,
                            amendment_body_id, amendment_mime_id, amendment_date,
                            amendment_title, amendment_desc, amendment_size, amendment_hash,
                            updated, created
                        ) VALUES (
                            :amendment_id, :bill_id, :local_copy, :adopted,
                            :amendment_body_id, :amendment_mime_id, :amendment_date,
                            :amendment_title, :amendment_desc, :amendment_size, :amendment_hash,
                            :updated, :created
                        )";

            $now = date('Y-m-d H:i:s');
            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':amendment_id', $amendment['amendment_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_id', $amendment['bill_id'], PDO::PARAM_INT);
            $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
            $stmt->bindValue(':adopted', $amendment['adopted'], PDO::PARAM_INT);
            $stmt->bindValue(':amendment_size', $amendment['amendment_size'], PDO::PARAM_INT);
            $stmt->bindValue(':amendment_hash', $amendment['amendment_hash'], PDO::PARAM_STR);
            $stmt->bindValue(':amendment_body_id', $amendment['chamber_id'], PDO::PARAM_INT);
            $stmt->bindValue(':amendment_mime_id', $amendment['mime_id'], PDO::PARAM_INT);
            $stmt->bindValue(':amendment_date', $this->dbDate($amendment['date']), PDO::PARAM_STR);
            $stmt->bindValue(':amendment_title', $amendment['title'], PDO::PARAM_STR);
            $stmt->bindValue(':amendment_desc', $amendment['description'], PDO::PARAM_STR);
            $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
            if (!$exists_id)
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);

            $stmt->execute();

            if ($amendment['doc'] != '')
            {
                $blob = base64_decode($amendment['doc']);
                if ($blob !== false)
                {
                    $cache_file = $this->getCacheFilename('amendment', $amendment['amendment_id']);
                    $this->cache->set($cache_file, $blob);
                    $sql = "UPDATE ls_bill_amendment
                            SET local_copy = :local_copy, local_fragment = :local_fragment
                            WHERE amendment_id = :amendment_id";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':local_copy', 1, PDO::PARAM_INT);
                    $stmt->bindValue(':local_fragment', $cache_file, PDO::PARAM_STR);
                    $stmt->bindValue(':amendment_id', $amendment['amendment_id'], PDO::PARAM_INT);
                    $stmt->execute();   
                }
                else
                {
                    throw new APIException('Could not decode amendment text blob');
                }
            }

            $this->db->commit();

            $this->middlewareSignal('amendment', $amendment['amendment_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        } catch (APIAccessException $e) {
            $this->db->rollback();

            throw($e);
        } catch (APIException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processSupplement()
    /** 
     * Process a supplement payload
     *
     * @throws APIException
     * @throws APIAccessException
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a supplement
     * 
     * @return boolean
     *   Indicating the success/failure of supplement payload processing
     */
    public function processSupplement($payload)
    {
        $this->resetMissing(); // For consistency

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processSupplement payload status = "' . $payload['status'] . '"');

        $supplement = $payload['supplement'];

        try {
            if (($exists_id = $this->checkExists('bill_supplement', $supplement['supplement_id'])))
                $sql = "UPDATE ls_bill_supplement
                        SET bill_id = :bill_id, local_copy = :local_copy,
                            supplement_type_id = :supplement_type_id,
                            supplement_mime_id = :supplement_mime_id,
                            supplement_date = :supplement_date,
                            supplement_title = :supplement_title,
                            supplement_desc = :supplement_desc,
                            supplement_size = :supplement_size,
                            supplement_hash = :supplement_hash,
                            updated = :updated
                        WHERE supplement_id = :supplement_id";
            else
                $sql = "INSERT INTO ls_bill_supplement (
                            supplement_id, bill_id, local_copy, supplement_type_id,
                            supplement_mime_id, supplement_date, supplement_title,
                            supplement_desc, supplement_size, supplement_hash,
                            updated, created
                        ) VALUES (
                            :supplement_id, :bill_id, :local_copy, :supplement_type_id,
                            :supplement_mime_id, :supplement_date, :supplement_title,
                            :supplement_desc, :supplement_size, :supplement_hash,
                            :updated, :created
                        )";

            $now = date('Y-m-d H:i:s');

            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':supplement_id', $supplement['supplement_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_id', $supplement['bill_id'], PDO::PARAM_INT);
            $stmt->bindValue(':local_copy', 0, PDO::PARAM_INT);
            $stmt->bindValue(':supplement_size', $supplement['supplement_size'], PDO::PARAM_INT);
            $stmt->bindValue(':supplement_hash', $supplement['supplement_hash'], PDO::PARAM_STR);
            $stmt->bindValue(':supplement_type_id', $supplement['type_id'], PDO::PARAM_INT);
            $stmt->bindValue(':supplement_mime_id', $supplement['mime_id'], PDO::PARAM_INT);
            $stmt->bindValue(':supplement_date', $this->dbDate($supplement['date']), PDO::PARAM_STR);
            $stmt->bindValue(':supplement_title', $supplement['title'], PDO::PARAM_STR);
            $stmt->bindValue(':supplement_desc', $supplement['description'], PDO::PARAM_STR);
            $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
            if (!$exists_id)
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            if ($supplement['doc'] != '')
            {
                $blob = base64_decode($supplement['doc']);
                if ($blob !== false)
                {
                    $cache_file = $this->getCacheFilename('supplement', $supplement['supplement_id']);
                    $this->cache->set($cache_file, $blob);
                    $sql = "UPDATE ls_bill_supplement
                            SET local_copy = :local_copy, local_fragment = :local_fragment
                            WHERE supplement_id = :supplement_id";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':local_copy', 1, PDO::PARAM_INT);
                    $stmt->bindValue(':local_fragment', $cache_file, PDO::PARAM_STR);
                    $stmt->bindValue(':supplement_id', $supplement['supplement_id'], PDO::PARAM_INT);
                    $stmt->execute();   
                }
                else
                {
                    throw new APIException('Could not decode supplement text blob');
                }
            }

            $this->db->commit();

            $this->middlewareSignal('supplement', $supplement['supplement_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);

        } catch (APIAccessException $e) {
            $this->db->rollback();

            throw($e);

        } catch (APIException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processRollCall()
    /** 
     * Process a roll call payload
     *
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a roll call
     * 
     * @return integer[]
     *   Returns a list of missing people_id FALSE on processing errors
     *
     */
    public function processRollCall($payload)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processRollCall payload status = "' . $payload['status'] . '"');

        $roll_call = $payload['roll_call'];

        try {
            if (($exists_id = $this->checkExists('bill_vote', $roll_call['roll_call_id'])))
                $sql = "UPDATE ls_bill_vote
                        SET bill_id = :bill_id, roll_call_body_id = :roll_call_body_id,
                            roll_call_date = :roll_call_date,
                            roll_call_desc = :roll_call_desc,
                            yea = :yea, nay = :nay,
                            nv = :nv, absent = :absent,
                            total = :total, passed = :passed,
                            updated = :updated
                        WHERE roll_call_id = :roll_call_id";
            else
                $sql = "INSERT INTO ls_bill_vote (
                            roll_call_id, bill_id, roll_call_body_id, roll_call_date,
                            roll_call_desc, yea, nay, nv, absent, total, passed,
                            updated, created
                        ) VALUES (
                            :roll_call_id, :bill_id, :roll_call_body_id, :roll_call_date,
                            :roll_call_desc, :yea, :nay, :nv, :absent, :total, :passed,
                            :updated, :created
                        )";

            $now = date('Y-m-d H:i:s');

            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':roll_call_id', $roll_call['roll_call_id'], PDO::PARAM_INT);
            $stmt->bindValue(':bill_id', $roll_call['bill_id'], PDO::PARAM_INT);
            $stmt->bindValue(':roll_call_body_id', $roll_call['chamber_id'], PDO::PARAM_INT);
            $stmt->bindValue(':roll_call_date', $this->dbDate($roll_call['date']), PDO::PARAM_STR);
            $stmt->bindValue(':roll_call_desc', $roll_call['desc'], PDO::PARAM_STR);
            $stmt->bindValue(':yea', $roll_call['yea'], PDO::PARAM_INT);
            $stmt->bindValue(':nay', $roll_call['nay'], PDO::PARAM_INT);
            $stmt->bindValue(':nv', $roll_call['nv'], PDO::PARAM_INT);
            $stmt->bindValue(':absent', $roll_call['absent'], PDO::PARAM_INT);
            $stmt->bindValue(':total', $roll_call['total'], PDO::PARAM_INT);
            $stmt->bindValue(':passed', $roll_call['passed'], PDO::PARAM_INT);
            $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
            if (!$exists_id)
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            // Clear out old data
            if ($exists_id)
                $this->db
                    ->prepare('DELETE FROM ls_bill_vote_detail WHERE roll_call_id = :roll_call_id')
                    ->execute([$roll_call['roll_call_id']]);

            foreach ($roll_call['votes'] as $vote)
            {
                $sql = "INSERT INTO ls_bill_vote_detail (
                            roll_call_id, people_id, vote_id
                        ) VALUES (
                            :roll_call_id, :people_id, :vote_id
                        )";
                $stmt = $this->db->prepare($sql);

                $stmt->bindValue(':roll_call_id', $roll_call['roll_call_id'], PDO::PARAM_INT);
                $stmt->bindValue(':people_id', $vote['people_id'], PDO::PARAM_INT);
                $stmt->bindValue(':vote_id', $vote['vote_id'], PDO::PARAM_INT);

                $stmt->execute();

                if (!$this->checkExists('people', $vote['people_id']))
                    $this->request('sponsors', $vote['people_id']);
            }

            $this->db->commit();

            $this->middlewareSignal('rollcall', $roll_call['roll_call_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        }

        if (isset($this->missing['sponsors']))
            return $this->missing['sponsors'];
        else
            return array();
    }
    // }}}

    // {{{ processPerson()
    /** 
     * Process a person payload
     *
     * @throws APIStatusException
     * @throws PDOException
     *
     * @param array $payload
     *   The decoded internal payload representing a person
     * 
     * @return boolean
     *   Indicating the success/failure of person payload processing
     */
    public function processPerson($payload)
    {
        $this->resetMissing(); // For consistency

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processPerson payload status = "' . $payload['status'] . '"');

        $person = $payload['person'];

        $now = date('Y-m-d H:i:s');

        try {
            if (($exists_id = $this->checkExists('people', $person['people_id'])))
                $sql = "UPDATE ls_people
                        SET state_id = :state_id, role_id = :role_id,
                            party_id = :party_id, name = :name,
                            first_name = :first_name, middle_name = :middle_name,
                            last_name = :last_name, suffix = :suffix,
                            nickname = :nickname, district = :district,
                            committee_sponsor_id = :committee_sponsor_id,
                            votesmart_id = :votesmart_id, followthemoney_eid = :followthemoney_eid,
                            opensecrets_id = :opensecrets_id, ballotpedia = :ballotpedia,
                            knowwho_pid = :knowwho_pid, person_hash = :person_hash,
                            updated = :updated
                        WHERE people_id = :people_id";
            else
                $sql = "INSERT INTO ls_people (
                            people_id, state_id, role_id, party_id, name,
                            first_name, middle_name, last_name, suffix,
                            nickname, district, committee_sponsor_id,
                            votesmart_id, followthemoney_eid, opensecrets_id,
                            ballotpedia, knowwho_pid, person_hash, updated, created
                        ) VALUES (
                            :people_id, :state_id, :role_id, :party_id, :name,
                            :first_name, :middle_name, :last_name, :suffix,
                            :nickname, :district, :committee_sponsor_id,
                            :votesmart_id, :followthemoney_eid, :opensecrets_id,
                            :ballotpedia, :knowwho_pid, :person_hash, :updated, :created
                        )";

            $this->db->beginTransaction();

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':people_id', $person['people_id'], PDO::PARAM_INT);
            $stmt->bindValue(':state_id', $person['state_id'], PDO::PARAM_INT);
            $stmt->bindValue(':role_id', $person['role_id'], PDO::PARAM_INT);
            $stmt->bindValue(':party_id', $person['party_id'], PDO::PARAM_INT);
            $stmt->bindValue(':name', $person['name'], PDO::PARAM_STR);
            $stmt->bindValue(':first_name', $person['first_name'], PDO::PARAM_STR);
            $stmt->bindValue(':middle_name', $person['middle_name'], PDO::PARAM_STR);
            $stmt->bindValue(':last_name', $person['last_name'], PDO::PARAM_STR);
            $stmt->bindValue(':suffix', $person['suffix'], PDO::PARAM_STR);
            $stmt->bindValue(':nickname', $person['nickname'], PDO::PARAM_STR);
            $stmt->bindValue(':district', $person['district'], PDO::PARAM_STR);
            $stmt->bindValue(':committee_sponsor_id', $person['committee_id'], PDO::PARAM_INT);
            $stmt->bindValue(':ballotpedia', $person['ballotpedia'], PDO::PARAM_STR);
            $stmt->bindValue(':followthemoney_eid', $person['ftm_eid'], PDO::PARAM_INT);
            $stmt->bindValue(':votesmart_id', $person['votesmart_id'], PDO::PARAM_INT);
            $stmt->bindValue(':knowwho_pid', $person['knowwho_pid'], PDO::PARAM_INT);
            $stmt->bindValue(':opensecrets_id', $person['opensecrets_id'], PDO::PARAM_STR);
            $stmt->bindValue(':person_hash', $person['person_hash'], PDO::PARAM_STR);
            $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
            if (!$exists_id)
                $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            $this->db->commit();

            $this->middlewareSignal('people', $person['people_id']);

        } catch (PDOException $e) {
            $this->db->rollback();

            throw($e);
        }

        return true;
    }
    // }}}

    // {{{ processSearch()
    /** 
     * Process a search result and identify bills to be imported
     *
     * @param array $payload
     *   The decoded internal payload representing a search result
     * 
     * @param integer $import_type
     *   Control what condition triggers a bill to be flagged for importing
     *
     * @param integer $relevance_cutoff
     *   Results must also match have this relevance score or higher
     * 
     * @return integer[]
     *   List of LegiScan bill_ids that qualify for importing
     *
     */
    public function processSearch($payload, $import_type = LegiScan::IMPORT_ALL, $relevance_cutoff = 0)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processSearch payload status = "' . $payload['status'] . '"');

        $results = $payload['searchresult'];

        $summary = array_shift($results);

        foreach ($results as $bill)
        {
            if ($bill['relevance'] > $relevance_cutoff)
            {
                switch ($import_type)
                {
                    case LegiScan::IMPORT_NEW:
                        if (!$this->checkExists('bill', $bill['bill_id']))
                            $this->request('bills', $bill['bill_id']);
                        break;

                    case LegiScan::IMPORT_CHANGED:
                    case LegiScan::IMPORT_ALL:
                        $sql = "SELECT bill_id
                                FROM ls_bill
                                WHERE bill_id = :bill_id AND change_hash = :change_hash";
                        $stmt = $this->db->prepare($sql);
                        $stmt->bindValue(':bill_id', $bill_id['bill_id'], PDO::PARAM_INT);
                        $stmt->bindValue(':change_hash', $bill_id['change_hash'], PDO::PARAM_STR);
                        $stmt->execute();
                        $exists = $stmt->fetchColumn();

                        if (!$exists)
                            $this->request('bills', $bill['bill_id']);
                        break;
                }
            }
        }

        if (isset($this->missing['bills']))
            return $this->missing['bills'];
        else
            return array();
    }
    // }}}

    // {{{ processMonitorList()
    /** 
     * Process a monitored list payload, adding any necessary missing child objects to 
     * the missing list
     *
     * @throws APIStatusException
     *
     * @param array $payload
     *   The decoded internal payload representing a monitored list
     *
     * @param integer $import_type 
     *   The `LegiScan::IMPORT_*` to control how bills are selected for import
     * 
     * @return integer[]
     *   Returns an array of missing/changed bill_id or FALSE on processing errors
     */
    public function processMonitorList($payload, $import_type = LegiScan::IMPORT_ALL)
    {
        $this->resetMissing();

        // If this came from a pull, make sure status == OK
        if (isset($payload['status']) && $payload['status'] != LegiScan::API_OK)
            throw new APIStatusException('processMonitoredList payload status = "' . $payload['status'] . '"');

        foreach ($payload['monitorlist'] as $bill)
        {
            switch ($import_type)
            {
                case LegiScan::IMPORT_NEW:
                    if (!$this->checkExists('bill', $bill['bill_id']))
                        $this->request('bills', $bill['bill_id']);
                    break;

                case LegiScan::IMPORT_CHANGED:
                case LegiScan::IMPORT_ALL:
                    $sql = "SELECT bill_id
                            FROM ls_bill
                            WHERE bill_id = :bill_id AND change_hash = :change_hash";
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':bill_id', $bill['bill_id'], PDO::PARAM_INT);
                    $stmt->bindValue(':change_hash', $bill['change_hash'], PDO::PARAM_STR);
                    $stmt->execute();
                    $exists = $stmt->fetchColumn();

                    if (!$exists)
                        $this->request('bills', $bill['bill_id']);
                    break;
            }
        }

        if (isset($this->missing['bills']))
            return $this->missing['bills'];
        else
            return array();
    }
    // }}}

    // {{{ monitor()
    /**
     * Add or remove a bill_id from the `ls_monitor` list
     *
     * @param integer $bill_id
     *   The bill_id to add or remove
     *
     * @param boolean $add
     *   Add to list when true, remove when false
     *
     * @param boolean $stance
     *   Stance on the bill from Watch, Support, Oppose
     *
     * @return boolean
     *   Indicating the success/failure of manipulating ignore list
     *
     */
    function monitor($bill_id, $add = true, $stance = LegiScan::STANCE_WATCH)
    {
        $stmt = null;

        if ($add)
        {
            $now = date('Y-m-d H:i:s');

            if (!$this->checkExists('monitor', $bill_id))
                $sql = 'INSERT INTO ls_monitor (bill_id, stance, created) VALUES (:bill_id, :stance, :created)';
            else
                $sql = 'UPDATE ls_monitor SET stance = :stance, created = :created WHERE bill_id = :bill_id';

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->bindValue(':stance', $stance, PDO::PARAM_INT);
            $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            // Make sure a monitored bill is not also ignored
            $sql = 'DELETE FROM ls_ignore WHERE bill_id = :bill_id';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->execute();
        }
        elseif (!$add)
        {
            $sql = 'DELETE FROM ls_monitor WHERE bill_id = :bill_id';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->execute();
        }

        return true;
    }
    // }}}

    // {{{ ignore()
    /**
     * Add or remove a bill_id from the `ls_ignore` list
     *
     * @param integer $bill_id
     *   The bill_id to add or remove
     *
     * @param boolean $add
     *   Add to list when true, remove when false
     *
     * @return boolean
     *   Indicating the success/failure of manipulating ignore list
     *
     */
    function ignore($bill_id, $add = true)
    {
        $stmt = null;

        if ($add && !$this->checkExists('ignore', $bill_id))
        {
            $now = date('Y-m-d H:i:s');
            $sql = 'INSERT INTO ls_ignore (bill_id, created) VALUES (:bill_id, :created)';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->bindValue(':created', $now, PDO::PARAM_STR);
            $stmt->execute();

            // Make sure an ignored bill is not also monitored
            $sql = 'DELETE FROM ls_monitor WHERE bill_id = :bill_id';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->execute();
        }
        elseif (!$add)
        {
            $sql = 'DELETE FROM ls_ignore WHERE bill_id = :bill_id';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
            $stmt->execute();
        }


        return true;
    }
    // }}}

    // {{{ checkExists()
    /**
     * Check to see if a key exists in a table, results are cached via {@see LegiScan_Cache_Memory}
     *
     * @param string $table
     *   Base name of the table to check
     *
     * @param integer $id
     *   Key id to check for
     *
     * @param boolean $skip_cache
     *   Bypass cache and do direct lookup
     *
     * @return boolean|integer
     *   Returns FALSE if does not exist, otherwise the $id is returned
     *
     */
    public function checkExists($table, $id, $skip_cache = false)
    {
        $table = str_replace('ls_', '', strtolower($table));

        $sql = '';
        $result = false;

        $key = 'ls_' . $table . ':' . $id;

        if (($result = $this->memcache->get($key)) === false || $skip_cache)
        {
            // This could be done building a dynamic query based on the inputs, while
            // 'safe' since only system generated inputs, but for demo verbosity...
            switch ($table)
            {
                case 'bill':
                    $sql = 'SELECT bill_id FROM ls_bill WHERE bill_id = :id';
                    break;
                case 'monitor':
                    $sql = 'SELECT bill_id FROM ls_monitor WHERE bill_id = :id';
                    break;
                case 'ignore':
                    $sql = 'SELECT bill_id FROM ls_ignore WHERE bill_id = :id';
                    break;
                case 'session':
                    $sql = 'SELECT session_id FROM ls_session WHERE session_id = :id';
                    break;
                case 'committee':
                    $sql = 'SELECT committee_id FROM ls_committee WHERE committee_id = :id';
                    break;
                case 'people':
                    $sql = 'SELECT people_id FROM ls_people WHERE people_id = :id';
                    break;
                case 'bill_text':
                    $sql = 'SELECT text_id FROM ls_bill_text WHERE text_id = :id';
                    break;
                case 'bill_vote':
                    $sql = 'SELECT roll_call_id FROM ls_bill_vote WHERE roll_call_id = :id';
                    break;
                case 'bill_amendment':
                    $sql = 'SELECT amendment_id FROM ls_bill_amendment WHERE amendment_id = :id';
                    break;
                case 'bill_supplement':
                    $sql = 'SELECT supplement_id FROM ls_bill_supplement WHERE supplement_id = :id';
                    break;
                case 'subject':
                    $sql = 'SELECT subject_id FROM ls_subject WHERE subject_id = :id';
                    break;
            }

            if ($sql)
            {
                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(':id', $id);
                $stmt->execute();
                $result = $stmt->fetchColumn();

                if ($result)
                {
                    // An annoying side effect of supporting Memcache and Memcached
                    // is their incompatible set() functions, Memcached and the internal
                    // LegiScan Memory Cache use the more sensible (key, value, expiration)
                    if (get_class($this->memcache) == 'Memcache')
                        $this->memcache->set($key, $result, false, 1800);
                    else
                        $this->memcache->set($key, $result, 1800);
                }
            }
        }

        return $result;
    }
    // }}}

    // {{{ makeSQLStatement()
    /**
     * Create a PDO statatement for INSERT/UPDATE and bound values ready for execute()
     *
     * @param string $type
     *   Type of SQL statement to generate 
     *
     * @param string $table
     *   Table name
     *
     * @param array[] $updates
     *   Array of field => array(value, type) pairs for updates
     *
     * @param array[] $keys
     *   Arry of field => array(value, type) pairs for table key(s)
     *
     * @return PDOStatement
     *   PDO prepared and bound statement
     *
     */
    private function makeSQLStatement($type, $table, $updates, $keys)
    {
        $type = strtolower($type);

        if ($type == 'update')
        {
            $sets = array();
            $sql = "UPDATE $table SET ";
            foreach (array_keys($updates) as $field)
            {
                $sets[] = "{$field} = :{$field}";
            }
            $sql .= implode(', ', $sets);

            $where = array();
            foreach (array_keys($keys) as $field)
            {
                $where[] = "{$field} = :{$field}";
            }
            $sql .= ' WHERE ' . implode(' AND ', $where);
        }
        else
        {
            $vals = array();
            $sql = "INSERT INTO $table (" . implode(', ', array_keys(array_merge($keys, $updates))) . ') VALUES (';
            foreach (array_keys(array_merge($keys, $updates)) as $field)
            {
                $vals[] = ":{$field}";
            }
            $sql .= implode(', ', $vals);
            $sql .= ')';
        }
        
        try {
            $stmt = $this->db->prepare($sql);

        } catch (PDOException $e) {
            // Catch this here to make it more verbose since EIP will be in
            // in the wrong place is there is a paramenter problem so give
            // a better hint of what when pear shaped in the throw
            throw new APIException("makeSQLStatement $type $table - " . $e->getMessage());
        }

        foreach (array_merge($updates, $keys) as $field => $value)
        {
            $stmt->bindValue(":{$field}", $value[0], $value[1]);
        }

        return $stmt;
    }
    // }}}

    // {{{ billInit()
    /**
     * Create a skeleton data structure that will be filled by {@see loadBill()}
     *
     * @return mixed[]
     *   The blank bill data structure
     *
     */
    private function billInit()
    {
        // Empty template bill array

        $bill = array();

        $bill['bill_id'] = 0;
        $bill['change_hash'] = '';
        $bill['session'] = array('prefile'=>0,'sine_die'=>0,'prior'=>0);
        $bill['session_id'] = 0;
        $bill['url'] = '';
        $bill['state_link'] = '';
        $bill['completed'] = 0; // DEPRECATED DO NOT USE
        $bill['status'] = 0;
        $bill['status_date'] = '';
        $bill['progress'] = array();
        $bill['state'] = '';
        $bill['state_id'] = 0;
        $bill['bill_number'] = '';
        $bill['bill_type'] = '';
        $bill['bill_type_id'] = 0;
        $bill['body'] = '';
        $bill['body_id'] = 0;
        $bill['current_body'] = '';
        $bill['current_body_id'] = 0;
        $bill['title'] = '';
        $bill['description'] = '';
        $bill['pending_committee_id'] = 0;
        $bill['committee'] = array();
        $bill['referrals'] = array();
        $bill['history'] = array();
        $bill['sponsors'] = array();
        $bill['sasts'] = array();
        $bill['subjects'] = array();
        $bill['texts'] = array();
        $bill['votes'] = array();
        $bill['amendments'] = array();
        $bill['supplements'] = array();
        $bill['calendar'] = array();

        return $bill;
    }
    // }}}

    // {{{ loadBill()
    /**
     * Recreate a data structure that mimics the getBill payload for comparison when updating existing bills
     *
     * @param integer $bill_id
     *   The bill_id to load
     * 
     * @return mixed[]
     *   The data structure representing a bill
     *
     */
    public function loadBill($bill_id)
    {
        $bill = $this->billInit();

        $sql = 'SELECT *
                FROM ls_bill b
                INNER JOIN ls_session s ON b.session_id = s.session_id
                WHERE b.bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        $r = $stmt->fetch();

        if (empty($r))
            return $bill;

        $bill['bill_id'] = $r['bill_id'];
        $bill['change_hash'] = $r['change_hash'];
        $bill['session_id'] = $r['session_id'];
        $bill['session'] = array(
            'session_id' => $r['session_id'],
            'year_start' => $r['year_start'],
            'year_end' => $r['year_end'],
            'prefile' => $r['prefile'],
            'sine_die' => $r['sine_die'],
            'prior' => $r['prior'],
            'special' => $r['special'],
            'session_tag' => $r['session_tag'],
            'session_title' => $r['session_title'],
            'session_name' => $r['session_name'],
        );
        $bill['url'] = $r['legiscan_url'];
        $bill['state_link'] = $r['state_url'];
        $bill['status'] = $r['status_id'];
        $bill['status_date'] = $r['status_date'];
        $bill['state_id'] = $r['state_id'];
        $bill['bill_number'] = $r['bill_number'];
        $bill['bill_type_id'] = $r['bill_type_id'];
        $bill['body_id'] = $r['body_id'];
        $bill['current_body_id'] = $r['current_body_id'];
        $bill['title'] = $r['title'];
        $bill['description'] = $r['description'];
        $bill['pending_committee_id'] = $r['pending_committee_id'];
        if ($r['pending_committee_id'])
        {
            $sql = 'SELECT * FROM ls_committee WHERE committee_id = :pending_committee_id';
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':pending_committee_id', $r['pending_committee_id'], PDO::PARAM_INT);
            $stmt->execute();
            $r = $stmt->fetch();
            $bill['committee'] = array(
                'committee_id' => $r['committee_id'],
                'chamber_id' => $r['committee_body_id'],
                'name' => $r['committee_name']
            );
        }

        $sql = 'SELECT * FROM ls_bill_referral WHERE bill_id = :bill_id ORDER BY referral_step';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['referrals'][] = array(
                'date' => $r['referral_date'],
                'committee_id' => $r['committee_id'],
            );
        }

        $sql = 'SELECT * FROM ls_bill_history WHERE bill_id = :bill_id ORDER BY history_step';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['history'][] = array(
                'date' => $r['history_date'],
                'action' => $r['history_action'],
                'chamber_id' => $r['history_body_id'],
                'importance' => $r['history_major']
            );
        }

        $sql = 'SELECT * FROM ls_bill_progress WHERE bill_id = :bill_id ORDER BY progress_step';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['progress'][] = array(
                'date' => $r['progress_date'],
                'event' => $r['progress_event_id']
            );
        }

        $sql = 'SELECT *
                FROM ls_bill_sponsor bs
                    INNER JOIN ls_people p ON bs.people_id = p.people_id
                WHERE bill_id = :bill_id ORDER BY bs.sponsor_order';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['sponsors'][] = array(
                'people_id' => $r['people_id'],
                'person_hash' => $r['person_hash'],
                'party_id' => $r['party_id'],
                'role_id' => $r['role_id'],
                'name' => $r['name'],
                'first_name' => $r['first_name'],
                'middle_name' => $r['middle_name'],
                'last_name' => $r['last_name'],
                'suffix' => $r['suffix'],
                'nickname' => $r['nickname'],
                'district' => $r['district'],
                'ftm_eid' => $r['followthemoney_eid'],
                'votesmart_id' => $r['votesmart_id'],
                'opensecrets_id' => $r['opensecrets_id'],
                'ballotpedia' => $r['ballotpedia'],
                'sponsor_type_id' => $r['sponsor_type_id'],
                'sponsor_order' => $r['sponsor_order'],
                'committee_id' => $r['committee_sponsor_id']
            );
        }

        $sql = 'SELECT * FROM ls_bill_vote WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['votes'][] = array(
                'roll_call_id' => $r['roll_call_id'],
                'date' => $r['roll_call_date'],
                'desc' => $r['roll_call_desc'],
                'yea' => $r['yea'],
                'nay' => $r['nay'],
                'nv' => $r['nv'],
                'absent' => $r['absent'],
                'total' => $r['total'],
                'passed' => $r['passed'],
                'chamber_id' => $r['roll_call_body_id'],
                'url' => $r['legiscan_url'],
                'state_link' => $r['state_url'] 
            );
        }

        $sql = 'SELECT * FROM ls_bill_text WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['texts'][] = array(
                'doc_id' => $r['text_id'],
                'date' => $r['bill_text_date'],
                'type_id' => $r['bill_text_type_id'],
                'mime_id' => $r['bill_text_mime_id'],
                'url' => $r['legiscan_url'],
                'state_link' => $r['state_url'],
                'text_size' => $r['bill_text_size'],
                'text_hash' => $r['bill_text_hash']
            );
        }

        $sql = 'SELECT * FROM ls_bill_amendment WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['amendments'][] = array(
                'amendment_id' => $r['amendment_id'],
                'adopted' => $r['adopted'],
                'chamber_id' => $r['amendment_body_id'],
                'date' => $r['amendment_date'],
                'title' => $r['amendment_title'],
                'description' => $r['amendment_desc'],
                'mime_id' => $r['amendment_mime_id'],
                'url' => $r['legiscan_url'],
                'state_link' => $r['state_url'],
                'amendment_size' => $r['amendment_size'],
                'amendment_hash' => $r['amendment_hash']
            );
        }

        $sql = 'SELECT * FROM ls_bill_supplement WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['supplements'][] = array(
                'supplement_id' => $r['supplement_id'],
                'date' => $r['supplement_date'],
                'type_id' => $r['supplement_type_id'],
                'title' => $r['supplement_title'],
                'description' => $r['supplement_desc'],
                'mime_id' => $r['supplement_mime_id'],
                'url' => $r['legiscan_url'],
                'state_link' => $r['state_url'],
                'supplement_size' => $r['supplement_size'],
                'supplement_hash' => $r['supplement_hash']
            );
        }

        $sql = 'SELECT * FROM ls_bill_sast WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['sasts'][] = array(
                'sast_bill_id' => $r['sast_bill_id'],
                'sast_type_id' => $r['sast_type_id']
            );
        }

        $sql = 'SELECT bs.subject_id, s.subject_name
                FROM ls_bill_subject bs
                    INNER JOIN ls_subject s ON bs.subject_id = s.subject_id
                WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['subjects'][] = array(
                'subject_id' => $r['subject_id'],
                'subject_name' => $r['subject_name']
            );
        }

        $sql = 'SELECT * FROM ls_bill_calendar WHERE bill_id = :bill_id';
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':bill_id', $bill_id, PDO::PARAM_INT);
        $stmt->execute();
        while ($r = $stmt->fetch())
        {
            $bill['calendar'][] = array(
                'type_id' => $r['event_type_id'],
                'date' => $r['event_date'],
                'time' => $r['event_time'],
                'location' => $r['event_location'],
                'description' => $r['event_desc']
            );
        }

        return $bill;
    }
    // }}}

    // {{{ request()
    /**
     * Add an `object:id` pair to the missing list while preventing duplications
     *
     * @param string $object
     *   The name of the type of object being requested
     *
     * @param integer $id
     *   The object LegiScan ID of the requested object
     * 
     * @return boolean
     *   Indicating the success/failure of adding to the missing list
     *
     */
    public function request($object, $id)
    {
        // Hobo cache
        static $requested = array();

        $key = $object . ':' . $id;

        if (!isset($requested[$key]))
        {
            $this->missing[$object][] = $id;
            $requested[$key] = 1;
        }

        return true;
    }
    // }}}

    // {{{ getMissing()
    /** 
     * Get the list of missing objects, if any, from the most recent process run
     *
     * @return array
     *   The structured list of missing objects
     */
    public function getMissing()
    {
        return $this->missing;
    }
    // }}}

    // {{{ resetMissing()
    /** 
     * Reset the list of missing objects
     *
     * @return boolean
     *   Indicating success/failure of the reset
     *
     */
    public function resetMissing()
    {
        $this->missing = array();

        return true;
    }
    // }}}

    // {{{ getDB()
    /**
     * Return the DB handle so others can play if needed
     *
     * @return PDO
     *   Database PDO handle
     *
     */
    public function getDB()
    {
        return $this->db;
    }
    // }}}

    // {{{ dbDate()
    /**
     * Massage '0000-00-00' date to NULL when operating under PostgreSQL
     *
     * @param string $date
     *   The date to massage
     *
     * @return string|null
     *   The massaged and relaxed date
     *
     */
    private function dbDate($date)
    {
        if ($this->massage && ($date == '0000-00-00' || $date == ''))
            $date = null;

        return $date;
    }
    // }}}

    // {{{ getStateList()
    /**
     * Get a list of state table records for ALL substitutions
     *
     * @return array[]
     *   Array of state table rows
     */
    public function getStateList()
    {
        static $state_list = array();

        if (empty($state_list))
        {
            $sql = 'SELECT * FROM ls_state ORDER BY state_abbr';
            $rs = $this->db->query($sql);
            while ($r = $rs->fetch())
            {
                $state_list[$r['state_id']] = $r;
            }
        }

        return $state_list;
    }
    // }}}

    // {{{ middlewareSignal()
    /**
     * Send a signal to the middleware that data has changed so appropriate
     * steps can be taken
     *
     * @param string $object
     *   The type of object
     *
     * @param integer $id
     *   The object ID
     * 
     * @return boolean
     *   Indicating the success/failure of sending signal
     *
     */
    public function middlewareSignal($object, $id)
    {
        $signal = strtolower(LegiScan::getConfig('middleware_signal'));

        if (!$signal)
            return true;

        $now = date('Y-m-d H:i:s', time());
        $object = strtolower($object);

        if ($signal == 'table')
        {
            try {
                $sql = "SELECT * FROM ls_signal WHERE object_type = :object AND object_id = :id";
                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(':object', $object, PDO::PARAM_STR);
                $stmt->bindValue(':id', $id, PDO::PARAM_INT);
                $stmt->execute();
                $semaphore = $stmt->fetch();

                if ($semaphore == false)
                    $sql = 'INSERT INTO ls_signal (
                                object_type, object_id, processed,
                                updated, created
                            ) VALUES (
                                :object_type, :object_id, :processed,
                                :updated, :created
                            )';
                else
                    $sql = 'UPDATE ls_signal
                            SET processed = :processed, updated = :updated
                            WHERE object_type = :object_type AND object_id = :object_id';

                // Only for new entries and those that have been processed,
                // this preserves the original timestamp in case a second
                // update comes in before middleware processing
                if (!$semaphore || isset($semaphore['processed']) && $semaphore['processed'] >= 1)
                {
                    $stmt = $this->db->prepare($sql);
                    $stmt->bindValue(':object_type', $object, PDO::PARAM_STR);
                    $stmt->bindValue(':object_id', $id, PDO::PARAM_INT);
                    $stmt->bindValue(':processed', 0, PDO::PARAM_INT);
                    $stmt->bindValue(':updated', $now, PDO::PARAM_STR);
                    if (!$semaphore)
                        $stmt->bindValue(':created', $now, PDO::PARAM_STR);
                    $stmt->execute();
                }

            } catch (PDOException $e) {
                return false;
            }
        }
        elseif ($signal == 'directory')
        {
            $filename = $object . '.' . $id;
            $file = realpath(__DIR__ . '/' . 'signal') . '/' . $filename;

            $data = json_encode(array(
                'object_type' => (string) $object,
                'object_id' => (int) $id,
                'updated' => (string) $now,
            ));

            // If the second update comes in before middleware processes
            // leave the contents to preserve the original timestamp
            if (!file_exists($file))
            {
                if (!@file_put_contents($file, $data))
                    return false;
            }
        }

        return true;
    }
    // }}}

    // {{{ getCacheFilename()
    /**
     * Generate a document cache filename path fragment
     *
     * @param string $object
     *   The type of document object
     *
     * @param integer $id
     *   The document object ID
     * 
     * @return string
     *   Filename path fragment for the location under the document cache directory
     *
     */
    public function getCacheFilename($object, $id)
    {
        // This is a mapping of mime_type_id to extension
        $suffix = array(
            1 => 'html',
            2 => 'pdf',
            3 => 'wpd',
            4 => 'doc',
            5 => 'rtf',
            6 => 'docx',
        );

        $object = strtolower($object);

        switch ($object)
        {
            case 'text':
                $sql = 'SELECT s.state_abbr, b.bill_number, b.session_id, bt.bill_text_mime_id AS mime_id
                        FROM ls_bill_text bt
                            INNER JOIN ls_bill b ON bt.bill_id = b.bill_id
                            INNER JOIN ls_state s ON b.state_id = s.state_id
                        WHERE bt.text_id = :id';
                break;
            case 'amendment':
                $sql = 'SELECT s.state_abbr, b.bill_number, b.session_id, ba.amendment_mime_id AS mime_id
                        FROM ls_bill_amendment ba
                            INNER JOIN ls_bill b ON ba.bill_id = b.bill_id
                            INNER JOIN ls_state s ON b.state_id = s.state_id
                        WHERE ba.amendment_id = :id';
                break;
            case 'supplement':
                $sql = 'SELECT s.state_abbr, b.bill_number, b.session_id, bs.supplement_mime_id AS mime_id
                        FROM ls_bill_supplement bs
                            INNER JOIN ls_bill b ON bs.bill_id = b.bill_id
                            INNER JOIN ls_state s ON b.state_id = s.state_id
                        WHERE bs.supplement_id = :id';
                break;
        }

        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
        $stmt->execute();
        $r = $stmt->fetch();

        // Custom storage layout? Make the change here and the system 
        // will take care of creating any needed subdirectories. Though
        // it is an exercise for the reader to relocate any existing
        // files in the standard location
        
        // Build the actual filename
        $filename = $object . '/' . $r['state_abbr'] . '/' . $r['session_id'] . '/' . $r['bill_number'] . '/' . $id . '/' . $suffix[$r['mime_id']];
        $filename = strtolower($filename);

        return $filename;
    }
    // }}}
}
// }}}
LegiScan API Client API documentation generated by ApiGen