r109314 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109313‎ | r109314 | r109315 >
Date:05:30, 18 January 2012
Author:khorn
Status:ok
Tags:
Comment:
Added db manipulation functions to the maintenance script. This way, we can edit locally, do a dump, and re-upload.
Modified paths:
  • /trunk/extensions/CongressLookup/scripts/zip_file_parser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CongressLookup/scripts/zip_file_parser.php
@@ -20,15 +20,60 @@
2121 $skip_up_to = $_SERVER['argv'][1];
2222 }
2323 $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];
2626 }
2727
2828 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;
2973 case 'fillOutNulls':
3074 $this->fill_out_nulls( $skip_up_to );
3175 break;
3276 case 'findSplits':
 77+ default:
3378 $this->find_split_zipcodes( $skip_up_to );
3479 break;
3580 }
@@ -121,7 +166,7 @@
122167 if ( is_array($reps) && count( $reps ) > 0 ){
123168 foreach ( $reps as $rep_id ){
124169 echo "$zip Rep ID: $rep_id\n";
125 - $this->insert_rep( $zip, $rep_id );
 170+ $this->update_rep( $zip, $rep_id );
126171 }
127172 } else {
128173 echo "We got no reps in a really odd way.\n";
@@ -188,7 +233,44 @@
189234
190235 }
191236
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 ){
193275 $dbr = wfGetDB( DB_SLAVE );
194276 $dbr->update( 'cl_zip5',
195277 array(
@@ -201,6 +283,46 @@
202284 echo "Updated $zip to include rep id $rep_id\n";
203285 }
204286
 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+
205327 function insert_reps_by_district( $zip, $districts ){
206328 $dbr = wfGetDB( DB_SLAVE );
207329 echo "Zip: $zip\n";

Status & tagging log