Script to update Drupal teasers

One of the annoying things about changing the number of characters for teasers in Drupal is that you have to edit and save every single node (page, story etc) for the change to take effect. Thats ok if you only have 10 pages, but what if you have 1000 or even 10,000 pages? Nightmere times.

Well, it doesn't have to be that difficult, because here is a nice little script that will update all of the teasers at once.

The first step is to change the teaser length in admin -> content ->node settings -> length of trimmed posts.

The next step is to copy the following code into a php page and upload it to the root directory of your site. Then run the script and like magic, all the teasers will change to the desired length.

include "includes/bootstrap.inc";
include "includes/common.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$result = db_query("SELECT nid,body,teaser FROM node_revisions");

while ($node = db_fetch_object($result)) {
$teaser = node_teaser($node->body);
db_query("UPDATE node_revisions SET teaser = '%s' WHERE nid = %d", $teaser, $node->nid);
$i++;
echo $i;
}

Comments

Thank you for post this.

I'm new on Drupal and I was looking for some like this.

But i have a question, It is normal that after execute the script appears a large chain of numbers?

Thanks in advance for the response.

Blair Wadman's picture

Hi Francisco,

Yes it is normal. The variable $i increases by one each time the while loop runs and $i is echoed out each time, so the chain of numbers is the variable $i being returned to the screen. I find that useful to indicate that the script is running correctly, but feel free to remove the following line if you wish:
echo $i;

Many thanks for the response mate.

Better I leave the script like the original, this way like you said, I can see the script it's running fine.

Thanks for write great things about Drupal.

you should use vid rather than nid, because this script will not work if you have node revisioning. your script will set all revisions of a node to have the same teaser.

$result = db_query("SELECT vid,body,teaser FROM node_revisions");
...

db_query("UPDATE node_revisions SET teaser = '%s' WHERE nid = %d", $teaser, $node->vid);

Blair Wadman's picture

Good point! Thanks for pointing that out and provide an improved snippet.

Thanks for writing an INCREDIBLY USEFUL Script. Saved me heaps of time after upgrading from Drupal 5 with node teaser module to drupal 6. I modified it slightly for my purposes to avoid writing over teasers which were already set with different content. This is not the general solution for even that problem but worked because of my situation. It would have been better maybe to compare the first 50 characters of the body with the first 50 characters of the teaser and not update where they are different

<?
include "includes/bootstrap.inc";
include "includes/common.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$result = db_query("SELECT vid,body,teaser FROM node_revisions");

while ($node = db_fetch_object($result)) {
if ($node->teaser == '') {
$teaser = node_teaser($node->body);
db_query("UPDATE node_revisions SET teaser = '%s' WHERE nid = %d", $teaser, $node->vid);
$i++;
echo $i;
}
}
?>

Add new comment