Index: trunk/extensions/CongressLookup/scripts/zip_file_parser.php |
— | — | @@ -20,15 +20,60 @@ |
21 | 21 | $skip_up_to = $_SERVER['argv'][1]; |
22 | 22 | } |
23 | 23 | $function = 'fillOutNulls'; |
24 | | - if ( !empty( $_SERVER['argv'][1] ) && $_SERVER['argv'][1] === 'findSplits' ){ |
25 | | - $function = 'findSplits'; |
| 24 | + if ( !empty( $_SERVER['argv'][1] ) && !is_numeric( $_SERVER['argv'][1] ) ){ |
| 25 | + $function = $_SERVER['argv'][1]; |
26 | 26 | } |
27 | 27 | |
28 | 28 | switch ( $function ){ |
| 29 | + case 'associate': |
| 30 | + //it should go zip, state, district for the next three params. |
| 31 | + |
| 32 | + if ( !empty( $_SERVER['argv'][2] ) ){ |
| 33 | + $zip = $_SERVER['argv'][2]; |
| 34 | + } else { |
| 35 | + die( "Missing parameter 2: 5-digit zip code." ); |
| 36 | + } |
| 37 | + if ( !empty( $_SERVER['argv'][3] ) ){ |
| 38 | + $state = $_SERVER['argv'][3]; |
| 39 | + } else { |
| 40 | + die( "Missing parameter 3: state code." ); |
| 41 | + } |
| 42 | + if ( !empty( $_SERVER['argv'][4] ) ){ |
| 43 | + $district = $_SERVER['argv'][4]; |
| 44 | + } else { |
| 45 | + die( "Missing parameter 4: district code." ); |
| 46 | + } |
| 47 | + |
| 48 | + $this->associate_zip5_reps( $zip, $state, $district ); |
| 49 | + |
| 50 | + break; |
| 51 | + case 'disassociate': |
| 52 | + //it should go zip, state, district for the next three params. |
| 53 | + |
| 54 | + if ( !empty( $_SERVER['argv'][2] ) ){ |
| 55 | + $zip = $_SERVER['argv'][2]; |
| 56 | + } else { |
| 57 | + die( "Missing parameter 2: 5-digit zip code." ); |
| 58 | + } |
| 59 | + if ( !empty( $_SERVER['argv'][3] ) ){ |
| 60 | + $state = $_SERVER['argv'][3]; |
| 61 | + } else { |
| 62 | + die( "Missing parameter 3: state code." ); |
| 63 | + } |
| 64 | + if ( !empty( $_SERVER['argv'][4] ) ){ |
| 65 | + $district = $_SERVER['argv'][4]; |
| 66 | + } else { |
| 67 | + die( "Missing parameter 4: district code." ); |
| 68 | + } |
| 69 | + |
| 70 | + $this->disassociate_zip5_reps( $zip, $state, $district ); |
| 71 | + |
| 72 | + break; |
29 | 73 | case 'fillOutNulls': |
30 | 74 | $this->fill_out_nulls( $skip_up_to ); |
31 | 75 | break; |
32 | 76 | case 'findSplits': |
| 77 | + default: |
33 | 78 | $this->find_split_zipcodes( $skip_up_to ); |
34 | 79 | break; |
35 | 80 | } |
— | — | @@ -121,7 +166,7 @@ |
122 | 167 | if ( is_array($reps) && count( $reps ) > 0 ){ |
123 | 168 | foreach ( $reps as $rep_id ){ |
124 | 169 | echo "$zip Rep ID: $rep_id\n"; |
125 | | - $this->insert_rep( $zip, $rep_id ); |
| 170 | + $this->update_rep( $zip, $rep_id ); |
126 | 171 | } |
127 | 172 | } else { |
128 | 173 | echo "We got no reps in a really odd way.\n"; |
— | — | @@ -188,7 +233,44 @@ |
189 | 234 | |
190 | 235 | } |
191 | 236 | |
192 | | - function insert_rep( $zip, $rep_id ){ |
| 237 | + function associate_zip5_reps( $zip, $state, $district ){ |
| 238 | + //first, retrieve the state and district rep. |
| 239 | + $reps = $this->get_rep_ids( $state, $district ); |
| 240 | + if ( count( $reps ) !== 1 ){ |
| 241 | + if ( empty($reps) ){ |
| 242 | + die("No rep found in $state $district"); |
| 243 | + } else { |
| 244 | + die("Something very funky happened just then..."); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + $rep = $reps[0]; |
| 249 | + $this->insert_rep( $zip, $reps[0] ); |
| 250 | + |
| 251 | + //if it's not already in there, it should put it in there. |
| 252 | + //if it is already in there, do nothing. |
| 253 | + } |
| 254 | + |
| 255 | + function disassociate_zip5_reps( $zip, $state, $district ){ |
| 256 | + //first, retrieve the state and district rep. |
| 257 | + $reps = $this->get_rep_ids( $state, $district ); |
| 258 | + if ( count( $reps ) !== 1 ){ |
| 259 | + if ( empty($reps) ){ |
| 260 | + die("No rep found in $state $district"); |
| 261 | + } else { |
| 262 | + die("Something very funky happened just then..."); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + $rep = $reps[0]; |
| 267 | + $this->remove_rep( $zip, $reps[0] ); |
| 268 | + |
| 269 | + //if it's not already in there, it should put it in there. |
| 270 | + //if it is already in there, do nothing. |
| 271 | + } |
| 272 | + |
| 273 | + |
| 274 | + function update_rep( $zip, $rep_id ){ |
193 | 275 | $dbr = wfGetDB( DB_SLAVE ); |
194 | 276 | $dbr->update( 'cl_zip5', |
195 | 277 | array( |
— | — | @@ -201,6 +283,46 @@ |
202 | 284 | echo "Updated $zip to include rep id $rep_id\n"; |
203 | 285 | } |
204 | 286 | |
| 287 | + |
| 288 | + function remove_rep( $zip, $rep_id ){ |
| 289 | + $dbr = wfGetDB( DB_SLAVE ); |
| 290 | + |
| 291 | + $dbr->delete( 'cl_zip5', |
| 292 | + array( |
| 293 | + 'clz5_rep_id' => $rep_id, |
| 294 | + 'clz5_zip' => $zip, |
| 295 | + ) |
| 296 | + ); |
| 297 | + echo "Removed $rep_id from $zip\n"; |
| 298 | + } |
| 299 | + |
| 300 | + function insert_rep( $zip, $rep_id ){ |
| 301 | + $dbr = wfGetDB( DB_SLAVE ); |
| 302 | + |
| 303 | + $rowCheck = $dbr->selectRow( 'cl_zip5', |
| 304 | + array( |
| 305 | + 'clz5_rep_id', |
| 306 | + 'clz5_zip', |
| 307 | + ), |
| 308 | + array( |
| 309 | + 'clz5_rep_id' => $rep_id, |
| 310 | + 'clz5_zip' => $zip, |
| 311 | + ) |
| 312 | + ); |
| 313 | + |
| 314 | + if ( $rowCheck ){ |
| 315 | + echo "Rep id $rep_id already found in $zip."; |
| 316 | + } else { |
| 317 | + $dbr->insert( 'cl_zip5', |
| 318 | + array( |
| 319 | + 'clz5_rep_id' => $rep_id, |
| 320 | + 'clz5_zip' => $zip, |
| 321 | + ) |
| 322 | + ); |
| 323 | + echo "Updated $zip to include rep id $rep_id\n"; |
| 324 | + } |
| 325 | + } |
| 326 | + |
205 | 327 | function insert_reps_by_district( $zip, $districts ){ |
206 | 328 | $dbr = wfGetDB( DB_SLAVE ); |
207 | 329 | echo "Zip: $zip\n"; |