Remove an input filter by storing the post-processed text in the DB.
I'm new to the Drupal API, so I look forward to having this snippet corrected / improved by others.
Here's the scenario, but others may have parallel ones to which what follows may be adaptable:
I'm running a Drupal 5 site, built making heavy use of the Textile module. I have about 250 nodes, nearly 200 of which contain Textile markup. I now want to upgrade to Drupal 6, but Textile is not (at time of writing) available as an input format. So I want to run all my nodes through Textile, process them, then store the post-processed text in the Database. I can then disable the Textile filter, and my site will still render as before.
The answer is a PHP snippet that iterates over all nodes using the Input Format with index 1. (This is, by default, Filtered HTML. This is the only input format on my site to use Textile. If Textile is in different formats on your site, you'll need to change this clause of the SQL statement appropriately). It retrieves the body and teaser, applies input format 1 (which contains the textile filter) and then saves the modified body and teaser back into the database.
Before you run this, you need to put the site into maintenance mode and make sure that the only filters enabled in the default input format are the ones you want saving into the database. For example, you probably don't want the line break converter in the default format, otherwise all the <br /> sections will be hard-coded into the DB which is ugly to maintain later.
Warning: Drupal 5 doesn't remember the weighting of your input filters when you remove them from an input format. Make a note of the weight of any formats you remove, so that you can set the weight back again afterwards.
After running this, you can remove the filter you have just hard-processed, and put back any of the filters you temporarily removed.
<?php
$nodes = db_query('SELECT nid, body, teaser FROM {node_revisions} WHERE format=1');
// It's the WHERE format = 1 that you need to change if you have Textile in other input formats
$nodeChangeCount = 0;
while ($node = db_fetch_object($nodes))
{
db_query('UPDATE {node_revisions} SET teaser = \'%s\', body= \'%s\' WHERE nid = %d', check_markup($node->teaser), check_markup($node->body), $node->nid);
//Note check_markup, when used with only one argument like this, applies the default input format (usually Filtered HTML) to the supplied text. You may need to use a different format by supplying a second optional parameter
$nodeChangeCount += 1;
}
print 'Update complete. Number of nodes updated is ' . $nodeChangeCount;
?>Caution
Needless to say, the snippet I posted won't affect any comments that are written using the filter you're trying to remove, or headers and footers in Views pages, or (possibly other contributed modules). It'll need adapting to work for the DB schema for the affected modules. This didn't apply to me, so I haven't done that extra step.
