Index: trunk/extensions/CongressLookup/scripts/zip_file_parser.php |
— | — | @@ -69,6 +69,17 @@ |
70 | 70 | $this->disassociate_zip5_reps( $zip, $state, $district ); |
71 | 71 | |
72 | 72 | break; |
| 73 | + |
| 74 | + case 'writeSQLFile': |
| 75 | + if ( !empty( $_SERVER['argv'][2] ) ){ |
| 76 | + $table = $_SERVER['argv'][2]; |
| 77 | + } else { |
| 78 | + die( "Missing parameter 2: Table name" ); |
| 79 | + } |
| 80 | + |
| 81 | + $this->write_happy_table_dump( $table ); |
| 82 | + break; |
| 83 | + |
73 | 84 | case 'fillOutNulls': |
74 | 85 | $this->fill_out_nulls( $skip_up_to ); |
75 | 86 | break; |
— | — | @@ -451,6 +462,92 @@ |
452 | 463 | } |
453 | 464 | } |
454 | 465 | |
| 466 | + function write_happy_table_dump( $table ){ |
| 467 | + $dbr = wfGetDB( DB_SLAVE ); |
| 468 | + |
| 469 | + if ( !(strpos( $table, 'cl_' ) === 0) ){ |
| 470 | + die("Table $table is not recognized by CongressLookup"); |
| 471 | + } |
| 472 | + |
| 473 | + if (!$dbr->tableExists( $table ) ) { |
| 474 | + die("Table $table does not exist."); |
| 475 | + } |
| 476 | + |
| 477 | + $ignore_me = array( |
| 478 | + 'clz5_id', |
| 479 | + 'clz3_id', |
| 480 | + ); |
| 481 | + |
| 482 | + |
| 483 | + $batch_size = 140; |
| 484 | + $batch_start_line = null; |
| 485 | + $batch_end_line = ";\n"; |
| 486 | + $current_count = 0; |
| 487 | + |
| 488 | + |
| 489 | + //we're running locally, so just go for it. |
| 490 | + $results = $dbr->select( $table, '*' ); |
| 491 | + if ( $results ){ |
| 492 | + $file = fopen("../data/$table.new.sql", 'w'); |
| 493 | + if ( !$file ){ |
| 494 | + die("No dice."); |
| 495 | + } |
| 496 | + foreach ( $results as $row ){ |
| 497 | + |
| 498 | + $row = get_object_vars($row); |
| 499 | + foreach ( $ignore_me as $key ){ |
| 500 | + unset( $row[$key] ); |
| 501 | + } |
| 502 | + |
| 503 | + if ( $batch_start_line === null ){ |
| 504 | + $batch_start_line = 'REPLACE INTO /*$wgDBprefix*/' . $table . " ( "; |
| 505 | + $keys = array(); |
| 506 | + foreach ( $row as $key => $data ){ |
| 507 | + $keys[] = '`' . $key . '`'; |
| 508 | + } |
| 509 | + $batch_start_line .= implode( ', ', $keys ); |
| 510 | + $batch_start_line .= ") VALUES\n"; |
| 511 | + } |
| 512 | + if ( $current_count === 0 ){ |
| 513 | + //echo "Writing $batch_start_line"; |
| 514 | + fwrite($file, $batch_start_line); |
| 515 | + } |
| 516 | + $line_data = '( 501, 400031 ), ' . "\n"; |
| 517 | + foreach ( $row as $key=>$data ){ |
| 518 | + if ( !is_numeric($data) ){ |
| 519 | + $row[$key] = "'$data'"; |
| 520 | + if ( $row[$key] === "''" ){ |
| 521 | + $row[$key] = 'NULL'; |
| 522 | + } |
| 523 | + } |
| 524 | + } |
| 525 | + |
| 526 | + $line_data = '( ' . implode( ', ', $row ) . ' )'; |
| 527 | + |
| 528 | + ++$current_count; |
| 529 | + |
| 530 | + if ( $current_count === $batch_size ){ |
| 531 | + $line_data .= $batch_end_line; |
| 532 | + $current_count = 0; |
| 533 | + } else { |
| 534 | + $line_data .= ", \n"; |
| 535 | + } |
| 536 | + |
| 537 | + //echo "Writing $line_data"; |
| 538 | + fwrite($file, $line_data); |
| 539 | + } |
| 540 | + fwrite($file, ';'); |
| 541 | + |
| 542 | + fclose($file); |
| 543 | + |
| 544 | + } else { |
| 545 | + die("Nothing to export."); |
| 546 | + } |
| 547 | + |
| 548 | + |
| 549 | + |
| 550 | + } |
| 551 | + |
455 | 552 | } |
456 | 553 | |
457 | 554 | $maintClass = "ZipFileParser"; |