2011 Review

Well another year comes to an end so I thought a traditional review would be in order.

This year has been a different one for me, my first year working for myself. I can only say it has so far been nothing short of a success. Whilst I perhaps haven’t had as many clients who are purely mine as I would have liked I have nonetheless forged some really good relationships with other agencies both big and small. I have also renewed old relationships with some old work colleagues who have also gone out on their own.

I have also dabbled in my first contract position which has also proved to be to my liking. I like being able to add something different to the project mix and bring my experience to the fore. All this without having the ultimate responsibility for multiple clients has been refreshing for me personally.

Overall it has been a lot of hard work and I’ve probably worked a bit too much this year. I’ve found it especially hard to turn work down and this has been to my detriment sometimes.

Going back to development after a few years out has also been good. It hasn’t taken long to get back into the swing of things and all the old skills returned. I’ve also learnt a few new things this year. JQuery is fun and makes JavaScript a pleasure again, I hope to get a lot more proficient with this. I’ve also tried out RabbitMQ which was cool, a little Amazon EC2 and integration with both Google Checkout and Sagepay. All very good experience.

My business knowledge has increased although I think I have been lucky so far with my clients and I haven’t had any bad experiences yet which is good. Long may this continue. Managing cash flow has perhaps been the hardest part of working for myself. I think I’m in a fairly good place right now but it is worth keeping an eye on.

I had hoped to devote a bit more time to this blog and whilst I managed to do this early in the year the second half waned a bit due to workload pressures. I’ve still managed (just) at least 1 blog post a month which is my aim to continue. As the second anniversary approaches I’m glad that I have kept this up. Would still love to grow my audience a bit.

Finally from a personal point of view this year continues to keep me on my toes with my wonderfully energetic son. He changes practically every day and watching him grow up and master new skills is very rewarding. Still by far the biggest challenge I have ever faced but one I intend to continue to rise to.

As for 2012, well assuming the world doesn’t end, I have a lot more goals. Try to re-balance the work/life situation is one, continue to learn and get more clients and relationships. Nothing extreme a steady progression is all I ask.

Good luck to all in 2012!

Twitter Boostrap

One thing I love about the open web is the multitude of tools available to you in order to build web sites and applications quickly and to a very high standard. Over the past few years frameworks and libraries have sprung into existence all aimed at a particular problem domain. Back in the day when these libraries weren’t available throwing together a site, with a content management system, that worked across all browsers, and was very robust, was no small task. Thankfully this has become a lot easier. You have MVC frameworks such as CakePHP that take the heavy lifting out of building the backend, you have JavaScript frameworks such as jQuery that abstract out all of the browser quirks between JavaScript engines and make animation and DOM manipulation a breeze, and finally you have CSS frameworks such as 960 grid which make it much easier to do complex layouts.

I utilise a lot of these frameworks, and the examples mentioned above are just one of the many available. One area of framework which I often would like to use is a UI framework for building interfaces. There is jQuery UI which is a very comprehensive framework but having looked at it I find it a little too bloated.

Another such framework which I recently became aware of was Twitter Bootstrap. This is an interface library which provides common markup patterns with associated CSS and a bit of JavaScript to build lovely cross browser user interfaces. The framework isn’t really suitable for public facing websites but is absolutely ideal as an interface to a CMS.

There are plenty of widgets available which makes it easy and quick to develop a consistent set of forms and tables to display and manipulate your data. There are buttons, tabs, menus, messages and even a handy grid system for your complete UI. I have used it recently on a big CMS build and the results were excellent.

There are a few missing widgets which I would particularly like such as a date picker, but these aren’t too hard to plugin from elsewhere. Also there is a robust roadmap for the framework so I expect things like this to be developed soon.

The CMS I built was based on CakePHP which has a number of in-built helpers which make it quick and simple to build forms. Integrating Twitter Bootstrap with CakePHP wasn’t that hard a task but it doesn’t work out of the box. The markup patterns for the helper and bootstrap weren’t compatible. Fortunately the helpers on CakePHP can be highly customised, its not easy but also not a big task.

I aim to release a plugin helper which will make this a lot easier. It will replace the FormHelper and provides some extra methods to output of UI elements. I’ve been very busy recently so haven’t had chance to write it yet, but look out for it soon on github.

Rugby World Cup Post Mortem

So the rugby world cup is over, well for England anyway, for the teams which have embraced the future it continues and good luck to them all. For England the glory days are well and truly behind them 2000-2003. It was always going to be hard to live up to the world class team of 2003, but we’ve been in the doldrums for a while now, and its simply because the game has moved on.

England have nearly always played knock out rugby well, 2007 being the prime example. Grind out results, hey it might not be pretty but it works. Unfortunately it no longer works over 7 games and things need to change drastically or England face many years of toil, disappointment, the occasional high followed by many lows.

Could the quarter final knockout actual be good for England. The answer is yes if they learn from it. I’m no coach, just a long time fan of the game, but I’d like to analyse 5 things which I think needs to be looked at. Take them with a pinch of salt of course, and feel free to disagree, this is my list and I’m not even qualified to comment, its more of a rant than anything else.

Another point to make is that I’m not going to touch on the media circus surrounding the team. Off pitch calamities have been overblown and help no one but the media outlets sell more papers. Yes the team could be more professional and make sacrifices for a 6 week tournament but that isn’t the heart of the issue.

Basic Skills

Much like a footballer who’s paid to kick a leather ball around a field the basics of the game are not that hard. In my eyes every single professional footballer should be able to dribble, shoot with both feet and head the ball, the degree of proficiency with each skill depends on your position but you should be able to do them, after all thats what you are paid handsomely to do.

In rugby the basics are throw the ball, catch the ball and tackle. Much like football your position demands different levels of proficiency in each skill but you should be able to do each of them from 1-15.

So why does the England team knock on so much, and miss so many tackles? Maybe its cause I am so focussed on the England team but it seems to me that we make far too many passes above the receivers head, drop a high percentage of pop passes and miss a lot of tackles, especially when chasing a high ball.

Come on these are the basics, if we can’t get these right how on earth are we supposed to move on to the intermediate skills? The reason the southern hemisphere teams are so much better than us is because they get the basics right. They know that if they pop a pass out of a tackle there will a) be a man on the shoulder to receive the pass, and b) that player will catch the pass.

Its all well and good having a great forward pack, but if every time a chance is created or a break is made, you know the good stuff a player does, if it breaks down because a player can’t catch a ball well thats just pathetic really.

Discipline

The game of rugby has a lot of rules, some of them are strict and are easy to enforce, like no punching or gouging etc. If you do it on the pitch either the ref will see it and punish you or you’ll get cited after the game. That’s the way of it and its the same for every game.

Other rules are a little looser and can go either way, and some of the best players in the world know how to bend the rules to their advantage. And sometimes you have to break the rules to prevent points being scored. Its always been the way and I’ve got no issue with that. You are never going to have a game where no penalties are awarded.

The ref is there to enforce the rules and some refs enforce them better than others (more on that later) but more often than not a ref is going to tell you as a player if you need to leave a ball on the ground alone or if you are in danger of giving a penalty away. If it isn’t one of those times when its give the penalty away or its points, and high points at that, desist in what you are doing. Simple as that. If you are near the half way line and isolated, and risk being turned over, let it happen. Rely on your team mates to win it back, thats what they are there for. Conceding possession, followed be territory and/or points is senseless, and infuriating to watch.

Instinct may stop a player from doing this. Well that isn’t good enough coaches need to coach this instinct out of the players, and players need to work to do it. What there needs to be is a punishment system for persistent offenders. Its no good being the best scrummager in the world if you cost a team 9-12 points a game, and label your team as persistent offenders.

New Talent

You can’t buy experience apparently well that doesn’t seem to ring true very much anymore. Experience at the expense of talent is wrong. Just because a player has played 50-80 times for your country it shouldn’t automatically guarantee you a place. The reason is that a player may have got that experience by simply being the best of a bad bunch, or a mediocre bunch at best.

I’m looking at players like Nick Easter, Louis Deacon, Mark Cueto and Mike Tindall. All good players and experienced ones at that but they have never been world class players and the experience they bring does not make up for this fact.

Our best player by far this tournament was Manu Tuilagi, young, inexperienced and therefore doesn’t know how to grind out a result. Well bollocks to that, he was cleaning up after Tindall all tournament and the only player to consistently break the line. Look at the welsh team, the team isn’t made up of experienced players for the sake of it. Hell Stephen Jones wasn’t even on the bench in the Quarters, and they didn’t even take Martyn Williams!

Its time to ditch this experience, take a plunge and build a new team. If that means we have a couple of years of being in the bottom 3 in the nations, so be it. Its better than persisting with players who are never going to be good enough. And I never thought I’d say this but that may mean ditching Jonny Wilkinson, however for me, and this is quite a personal preference I don’t think Flood is a replacement.

Injuries

Some players never have any luck, if we look back to football there are always some players who always seem to be injured. Kieron Dyer a prime example. So is the case with rugby. Some players seem to be perpetually injured.

Two players spring to mind here, Andrew Sheridan and captain Lewis Moody. On his day Andrew Sheridan is a fearsome player, strong as an Ox and a powerful scrummager. Unfortunately his days are few and far between as he’s always getting injured.

Injuries are part of the game and its gotten a lot worse in the last few years as players have gotten bigger and more mobile due to the changing game and its professionalism. Problem is players who are prone to injury are a liability.

Should a place in a squad go to a player who might last a game just cause he’s quite good. Or is his likely injury going to end up being more disruptive to the squad as a whole?

And your captain’s name is first on the team sheet, a guaranteed spot. Is certainly shouldn’t be the likely case of if he’s not injured, and he shouldn’t be coming off every game after 60 minutes. If you’re prone to injury you probably are not worth the gamble, sad as that may be.

Referees/Laws

My final point is probably going to be a bit contentious as I have always considered the refs in rugby to be sacred and not to be argued with. But for a while now refs have been ruining the experience of watching the game.

The best refs for me have two qualities, they let games flow and deal with incidents as and when they need to, and they treat both teams the same. Sometimes these can be at odds with one another as if the ref is going to be strict and punish both teams then the game doesn’t flow but I’d rather have that than a ref who calls one team up on everything and lets the odd thing slip for the other team.

I would never go as far as to say refs are intentionally biased as I really don’t believe they are but I would say refs can be influenced by the state of a game and give decisions based on what has gone before.

The people running the game need to take a look at some of the rules and see where it is ruining the experience of watching. Things like the scrum where it seems either side is as likely to give a penalty away no matter what is happening.

Captains/players are talking to the ref more and more, which doesn’t seem right. But when the ref is having to explain his decision as the players don’t know what they’ve done wrong, well that isn’t right either and I’ve seen quite a few instances of that.

It may just be sour grapes but I shouldn’t be talking about the refs performance at the end of a game. I’d much rather talk about how good/poor a side have been in the game itself. Unfortunately this isn’t the case very much, and that just feels wrong.

For what its worth I thought the ref had a good game in the one which England lost, he was fair and consistent and thats how it should be.

So there we go the end of my rant. Thats what is wrong in my eyes, fixing things will be down to the England Team and Management whoever that may be post world cup. For now I’d like to wish Wales all the best for the World Cup. You were my dark horses at the start of the tournament and are playing the game which deserves to win. Whether you have enough to beat NZ is still to be seen, but I’ll be cheering you on hopefully all the way!

Bringing the Magic of Star Wars to the Next Generation

A bit more of a light-hearted blog post here. Prompted by seeing this video on the kottke blog got me to thinking randomly about the Star Wars saga today. I was born in the same year as Star Wars came out, its pretty much always been a part of my life and I have seen all six movies quite a few times!

The release order of the films was always a wonder to me as a kid, and when they announced the first three films were being made I literally had a geekgasm. Ultimately the first three films, whilst visually stunning, never really lived up to expectation. They never really had a chance, I was no longer 6 years old, my tastes and expectations had moved on considerably. My opinion now is that the first three films were aimed much like the first movies at the children and not me as an adult. You could say they weren’t as good and probably aren’t, after all the original movies are widely recognised as three of the best movies ever made which is a lot to live up to.

Anyway I’ve moved on and now I have a kid and think its my duty to introduce him to the saga as well. But now I see a bit of a dilemma, what order do I present them in? My initial reaction is the same order they were released, the same order I saw them in. But is this right? Given that the three newer movies would likely be enjoyed by him more than me and seeing the saga in chronological order should make more sense?

Seeing the kids reaction in the video above presents a strong argument to see them in non-chronological order, after all if he’d seen the third movie he’d already know Darth Vader was Luke’s father. George Lucas you have effectively ruined this surprise and many others by revealing stuff that was yet to happen in later movies – you utter bastard.

Would Star Wars have the same magic for Adam as it did for me if he saw the weaker films first? In fact would it have the same magic for him full stop, given that movies have arguably better special effects now than the original movie (although it still holds it own), would it still have the wow factor?

So what to do? I’m still undecided and don’t have to make a decision for a couple of years at least. One thing I’m sure of is there will be more difficult decisions than this to make over the coming years! In the meantime, that reaction above… priceless!

Using S3 with CakePHP

Despite the fact I’ve used amazon S3 on and off for the past 3-4 years I’ve never had to integrate the service into anything and on having to do so for a project I’m currently involved in I’ve come across a bit of a misunderstanding of how it works.

I’ve always assumed it was basically access to a file system on a bunch of server space. There was some clever mapping in the background to push the files/directories around but nothing more than that. Turns out my understanding was wrong. I’m sure the files are still pushed around or mapped to huge SANs in a very clever way but amazon S3 does not have the concept of physical directories.

Amazon S3 is simply a map of keys to files at its most basic level. The keys can contain URI mappings but the actual concept of a directory is incorrect. This misunderstanding stems from using third party tools to access S3. It always seemed like you could created a directory in a bucket, but in most of these tools what you are actually doing is creating a specially named file to represent a directory.

This misunderstanding only became apparent when I was using a simple plugin to my system which interfaced with S3. The plugin uploaded physical files, but what I wanted to do was upload directories for the files first. Something which this library didn’t do.

As always there has to be a better way and in fact there most certainly was. I ended up using the excellent PHP S3 class created by Donovan Schönknecht. Using it was a breeze and integrating it with my CakePHP app turned out to be very simple. Below is an example of pushing a file to S3. Its barebones and not a complete example, in the app I’m working on I have the whole thing integrated with RabbitMQ and the Media plugin through a shell but it should give you an idea of how easy this class is to use.

Download the S3 class and place it in your vendors folder, then in your controller assuming the file is already on the server.


public function pushToS3($file) {

  App::import('Vendor', 'S3');
  $s3 = new S3('access_key', 'secret_key');

  $upload_file = $s3->inputFile($file, false);
  $s3->putObject($upload_file, 'bucket', $file, S3::ACL_PUBLIC_READ);

}

Its as simple as that. The third argument to the putObject() function is where you specify the key to the file, if you want a URI with a directory like structure here is where you specify it.

Huge thanks must go to Donovan Schönknecht as his class takes absolutely all of the heavy lifting required out of interfacing with S3. There’s a whole bunch of operations which the class can do, so if you need to do stuff a bit more complex I would highly recommend using this class.

England Test Cricket – Currently Very Good

A bit of a break from the dev stuff. We’re half way through the 3rd test against India and I am already comfortable enough (weather obliging) to say England are about to become the No 1 ranked test side in the world. If/when this happens I am sure a lot will be made of it by the media and rightly so, in my 30ish years of following English cricket there have been a lot of lows and very few highs. It seems almost strange that rightly England will be the best team in the world, and we could consider them that even if they don’t claim the spot of the coming 1 1/2 tests.

If you look at the side its not hard to see why they are the best in the world, and in true geek fashion and an indulgence of my love of statistics here’s concrete proof why.

England are a very good side

Notice I have said a very good side and not a great side. They may go on to become a great side (more on that later) but for now they are just very good.

Lets start by looking at the batting. Currently if you look at the batting line up they have great/good/competent batsman all the way through the side right down to number 11. Each and every batsman has a batting average in double figures.

England batting averages – Test matches
Player Span Mat Inns NO Runs HS Ave BF SR 100 50 0 4s 6s
JM Anderson 2003-2011 62* 80 33 559 34 11.89 1563 35.76 0 0 6 69 1
IR Bell 2004-2011 68* 115 15 4792 199 47.92 9170 52.25 15 28 8 562 19
RS Bopara 2007-2011 11* 15 0 502 143 33.46 936 53.63 3 0 4 64 2
TT Bresnan 2009-2011 9* 7 0 265 91 37.85 603 43.94 0 2 0 33 0
SCJ Broad 2007-2011 40* 53 7 1335 169 29.02 2101 63.54 1 8 7 173 9
AN Cook 2006-2011 71* 124 8 5640 235* 48.62 11540 48.87 19 26 3 654 5
EJG Morgan 2010-2011 12* 16 1 513 130 34.20 897 57.19 1 3 3 59 4
KP Pietersen 2005-2011 77* 131 7 6123 227 49.37 9844 62.20 18 24 7 730 55
MJ Prior 2007-2011 46* 68 12 2526 131* 45.10 3768 67.03 6 18 8 287 12
AJ Strauss 2004-2011 88* 156 6 6300 177 42.00 12666 49.73 19 25 13 780 10
GP Swann 2008-2011 35* 40 6 800 85 23.52 979 81.71 0 4 2 98 10

How often through the 2001 tests can that have been said, not often I would guess. And if you look at the averages for most of the batsmen, Ravi Bopara aside they are all averaging 40+. Take Ravi out and bring the usual Trott only strengthens the situation Trott has an average of 57+. These aren’t the averages of people at the start of their careers they are highly established batsmen with 5000+ test runs.

At the moment any team would struggle to contain a team which can realistically get a 50 from anyone down to number 10.

Ok on to the bowling:

England bowling averages – Test matches
Player Span Mat Inns Overs Mdns Runs Wkts BBI BBM Ave Econ SR 5 10 Ct St
JM Anderson 2003-2011 62* 113 2206.3 499 7150 233 7/43 11/71 30.68 3.24 56.8 11 1 28 0
IR Bell 2004-2011 68* 6 18.0 3 76 1 1/33 1/33 76.00 4.22 108.0 0 0 56 0
RS Bopara 2007-2011 11* 7 49.2 7 199 1 1/39 1/39 199.00 4.03 296.0 0 0 5 0
TT Bresnan 2009-2011 9* 16 300.0 84 865 36 5/48 7/96 24.02 2.88 50.0 1 0 3 0
SCJ Broad 2007-2011 40* 71 1332.5 285 4102 126 6/46 8/76 32.55 3.07 63.4 4 0 12 0
AN Cook 2006-2011 71* 1 1.0 0 1 0 - - - 1.00 - 0 0 62 0
EJG Morgan 2010-2011 12* - - - - - - - - - - - - 8 0
KP Pietersen 2005-2011 77* 47 167.3 11 666 5 1/0 1/10 133.20 3.97 201.0 0 0 47 0
MJ Prior 2007-2011 46* - - - - - - - - - - - - 139 5
AJ Strauss 2004-2011 88* - - - - - - - - - - - - 106 0
GP Swann 2008-2011 35* 63 1401.5 282 4114 142 6/65 10/217 28.97 2.93 59.2 10 1 30 0

Not as great here if you look over the averages, mid 20′s to low 30′s for a wicket isn’t that good, but there is a decent total number of wickets between them so in reality pretty good.

So not the strongest attack in the world but far from the weakest either.

Why England Deserve to be No 1

Here’s where things start to get interesting. The above shows that currently England have a very good side but not a great side in terms of overall career figures. If you take Indias big 5 batman, Tendulkar, Dravid, Sehwag, Laxman and Gambhir they have countless runs between them and career averages that most teams can only boast of having 1-2 of per team. That’s why they are currently no 1 and why they are a great batting team.

So how come England are destroying them and other teams if they don’t have great batsmen? Well lets take a look over the past 2 years at the players averages.

Overall figures
AN Cook 2009-2011 24 39 3 2155 235* 59.86 4156 51.85 10 6 0 240 3 investigate this query
IJL Trott 2009-2011 23 38 4 1965 226 57.79 4050 48.51 6 7 1 215 0 investigate this query
IR Bell 2009-2011 20 29 5 1724 159 71.83 3021 57.06 7 8 1 203 6 investigate this query
KP Pietersen 2009-2011 23 35 4 1488 227 48.00 2459 60.51 2 9 3 195 7 investigate this query
MJ Prior 2009-2011 24 33 5 1222 126 43.64 1749 69.86 4 8 4 133 7 investigate this query
AJ Strauss 2009-2011 22 35 1 1164 110 34.23 2319 50.19 1 10 4 144 2 investigate this query
PD Collingwood 2009-2011 16 24 1 719 145 31.26 1524 47.17 1 4 2 79 8 investigate this query
SCJ Broad 2009-2011 19 24 1 634 169 27.56 977 64.89 1 3 6 79 5 investigate this query
GP Swann 2009-2011 24 28 2 527 85 20.26 610 86.39 0 2 1 62 8 investigate this query
EJG Morgan 2010-2011 12 16 1 513 130 34.20 897 57.19 1 3 3 59 4 investigate this query
TT Bresnan 2010-2011 7 6 0 256 91 42.66 587 43.61 0 2 0 32 0 investigate this query
JM Anderson 2009-2011 21 26 7 162 29 8.52 482 33.60 0 0 6 23 1 investigate this query
MA Carberry 2010-2010 1 2 0 64 34 32.00 140 45.71 0 0 0 9 0 investigate this query
CT Tremlett 2010-2011 7 6 3 47 24* 15.66 98 47.95 0 0 0 5 0 investigate this query
JC Tredwell 2010-2010 1 1 0 37 37 37.00 63 58.73 0 0 0 6 0 investigate this query
ST Finn 2010-2011 12 13 9 35 19 8.75 128 27.34 0 0 2 6 0 investigate this query
A Flintoff 2009-2009 1 2 0 29 22 14.50 37 78.37 0 0 0 5 0 investigate this query
RJ Sidebottom 2010-2010 1 2 0 15 15 7.50 23 65.21 0 0 1 2 0 investigate this query
SJ Harmison 2009-2009 1 1 1 12 12* - 12 100.00 0 0 0 3 0 investigate this query
G Onions 2009-2010 3 5 5 11 4* - 45 24.44 0 0 0 2 0 investigate this query
A Shahzad 2010-2010 1 1 0 5 5 5.00 12 41.66 0 0 0 1 0 investigate this query
RS Bopara 2011-2011 1 - - - - - - - - - - - - investigate this query

Those averages now are pretty much as good as India and in some cases so much better. Bell averaging 71 over the past 2 years, which includes playing Australia twice! Those are the averages of great players.

Look now at the bowling averages and the picture gets much prettier.

Overall figures
GP Swann 2009-2011 24 44 966.0 185 2813 102 6/65 10/217 27.57 2.91 56.8 8 1 investigate this query
JM Anderson 2009-2011 21 39 818.4 204 2342 93 6/17 11/71 25.18 2.86 52.8 4 1 investigate this query
SCJ Broad 2009-2011 19 36 669.0 156 1920 68 6/46 8/76 28.23 2.86 59.0 2 0 investigate this query
ST Finn 2010-2011 12 24 345.4 74 1346 50 6/125 9/187 26.92 3.89 41.4 3 0 investigate this query
CT Tremlett 2010-2011 7 14 283.3 67 872 36 6/48 8/150 24.22 3.07 47.2 2 0 investigate this query
TT Bresnan 2010-2011 7 13 269.0 77 768 33 5/48 7/96 23.27 2.85 48.9 1 0 investigate this query
G Onions 2009-2010 3 6 115.0 23 366 8 3/86 4/136 45.75 3.18 86.2 0 0 investigate this query
JC Tredwell 2010-2010 1 2 65.0 13 181 6 4/82 6/181 30.16 2.78 65.0 0 0 investigate this query
A Shahzad 2010-2010 1 2 17.0 4 63 4 3/45 4/63 15.75 3.70 25.5 0 0 investigate this query
SJ Harmison 2009-2009 1 2 20.0 6 69 3 3/54 3/69 23.00 3.45 40.0 0 0 investigate this query
PD Collingwood 2009-2011 16 14 64.0 11 173 2 1/3 1/5 86.50 2.70 192.0 0 0 investigate this query
RJ Sidebottom 2010-2010 1 1 31.0 6 98 2 2/98 2/98 49.00 3.16 93.0 0 0 investigate this query
IJL Trott 2009-2011 23 12 44.0 3 185 2 1/5 1/5 92.50 4.20 132.0 0 0 investigate this query
A Flintoff 2009-2009 1 2 24.5 5 77 1 1/35 1/77 77.00 3.10 149.0 0 0 investigate this query
KP Pietersen 2009-2011 23 16 45.0 2 148 1 1/10 1/10 148.00 3.28 270.0 0 0 investigate this query
IR Bell 2009-2011 20 - - - - - - - - - - - - investigate this query
RS Bopara 2011-2011 1 - - - - - - - - - - - - investigate this query
MA Carberry 2010-2010 1 - - - - - - - - - - - - investigate this query
AN Cook 2009-2011 24 - - - - - - - - - - - - investigate this query
EJG Morgan 2010-2011 12 - - - - - - - - - - - - investigate this query
MJ Prior 2009-2011 24 - - - - - - - - - - - - investigate this query
AJ Strauss 2009-2011 22 - - - - - - - - - - - - investigate this query

England have 6 bowlers all averaging in the low to mid 20′s. To have a team like that shows why all teams are being blown away, and england only play 4 of them in a game!

All these stats are backed up by the ICC rankings where England have 2 batsman in the top 10 and 5 in the top 20, as well as well as 4 bowlers in the top 10 and 5 in the top 20. After this test these are likely to improve as well.

Will England go on to be a great team?

So currently we England are a very good side playing great cricket. To be considered a great side they need to maintain their current form over many years. The West Indies dominated in the 80′s, Australia dominated in the 90′s/00′s. England must dominate the next 5 years to be considered a great side. They also need to up their game in other formats of the game, particularly ODI where they are wildly inconstant, the great Aussie sides dominated both formats of the game.

They may just have the personnel to do that. Most of the bowling attack are young and have many years of cricket ahead of them. Not so much with the batting side but there are players who can form the spine of the team for years to come, Cook and Bell, allowing other batsmen to come in and bed themselves into the team when the likes of Strauss and Pietersen inevitably retire.

A special mention must go to Matt Prior who is currently without doubt the best wicketkeeper batsman in the world. Replacing him when he goes will be pretty difficult but not impossible. I am not as up on the county scene to know whether there are any promising keepers coming through.

Finally I guess we should always have one eye on a good spinner, Swann isn’t young so it would be good to have backup for him. I hear Rashid has lots of potential though.

For now enjoy the good times, and long may they live!

All stats have come from cricinfo which is an amazing source. I wish other sports had such great publicly available information!

Getting CakePHP database session data for an external script

I have a requirement for a current project to be able access the CakePHP session data for a script which is running outside of the framework. In normal circumstances this would be as simple as:

session_name('CAKEPHP');
session_start()
print_r($_SESSION);

However in my case the session data is stored in the database. In this situation you need to do a slight bit more to retrieve the session data. Thought I would share the code here for anyone who may need it.

The data is stored in the database in a serialised form and the session_key is essentially stored in a cookie. Therefore the equivalent of the above is:

include_once('../config/database.php');
$db = new DATABASE_CONFIG();

$dbh = mysql_connect($db->default['host'],$db->default['login'],$db->default['password']);
$dbn = mysql_select_db($db->default['database'], $dbh);

$session_qry = mysql_query('SELECT `data` FROM `sessions` WHERE `id`="'.$_COOKIE['CAKEPHP'].'"', $dbh);
$session_data = mysql_result($session_qry, 0, 'data');

session_start();
session_decode($session_data);
print_r($_SESSION);

Hope this comes in useful.

CakePHP Auth Component and Multiple Models

It seems recently all I have been doing is chasing things that are hard to debug :-( This may or may not be a bug but has had me chasing things for a while. I have a system which has two (three) different types of user, a system for users and a group user. They have very different use cases and it didn’t make sense to have a user type. One person may be either a user, a group admin or both. The features for each type were completely separate, i.e. if you were a group admin you wouldn’t automatically be a user as well.

As this was the case I decided to create a separate login system for both functions. Different tables, different models. Both had a login page and both used the Auth component. I did a simple switch in the app_controller to set up the Auth components variables and everything worked on an individual level, i.e. if you visited each login page without having first visited the other login page during the session. However if you went to one login page first and then to the other login page you couldn’t login to the second area.

After a few hours debugging I discovered that setting the $this->Auth->loginRedirect property isn’t enough to control the Auth component. When you first visit a page in a session cakephp sets the Auth.redirect session variable to the Auth::loginRedirect property. The Auth component then checks against this session variable when you login. If they don’t match then you are redirected to the session variable, which you probably don’t have access to as you haven’t logged in to this area.

The solution to this is to manually set this session variable when you change any Auth component properties.

function beforeFilter() {

  if($this->params['controller'] == 'groups' or $this->params['controller'] == 'group_users') {
  
    $this->Auth->userModel = 'GroupUser';
    $this->Auth->loginAction = array('controller' => 'group_users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'groups', 'action' => 'index');
    $this->Auth->logoutRedirect = array('controller' => 'group_users', 'action' => 'logout');
    $this->Auth->allow('display','login','logout');
    $this->Auth->authorize = 'controller';
    $this->Auth->userScope = array('GroupUser.active' => 1);
    
    $this->Session->write('Auth.redirect','/groups/index');
    
  }
  else {
  
    $this->Auth->userModel = 'User';
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->allow('display','login','logout');
    $this->Auth->authorize = 'controller';
    $this->Auth->userScope = array('User.active' => 1);
    
    $this->Session->write('Auth.redirect','/users/index');
    
  }
  
}

At the moment I can’t decide whether this is a bug or whether my use case is a bit wrong. Obviously just setting the loginRedirect property of the Auth component shouldn’t affect the session. Maybe there needs to be a method to set this property instead which handles the session?

Apache, .htaccess, mod_rewrite, git and CRLF

Today I came across a frustrating and ultimately difficult to debug situation brought about by a cocktail of circumstances. Working on a site the rewrite rules suddenly stopped working when I made a small change to the .htaccess file. Nothing unusual here, back up the change and carry on. Except backing up the change made no difference. Ultimately the site still wouldn’t work.

Two fruitless hours later checking and rechecking syntax, conf directives and even the changing of DOCUMENT_ROOT just in case and still the issue wouldn’t resolve. The only thing left to do was to start again.

Delete the files, check the repos back out of git and see what happened. Magically it started working again, obviously something I had done had broken the site but what could it be. I retraced my steps and got once again to the .htaccess file and again the site broke. Instead of backing up the change I simply rechecked it out of git, which again fixed the issue. I then opened the file, made no changes and saved it, and this broke the site.

So basically it worked fine from the checkout of git, but not once my text editor had touched it. This of course screamed line endings at me, and indeed this was the culprit. CRLF in the repos, LF when it hit my editor.

What was strange here was that it worked when it was from git, but not when my editor had touched it. Even stranger was that Apache couldn’t tell me if anything was wrong no matter what steps I took to debug. Nothing in the logs the .htaccess file was simply ignored silently. Not very helpful.

The answer lay on github, where setting a global config setting in git would handle this situation gracefully but tracking down the issue was very hard.

git config --global core.autocrlf input

Fixing it was a simple matter of using dos2unix, a utility I had experience with due to similar issues with Perl scripts back in my early days as a programmer.

As ever Google turned out to be my friend, but only after I had diagnosed the issue, searching for issues with .htaccess files had a far too high signal to noise ratio.

Another one to chalk up to experience.

CakePHP Self Referencing User Model

I recently needed to create a data model which included a friend-type system. Not a social network type thing but simply the ability to tag other users of a system. Sounds simple enough!

The application is written in CakePHP which has some fairly good ORM facilities but what I wanted wasn’t really simple to implement. The relationship I wanted to configure was a hasAndBelongsToMany relationship with a single table. In other words the join table referenced just one table. The join table looked like this:

CREATE TABLE IF NOT EXISTS `users_tags` (
  `user_id` INT(11) NOT NULL,
  `tag_id` INT(11) NOT NULL
);

And here is where things get a bit harder. There is no tags table which by convention the HABTM would usually join up to. What I really wanted was this:

CREATE TABLE IF NOT EXISTS `users_users` (
  `user_id` INT(11) NOT NULL,
  `user_id` INT(11) NOT NULL
);

Which obviously won’t work as you can’t have two columns named the same. Fortunately everything in CakePHP is configurable so setting the table up like this:

CREATE TABLE IF NOT EXISTS `users_users` (
  `user_id` INT(11) NOT NULL,
  `tag_id` INT(11) NOT NULL
);

Then configuring the User model with this relationship:

var $hasAndBelongsToMany = array(
    'Tag' => array(
      'className' => 'User',
      'join_table' => 'users_users',
      'foreignKey' => 'user_id',
      'associationForeignKey' => 'tag_id'
    )
  );

Enabled me to pull out all the tagged users for a particular user. This is where the fun started for me. I now needed the ability to tag players. I set up a list of users you could tag with a controller action like this /users/tag/$id. As I was logged in I already had the user_id bit and was passing the tag_id into the controller action. I expect to be able to do:

$this->User->UserUser->set('tag_id', $tag_id);
$this->User->UserUser->set('user_id', $user_id);
$this->User->UserUser->save();

But no matter what I tried the data wouldn’t save as one row. For some reason two rows were inserted, both with a blank user_id and the two ids supplied as tag_ids. I know that HABTM calls delete before saving but I didn’t see how it would be affecting the code as above. I tried for a while to get the CakePHP way of doing this but couldn’t work it out. In the end I just used a plain old query() to achieve what I wanted:

$this->User->query('INSERT IGNORE INTO `users_users` SET `user_id`='.$id.', `tag_id`='.$tag_id);

Whilst this does the job I would rather have worked out why it wasn’t working in the CakePHP way, so if any experts out there can shed some light please do!

This wasn’t the end of my woes tho. The application also uses the Auth component. The HABTM relationship seemed to be causing the Auth component some issues, when it logged in it was issuing a save() call which resulted in all my tags being wiped out. The solution to this was to unbind the model in the beforeFilter() method:

function beforeFilter() {
    
    $this->Session->start();
    
    $this->User->unbindModel(array('hasAndBelongsToMany' => array('Tag')));

    $this->Auth->autoRedirect = false;
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'games', 'action' => 'grid');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->allow('display','login','logout');
    $this->Auth->authorize = 'controller';
    $this->Auth->userScope = array('User.active' => 1);

  }

After this the tagging relationship seems to work a treat. Hopefully this will post will prove useful to anyone trying to achieve a similar result.