Automatically redirect based on del.icio.us tag combinations

This file takes a set of tags and redirects to the newest del.icio.us entry with all of them. On error, it just redirects to http://del.icio.us/user-name. It needs the tags cache file written by backup-delicio.us.pl: gzipped raw source code is here.

Sun Aug 13 12:17:29 CEST 2006: updated to fix a bug where redirection didn’t happen if one of the tags didn’t exist.


<?php /* -*- mode: php; coding: utf-8 -*- */

/* Take a list of tags, redirect to the newest del.icio.us entry with all of
   them, reading the tag details from a .dbm file that's written by a daily
   backup script. If no entry is found, redirect to del.icio.us/user/query
   for the error handling.

   Aidan Kehoe, Thu Aug 10 12:35:15 CEST 2006. Public Domain. */

define('TAG_DB_FILE_NAME', 'YOU NEED TO SET THIS');
define('DELICIOUS_USER_NAME', 'YOU NEED TO SET THIS TOO');

function main()
{
        $query = '';

        if (isset($_GET['query'])) {
                $query = $_GET['query'];
        }

        if ('' == $query) {
                $query = $_SERVER['PATH_INFO'];
                if (preg_match(',^/(.*)$,', $query, $matches)) {
                        $query = $matches[1];
                }
        }

        if ('' == $query) {
                /* Let del.icio.us handle the empty query. */
                header('Location: http://del.icio.us/'.DELICIOUS_USER_NAME);
                exit;
        }

        /* Only transform ASCII characters for case: */
        $query = strtr($query,
                       ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                       abcdefghijklmnopqrstuvwxyz");

        $dbm_handle = dba_open(TAG_DB_FILE_NAME, 'r', 'db4');
        assert($dbm_handle);

        $tag_url_ordered_array = array();
        $tag_list = preg_split('/[+ ]{1,1}/', $query, -1,
                  PREG_SPLIT_NO_EMPTY);
        $tag_url_lookup_hash = array();

        foreach ($tag_list as $tag) {
                if ('' == $tag) {
                        continue;
                }
                $val = dba_fetch($tag, $dbm_handle);
                $tag_url_ordered_array[$tag] = explode("\0", $val);

                $tag_url_lookup_hash[$tag] = explode("\0", $val);
                array_unshift($tag_url_lookup_hash[$tag], "");
                unset($tag_url_lookup_hash[$tag][0]);
                $tag_url_lookup_hash[$tag] 
                        = array_flip($tag_url_lookup_hash[$tag]);
        }

        foreach ($tag_url_ordered_array[array_shift($tag_list)] as $url) {
                $found = false;
                foreach ($tag_list as $tag) {
                        $found = isset($tag_url_lookup_hash[$tag][$url]);
                }
                if ($found || (0 == count($tag_list) && '' != $url)) {
                        header('Location: ' . $url);
                        exit;
                }
        }

        /* Let del.icio.us do the error handling. */
        header('Location: http://del.icio.us/'.DELICIOUS_USER_NAME.
               '/'.$query);
        exit;
}

main();

 ?>

Aidan Kehoe
Last modified: Sun Aug 13 12:18:17 CEST 2006