Latest Publications

Awesome Command-fu

A good friend of mine mentioned that SSH supports escape sequences – something I had never thought of. Or rather, something I had wished for every time I’d been faced with a dead SSH connection. Turns out, you can easily close any SSH connection (dead or alive) by issuing the following sequence:

<CR>~.

The CR in there is a carriage return, i.e. pressing the enter key to create a new line. One caveat though: the ~ is the default escape character for SSH but it can be changed (or removed) so you need to make sure you’re using the proper escape character when issuing escape sequences – otherwise, no fun/profit.

Closing an SSH connection is not the only thing you can do: you can also suspend an SSH connection by doing

<CR>~^Z

where ^Z is <ctrl>+z. But wait, there’s more! You can also change the SSH connection you’re in to add or remove port forwards, by opening a command line:

<CR>~C

And a personal favourite: if you’re using several SSH connections in serial, you can forward an escape sequence by adding another ~ after the first. Closing connection #2 then becomes:

<CR>~~.

And in the same fashion, suspending the second connection becomes:

<CR>~~^Z

This, dear reader, is awesome! There’s just no end to the fun :)

The moral: read your man pages (so much good stuff hidden away in there)!

PLSEO

Aaaaaaaaaand … another tool put up for the general public to enjoy. This time it’s PLSEO, and it’s available at GitHub as per usual. It’s a collection of SEO tools written in PHP. Well, for now it’s a collection of one tool: some classes to query search engines to get position of a site given a keyword. You can read more on the PLSEO page.

Review of TYPO3 Multimedia Cookbook

As mentioned a while ago in another blogpost of mine, I received a copy of a Typo3 book for reviewing. Tons of time has passed and I have now finally managed to do the review (yes, I know, it’s a month and half late … or more). Anyway … on to the reading.

Introduction

The book I’m reviewing here is TYPO3 4.3 Multimedia Cookbook by Dan Osipov, published by Packt Publishing. The title gives you a good idea as to the content but if you were in doubt, the sub title spells it out: ”Over 50 great recipes for effectively managing multimedia content to create an organized website in TYPO3”. The aim of the book is thus to help you take care of the more or less typical problems connected with handling multimedia in Typo3, by giving you step-by-step recipes for putting solutions in place. How well the book achieves that is what I’ll be looking at here.

The book

Before I dive into the recipes, there are a two general points to make. First, for a book about multimedia, it unfortunately comes across a bit lacking: the images in the book (of which there are a few) are consistently too dark. It might have been a tradeoff to make sure the graphics stands out better, but it just doesn’t look good and hence gives the book a slightly unfinished or rushed feel.

Another thing to note is the approach to code sections the author has taken. A book like this could easily fill up most of it’s pages with code, leading either to a very large book or very short descriptions. The approach chosen here is another: most of the recipes that involve code of some size direct you to download the appropriate code, after which the most important parts are explained. Whether you see this as a positive thing or not depends mainly on taste, I suppose – personally I prefer being able to look at all the code that makes up a given recipe, instead of having to move to another media to get an overview.

The recipes

The book is divided into eight categories: Getting Started, Managing Digital Assets, Operating with Metadata in Media Files, Rendering Images, Rendering Video and Audio, Connecting to External APIs, Creating Services, and Automating Processes. As should be obvious given the list, it’s covering a lot of different situations, which is both positive and negative. It’s great because it gives the book a lot of diversity and you’re almost certain to get inspired by some of the recipes – too much ground is covered for there not to be something interesting for everyone.

However, among all the things the book covers you’ll also find things that seem irrelevant to the topic. For instance, the first chapter has a total of eight recipes – only three of these actually deal with Typo3. The rest are short recipes on how to setup a web server or an NFS share – not exactly everyday topics when running your Typo3 site. The three recipes that do deal with Typo3 focus on setting Typo3 up and creating a template – again, not something you’re likely to be looking for when you’re having multimedia problems. It seems as though the first chapter aims to create a common ground, so the Typo3 newbie can join the party as well – but that’s not the premise of the book. The second chapter is better but also has some chapters that suffer from this. The third chapter is a mix: two recipes explain how to set metadata in image and audio files using tools like Photoshop, while the rest explain how you can extract the data in Typo3. After that, the rest of the book is on topic.

That’s about the negative I have to note about the book – the rest is positive. The remaining recipes range from ”Good to know” or ”I can use that” to ”Hey, that’s pretty cool!”. The chapters on rendering images and audio/videofall into the first category, while the last three chapters (Connecting to External APIs, Creating Services and Automating Processes) are in the latter. For instance, the recipes on using Amazons S3 services are pretty nifty as are the recipes on creating services (especially the detailed walkthrough of how to convert audio using a service).

Overall, the recipes are well written up: they all follow the same useful layout (preparation, step-by-step guide, explanation, further points, references to other recipes) which helps you grasp the information quicker, the language is easy to dig into, and the recipes contain helpful screenshots of interfaces you’ll be dealing with (one of the things that might otherwise overwhelm you). As pointed out, most of the recipes do not include all of the code needed for functionality but they do explain the key points and they use enough space to make sure you understand what’s going on – which makes it a fair chance you’ll be able to use what you learn in other contexts.

Conclusion

I was happy to receive a copy of the Multimedia Cookbook to review: after reading it, I think it’s a good guide to multimedia issues and I’m pretty sure I’ll be using some of the recipes. As such, it’s given me some practical hints on how to do things. It’s also given me a better insight into some things Typo3 though, which is another bonus from reading it. I would expect the payoff for experienced Typo3 developers to be less on both accounts, but I still imagine it would be an ok addition to the library, as the cookbook layout makes it ideal for reading whenever you come across a specific multimedia related problem.

That said, it’s not the best handbook I’ve even com across – the problems mentioned (irrelevant recipes and minor layout problems) coupled with the ”get the code online then return to the book”-approach subtract somewhat from it, unfortunately. I still think it’s a good guide though and have no trouble recommending it. So if you’re wondering how to handle multimedia in Typo3 or just feel like expanding your Typo knowledge, I’d say give it a read.

.htaccess voodoo

Every once in a while I have to do some .htaccess rewriting and every time I end up deeply fascinated at the possibilities that it offers. This time round the situation was as follows: for a client we had done some advanced search functionality, which uses fairly detailed URLs to store the search (the search is parsed into an abstract syntax tree, then serialized to a URL). The problem now was that we had rewritten the serialized syntax … and somehow a famous search engine had picked up on the URLs and was getting bad results from them. What to do?

The solution was URL rewriting using .htaccess. However, it wasn’t straight forward URL rewriting – the serialized search was encoded in the query string, and that was the only part that needed a rewrite. How does the Apache Rewrite module handle this? Very well, it turns out.

Solution – part 1

The first thing to realise is that RewriteRules by themselves won’t do any good – they only work on the base part of the URL, ignoring the query string. This means you have to turn to the second part of the rewrite: the RewriteCond. Now, this presented the first part of my eye-opening experience: RewriteCond allows for using regexes. This means you can do the following:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)

And you’ll match on query strings that start with id = some number of digits. You can use pretty much any extended regular expression you desire … which makes it very powerful!

Solution – part 2

As you can probably guess from the above code bit, you can also use capturing groups in the RewriteCond regexes. Not only that, though: you can reference these captured groups in a RewriteRule. It’s done slightly different from capture reference in RewriteRule regexes (these are done using $) in that a reference to a captured group from a RewriteCond uses a % as prefix. Hence, you can do:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^product.php /product/%1? [R=301,L]

And you’ll be redirecting product.php?id=123 to product/123 using a 301. Notice the ? at the end of the rewritten URL – it’s there to make sure ModRewrite doesn’t append the original query string.

At this point, my woes were almost over – there was just one obstacle left.

Solution – part 3

When the Rewrite module does redirects, it normally also escapes characters in the URL, that could otherwise turn out problematic. One such character is %. However, this escaping is itself very problematic upon redirects, because any url_encode()d URL will contain lots of % characters followed by a character code. When ModRewrite is done with the URL, it’ll have substituted all %2F with %252F, for instance … not what you want.

There’s a very simple solution to this, though: you can set a flag to stop the Rewrite module from doing any escaping. What you do is:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^product.php /product/%1? [R=301,L,NE]

This stops ModRewrite from escaping the URL, leaving you with whatever was in the original.

Using the above bits and pieces you can rewrite a URL like

/search?blah=hum%20dinger%20and%what%20not%3Aliteral

to

/search?q=hum%20dinger%20and%what%20not%3Aliteral&rewritten=true

It’s voodoo for sure, but damned cool.

Geonames library

A couple of days I was doing some work on BeWelcome, updating some code to fetch geo data from Geonames.org. Our old code had been using a mixture of a spaf maps class and php functions and trying to get it to work properly seemed to demand quite a bit of tinkering – plus, there was the chance I might break something. I then had a look at the Geonames site and there are quite a few different libraries for querying Geonames – but nothing in plain PHP. However, looking at the documentation of their API it didn’t seem like it would be very hard to knock something up – so that’s what I did.

After about 2 hours of coding, I had some very basic classes that provide the most useful functionality of Geonames (useful for BeWelcome anyway): doing a fulltext wildcard search, fetching details for one specific location, and fetching hierarchies of locations. The library provides some static functions for generating location objects, and these location objects can then in turn be worked with using some of the same methods that the library provides (such as getting child locations or parent locations).

The usage is currently extremely simple – some examples:

// searching for a location
$results = GeoNamesService::search($searchterm, $rowcount /* optional, defaults to 100 */);

// fetching parent hierarchy for a location
$hierarchy = GeoNamesService::hierarchy($id);

// getting the parent of an existing GeoNamesObject
$parent = $geo_object->getParent();

As you can probably guess, the main reasoning was taking out the boilerplate code and removing the stuff we just didn’t use. As a consequence, this library doesn’t currently offer that much extra functionality. This will be built in later though, so you’re able to use some of the options when searching, for instance. Also, a number of the other ways of interacting with Geonames are worth looking at, like finding a location by postcode.

After I had spent a bit of time on the project and got it to a working basic (including some unit-tests), I figured I’d breathe some life into it and so I put it on GitHub – so anyone can take it and use it if they have a need for it. It’s available here: PHP Geonames Library and the license is a GPL v.3 so you can just grab and use in any OS project as you see fit.