login
tags, auto-clip and namestorming posted: Mon 2011-08-08 20:51:39 tags: unnamed blog project
The trivial part of the tag-support revision was, well, trivial: Add a "tags" column in the entries table. Add an input field and fix the SQL accordingly in the Create / Update phases of the post form. Pretty up the entry-view template and that was that.

The not-so-trivial part is to manage the tags table "on the fly". Say we're creating a new entry, and the tags table is empty. We explode() and trim() the tags field, and find 3 tags: "fitness", "mileage" and "web design". We SELECT COUNT(*) FROM tags WHERE tag = 'fitness', and zero records match, so we tell MySQL to INSERT INTO tags (tag) VALUES ('fitness'). We repeat the process for "mileage" and "web design". Now in the future, when our SELECT COUNT(*) queries return a nonzero result, we just skip the INSERT INTO tags operation. Straightforward, yes.

But now let's say we go back, after having added more entries and tags, and edit (Update) this entry, removing the "mileage" tag. The only way the app is going to know if we removed a tag is if we compare against the stored record before we update. So we do that, and determine that we MIGHT be able to retire the tag, "mileage"... but we don't want to just charge in and DELETE FROM tags WHERE tag = 'mileage' because it's possible, perhaps even likely, that other entries are still tagged with "mileage".

The app won't know unless we either a) update a usage-count every time we operate on a tag, or b) loop through the whole entry table to check every time we edit any entry's tags (or delete an entry). (We could just leave potentially orphaned tags in the tags table, but we're not going to.) In a one-man blog the latter approach could be fine for a long time, perhaps years. In a multi-user system, it would bog down quickly. And what's the classic extension of tag support? Yep, tag clouds; and if you don't store a use-count in the tag table, you may as well not even have a tag table because you'll be rebuilding tag-counts from the entries table on every load of the tag cloud widget.

Which isn't to say that we should never rebuild the tag counts to fix orphaned, missed, and miscounted tags. We just shouldn't have do it every time we edit or delete an entry, and certainly not on every read of a tag-stats or tag-cloud widget.


Entries are getting longer and longer as I get happier with my blog engine. An "auto-clip" feature comparable to LJ's lj-cut pseudo-element would go a long way toward keeping the landing page neat.


One of the epithets of the Greco-Roman titan-goddess Hecate is Phosphoros, "light-bringer". I'm pretty fond of the name Phosphoros under this Greek-ish spelling. The symbolism connoted by association with the highly reactive 15th chemical element should be obvious.