r109299 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109298‎ | r109299 | r109300 >
Date:03:40, 18 January 2012
Author:awjrichards
Status:ok (Comments)
Tags:
Comment:
Adding a maint script to prepopulate caches
Modified paths:
  • /trunk/extensions/CongressLookup/maintenance/populateCache.php (added) (history)

Diff [purge]

Index: trunk/extensions/CongressLookup/maintenance/populateCache.php
@@ -0,0 +1,89 @@
 2+<?php
 3+/**
 4+ * @ingroup Maintenance
 5+ */
 6+if ( getenv( 'MW_INSTALL_PATH' ) ) {
 7+ $IP = getenv( 'MW_INSTALL_PATH' );
 8+} else {
 9+ $IP = dirname(__FILE__).'/../../..';
 10+}
 11+
 12+require_once( "$IP/maintenance/Maintenance.php" );
 13+
 14+class PopulateCache extends Maintenance {
 15+ public $isOK = 0;
 16+ public $isBad = 0;
 17+
 18+ public function __construct() {
 19+ parent::__construct();
 20+ $this->mDescription = "Prepopulate caches with zipcode queries";
 21+
 22+ $this->addOption( 'url', 'The base URL for zipcode lookup.', true, true );
 23+ $this->addOption( 'limit', 'The amount of values to return during a db query.', false, false );
 24+ }
 25+
 26+ public function execute() {
 27+ $this->output( "Populating caches...\n" );
 28+ $dbr = wfGetDB( DB_SLAVE );
 29+
 30+ $limit = $this->getOption( 'limit', 1000 );
 31+ $offset = 0;
 32+ $keepGoing = true;
 33+
 34+ while( $keepGoing ) {
 35+ $result = $dbr->select(
 36+ 'cl_zip5',
 37+ 'clz5_zip',
 38+ array(),
 39+ __METHOD__,
 40+ array(
 41+ 'LIMIT' => $limit,
 42+ 'OFFSET' => $offset,
 43+ )
 44+ );
 45+
 46+ if ( !$result ) {
 47+ $keepGoing = false;
 48+ }
 49+
 50+ foreach ( $result as $row ) {
 51+ $this->hitUrl( $row->clz5_zip );
 52+ }
 53+
 54+ $offset += $limit;
 55+ $this->output( "...Attempted $offset URLs so far.\n" );
 56+ $this->output( "...OK so far: " . $this->isOK . "\n" );
 57+ $this->output( "...Bad so far: " . $this->isBad . "\n" );
 58+ sleep( 1 ); // rate limit
 59+ }
 60+ }
 61+
 62+ 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;
 68+ //$this->output( "*Trying to hit $url\n" );
 69+ $req = MWHttpRequest::factory( $url, array(
 70+ 'method' => 'GET',
 71+ 'timeout' => 2,
 72+ 'sslVerifyHost' => false, // just check if it can be reached
 73+ 'sslVerifyCert' => false, // just check if it can be reached
 74+ ) );
 75+ if ( $req->execute()->isOK()) {
 76+ $this->isOK++;
 77+ } else {
 78+ sleep( 2 );
 79+ $attempt++;
 80+ if ( $attempt < 3 ) {
 81+ $this->hitUrl( $zip, $attempt );
 82+ } else {
 83+ $this->isBad++;
 84+ }
 85+ }
 86+ }
 87+}
 88+
 89+$maintClass = "PopulateCache";
 90+require_once( DO_MAINTENANCE );
\ No newline at end of file
Property changes on: trunk/extensions/CongressLookup/maintenance/populateCache.php
___________________________________________________________________
Added: svn:eol-style
191 + native

Sign-offs

UserFlagDate
Jpostlethwaitetested04:10, 18 January 2012

Comments

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

Running populateCache.php now. This is going to take a while :)

Status & tagging log