Code

v6 MSN Adcenter for PHP

Mike Taggart of Envoy Media Group and I have collaborated on a new little project to build an easy to use PHP library for MSN AdCenter (v6). Handles Ads, AdGroups, Campaigns and Keywords, here.

Mish(S?)Mash

MishMashSMash is my ambitious new OSS opensocial container for housing individuals own personal, arbitrary and portable online datastores, where they can build personal or social data feeds amongst ‘friends’ with a finer grain of access control than is generally offered by seed funded social networks. Think Yahoo! Pipes, but for ‘local’ feeds you create yourself (like your record collection, favourite OMGLOLCatzHAHAOHDAD! or whatever), or with friends (a collection of my mates french jungle records circa 2003-2005 who live in Brisbane). Clear as mud?

The project has just been started and is available on github here (feel free to fork), with an ongoing developer blog here relating to opensocial standards, LOD/RDF(s) + semantic web, online privacy, interoperability, the irony of building a possibly monetising system to avoid vendor lockin and every one of my tedious technical discoveries. It’d be nice if this talked building AR/Layar services as well.

Tech is PHP/CodeIgniter for a quick backend prototype + some serious client side behaviour using Mootools + Gears (offline mode) + Cassandra cluster for elasticity with *minimal* server-side application logic. So much to chew! lucky these meats are truly delicious.

HipHop PHP, the day has arrived

HipHop PHP has been a rumored Facebook project for a few months now, finally it’s arrived.

“HipHop transforms your PHP source code into highly optimized C++ and then compiles it with g++ to build binary files. You keep coding in simpler PHP, then HipHop executes your source code in a semantically equivalent manner and sacrifices some rarely used features – such as eval() – in exchange for improved performance.” (more info, repo).

It looks neat (in a Google GWT kind of way), keen to see how it plays with opcode caches, xdebug, shared memory stuff etc. and whether it makes pcntl_fork() actually useful (no eval(); so chances are slim). Facebook has very different requirements to the rest of us and an already highly optimised code base, so HipHop may be filling a very specific niche. Hopefully developers won’t use it as a bandaid to actual performance profiling, or expect it to solve their I/O issues. fingers crossed :p

Cassandra 0.5.0 released

After much work behind the scenes, Cassandra 0.5.0 has made it out of RC and into release, representing significant performance, API and bootstrapping improvements across the board.

XE currency feed imports w/PostgreSQL

This table and trigger will facilitate periodic currency updates (ie: race condition free) from XE feeds for a single base currency. In this case, USD.

CREATE TABLE currency_feed
(
  currency character(3) NOT NULL,
  country character varying NOT NULL,
  from_usd double precision NOT NULL,
  to_usd double precision NOT NULL,
  CONSTRAINT currency_feed_pkey PRIMARY KEY (currency)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE currency_feed OWNER TO postgres;

-- this trigger replicates the 'on duplicate key update' mysql directive
CREATE FUNCTION repl_currency() RETURNS trigger AS $BODY$
BEGIN
  IF (EXISTS(SELECT 1 FROM "currency_feed" C WHERE C."currency" = NEW."currency")) THEN
    UPDATE "currency_feed" SET "from_usd" = NEW."from_usd", "to_usd" = NEW."to_usd" WHERE "currency" = NEW."currency";
    RETURN OLD;
  ELSE
    RETURN NEW;
  END IF;
END;
$BODY$ LANGUAGE plpgsql;

ALTER FUNCTION repl_currency() OWNER TO postgres;
CREATE TRIGGER repl_currency BEFORE INSERT ON "currency_feed" FOR EACH ROW EXECUTE PROCEDURE repl_currency();

Periodic import is then just :

set client_encoding to 'ISO-8859-1';
copy currency_feed from '/path/to/feed.csv' delimiter ',' csv quote as '"';

… fast falling in love with Postgres ;)

Form Validation with PHP + jQuery

Form validation tutorials – done to death right? As a player for Team Redundancy, I’ve built one for the Pandra library (PandraValidator class) which performs server side validation with the help of jQuery. Both client and server side uses the same validation function, and the good thing is – it’s really fast. It ticks a few security boxes also (referrer checking, a honeypot field, POST timer, GET > POST method enforce) and may be a good conceptual guide for building your own.

Source is here, enjoy.

Writing a simple Cassandra CRUD class in PHP with ThriftInterface

NOSQL – so hot right now. As part of my own learning curve, I’m building a simple abstract and schema-less Factory Patterned CRUD class for Cassandra, and shoe-horning it in with Apache Thrift.  There’s a very thorough blog by Evan Weaver to help dealing with the RDBMS to Cassandra conceptual jump. If you haven’t dealt with Cassandra before, it may be a good place to start. For a very decent cluster setup example, check here, and for Thrift just grab the source and follow the instructions I guess, but if you’re using debian/ubuntu I’ve written a quick install guide.

Dubbed Pandra, it will be maintained via github. Requires PHP 5.2, Cassandra 0.4, and latest Thrift Interface.

for fun, Euler Solution 11 (PHP)

A bit bored so knocked up a solution to this Project Euler problem :

11: What is the greatest product of four numbers on the same straight line in the 20 by 20 grid?
More >

Finding the first and last instance of a date sorted pkey

Came across a bit of a strange problem with an ad-hoc query. Basically I have a log of categorisation actions against a realm dataset for which I needed to find a list of realms in the last month where the last action on a pkey group (realm + userid, realms can multiple userid’s) different from the first action, and the first action was ‘A’. Luckily there’s an indexed ‘logdate’ field, otherwise I would have no idea how to find this data in a single query – so I’m extremely keen for feedback from anyone who could do it with elegance! Ne’er a dirtier subselect has ye seen :D
More >