php|tek 2009: Tutorial Day

I’m currently attending the php|tek conference in Chicago. Today is tutorial day, and I delivered a half-day session with Matthew Weier O’Phinney of Zend. Our session was entitled “Practical SVN for PHP Developers” and the slides are online. The session seems to have gone over pretty well – this is my first time delivering a tutorial and overall it was a good experience.

Later in the week I’ll deliver two more talks – Linux-Fu for PHP Developers and A Guide to Using and Understanding the Community – plus another in the unconference entitled Architecting Web Services in the unconference. So far I’m meeting old friends and new ones, and having a blast :)

Error Feedback for Web Services

I have been thinking, writing and speaking a little bit about web services recently, starting with a few thoughts on authorisation mechanisms for services. Today we’ll look at another really important aspect of authoring web services, and one feature that will definitely get used – error handling and feedback! Having clear and robust error handling in your service will help those trying to consume it immeasurably. Nothing is more annoying that impenetrable errors, unclear responses, or a service which accepts your input but then turns out not to have done what you expected. And I say that from experience.

Stacks of Errors

What’s more annoying than getting an error from a web service? Getting another error from the service every time you fix the first one!

Most services have a few steps of checking incoming variables. Checking that the user has supplied all required fields, and that all incoming fields are of the required format, and that the data they refer to does actually exist – there’s a lot going on here. Too many systems take fright at the first sight of an error, and return straight to the user like a child reporting a sibling’s misdeeds to a parent. I mean something along these lines:

if(!isset($_POST['username'])) {
  return 'username is missing!';
}
if(!isset($_POST['password'])) {
  return 'password is missing!';
}
foreach($_POST as $key => $value) {
  $expected_fields = array(
    "username",
    "password"
  );
  if(!in_array($key,$expected_fields)) {
    return "unexpected field ".$key;
  }
}

What’s more useful is if the user can have a better overall view of everything that is going wrong, since often they might be caused by the same misunderstanding or perhaps be related to one another. So I’m considering code that looks more like this:

$messages = array();
$can_proceed = true;

if(!isset($_POST['username'])) {
  $messages[] = 'username is missing!';
  $can_proceed = false;
}
if(!isset($_POST['password'])) {
  $messages[] = 'password is missing!';
  $can_proceed = false;
}
foreach($_POST as $key => $value) {
  $expected_fields = array(
    "username",
    "password"
  );
  if(!in_array($key,$expected_fields)) {
    $messages[] = "unexpected field ".$key;
    $can_proceed = false;
  }
}

if(!$can_proceed) {
  return $messages;
}

The nice thing about something like this is you’ll see a series of messages where there are problems – so when you mis-spell a field name, you’ll see the “missing field” message for a field you know you are sending, but you’ll also see the “unexpected field” message and hopefully that will make it easier to spot what’s gone amiss.

Error format

Its tempting to return error information in a completely different format, after all it is quite different from the request that probably would have been returned from a correct request. Some web service protocols dictate how errors should be sent – SOAP has the soap-error response, for example. But for something where we have more control, such as an RPC style or REST service, we can choose. Usually I think its appropriate to return an appropriate status code (for REST) or wrapper (for RPC) and then include the error information in the same format as the response would have been. This is mostly for ease of consuming the response, saving clients from having to parse an additional format!

Approaching Errors

Having malformed input to services is inevitable – through misunderstandings, typos, and of course rubbish input by users. Making sure that all these eventualities are gracefully caught and information returned to the user means that the user stands a much better chance of being able to interact successfully. If only the success case works, but the service either doesn’t respond, returns nonsense (or worse, misleading information!), or appears to work but actually hasn’t, your users won’t hang out for long to work out why.

I’ve covered some really basic ideas here but I’m sure there are plenty of other nice ways to help guide users – feel free to add comments for the features you implement in your systems to help users when things aren’t going quite right!

By giving more information to users, it becomes much easier for them to develop against your service quickly and easily – and its not much more to add on the service side.

Speaking at FOWA Tour, Leeds, May 28th

I’m surprised and delighted to announce that I will be one of the speakers at the rather excellent FOWA Tour even in Leeds on 28th May (two weeks today!). The day is like a roadshow version of the famous FOWA conferences, with a morning of (free!) cloud workshops and an afternoon of excellent speakers. I had been hoping to attend this event anyway so to get the invite to speak is very exciting indeed!

My talk is entitled “Architecting Web Services” and will take a look at the types of service available, and identifying functions which would be useful as a service. I’ll run through how decisions about service and format can be taken and we’ll look at some examples of these while we’re at it. I’ve been building a few services lately for one thing and another and I’m pretty excited about the whole topic! Luckily I only have a half-hour on stage but rest assured I’ll probably still be talking about it over drinks later in the day!

Anyone going? Thinking of going? Let me know and make sure to come and say hi on the day :)

Leeds GirlGeek Dinner 27th May 2009

I’m delighted to hear that the date has been set for the next Leeds GirlGeek Dinner – Wednesday May 27th 2009! I’ve been really enjoying this series of events and always meet some really interesting ladies when I attend these events. The idea is to get geek girls together for some talks (and there have been some really good ones in Leeds), some food, some drinks and some networking – men are allowed but only as the invited guest of one of the girlgeeks. This enables us to keep the balance and actually makes for a brilliant atmosphere. Its not often the men in the room are horribly outnumbered :)

This month we have Reshma Sohoni from SeedCamp and Jennifer O’Grady from Democracy PR – and I can’t wait to hear both these women speak and hear their stories. The details are all on the website but in short the event starts at 6pm, its at The Loft Leeds, and I hope I’ll see you there!

Presenting Under Linux: xrandr

This is a mini-primer on using xrandr to configure a second screen under Linux, but first I’ll start with some background.

I’ve been a linux user for a few years now but when I started working for Ibuildings I started giving presentations, either as a speaker or when delivering training – and for a long time I used to boot into Windows for those, using a VM if I needed to code.

The main reasons I wasn’t comfortable using Linux to present were:

  • I have Powerpoint and I use the presenter view a lot
  • Linux doesn’t automatically find and configure monitors like Windows can.

Since Ubuntu Intrepid Ibex (8.10), its been possible to reconfigure screen in Linux without restarting X. But I was kind of scared of xrandr and I still liked the Powerpoint presenter view so I just carried on with windows. Then I realised that Open Office had also released a presenter view and I gave it a try. With one obstacle removed it was time to learn to work xrandr, which means I can also work external monitors without fiddling with xorg.conf, very useful. I’m now pretty confident with it so here’s the benefit of my wisdom*

xrandr

The first thing to do is figure out what you have and what your options are. So plug in the second screen and run xrandr with no arguments. On my system I get something like this:

Screen 0: minimum 320 x 200, current 1024 x 768, maximum 2560 x 1024
VGA connected 1024x768+0+0 (normal left inverted right x axis y axis) 338mm x 270mm
   1280x1024      60.0 +   75.0
   1024x768       75.0*    60.0
   800x600        75.0     60.3
   640x480        75.0     59.9
   720x400        70.1
LVDS connected 1024x768+0+0 (normal left inverted right x axis y axis) 304mm x 190mm
   1280x800       60.0 +   50.0
   1024x768       85.0     75.0*    70.1     60.0
   832x624        74.6
   800x600        85.1     72.2     75.0     60.3     56.2
   640x480        85.0     72.8     75.0     59.9
   720x400        85.0
   640x400        85.1
   640x350        85.1
HDMI-1 disconnected (normal left inverted right x axis y axis)
HDMI-2 disconnected (normal left inverted right x axis y axis)

This shows VGA (your external output) and LVDS (the laptop screen) and all the various modes they support. I sometimes see output under HDMI-1, and this seems to cause problems so I always turn off this output by running:

xrandr --output HDMI-1 --off

Next I send a command to set the resolutions on both screens. For projectors I tend to go with 800×600 as this almost always works and it means the text on my laptop screen is then so large I can see it from several feet away!

xrandr --output LVDS --mode 800x600 --output VGA --mode 800x600

Its perfectly fine to have different resolutions on both machines but bear in mind that X has one desktop and will show you sections of it on each monitor so if they’re different sizes you sometimes get either bits missing off one monitor or one of the outputs looks stretched. I don’t really have any general advice with this other than get the projector in the right ratio and then fiddle with the available modes to see what works for you.

To set the second screen to be different from the first (i.e. tiled rather than mirrored), just tell xrandr where you want it.

xrandr --output LVDS --mode 1280x800 --output VGA --mode 1024x768 --right-of LVDS

Again you’ll get sections of a single desktop, not two separate areas, so things might look odd if they are on both screens. For me this command has worked really well since I got to grips with it in the last few months, and I’ve plugged various machines in to various external monitors and given a few different talks. I’m sure there is a lot more to know about xrandr but I haven’t needed it yet – hope this is helpful! If you have any experiences with this or anything to add, just drop a line into the comments form.

* I’m not an expert by any means, but the above works for me on Ubuntu with my Thinkpad T400 and my Acer Aspire One.

PHPNW TestFest

Yesterday saw the PHP North West User Group hold their first Test Fest in Manchester. As a relatively young user group (at less than a year old) with not a single @php.net email address between us, this was an ambitious venture. Scott MacVicar very kindly offered to come and be our mentor – to show us around PHP and get us started writing tests, and in fact he did very well and everyone was able to begin quite quickly.

ScottMac's To-Do List

With 14 attendees to support, Scott had his hands full but between all of us we managed 84 tests which was very impressive!! Most people seemed to pick it up really easily and I’m almost certain that there will be a few more tests coming in from various people who were there on the day – which is great because the increased test coverage really does make a big difference to PHP itself.

PHPNW TestFest Crowd

I’d like to say a huge thanks to everyone who turned up and wrote tests, to Scott for coming and dragging us all through the process with such good humour, to Salford University for lending us the space for the event and to Ibuildings for sponsoring the event and making it all possible! There are a few more photos as well – a huge well done to everyone who took part, here and at the other test fest events!

Looking Forward to php|tek

This year I’m excited to be heading to the php|tek conference in Chicago (May 19th to 22nd), and even more excited to be going there as a speaker. Over the course of the conference I will present or co-present three sessions; one tutorial and two talks.

The tutorial session is a half-day on Tuesday afternoon – I’m co-presenting with Matthew Weier O’Phinney of Zend and our session is “Practical SVN for PHP Developers”. We’ll be walking through the concepts of source control, adminstering subversion, repository structure strategies and also showing some hands-on use of subversion. We’ll talk about the issues around introducing subversion into a team and looking at how this tool fits in as part of the development process. If you’re a subversion user looking to learn to get more fromthe tool, an architect looking to include it in new or existing setups, or a development team leader or manager looking for information about setting up and managing subversion, then come along!

The next talk I’ll give is on Wednesday when I’ll be presenting “Linux-Fu for PHP Developers”. If you’re already a linux wizard then feel free to drop by and heckle – but if you’re either a beginner or a competent user looking to raise your game then this session is for you. Its a tour of the commands I use day-to-day; an opportunity to metaphorically look over my shoulder and take a peek at the tools I use and how I use them. There are code samples and although there is a lot to take in, I hope it will make command-line linux usevery approachable for everyone – if I can do it, anyone can :)

Finally I get to co-present with my friend and Ibuildings colleague Stefan Koopmanschap a session entitled “A Guide to Using and Understanding the Community”. This session is an overview of what happens in the community outside of people’s nine-to-five jobs, how to access the community and what you might find there if you do. A lot of the value of open source software is in the community but knowing what information and interactions you can find there and how to find them is a mystery to many. For business in particular, free software with no guarantees can be slightly daunting! Stefan and I have lots of community involvement and will give a tour of what’s happening, how you or your employees can gain fromthe community, and how you can give back.

I’m really looking forward to the conference, to meeting old friends, virtual friends, and friends I haven’t made yet – if you will be there let me know and make sure to come and say “hi”!

Screen-Profiles – Improved Screen in Ubuntu Jaunty

I recently upgraded my kubuntu installation to 9.04 “Jaunty Jackalope”. It turns out that they have added some very cool features to screen in this version. If you don’t use screen its its worth finding out more, certainly its a tool I now can’t live without.

The first thing to say is that if you have an existing .screenrc file, you don’t get any notification or prompt about the new features of screen, I was lucky and fell over the information. To get to the features:

  • if you have a .screenrc, rename it for a moment
  • aptitude install screen-profiles screen-profiles-extras
  • run screen

EDIT: From Karmic Koala (9.10) and later these packages are now byobu and byobu-extras – they work as outlined here however, just the package names changed.

The first thing you’ll notice is that there’s a whole lot more stuff going on at the bottom of the screen. I have had a custom .screenrc file which gave me tabs for a while, but now they’re part of the standard setup.

If you look in the bottom right corner you’ll see there’s an “F9 Menu” – yep, screen responds to a whole bunch of function keys! I’ll keep my traditional ctrl+a mapping I think, but you can reconfigure this to your hearts content. And look at the menu itself:

You can change the colour themes (more on that another day, I’m still playing with the settings) and you can also turn on and off a load of different widgets, basically lots of different system information. I’ve got most of it turned off on my laptop (since I already have a battery meter etc) but for a server this could be really useful. Examples:

I’m really happy to see Canonical improving this particular tool, and I wanted to mention it purely because existing users of screen won’t see the new functionality by default which seems like a bit of an oversight. If you’re a screen user, upgrade to Jaunty, try it out, and leave a comment on how it works out for you.

Testing PHP

I’m organising the PHP Test Fest that is happening in Manchester next weekend, and in preparation I decided it was high time to sit down and figure out what testing PHP is all about. People kept telling me it was easy but I had no clear picture of how all the pieces went together – there are different ways of doing the same thing and although I have been keen to get involved with testing for some time, I haven’t been able to get started until now.

I’m a linux user, so this whole post relates to how this works for me, on my ubuntu system.

First of all I grabbed a copy of PHP from http://snaps.php.net. The TestFest focuses on writing tests for PHP 5.3 so I took the most recent copy of that version.

I unzipped the code, changed into that directory and compiled the code and ran all the tests:

./configure  --enable-gcov
make
make test

At this point I was prompted to send the report of the tests off to the PHP QA team – I always make sure to do this when I compile PHP, it helps them a lot to collect the data and it is no bother for me. I will say that due to the huge increase in the number of tests in PHP 5.3, it takes a bit longer than it used to, but I don’t mind.

One thing which I keep seeing when I see people talk about tests is the lcov test coverage graphs, the ones that look something like this:

“Just pick something to test from there”, people said. I just didn’t understand at all how to pick which bit of PHP I would need to write test code for. It turns out that if you click on an extension, you get a list of files, and these too have coverage numbers.

Then if you click on an individual file, you see the C code of PHP itself there, with highlights to show which lines are executed in tests and which are not. So now you can start to get an idea of what needs testing (although you have to read C to do it which is fine since I’ve studied it, and PHP is pretty similar, but it does get confusing in places).

It is pretty easy to run a subset of tests, despite some quite long-winded documentation on the subject. There is a script inside the PHP directory called run-tests.php which takes an argument of either a single test or a directory of tests you’d like to run. Just call this with CLI PHP and specify which tests you want – I specify the path to the PHP since I want to use PHP 5.3 and not the existing PHP which I already use on my system

export TEST_PHP_EXECUTABLE=/sapi/cli/php
$TEST_PHP_EXECUTABLE /run-tests.php tests/ext/spl/

The “make test” command also takes a TESTS= argument, and this is also an option. Its just a wrapper for run-tests though so either works fine. I built my php inside the directory with the source in it, just for convenience, but you don’t have to – and if you don’t then you need to adjust the paths above accordingly.

Then its time to read the phpt documentation and start writing tests. There are groups all over the world taking part in the testfest, so check the list and find your nearest. For me that’s Manchester, 9th May 2009 – and I’m looking forward to it!

Kubuntu Freezing on Splash Screen after Jaunty Upgrade

Today I upgraded a bunch of machines to the latest Kubuntu offering, version 9.04, or “Jaunty Jackalope”. Well, it was all going a bit too well and when I came to upgrade my main work machine, a thinkpad T400, I ran into problems. There are a couple more kinks but I had one particular error which I wanted to blog about. When I upgraded and then restarted Kubuntu, it froze on the splash screen after showing three icons (up to the globe).

The problem was that the files in the ~/.ICEauthority directory were owned by root so couldn’t be written to by my user. I followed the instructions I found on this post where someone had exactly the same problem, and just chowning the whole directory to my user fixed the problem and allowed me to boot Kubuntu.

Hopefully if anyone else has this problem they’ll find either this post or the other one and also find the easy fix! If this works for you, leave a comment.