Tonight's Major Hacking Feat

The WAP bit of Hype didn’t work. I fixed it. I’d been meaning to rewrite it ever since the day I wrote the first version. Today I got around to it. It uses Smarty, PEAR, mod_rewrite, WML and a big PHP class (400 lines).

It’s the most recent thing in the Afternoon-becomes-a-master-PHP-coder Hall of Fame. Every time I think about re-implementing Hype (which is often), I think about what would be the best way to do it. With each app that goes into the Hall of Fame, my ideas about the structure of Hype 2 become more specific. For example, mod_rewrite, Smarty and classes are all going to be in there. I’m going to use get_browser() to make sure every part of the site has access to browsercap info.

So what did I learn from rebuilding WAP? Well, the class is nice. I have one script which implements the URI interface with a small switch statement (I used the same exploding to path_tokens approach used here at aftnn). This means that there is a degree of separation between the URIs used and the actual code. However, I still found myself writing a function for each section of the site. I broke some things, like DB access, off into functions, which is good. But before I really crack it I need to break the site-section functions down into blocks and treat those in a generalised manner, ensuring that I’m not having to edit code for 5 sections when I decide to change one minor feature. E.g. the post message and send mail sections of the WAP site are virtually identical, but they use separate functions, same with a list of messages and a list of search results! This is probably a good starting point to continue my quest for PHP-perfection.

One thing that I have been thinking about that would really help this is to push more HTML processing off to Smarty. I often have large, complex blocks to construct pretty HTML or grammatically correct phrases. Whilst the latter is essential, passing arrays and using $smarty->fetch() for anything that is formatted HTML would clean a lot of functions up, especially ones where forms are involved.

At the end of the day a lot of Hype 2 is in the background support. The sections aren’t going to number in the hundreds or anything, it’s the functionality that’s available generally around the site, the personalisation, that really matters. This will be easier to extract out to different functions.

Oh, and I successfully used my XML-based BrochureWare generator to generate a website. I’ve learnt XSLT and XPath in order to put it together. They form an interesting system. I found them really great for creating a dirt simple accounts system. For collecting info about groups of nodes of unknown size and making management-style reports out of the numbers. In order to build my BrochureWare generator I implemented some extensions for Xalan that allow me to create dirs and copy files from the XSLT stylesheet. People on the Xalan mailing list thought this probably wasn’t a good idea, they’re right but it’s a quick and usable hack. If I put much more effort into the system I’ll probably go a different route. If I get it to that romantic state called finished I’ll put it up for download.

All in all it’s been a successful couple of weeks of hacking. I’ve completed at least four major tasks. I’ve been acutely aware recently of how rarely I actually finish things. It’s spurred me to go that extra little bit further with things like this journal and the WAP service.