Installing Alternative PHP Cache (APC) on Mac OS X Lion

The APC extension should be a straightforward PECL install, but sadly it does not work on Mac OS X Lion. Attempting to install will result in make errors similar to the following:

In file included from /private/tmp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory
In file included from /private/tmp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:44: error: expected specifier-qualifier-list before ‘pcre’
/private/tmp/APC/apc.c:393: error: expected specifier-qualifier-list before ‘pcre’
/private/tmp/APC/apc.c: In function ‘apc_regex_compile_array’:
/private/tmp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c: In function ‘apc_regex_match_array’:
/private/tmp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
make: *** [apc.lo] Error 1
ERROR: `make' failed

Thankfully, the solution is simple. APC requires PCRE libraries and header files to be available, so we can download and install these from source:

./configure
make
sudo make install

Next, we can install the APC extension using the normal PECL route:

sudo pecl install apc

Confirm that you have the following line in your php.ini file:

extension=apc.so

Restart Apache, if required:

sudo apachectl graceful

You should now have APC available, and you can confirm this by looking at your phpinfo output:

APC PHP info

SimpleCrypt PHP class for simple cryptography

I have just pushed a simple class called SimpleCrypt to GitHub:

https://github.com/glenscott/simple-crypt

This is an OO wrapper around the most common mcrypt functions. An example of use:

require_once 'SimpleCrypt.php';

// show the algorithms available for use
print_r( SimpleCrypt::get_available_algorithms() );

// encrypt a string using Triple DES (CBC mode)
$crypt = new SimpleCrypt( 'tripledes', 'mysecretkey' );
$encrypted_data = $crypt->encrypt_data( 'stringtoencrypt' );
echo "Encrypted data: " . bin2hex( $encrypted_data ) . "\n";
echo "Decrypted data: " . $crypt->decrypt_data( $encrypted_data ) . "\n";

If you find it useful, please let me know!

Map invoke errors when using MongoDB MapReduce

When running MapReduce operations on your data, you must make sure that any fields you refer to within your map operation are available for every document in your collection. If you try a map operation and some documents do not have the required field, you will get the following assertion error when running the command:

map invoke failed: JS Error: TypeError: this.fieldname has no properties

fieldname in this case is a field that does not exist in all of the documents.

To prevent this error, you can pass the optional query parameter to the command to make sure that only documents with this field are queried. For example, in PHP, add the following to the command operation:


$db->command(
    'mapreduce' => 'collection',
    'map'       => $map,
    'reduce'    => $reduce,
    'out'       => array( 'inline' => 1 ),
    'query'     => array( "fieldname" => array( '$exists' => true, ) )
);

This makes sure that MapReduce operations are only run on a subset of the collection.

Complaining to Virgin Media

"Broadband power to all the people"

Yesterday, the 17th January 2012, Virgin Media broadband customers experienced partial or total loss of connectivity throughout the afternoon and evening. As one of those customers affected by the outage, I am making a formal complaint to the company. As I work from home and require internet connectivity at all times, this outage had a serious affect on my business.

For others that are considering complaining, here is how you can do it — it took me a fair bit of searching to find this, so I hope this is helpful to others.

Firstly, you may wish to read Virgin Media’s Code of Practice for complaints.

To submit a complaint, you can use the web form located here:

Virgin Media – Contact Us

  1. Select General Enquiry
  2. Choose I have cable services
  3. Select Make a customer services complaint
  4. Click the red Next button at the bottom of the page

This leads you to a contact form where you can submit your complaint. You will also need you Virgin Media account number and the password you use for logging into the site.

Good luck!

Photo credit: Gene Hunt

Going Freelance – Part 3

Balancing The Account By Hand

In Part 2, I mentioned how I used Google Docs as a way of managing business records. Although this turned out to be a reasonable solution, I soon found myself wanting something more bespoke.

Enter FreeAgent. I heard a few other freelancers recommending this service, so I signed up for a free 30 day trial to see what the fuss was about. FreeAgent provides a way of managing your client details, invoices and tax affairs. The interface is very slick and friendly, and means keeping on top of record keeping is now much less of a chore!

I’ve now been using the paid service for 2 months and I can heartily recommend it. If you want to give it a go, you can use this link to get 10% off the subscription price of FreeAgent.

(photo credit: www.seniorliving.org)

Exporting specific documents from MongoDB

Mongodb

MongoDB comes with a useful tool, mongoexport, for exporting collections. However, you can also use it to export specific documents by utilising the query parameter. You can export a single document using it’s ObjectId using a command similar to the following:

mongoexport -h "host_name" -u "username" -d "database_name" -c "collection_name" -q '{"_id":ObjectId("4f045677a1ef264746000011")}' -o output.js -p
  • Replace host_name, username, database_name and collection_name with the appropriate values.
  • Replace 4f045677a1ef264746000011 with the ObjectID that you wish to export

You will be prompted for a password, and then the document will be written to output.js.

To export multiple documents, you can utilise an $in clause in the query parameter like this:

-q '{"_id":{"$in" :[ ObjectId("4f048dc6a1ef26da4b000008"),ObjectId("4ed8ee16a1ef26085600001a"),ObjectId("4efc46e0a1ef26b73d0007be")]}}'

To import these exported documents into another MongoDB server, you can use the mongoimport like so:

mongoimport -h "host_name" -u "username" -p -d "database_name" -c "collection_name" output.js

iCal 502 error when syncing with Yahoo! Calendar

In the last few days, my iCal has been giving me the following error message when trying to access my Yahoo! Calendar via CalDAV:

The request for account "Yahoo! Calendar" failed.

The server responded with
"502"
to operation
CalDAVAccountRefreshQueueableOperation.

iCal error message when syncing with yahoo! calendar

Yahoo! have acknowledged this is a problem on their side:

We are aware of a Calendar (Proxy error) and our engineering team is working to resolve this issue. We apologize for the inconvenience and hope to have it resolved soon.

(source: Syncing / Mobile Sync | Yahoo! Calendar Help)

update 2nd November 2011: I am no longer seeing this error message, and Yahoo! have removed the notice from their help page. Looks like the issue has been resolved!

update 3rd November 2011: Seems like I spoke too soon, the error is back!

Camera and photo improvements in iOS5

iPhone Camera

The camera functionality in the iPhone has always been one of my most used features. iOS5 brings some enhancements, which are listed below:

Camera improvements

  • Double-click the Home button when device is asleep to bring up a camera shortcut on iPhone 4S, iPhone 4, iPhone 3GS and iPod touch (4th generation)
  • Volume Up button to take a picture
  • Optional grid lines to line up shots
  • Pinch to zoom in the preview screen
  • Swipe to camera roll from preview screen
  • Tap and hold to lock focus and exposure; iPad 2 and iPod touch (4th generation) only support exposure lock

Photo improvements

  • Crop and rotate
  • Red-eye removal
  • One-tap enhance
  • Organise photos into albums

This list was taken from the official release notes, which were accessible via iTunes pre-installation.

Getting started with MongoDB and PHP on Mac OS X

NYみやげのMongo goods貰った!

Introduction

MongoDB is a document-orientated database written with scalability and high-performance in mind. It is one of a growing number of NoSQL systems – a database that does not rely on SQL or relational theory at all.

Getting a MongoDB server working with PHP on Mac OS X is relatively straightforward, and this tutorial shows you how.

Installing the MongoDB Server

The first step requires you to download and install the actual MongoDB system. The example shown below downloads v2.0.0 64 bit binaries for OS X. For other binaries, please check out the MongoDB Downloads page.

cd /tmp
curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.0.tgz
tar zxvf mongodb-osx-x86_64-2.0.0.tgz
sudo mv mongodb-osx-x86_64-2.0.0 /usr/local/mongodb
mkdir /usr/local/mongodb/data

Configuring the MongoDB server

We need to create a small configuration file so that MongoDB knows where its data files reside. Create the file /usr/local/mongodb/mongod.conf and add the following line:

dbpath = /usr/local/mongodb/data

Starting MongoDB

To manually start the MongoDB server, use the following command:

/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.conf

This will start the server and will by default log all output to STDOUT.

Installing the MongoDB PHP Extension

Installing the PHP extension is simple:

sudo pecl install mongo

Once this has completed, add the following line to your /etc/php.ini file:

extension=mongo.so

Restart apache using sudo apachectl restart, and the extension should be available. This can be verified with the phpinfo call:

MongoDB extension for PHP

Example PHP script

To test your setup, the following simple script can be used to create a new collection and add two new records:

    <?php

    // connect
    $m = new Mongo();

    // select a database
    $db = $m->comedy;

    // select a collection (analogous to a relational database's table)
    $collection = $db->cartoons;

    // add a record
    $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
    $collection->insert($obj);

    // add another record, with a different "shape"
    $obj = array( "title" => "XKCD", "online" => true );
    $collection->insert($obj);

    // find everything in the collection
    $cursor = $collection->find();

    // iterate through the results
    foreach ($cursor as $obj) {
        echo $obj["title"] . "\n";
    }

    ?>

The script should output the following:

Calvin and Hobbes
XKCD

And that’s it! You now have a working MongoDB setup on Mac OS X.

For more information on the PHP extension for MongoDB, please see the following pages in the PHP Manual:

PHP: Mongo – Manual