r109301 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109300‎ | r109301 | r109302 >
Date:04:10, 18 January 2012
Author:awjrichards
Status:ok (Comments)
Tags:
Comment:
Added the ability to just output a file of all URLs to load into wget or something. Also fixed the way in which we check to see if there are any more data results.
Modified paths:
  • /trunk/extensions/CongressLookup/maintenance/populateCache.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CongressLookup/maintenance/populateCache.php
@@ -13,6 +13,7 @@
1414 class PopulateCache extends Maintenance {
1515 public $isOK = 0;
1616 public $isBad = 0;
 17+ protected $dbr;
1718
1819 public function __construct() {
1920 parent::__construct();
@@ -20,18 +21,31 @@
2122
2223 $this->addOption( 'url', 'The base URL for zipcode lookup.', true, true );
2324 $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+
2428 }
2529
2630 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() {
2742 $this->output( "Populating caches...\n" );
28 - $dbr = wfGetDB( DB_SLAVE );
2943
3044 $limit = $this->getOption( 'limit', 1000 );
3145 $offset = 0;
3246 $keepGoing = true;
3347
3448 while( $keepGoing ) {
35 - $result = $dbr->select(
 49+ $result = $this->dbr->select(
3650 'cl_zip5',
3751 'clz5_zip',
3852 array(),
@@ -42,7 +56,7 @@
4357 )
4458 );
4559
46 - if ( !$result ) {
 60+ if ( !$result->numRows() ) {
4761 $keepGoing = false;
4862 }
4963
@@ -56,14 +70,44 @@
5771 $this->output( "...Bad so far: " . $this->isBad . "\n" );
5872 sleep( 1 ); // rate limit
5973 }
 74+ $this->ouput( "Done!\n" );
6075 }
6176
 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+
62110 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 );
68112 //$this->output( "*Trying to hit $url\n" );
69113 $req = MWHttpRequest::factory( $url, array(
70114 'method' => 'GET',
@@ -83,6 +127,15 @@
84128 }
85129 }
86130 }
 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+ }
87140 }
88141
89142 $maintClass = "PopulateCache";

Comments

#Comment by Jpostlethwaite (talk | contribs)   04:18, 18 January 2012

You need to put an equals sign for path:

php populateCache.php --url http://wikimedia-commit.localhost.wikimedia.org/index.php/Special:CongressLookup --path="/tmp/urls.txt"

This works.

Status & tagging log