Index: trunk/extensions/CongressLookup/maintenance/populateCache.php |
— | — | @@ -13,6 +13,7 @@ |
14 | 14 | class PopulateCache extends Maintenance { |
15 | 15 | public $isOK = 0; |
16 | 16 | public $isBad = 0; |
| 17 | + protected $dbr; |
17 | 18 | |
18 | 19 | public function __construct() { |
19 | 20 | parent::__construct(); |
— | — | @@ -20,18 +21,31 @@ |
21 | 22 | |
22 | 23 | $this->addOption( 'url', 'The base URL for zipcode lookup.', true, true ); |
23 | 24 | $this->addOption( 'limit', 'The amount of values to return during a db query.', false, false ); |
| 25 | + $this->addOption( 'cache_warmup', 'If this is used, the script will attempt to hit all of the possible URLs for you to warm up the cache.', false, false ); |
| 26 | + $this->addOption( 'path', 'The file path to output all possible zip code URLs. If this option is specified, this script will NOT attempt to hit the URLs for you.', false, false ); |
| 27 | + |
24 | 28 | } |
25 | 29 | |
26 | 30 | public function execute() { |
| 31 | + $this->dbr = wfGetDB( DB_SLAVE ); |
| 32 | + $path = $this->getOption( 'path', null ); |
| 33 | + if ( $path ) { |
| 34 | + $this->writeUrlsToFile( $path ); |
| 35 | + } |
| 36 | + if ( $this->getOption( 'cache_warmup', null )) { |
| 37 | + $this->warmUpCache(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function warmUpCache() { |
27 | 42 | $this->output( "Populating caches...\n" ); |
28 | | - $dbr = wfGetDB( DB_SLAVE ); |
29 | 43 | |
30 | 44 | $limit = $this->getOption( 'limit', 1000 ); |
31 | 45 | $offset = 0; |
32 | 46 | $keepGoing = true; |
33 | 47 | |
34 | 48 | while( $keepGoing ) { |
35 | | - $result = $dbr->select( |
| 49 | + $result = $this->dbr->select( |
36 | 50 | 'cl_zip5', |
37 | 51 | 'clz5_zip', |
38 | 52 | array(), |
— | — | @@ -42,7 +56,7 @@ |
43 | 57 | ) |
44 | 58 | ); |
45 | 59 | |
46 | | - if ( !$result ) { |
| 60 | + if ( !$result->numRows() ) { |
47 | 61 | $keepGoing = false; |
48 | 62 | } |
49 | 63 | |
— | — | @@ -56,14 +70,44 @@ |
57 | 71 | $this->output( "...Bad so far: " . $this->isBad . "\n" ); |
58 | 72 | sleep( 1 ); // rate limit |
59 | 73 | } |
| 74 | + $this->ouput( "Done!\n" ); |
60 | 75 | } |
61 | 76 | |
| 77 | + public function writeUrlsToFile( $path ) { |
| 78 | + $this->output( "Preparing to write URLs to file...\n" ); |
| 79 | + $limit = $this->getOption( 'limit', 1000 ); |
| 80 | + $offset = 0; |
| 81 | + $keepGoing = true; |
| 82 | + $fh = fopen( $path, 'w' ); |
| 83 | + |
| 84 | + while( $keepGoing ) { |
| 85 | + $result = $this->dbr->select( |
| 86 | + 'cl_zip5', |
| 87 | + 'clz5_zip', |
| 88 | + array(), |
| 89 | + __METHOD__, |
| 90 | + array( |
| 91 | + 'LIMIT' => $limit, |
| 92 | + 'OFFSET' => $offset, |
| 93 | + ) |
| 94 | + ); |
| 95 | + |
| 96 | + if ( !$result->numRows() ) { |
| 97 | + $keepGoing = false; |
| 98 | + } |
| 99 | + |
| 100 | + foreach ( $result as $row ) { |
| 101 | + $url = $this->makeUrl( $row->clz5_zip ); |
| 102 | + fwrite( $fh, $url ."\n" ); |
| 103 | + } |
| 104 | + $offset += $limit; |
| 105 | + } |
| 106 | + fclose( $fh ); |
| 107 | + $this->output( "Done!\n" ); |
| 108 | + } |
| 109 | + |
62 | 110 | public function hitUrl( $zip, $attempt=0 ) { |
63 | | - $zip = intval( $zip ); |
64 | | - if ( $zip < 10000 ) { // make sure there are 5 digits |
65 | | - $zip = sprintf( "%05d", $zip ); |
66 | | - } |
67 | | - $url = $this->getOption( 'url' ) . "?zip=" . $zip; |
| 111 | + $url = $this->makeUrl( $zip ); |
68 | 112 | //$this->output( "*Trying to hit $url\n" ); |
69 | 113 | $req = MWHttpRequest::factory( $url, array( |
70 | 114 | 'method' => 'GET', |
— | — | @@ -83,6 +127,15 @@ |
84 | 128 | } |
85 | 129 | } |
86 | 130 | } |
| 131 | + |
| 132 | + public function makeUrl( $zip ) { |
| 133 | + $zip = intval( $zip ); |
| 134 | + if ( $zip < 10000 ) { // make sure there are 5 digits |
| 135 | + $zip = sprintf( "%05d", $zip ); |
| 136 | + } |
| 137 | + $url = $this->getOption( 'url' ) . "?zip=" . $zip; |
| 138 | + return $url; |
| 139 | + } |
87 | 140 | } |
88 | 141 | |
89 | 142 | $maintClass = "PopulateCache"; |