Welcome to the Boolean blog

Here you will find details of news, projects, releases and other bits and pieces that we are working on at Boolean. We hope that you find something of interest amongst the archives.

Code snippets, methodologies and useful links will also be mentioned here.

Where there is more to logic than TRUE or FALSE

CodeIgniter routes and .htaccess

CodeIgniter has a robust routing module as part of its MVC architecture. A trap for the inexperienced arises when the default controller works (e.g. site.com) but anything else such as mysite.com/dashboard goes to a server based 404, not the CodeIgniter one.

Solution: in your config.php file ensure this line is present

$config['index_page'] = ”;

and place the following code in an .htacess file

Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

# activate URL rewriting
RewriteEngine on

# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)

# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html)

# rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]
<I/IfModule>

<IIfModule !mod_rewrite.c>

# If we don’t have mod_rewrite installed, all 404′s can be sent to index.php, and everything works as normal.

ErrorDocument 404 index.php

<I/IfModule>

Raspberry Pi SD card setup

Whilst following the instructions available at enlinux I had repeated errors whilst attempting to move my OS image to the SD card for the Pi to use. Specifically this line:

sudo dd bs=1m if=~/Downloads/2012-10-28-wheezy-raspbian/2012-10-28-wheezy-raspbian.img of=/dev/rdisk3

What ended up solving the problem for me (after much hair pulling) was to not use the built in SD card reader of my MacBook Pro, and to use a cheap external USB dongle. Sometimes it is the most simple thing.

Derived Statistics

Twitter

In a follow up to this post I thought I’d explain how I obtained the statistics used.

The first indicators were easy to obtain – No Avatar Picture, No Tweets and No Followers and are fairly self explanatory. Obtaining a figure for Inactive Accounts however was a little more difficult. Because Twitter does not return any information (other than a total) for a private accounts tweets, the last_tweet_date was not available for all users. To stop this from skewing the dataset I removed these accounts from the sample size.

  John Key Clare Curran Russel Norman Metiria Turei David Shearer Gareth Hughes
Private Accounts 4269 153 342 284 284 377
Percentage 8.5 6.3 8.7 8.3 8.9 8.9

As you can see the percentage of private accounts is roughly equivalent – and not enough to skew our sample.
This gave me a new followers total to calculate averages for tweet related metrics Inactive Accounts and “Real” Followers

  John Key Clare Curran Russel Norman Metiria Turei David Shearer Gareth Hughes
No Avatar 27% 8% 11% 10% 11% 6%
No Followers 17% 1% 4% 3% 4% 2%
Inactive 39% 17% 20% 19% 14% 14%
‘Real’ 43% 77% 72% 74% 74% 80%

I’ll leave it as an exercise for the reader to interpret accordingly.

NB: These accounts were selected as Leader of the Opposition, Leaders of the Greens, and Clare Curran and Gareth Hughes as they had respectable follower numbers of their own for comparison.

EDIT 18/06/2012

I located a site that can graph followers over time for a Twitter account, sadly the scale of the x-axis is not consistent which tends to cause spikes in growth that wouldn’t normally be visible.

John Key Followers vs Time

As you can see the growth is fairly consistent: the first dip is presumably a statistic gathering error and should be discarded.
As only an idiot would buy followers in large and obviously identifiable quantities (Rudd, Gingrich…Lady Gaga) steady organic looking growth doesn’t mean anything if you’re smart and feed them in gradually. The jump at the end of the graph actually indicates only 375 increased followers a day which could of course easily be attributed to a particularly good press release. (Although I can’t seem to recall any of late) and isn’t a smoking gun by any stretch.

A commenter elsewhere suggested comparing other politicians with 50,000 followers to see how they stack up. Their point may have been purely that we don’t have any…however we do have some celebs and sportspeople who match up. I’ve started the data collection process so we should have some comparisons late today or tomorrow.

UPDATE 21/06/2012 Obtaining additional data for comparison took a lot longer than expected due to unforeseen issues with the Twitter API.

  John Key Piri Weepu Corey Jane John Campbell Rhys Darby Helen Clark
Followers 50,000 50,000 50,000 32,04 88,396 18,392
No Avatar 27% 15% 20% 15% 14% 11%
No Followers 17% 10% 14% 8% 6% 4%
Inactive 39% 12% 22% 2% 22% 12%
‘Real’ 43% 67% 56% 67% 64% 79%

Interesting to see how ‘active’ @johnjcampbell‘s followers are, and that Piri and Corey’s followers are so different given that you would expect them to be followed by similar accounts.

Update 31/07/2012
There has been an application released that people can use to analyse their own accounts called Status People

The tool largely mirrors my own assumptions and subsequent results, but is much more user friendly than the tools I created for my own use.

More discussion in and around the subject is available in this National Business Review article

Twitter follower numbers

John Key

John Key – Less debt, more jobs

…more ghost followers?

Chris Keall’s recent article in The National Business Review had John Key @johnkeypm as ‘top of the twits’ with over 50,000 subscribers. With Newt Gingrich and Kevin Rudd amongst others being accused of buying Twitter followers I thought it would be interesting to investigate some of John Key’s followers.

In the comments for the NBR article MikePSmith suggested most of the followers were spambots with CK replying “overall it looks like most are living breathing New Zealanders”. I cast my eye over the followers and decided exactly the opposite: most of the following accounts looked extremely fishy.

The @johnkeypm account is a managed account – that is the man himself does not update it personally. Much like the man it is decidedly bland and delivers the party line more like an RSS feed.

To make the account look as popular as possible accounts that regular users block and report for spam are kept as active followers. If the account isn’t actually being used in the traditional manner those annoying spam accounts aren’t interfering with everyday use.

So with the help of Twitter API I analysed the followers of the @johnkeypm account. The results confirmed almost exactly what my eyes had already told me: most of the accounts were spambots, zombies, or worse.

John Key (thumbnail) Zombie Horde
Click to enlarge Join the horde? courtesy Björn Söderqvist

With a very loose definition of an actual Twitter user as opposed to a spambot zombie (10 followers, 10 tweets, tweeted this year, have bothered to change the display picture from the default) the follower count falls to less than half at 22,000 accounts.
If you care to further refine the criteria to what most of us would consider regular active users the numbers just fall away. Limiting to accounts based in New Zealand (not fair to expats or those who choose not to disclose) then the numbers dwindle even further.

John Key probably remains New Zealand politician with the largest Twitter follower count but with a much smaller margin than the numbers suggest – if one is at all concerned about quality or real accounts.

UPDATE 15/06/2012
Other accounts will be analysed for comparison

Clare Curran @clarecurranmp
No avatar pic 8% (197 accounts)
No tweets 5% (115 accounts)
No followers 1% (15 accounts)
Inactives 17% (387 accounts) Corrected 17/06/2012
‘Real’ followers 77% (1764 accounts)

UPDATE 17/06/2012
A follow up post explaining how the numbers were determined will follow shortly

MySQL datatype for storing IP addresses

The best way to store a (version 4) IP address in a MySQL database is as an integer. This may sound strange but MySQL has two powerful functions to enable you to store an IP address as an unsigned INT. Searching an integer is much faster than searching a string and additionally integers take up less storage space which is great when working with large datasets.

mysql> SELECT INET_ATON(’10.0.5.9′);
———————
INET_ATON(’10.0.5.9′)
———————
167773449
———————
1 row in set (0.00 sec)

and the reverse function

mysql> SELECT INET_NTOA(167773449);
———————
INET_NTOA(167773449)
———————
10.0.5.9
———————
1 row in set (0.00 sec)

Speeding up Firefox on OSX

Firefox had been running really slowly lately and as I am an awful hoarder of tabs closing the application took forever. I’d heard that since version 3 there was a sql lite table that according to Google could do with vacuuming – whatever that meant.

The SQLite database uses flat files for local storage and I wondered how big and bloated they had become over time. It turned out to be much worse than I expected. To view I typed the following into a command terminal

find ~/Library/Application\ Support/Firefox/Profiles -type f -name ‘*.sqlite’ -exec ls -arlth {} \;

My .places file was 40 megabytes which certainly wasn’t helping speed at all. The following command took care of the necessary housekeeping

find ~/Library/Application\ Support/Firefox/Profiles -type f -name ‘*.sqlite’ -exec sqlite3 {} VACUUM \;
find ~/Library/Application\ Support/Firefox/Profiles -type f -name ‘*.sqlite’ -exec sqlite3 {} REINDEX \;

Speeding up PHP FOR loops

Refactoring some client code the other day I found a lot of inefficient logic. Whilst this example is written in PHP the concept is applicable in other programming languages.

Many people use for loops in the belief that they are faster than a foreach, and while this is often the case it can be slower and less flexible if you make some simple mistakes. Consider the following:

A slow FOR loop

$text=”thequickbrownfoxjumpsoverthelazydog”;
for($i=0; $i < strlen($text); $i++){ // Loop through the characters
echo ( substr($text,$i,1) == ‘o’ ) ? “Its an o” : “Not an o”; // Display true or false as relevant
}

A faster FOR loop

$text=”thequickbrownfoxjumpsoverthelazydog”;
$length = strlen($text); // obtain length of the string
for($i=0; $i < $length ; $i++){ // Loop through the characters
echo ( substr($text,$i,1) == ‘o’ ) ? “Its an o” : “Not an o”; // Display true or false as relevant
}

The second loop is faster because it does not evaluate the string length each time the loop runs strlen($text) as in the first example. Small differences like this can have a large effect on the speed of scripts especially if there are many loops or iterations.

LinkedIn dynamic button rendering

It has become standard to offer sharing functionality via major platforms such as Facebook, Twitter and LinkedIn on websites. Most of the platform providers have multiple options for inserting their sharing features often including the use of an iFrame.
LinkedIn is slightly different however choosing only to offer a javascript based solution similar to the following:

<script src=”//platform.linkedin.com/in.js” type=”text/javascript”></script>

Above the inclusion of the required javascript function, and below the addition of sharing functionality where required.

<script type=”IN/Share”></script>

When asynchronous pagination gets involved however this lack of an iFrame causes an issue: the javascript is not called as the asynchronous load takes place. This issue stumped for for a while until I found the simple solution to call:

IN.init();

Eugene O’Neill (Web Developer for LinkedIn) has stated:

1) IN.Parse() and IN.Init() are here to stay. While we do employ the policy that any undocumented methods may or may not be supported indefinitely, these two are uniquely crucial to the functionality of the framework. The amount of work it would require to remove IN.Parse()… I don’t even want to think about it. IN.Init() is our preferred method for loading the framework asynchronously and won’t be going anywhere. Feel free to use either method.