Drupal tips: showing recent searches

Submitted by Frederic Marand on Tue, 2005-11-29 09:06

Some sites include a list of terms recently searched for. I've created a patch for Drupal adding this functionality to the current 4.6.x search module.

Felt well awake this morning so, as I hadn't coded anything on the helpdesk module for a while, I thought I'd scratch some personal Drupal itch and add this feature: now any Drupal site can have a "recent searches" block.

This is currently available as a patch for 4.6.3, but should be readily adaptable to the current HEAD.

If you look at the code, it looks rather fragile, because search.module in Drupal 4.6 does not cleanly log queries, but logs them preformatted using the watchdog function as readily usable links, so some formatting is in order.

After coding it, some improvement ideas spring to mind, if anyone is interested:

  • Allow more than one block to be defined, to show either searches on nodes (as currently) or on users (but who would really want this to show ?)
  • Define the length of the list in a setting, instead of the fixed 5 values list in this first version.
  • Show the result count next to the search. This one would be heavy server-side, as it would involve performing the searches for just that display, or modifying search more heavily to log the result count along with the query link.
  • Display safety: this has not been checked in this version. If search does not protect against injection attacks, this first version will probably be affected.
  • This should be adapted for HEAD/4.7, unless it already includes a similar feature. As I'm not currently not giving HEAD a try because of the expected unstabilities, I don't know if such a block has been added.