Searching the Drupal Database by Regular Expression
I found myself wanting to use regular expressions to find some imported Drupal nodes containing broken old image paths. Naturally, I went looking for a module that might accommodate me and I did find the Scanner module. Unfortunately the site I was working with is in Drupal 6, and I didn't have the time to work on a Drupal 6 port of what looks like a pretty sophisticated module just to find a few nodes.
If you're comfortable with MySQL, there's no reason you can't query the database directly to do searches that Drupal doesn't accomodate natively. As a simple (if silly) example, let's say you wanted to find the nid of the most recent revision of every node in your site that begins with the word "The." Using the MySQL front-end of your choice, connect to your Drupal database and try the query:
SELECT nid FROM node_revisions WHERE body REGEXP '^The' GROUP BY nid ORDER BY vid DESC;
Even if you don't need regular expressions, it may still be useful to query the database directly to search for HTML markup that Drupal's search overlooks:
SELECT nid FROM node_revisions WHERE body LIKE '%my/old/path%' GROUP BY nid ORDER BY vid DESC;
Assuming you only need to fix a handful of nodes, you can jot down the resulting nids and go edit them manually.
If you need to fix dozens or hundreds of such nodes, it's still not necessarily worth the overhead of coding the UI for a complete find/replace module... such fixes are usually one-time operations anyway. You might want to consider writing a command-line script to bootstrap Drupal, run your query, then loop through the results to load the nodes, find/replace the offending strings, and re-save them.
Any post that discusses talking directly to the Drupal database wouldn't be complete without a caveat to be very careful, do read-only queries, and back up your database ahead of time. Likewise, if you do decide to try some automated find/replace operations on your site content, back up your database before doing anything. The warning on the Scanner module page says it well:
"This is a very powerful tool, and as such is very dangerous. You can easily destroy your entire site with it. We highly encourage you to backup your database before using it. No, really."
Tags: DrupalMySQLRegular ExpressionsRegex
-
var switchTo5x=true;stLight.options({publisher:'dr-8d4fe24c-a8ab-ba90-8086-3791b02244be'});