UK Petition: Ban the Bog Troll

Officially known as bathroom attendants, the bog troll lurks in bars and clubs, interrupting customers’ casual hygiene routine by obstructing sinks and hand-dryers and extorting money for performing such mundane tasks as turning on taps and distributing paper towels.

The bog troll is a menace. The bog troll does not encourage hygiene, it prevents it. Once the third-pint breaks the seal, regular users have no wish to pay an extra pound to offload their processed beer. With typical British manners, bathroom visitors are put in an uncomfortable position: wash their hands tax-free and risk offending the troll; or leave the bathroom unclean, hygiene-be-damned.

Many customers choose the latter. Damn you Bog Troll! Damn you for hoarding the paper towels. Damn you for covering three sinks with CK One. Damn you and your tray of Chupa Chups.

Please help us regulate this scourge, sign the Ban The Bog Troll petition at Downing Street.

This has been a public service announcement. Thank you.

Official Site: Ban The Bog Troll

3 comments so far, add yours

Upgrading WordPress using mysqli

Out-of-the-box WordPress assumes it’s going to be talking to a MySQL database using PHP’s original mysql extension instead of the newer mysqli extension (true up to WordPress 2.1.3 at least). If for some reason you need to run using mysqli, there are simple instructions on the WordPress support site: MySQLi for WordPress 2.1.x.

Switching to MySQLi was easy and worked as described, except for one thing: I couldn’t upgrade WordPress. After a bit of sleuthing I found there’s a MySQL version check buried in the upgrade process. For the current version (WordPress 2.1.3) it’s checking that MySQL is version 4.0.0 or higher. According to the PHP documentation for mysqli, it needs to run on MySQL 4.1 or above – so the version check is redundant.

Short-term fix: follow the first few steps of the WordPress upgrade process but before running the upgrade program edit wp-admin/upgrade-functions.php and gut this function:

function wp_check_mysql_version() {
    // (version-check removed)
}

You should be able to run the upgrade program fine now.

Comments Off

A handy PHP debug function

Like most developers, I often find it useful to spit out a few messages in the middle of a PHP script – especially when I’m debugging some problems.

Sadly, after only a few months our pristine code-base has become spotted with this, over and over again:

if (DEBUG) {
    echo "New user object: "; var_dump($user); echo "\\n";
}

After discovering var_export I felt like I’d improved things by switching to:

if (DEBUG) {
    printf("New user object: %s\\n", var_export($user, true));
}

I still didn’t like the fact that if (DEBUG) was appearing all over the place. So, after stumbling across call_user_func_array I’ve implemented by own debug function:

function debug()
{
    if (DEBUG)
    {
	$args = func_get_args();
	$args[0] = "<pre>" . $args[0] . "</pre>\\n";
	for ($i = 1, $l = count($args); $i < $l; $i++) {
	    $args[$i] = htmlspecialchars(var_export($args[$i], true));
	}
	call_user_func_array('printf', $args);
    }
}

Which means I can remove all the if (DEBUG) statements, leaving only:

debug("New user object: %s", $user);

I think that’s much cleaner.

One comment so far, add another

How to skip DVD intros

No-one enjoys the force-fed FBI, F.A.C.T. and Copyright propaganda on DVDs. Thankfully, there are ways of skipping them. Continue Reading

sprintf for JavaScript

Avoid writing formatting functions in JavaScript by grabbing yourself a decent sprintf implementation – handling padding, truncation, floating-point numbers, left/right alignment and re-ordered arguments.

You can download sprintf for JavaScript, available under the Create Commons Attribution License. Now license free (use it where/when/how you like.)

The version I’ve written is based strongly on Perl’s sprintf implementation, allowing argument reordering to help with internationalisation (i18n). Some overly simplistic examples follow, leaving room for obvious improvements like:

  1. letting the user specify their locale as a preference
  2. set default locale based on visitor demographics
  3. use locale-specific message-bundles with a fall-back to the default locale

On with the examples:

var locale = 'es';
var messages = {
    'en': 'I am %d years and %d months old.',
    'es': 'Tengo %2$d meses y %1$d años.'
};
var message = sprintf(messages[locale], 31, 7);

You could also use it for

var date = new Date;
var dateFormats = [
    /* ISO-8601: */ '%04d-%02d-%02d %02d:%02d:%02d',
    /* British:  */ '%3$02d/%2$02d/%1$02d',
    /* U.S.:     */ '%2$02d/%3$02d/%1$02d'
];

// for example only: choose random date format
var dateFormat = dateFormats[3 * Math.random() >>> 0];

var formatted = sprintf(dateFormat,
    date.getFullYear(), date.getMonth() + 1, date.getDate(),
    date.getHours(), date.getMinutes(), date.getSeconds());
alert(formatted);

The Perl documentation has more examples in the “order of arguments section“.
Note: this implementation allows the precision of a number to be set from a specific argument (using e.g. “%.*3$f”), which the perldocs (perldoc -f sprintf) say hasn’t been implemented yet.


I haven’t (re)written any documentation for it, but you should be able to use Firebug (or the javascript: protocol) to try out sprintf on this page, and you can also check out the test page for sprintf for more input/output samples.

As usual, writing test-cases uncovered a couple of browser-dependent issues. Safari has some bizarre behaviour with Maths.abs(0).toFixed(6) resulting in "0.0000-0" instead of "0.000000". Another issue I came across is that 0.5 rounds to 0 or 1 depending on browser (worded differently: numbers are rounded off inconsistently across browsers.)

Dependencies

On modern browsers, there are no dependencies. But to run sprintf on older browsers you’ll need to patch the Number and String objects. Number needs toFixed, toExponential and toPrecision methods, while String needs a replace method capable of using functions in the replacement parameter.

10 comments so far, add yours