I survived!

(Yes, the headline recycling is becoming a trend. Get on with the program!)

The view from the bridge at Hvaler at 05:45I can happily report that I survived this year’s version of Birkebeinerrittet! Together with 16.000 (!) other people, I set out from Rena on saturday morning, heading for Lillehammer – 94.5 kilometers away and with the highest mountain in the world (1100 meters) in between. I left from my home at Hvaler at 05:30 in the morning, joining my parents at Rolvsøy at 06:00 and leaving for Rena. About 3 hours and 25 minutes later we arrived, and I went to get my starting kit (transponder, number plate / start bib), etc).

The starting kit for birkebeinerrittet 2009The track had been made about 3 kms longer than last year, as we now followed an alternative route of Rena. Instead of riding the bridge over highway 3 right after the start, everyone started in the opposite direction and went under the highway instead. Nothing much to report about this, everything worked out fine and the additional kilometers don’t really matter. Compared to last year I spent two minutes more this year up to the first registration point at Skramstad, which means that I had about the exact same pace as last year. Remember to pick the LEFT track when the road splits (I did that last year, while I picked the right track this year – which seemed a lot steeper).

People getting ready for their start at birkebeinerrittet 2009
One of the groups heading out for their start!

But before we go any further, I’ll have to mention the weather. Oh, the weather. It had been raining for at least a day before the trip over the mountain, which meant that everything was muddy and dirty. Grenserittet was also muddy, but that were localized to a few key areas. At birkebeinerrittet everything was muddy (but a few areas a lot more, of course), and people were approaching zero recognizability. As one guy asked me at the second stage: “Atle?” (another common Norwegian name) “Noooo?” “Oh, Sorry. It was impossible to see who you were with all the mud..”. VG has a collection of pictures showing the mud problem.

One of the things that I’ve had on my todo list was to get a nice pair of glasses to use while biking. While I actually managed to get a new set of long biking shorts and a new long sleeved bike jacket before starting (and yes, those were probably this year’s best investment), I failed to get a pair of glasses. And how I regret that. It was completely impossible to follow anyones back wheel because of all the dirt that came blasting! I had to remove chunks of dirt from the corner of my eye for a day and a half after finishing the race. Quite a new experience!

The race went a lot better than last year, even under the current conditions. Although behind my previous time at the second checkpoint, I had a lot more energy and endurance this time. I was still able to get a bit of speed and passed quite a few other riders on my way to the next checkpoint. When I reached the famous “Rosinbakken” (“Raisin hill”), I were experiencing quite an energy loss, and I’ve realized in retrospect that this was because I failed to get any new energy into my body during the 20kms running up to the hill. I try to get at least one serving of energy gel each 30 minutes, but I think I had at least 1 hrs and 30 minutes in this segment. After getting some carbs into the system everything went a lot better, and I were able to get up on my bike and put in a few stints up until the highest location of the track.

In the middle of one of the downhill segments right before the second checkpoint we suddenly met three sheep walking right in the middle of the track! After a bit of panic braking we managed to avoid them, and they trotted along the road as nothing had happened. There were a lot of sheep along the track as usual, but at two occasions they went a bit further than just grassing by the side. Amazing experience anyhow.

After passing the highest point, everything goes downhill almost exclusivly until the finish. I’m usually a lot better at the downhill segments than the uphill parts, and I were able to tag along with a train consisting of five other bikers. We really got up a bit of speed and passed lots of other riders, and I were happy that I finally got a bit of effective riding. Next year I’ll hopefullly be able to tag along with someone for most of the trip, making it a faster journey for all of us. We’ll see.

In the last downhill segment after riding through the spectator stand around the ski jumping hill from the olympics at Lillehammer (and riding down the hill from the freestyle skiing competition at the same olympics), the guy right in front of me went over his handlebar and crashed into the side of the road. He was apparently OK, but it seemed to be a rather unpleasant experience. The rest of the track was covered with five centimeters of mud, which I managed to ride all the way through – although I almost went for a “I’ll plant my complete body into the mud here, thank you” after the rider right in front of me suddenly got problems with keeping her speed and I tried feveriously to free my shoes from my pedals. I saved it, and could ride the last 200 meters and finish my ride (although I’m not sure if anyone would have seen any difference wether I had fallen into the mud or not..)!

Two of my friends who rode the race for their first time, Christer and Magne, also finished. I’m happy to report (.. and Magne is not) that I actually managed to strike back after Magne crushed my time with 40 minutes at Grenserittet a year back. Ten minutes ahead baby, it’s all the time in the world! Christer had a very bad day with two punctures and three chain breakdowns. He finished in about 5:54.

Christer

Skramstad Bringbu Kvarstad Storåsen Goal
2009 00:46:53 01:48:55 03:15:48 04:55:07 05:54:36

Magne

Skramstad Bringbu Kvarstad Storåsen Goal
2010 00:47:55 01:42:17 02:50:42 04:14:53 05:21:34
2009 00:53:17 01:57:57 03:14:41 04:38:18 05:34:01

Mats

Skramstad Bringbu Kvarstad Storåsen Goal
2011 00:48:15 01:38:03 02:38:26 03:51:49 04:37:00
2010 00:50:43 01:51:31 03:03:17 04:30:54 05:29:07
2009 00:53:04 01:59:25 03:05:39 04:30:10 05:24:30
2008 00:51:36 01:47:20 03:08:54 04:47:30 05:47:13
1996 05:47:50

I’ll leave you with the final impression of one tired man and his new friend, the mud. This is after getting hosed down with water at least once to try to clean out the mud from my face.

Myself after finishing birkebeinerrittet 2009 - A bit muddy!

So, are you ready for next year? I am! (.. even after I got the hickups on the way back home .. for at least a couple of hours.)

Birkebeinerrittet Tomorrow

(Yes, I recycled my headline from last year, thanks for noticing)

Yet again I set sail (Yes, that’s what you do when you’re biking) over the mountain between Rena and Lillehammer tomorrow! 94.5kms of gravel, dirt and mud awaits! The weather seems to be pretty OK tomorrow for the area (a mm of downpour), but the forecast for tonight seems a bit rainy. It’s going to make everything a bit slippery tomorrow at least, but hopefully the roads are up to standard and everything goes as planned.

I’ve recently bought a new bike, so this will be the first trip really long trip with a new set wheels (managed to do about 150kms during the last seven days to at least settle everything in). Looking forward to it!

Three goals for this year too, and amazingly they’re the same as for last year:

Primary goal: finishing Secondary goal: finishing below 5 hours. Third goal: escape death.

I finished in 5:47 last year, but I’m feeling a tad more optimistic this year (.. well, I always do). Five hours, here I come!

What Happened To My Beautiful En-dashes?!

First, a small introduction to the problem: We’re running stuff in UTF-8 all the way. A few sites we’re reading feeds from are using ISO-8859-1 as their charset, but they either supply the feed with the correct encoding specific or the feeds arrive as UTF-8. Everything works nicely, except for the mentioned-in-the-headline en-dashes. Firefox only shows 00 96 (0x00 0x96), but everything looks correct when you view the headlines and similiar stuff on the original site.

Strange.

The digging, oh all the digging.

After the already mentioned digging (yes, the digging) in data at the large search engines (ok, maybe I did a search or two), I discovered that the windows cp1252 encoding uses 0x96 to store endashes. This seems similiar! We’re seeing 0x96 as one of the byte values above, so apparently cp1252 is sneaking into the mix somewhere along the lines. Most of the clients using the CMS-es are windows, so they might apparently be to blame.

ISO-8859-1 enters the scene

As the sites (and feeds) provide ISO-8859-1 as their encoding, I thought it would be interesting to see what ISO-8859-1 defines as the representation for the byte value 0x96. Lo’ and behold: 0x96 is not defined in ISO-8859-1. Which actually provides us with the solution.

I welcome thee, Mr. Solution

When the ISO-8859-1 encoded string is converted into UTF-8, the bytes with the value 0x96 (which is the endash in cp1252) is simply inserted as a valid code sequence in UTF-8 which represents a character that’s not defined.

We’re saying that the string is ISO-8859-1, although in reality it is either cp1252 or a mangled version of iso-8859-1 and cp1252 (for the endashes, at least).

If you’re on the parsing end of this mumbo jumbo, one solution is to replace the generated UTF-8 sequence (0xc2 0x96) (converted from 0x96 i ISO-8859-1) with the proper one (0xe2 0x80 0x93):

$data = str_replace("\xc2\x96", "\xE2\x80\x93", $data);

And voilá, everything works.

The Thumbs Up! of Awesome Approval

Every once in a while a few new interesting tools surface themselves and become a natural part of how a developer works. I’ve taken a look at which tools I’ve introduced in my regular workflow during the last six months.

NetBeans

NetBeans got the first version of what has become awesome PHP support in version 6.5, and after version 6.7 got released just before the summer, things have become very stable. NetBeans is absolutely worth looking into for PHP development (and Java), and you sure can’t beat the price (free!). In the good old days NetBeans were slow as hell, but I’ve not noticed any serious issues in 6.7 (.. although we didn’t really have quad cores and 4GB of memory back then either). Go try it out today!

Balsamiq Mockups

Balsamiq is an awesome tool for making quick mockups for UI designs. Previous I’d play around in Adobe Photoshop, dragging layers around and being concerned with all the wrong things. Mockups abstracts away all the UI elements (and comes with a vast library of standard elements), which makes it very easy to experiment and focus on the usability instead of the design and its implementation. For someone who’s more interested in the experience and the programming than the actual design (.. I’ll know what I want when I see it!) this makes it easy to convey my suggestions and create small, visual notes of my own usabilityideas.

You can try it out for free at their website, and they even give away licenses to people who are active in Open Source Development (disclaimer: I got a free license, but the experiences are all my own. This is not paid (or unpaid) advertising or product placement.)

GitHub

I’ve been playing around with git a bit, but after writing a patch for the PEAR-module for Gearman (.. which still doesn’t seem to have made it anywhere significant), I signed up for github to be able to fork the project and submit my patch there. A very good technical solution partnered with an easy way of notifying the original developers of your patch (which you simply provide in your own branch) by submitting a “pull request” makes it very easy to both have patches supplied to you and to submit patches to projects hosted at GitHub.

Thumbs up!

Parsing XML With Namespaces with SimpleXML

There’s one thing SimpleXML for PHP is horrible to use for: parsing XML containing namespaces. Namespaces requires special handling, and the only way I’ve found that allows you to refer to an element in another namespace, is to use the ->children() method with the namespace. I’m sure there’s an easier way than this, and if you know of any, please leave a comment!

Let’s start with the following XML snippet (using SOAP as an example):


    
        
            asdasd
        
    

The easiest way to do this is to “ignore” the namespaces, and simply do $root->{soap:Envelope} that to access the property. This will not work, as SimpleXML is quite peculiar about it’s namespaces (.. while everything else is simple and easy to use).

One solution is to provide the namespace you’re interested in to the $element->children() method, which returns all the children of the element in a particular namespace (or without arguments, outside any namespace):

$sxml = new SimpleXMLElement(file_get_contents('soap.xml'));

foreach($sxml->children('http://www.w3.org/2001/12/soap-envelope') as $el)
{
    if ($el->getName() == 'Body')
    {
        /* ... */
    }
}

Yes. That’s quite horrible.

But luckily the xpath method can help us:

$elements = $sxml->xpath('//soap:Envelope/soap:Body/queryInstantStreamResponse');

This will actually fetch all the elements titled “queryInstantStreamResponse” which are childs of soap:Envelope and soap:Body. And this works as you expect it to, without having to use children, provide the actual namespace URI, etc.

The xpath method returns an array containing all the matching elements, so in this case you’ll receive an array with a single element, containing the text inside the queryInstantStreamResponse element.

There should be an easier way than this.

Finding Substring in a String in Bash

If you’re ever in the need of checking if a variable in bash contains a certain string (and is not equal to, just a part of), the =~ operator to the test function comes in very handy. =~ is usually used to compare a variable against a regular expression, but can also be used for substrings (you may also use == *str*, but I prefer this way).

This short example submits a document to solr using curl, then emails the result if the Solr server responded with an error (.. I tried mapping this against the error code or something similiar instead, but didn’t find a better way. If you have something better, please leave a comment!):

    CURLRESULT=`cat $i | curl -s -X POST -H 'Content-Type: text/xml' -d @- $URL`
    if [[ $CURLRESULT =~ "Error report" ]]
      then 
	echo "Error!! Error!! CRISIS IMMINENT!!"
        echo $CURLRESULT | mail -s "Error importing to SOLR" mail@example.com
        exit
    fi

Neat to check that everything went OK before you remove the files you’ve submitted.

Avoiding Resetting the Scroll Position in a Textarea When Inserting Content

Now, that’s quite a headline. And this post will explain just the simple concept posted in the headline. How to avoid (at least) firefox from scrolling to the top when you insert content into a textarea.

It’s simple. Very simple. And it was shown to be so very simple for someone who didn’t remember scrollTop by this thread.

In jQuery (which we use with the caret plugin):

currentScrollPosition = $("#textareaId").scrollTop();
/* do stuff */
$("#textareaId").scrollTop(currentScrollPosition);

Yep. So simple that it actually hurts a bit.

NTFS Junctions and PHP 5.3.0

After upgrading to PHP 5.3.0 on my Windows XP Workstation, Junctions have suddenly stopped working in any PHP related code. I use junctions to hardlink directories from their version specific paths (NTFS symlinks where first introduced with Vista, so I’m still using Junctions), but after upgrading none of the libraries which live in directories that are linked through junctions work.

This seems to be a known bug, Files on NTFS Mounted Volumes (Junctions) inaccessible, although I’m also seeing the issue with completly local files (and not mounted from remote file systems). Seems like the thing to do is to wait for 5.3.1 to resolve the issue .. if it gets fixed to that. For the time being I’ll manually copy the directories.

Update: I’ve added a log of a test session showing the problem.

Changing The Source Directory in NetBeans

After reorganizing the directory structure on my workstation a bit, NetBeans refused to load the sources for the projects I have configured. The reason for this is probably because I store the NetBeans metadata files separate from the Source directory itself (as I don’t want the NetBeans project files in the repository, etc.).

NetBeans did however not have the option of choosing another source location (changing the existing location) for an existing project. Well, no worries. Luckily the NetBeans project files are in straight forward text format, so we can easily change it there instead! Close NetBeans (so that you don’t accidentally overwrite the new project file) Find the project directory and open “nbproject”. The file “project.properties” is the one we’re after. Close to the top (on line 3 here) you should find:

src.dir=<old path>

Simply change it to:

src.dir=<new path>

while remembering to pay attention to any escape sequences etc along the way (under Windows, \ needs to be escaped, so c:\\Directory\\SubDirectory is the appropriate path).