r109065 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109064‎ | r109065 | r109066 >
Date:21:47, 16 January 2012
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
some more work on CongressLookup
Modified paths:
  • /trunk/extensions/CongressLookup/CongressLookup.db.php (added) (history)
  • /trunk/extensions/CongressLookup/CongressLookup.php (modified) (history)
  • /trunk/extensions/CongressLookup/SpecialCongressLookup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CongressLookup/CongressLookup.php
@@ -47,6 +47,7 @@
4848 $dir = dirname( __FILE__ ) . '/';
4949
5050 $wgAutoloadClasses['SpecialCongressLookup'] = $dir . 'SpecialCongressLookup.php';
 51+$wgAutoloadClasses['CongressLookupDB'] = $dir . 'CongressLookup.db.php';
5152 $wgExtensionMessagesFiles['CongressLookup'] = $dir . 'CongressLookup.i18n.php';
5253 $wgExtensionMessagesFiles['CongressLookupAlias'] = $dir . 'CongressLookup.alias.php';
5354 $wgSpecialPages['CongressLookup'] = 'SpecialCongressLookup';
Index: trunk/extensions/CongressLookup/SpecialCongressLookup.php
@@ -23,12 +23,30 @@
2424 // Pull in query string parameters
2525 $zip = $wgRequest->getVal( 'zip' );
2626
27 - /* Setup */
 27+ // Setup
2828 $wgOut->setSquidMaxage( $wgCongressLookupMaxAge );
2929 $this->setHeaders();
3030
31 - /* Display */
32 - // TODO: Do some work here.
 31+ if ( $zip ) {
 32+ $zip = $this->trimZip( $zip );
 33+ showMatches( $zip );
 34+ }
3335 }
3436
 37+ /**
 38+ * Given a zip code, output HTML-formatted data for the representatives for that area
 39+ * @param $zip string: A zip code
 40+ * @return true
 41+ */
 42+ private function showMatches( $zip ) {
 43+ global $wgOut;
 44+
 45+ $myRepresentative = array();
 46+ $mySenators = array();
 47+ $myRepresentative = CongressLookupDB->getRepresentative();
 48+ $mySenators = CongressLookupDB->getSenators();
 49+
 50+ // TODO: stuffz.
 51+ }
 52+
3553 }
Index: trunk/extensions/CongressLookup/CongressLookup.db.php
@@ -0,0 +1,53 @@
 2+<?php
 3+/**
 4+ * Static methods that retrieve information from the database.
 5+ */
 6+class CongressLookupDB {
 7+
 8+ /**
 9+ * Given a zip code, return the data for that zip code's congressional representative
 10+ * @param $zip string
 11+ * @return array
 12+ */
 13+ public static function getRepresentative( $zip ) {
 14+ $dbr = wfGetDB( DB_SLAVE );
 15+
 16+ $zip = $this->trimZip( $zip, 5 ); // Trim it to 5 digit
 17+ $zip = intval( $zip ); // Convert into an integer
 18+
 19+ $row = $dbr->selectRow( 'cl_zip5', 'rep_id', array( 'zip' => $zip ) );
 20+ if ( $row ) {
 21+ // TODO: stuffz.
 22+ }
 23+ }
 24+
 25+ /**
 26+ * Given a zip code, return the data for that zip code's senators
 27+ * @param $zip string
 28+ * @return array
 29+ */
 30+ public static function getSenators( $zip ) {
 31+ $dbr = wfGetDB( DB_SLAVE );
 32+
 33+ $zip = $this->trimZip( $zip, 3 ); // Trim it to 3 digit
 34+ $zip = intval( $zip ); // Convert into an integer
 35+
 36+ $row = $dbr->selectRow( 'cl_zip3', 'state', array( 'zip' => $zip ) );
 37+ if ( $row ) {
 38+ // TODO: stuffz.
 39+ }
 40+ }
 41+
 42+ /**
 43+ * Helper method. Trim a zip code, but leave it as a string.
 44+ * @param $zip string: Raw zip code
 45+ * @param $length integer: How many digits we want to return (from the left)
 46+ * @return string The trimmed zip code
 47+ */
 48+ public static function trimZip( $zip, $length ) {
 49+ $zip = trim( $zip );
 50+ $zipPieces = explode( '-', $zip, 2 );
 51+ $zip = substr( $zipPieces[0], 0, $length );
 52+ return $zip;
 53+ }
 54+}
Property changes on: trunk/extensions/CongressLookup/CongressLookup.db.php
___________________________________________________________________
Added: svn:eol-style
155 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r109072followup r109065...khorn22:55, 16 January 2012
r109073followup r109065...khorn22:58, 16 January 2012

Comments

#Comment by Khorn (WMF) (talk | contribs)   21:53, 16 January 2012

You're missing the $length param when you call trimZip here:

+		if ( $zip ) {
+			$zip = $this->trimZip( $zip );
+			showMatches( $zip );
+		}

#Comment by Khorn (WMF) (talk | contribs)   22:35, 16 January 2012

Actually, it looks like you don't even have to call this there anymore anyway.

#Comment by Khorn (WMF) (talk | contribs)   22:41, 16 January 2012

Additional: These functions should be static, and get called with the $zip param.

+		$myRepresentative = CongressLookupDB->getRepresentative();
+		$mySenators = CongressLookupDB->getSenators();
#Comment by Khorn (WMF) (talk | contribs)   23:00, 16 January 2012

Resolved all my issues in r109072 and r109073.

Status & tagging log