Finding your iPad’s UDID for Test Flight

I don’t know what possessed the folks at apple to make it so incredibly difficult to find your iPad’s identifier, aka your iPad’s UDID. Not sure why its not just in the iPad’s about section of the settings app, but anyway, here is how you find your iPad’s UDID for your app developer’s test flight account:

  1. Connect your iPad to your computer and fire up iTunes.
  2. Go to your iPad’s summary page in iTunes
  3. Under your iPad’s name and capacity, click on the “Serial Number”
  4. “Serial Number” then changes to your UDID

So f’ing retarded. Thanks apple.

Hopefully this saves others time.

twitter bootstrap responsive container with padding and a border

I’m working on a project where I wanted to use the twitter bootstrap responsive css scaffolding, but ran into a bit of a snafu because I needed to add a border and background to the container class. 

I decided to extend the @media responsive stylesheet declarations to achieve this.  The padding is not quite as much as I would like, but its better than having the span divs right up on the edges of the container div and I’m too lazy to edit all the span widths.

.container,
.navbar-fixed-top .container,
.navbar-fixed-bottom .container {
	width: 940px;
	padding: 10px;
	border:1px solid #cecece;
	border-top:0px none;
	background:#fff;
}
@media (max-width: 767px) {
  body{
    padding-left: 0px;
    padding-right: 0px;
  }
  .container{
    padding:5px 19px;
    width: auto;
  }
}
@media (min-width: 768px) and (max-width: 979px) {
  .container,
  .navbar-fixed-top .container,
  .navbar-fixed-bottom .container {
    width: 724px;
    padding:10px 14px;
  }
}
@media (min-width: 1200px) {
  .container,
  .navbar-fixed-top .container,
  .navbar-fixed-bottom .container {
    width: 1170px;
    padding:10px 14px;
  }
}

PHP sendmail 90 second delay on Dotcloud [solved]

For months we have been having upstream timeout issues with our Dotcloud PHP service running nginx and php-fpm.  Amongst other causes, we found that after our instances were up for more than a day, php’s mail function using sendmail was consistantly taking exactly 90 seconds to send an email.  Unacceptable.

After going back and forth with Dotcloud’s support, we determined that sendmail was spawning 3 processes to send emails, and apparently on their boxes, it takes 30 seconds to spawn a new process.  Seems like it shouldn’t take that long, but it does.  The solution to this issue was to simply not use sendmail.  Instead, we are using SMTP protocol to talk to postfix locally on a php instance.  Originally I wanted to avoid sending emails via SMTP because it can be slow when authenticating to a remote server.  Because we are using SMTP locally with no authentication it is very fast.

In short, DO NOT USE sendmail on DotCloud PHP instances or it will cause all kinds of problems.

Upstream timeout issues with nginX + php-fpm + CodeIgniter + gzip + xdebug on DotCloud – [resolved]

We have been using DotCloud as our hosting platform for months now, and overall I have been extremely pleased with their service.  There were some bumps in the road early on while they were still in their beta phase, but things have been running very smoothly for a few months now.  Everything except an uncomfortable number of seemingly random nginX “upstream timed out (110: Connection timed out) while reading response header from upstream” errors.

If you want to get what you need and not read the rest of this post, I will make it super simple for you:

If you are using php-fpm, gzip, xdebug and CodeIgniter, disable xdebug!!!

For anyone who feels like listening to my story, please read on.  Not only were these errors unsettling, but they were causing DotCloud’s load balancer to bounce POST requests  back to our servers multiple times because it was getting error responses and assuming nothing was happening on our end.  This in turn was causing user input (comments, image uploads, password requests, etc) to be saved (or emails sent) on our application multiple times.  Super embarrassing.

After weeks of research and floundering, a DotCloud tech and I finally discovered the issue.  There is a known bug with xdebug and ob_gzhandler which was causing our php processes to seg fault.  The bug is documented here:

http://grokbase.com/p/php/php-bugs/0365rtcdgx/23985-bgs-ob-gzhandler-make-segmentation-fault

What was happening was the request was sent to our server, the php process was doing everything it was supposed to, then when CodeIgniter’s output buffer was being gzipped by ob_gziphandler(), the php process was segmentation faulting and causing nginx to time out waiting for the response from php-fpm.  So, while everything was successfully happening in the php script, the output back to the client was failing.

By disabling the xdebug extension in the php configuration, the php processes stopped seg faulting, and everything is happy again!  No more Upstream Timeouts! It took a really long time to track this issue down, so I hope this post helps someone =)

AddThis Pintrest button hack to look better in 32×32 format

Today I had to add a Pinterest Share button on our website, and run it through our AddThis account so we can track the analytics.  Unfortunately, there are only two options for the Pinterest button via AddThis, neither of which look good along side our 32×32 style share buttons.  So, I decided to hack it up =)

If you apply a fixed height to the a tag which holds the Pinterest button’s iframe, then set overflow to hidden, then apply a negative margin to the nested iframe, you can essentially get rid of that pesky count bubble that will not go away.

Resulting css is something like:

 
	.addthis_toolbox > a.addthis_button_pinterest_pinit{
		margin:8px 0px 4px;
		height:24px;
		overflow:hidden;
		vertical-align:baseline;
	}
	.addthis_toolbox > a.addthis_button_pinterest_pinit > iframe{
		margin-top:-34px;
	}

mongodb geospatial query example in php with distance radius in miles and km

Recently for a project involving 311 reports in New York city, I had to find a speedy way to query over 3 million records within a certain radius of a latitude longitude point. Mysql is slow for this sort of thing, so I decided to try the data set out with Solr. Solr worked great and was quite responsive, but required a massive amount of RAM on our server to hold the entire dataset. This was “ok” for our current dataset, but what would happen when we started to expand beyond NYC? or if we kept adding on new 311 reports from NYC as they come in over time? Solr was not scaleable. Mongodb to the rescue!

I wrote a simple import script in PHP to pull in the data we had already imported into mysql into mongodb. The only special part of the data mirgation was the creation of a “lnglat” field in each imported document array which is a “2d index” in mongo. You have to set a multivalued field with the lat/lng values cast as floats in the document array, like this:

$doc['lnglat'] = array('lng'=>(float)$row['lng'],'lat'=>(float)$row['lat']);

After the data has been imported to mongo, you create the 2d index on that multivalued field using the mongo command line, with this command:

my_mongodb.my_collection.ensureIndex( { lnglat : "2d" } )

Once I had the data in mongo, I had a hard time finding good examples of Geospatial queries for mongodb in php. I finally found this page:
http://comments.gmane.org/gmane.comp.db.mongodb.user/48789
which solved all my problems and pointed out that when querying mongo with a lat/lng point as an array, mongodb expects the lng to be first, not the lat. Durrrr…

Anyway, here is the code snippit that I pulled from the url above which was the first comprehensible example I found of php code for geospatial queries in with a distance radius specified:

public function getRadiusMiles($lng, $lat, $radius = 3) {
	$radiusOfEarth = 3956; //avg radius of earth in miles
	$cursor = $this->collection->find(
		array('lnglat' =>
			array('$within' =>
				array('$centerSphere' =>
					array(
						array(floatval($lng), floatval($lat)), $radius/$radiusOfEarth
					)
				)
			)
		)
	);
	$resultset = $this->jsonCursor($cursor);
	return json_encode($resultset);
}
 
public function getRadiusKm($lng, $lat, $radius = 5) {
	$radiusOfEarth = 6378.1; //avg radius of earth in km
	$cursor = $this->collection->find(
		array('lnglat' =>
			array('$within' =>
				array('$centerSphere' =>
					array(
						array(floatval($lng), floatval($lat)), $radius/$radiusOfEarth
					)
				)
			)
		)
	);
 
	$resultset = $this->jsonCursor($cursor);
	return json_encode($resultset);
}

Hopefully this saves someone some time!

mysqldump 5.1.36 unable to successfully dump a MySQL database in UTF-8 character encoding issues

Today I had a character encoding problem when importing some data from a mysqldump from a contractor we had hired to do some work.  Everything about the dump was fine, the database, tables, and fields were all using utf8_unicode_ci utf8 collation, the dump file was encoded in utf8, and the database I was importing to was all utf8 as well.  The contractor said he was able to re-import the dump into his server and local environment fine, but I was still unable to get the dump imported without all the utf8 characters getting garbled.  As it turns out, his mysql and mysqldump versions were older than mine and it was messing everything up.  Apparently, according to this mysql bug post:

http://bugs.mysql.com/bug.php?id=28969

“As of today (Feb 6, 2012) mysqldump 5.1.36 still exhibits the faulty behavior (unable to successfully dump a MySQL database in UTF-8)”.  We were able to fix this issue by following these steps:

  1. add latin1 default character set to the mysql dump command from the older versioned database, like so:
    mysqldump -u dbuser -p --default-character-set=latin1 my_database_name > db_dump.sql

    You can also try adding a “-r” flag to this command which should allow non-latin characters to dump properly as well, but this did not work in our case.

  2. before importing into the newer version database, open the in the newly created dump file and change the line that reads:
    /*!40101 SET NAMES latin1 */;

    to:

    /*!40101 SET NAMES utf8 */;

    then save the file

  3. import to the newer version database using:
    mysql -u dbuser -p --default-character-set=utf8 my_database_name < db_dump.sql

Took a lot of emails back and forth to finally come to this solution.  Hopefully this helps some one and thank god for the original bug post linked above!

disable firefox native inspect element context menu

With the latest update of Firefox to version 10.0, they included some new developer tools which are similar to what is available via Firebug or WebKit’s developer tools.  About time!  Unfortunately, I still am so used to firebug that will continue using it instead of these new native development tools.  One thing that really grinds my gears was the native “Inspect Element” context menu item because it lives directly above the Firebug one.  This was extremely frustrating for me because I kept clicking on that instead of the “Inspect with Firebug” menu item.  This would bring up the native inspector console, which I would then have to close, then go back and right click again and make sure I click on the inspect with firebug menu item.  Sigh…..how annoying.

Anyway, there is a simple way to disable the native inspect element context menu item.  In the Firefox address bar, put “about:config” and press Enter.  This will bring up Firefox’s preference configurations.  In the “filter” search box enter “inspector”.  This will bring up three configuration preferences.  Simply double click on the “devtools.inspector.enabled” preference to change it to “false” and viola!  No more native inspect element in the context menu!  Horray.

UPDATE:

A recent update to the firebug extension add’s config preferences for hiding the default inspector (thanks Ilya).  Now when you filter your config preferences by searching for “inspector”, some new firebug config settings appear as well.  Simply set “extensions.firebug.hideDefaultInspector” to “true” in addition to setting “devtools.inspector.enabled” to “false”.

 

Don’t muck around in here too much or you will mess up your firefox configuration.  I hope someone finds this useful =)

php mysql collation change script

There are times when we get stuck with bad collations on our mysql tables and we are overwhelmed by having to change them by hand. Well, I have written a php script which will go through all your tables and fields in a database and update them to the collation which works for you. In my case for this example, I have a database which contains a mix of collations and changes them all to utf8_unicode_ci. This example uses the mysqli object in php as the database connection.

Your will need to update the database connection credentials as they are all bogus in this example. You will also need to change the “$execute_sql” to equal true if you want the script to actually perform the collation change via this script.

<?php
 
	$execute_sql = false;
 
	$host = 'mydbhost';
	$username = 'username';
	$password = 'passsword';
	$dbname = 'databasename';
 
	$db = new mysqli($host, $username, $password, $dbname);
 
	$mysqldatabase = 'databasename';
	$collation = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci';
 
	echo '<div>';
	$db->query("ALTER DATABASE $mysqldatabase $collation");
 
	$result = $db->query("SHOW TABLES");
 
	$count = 0;
	while($row = $result->fetch_assoc()) {
		$table = $row['Tables_in_'.$mysqldatabase];
		if($execute_sql) $db->query("ALTER TABLE $table DEFAULT $collation");
		$result1 = $db->query("SHOW COLUMNS FROM $table");
		$alter = '';
		while($row1 = $result1->fetch_assoc()) {
			if (preg_match('~char|text|enum|set~', $row1["Type"])) {
				if(strpos($row1["Field"], 'uuid')){
					// why does this not work
				}else{
					$alter .= (strlen($alter)?", \n":" ") . "MODIFY `$row1[Field]` $row1[Type] $collation" . ($row1["Null"] ? "" : " NOT NULL") . ($row1["Default"] && $row1["Default"] != "NULL" ? " DEFAULT '$row1[Default]'" : "");
				}
			}
		}
		if(strlen($alter)){
			$sql = "ALTER TABLE $table".$alter.";";
			echo "<div>$sql\n\n</div>";
			if($execute_sql) $db->query($sql);
		}
		$count++;
	}
	echo '</div>';
 
?>

The only gotcha that I have run into with my script that I have yet to solve is that when you change the collation of an existing uuid field which has a foreign key, the copy table fails in mysql. Not that the collation of a uuid field really matters, but you can see a comment in the routine which questions why this does not work.

Anyway, I hope someone finds this useful =) Enjoy…

MySQL alter table to re-order columns

Sometimes because of my completely anal nature, I want the fields that I’ve already added to a table in MySQL to be in a different order. To do this you can simply alter that table column, without changing any of its properties, then adding “AFTER column_3″.

So for instance you had a table like this:

mysql> describe test;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| column_id | int(11) | NO   | PRI | NULL    | auto_increment |
| column_1  | int(11) | NO   |     | NULL    |                |
| column_2  | int(11) | NO   |     | NULL    |                |
| column_3  | int(11) | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

But you really want column_1 to be ordered after column_3 because you are as ridiculous as I am. You can run this query:

ALTER TABLE `test` CHANGE `column_1` `column_1` INT( 11 ) NULL AFTER column_3;

Now your table looks like this:

mysql> describe test;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| column_id | int(11) | NO   | PRI | NULL    | auto_increment |
| column_2  | int(11) | NO   |     | NULL    |                |
| column_3  | int(11) | NO   |     | NULL    |                |
| column_1  | int(11) | YES  |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

I find this to be useful for me on larger tables that have grown over time and I want some relevant columns grouped together for when I am viewing data in phpMyAdmin or whatever.

Hope this helps someone =)