Fix Node Teasers

Last modified: March 7, 2009 - 16:09

PLEASE NOTE These snippets are user submitted. Use at your own risk. For users who have setup Drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Here is the situation:

You have updated the length of trimmed posts at /admin/node/configure, but need to update the old posts to the new length.

Just copy this snippet to a new page (with the php input format selected), visit that page, and you are done.

Note: this will hammer the server if you have a lot of nodes. Consider limiting the SQL SELECT statement with LIMIT to do it in stages if this is the case.

Suggested solution for Drupal 4.6:

<?php
echo 'Fixing teasers for: ';
$query = db_rewrite_sql('SELECT n.nid, n.body FROM {node} n');
$result = db_query($query);
while (
$node = db_fetch_array($result)) {
 
$teaser= node_teaser($node['body'], 1);
 
$insert_query= db_rewrite_sql('UPDATE {node} n set n.teaser = '%s' where n.nid= %d);
  db_query($insert_query, array($teaser, $node['
nid']));
  echo $node['
nid] ."\n";
}
?>

Suggested solution for Drupal 4.7:

<?php
echo 'Fixing the latest node revision teasers for these published items: ';
$result = db_query(db_rewrite_sql("SELECT n.nid, n.status, n.vid, r.body, r.format FROM {node} n LEFT JOIN {node_revisions} r ON n.vid = r.vid WHERE n.status = %d ORDER BY n.nid DESC", 1));
while (
$node = db_fetch_array($result)) {
 
$teaser = node_teaser($node['body'], $node['format']);
 
db_query(db_rewrite_sql("UPDATE {node_revisions} n SET n.teaser = '%s' WHERE n.vid = %d"), $teaser, $node['vid']);
  echo
$node['nid'] ." / ". $node['vid'] ."<br/>";
    }
?>

If this does not work well for you in the 4.7 installation consider doing the following:

<?php
echo 'Fixing the latest node revision teasers for these published items: ';
$result = db_query(db_rewrite_sql("SELECT n.nid, n.status, n.vid, r.body, r.format FROM {node} n LEFT JOIN {node_revisions} r ON n.vid = r.vid WHERE n.status = '1' ORDER BY n.nid DESC"));
while (
$node = db_fetch_array($result)) {
    
$teaser = node_teaser($node['body'], $node['format']);
    
db_query(db_rewrite_sql("UPDATE {node_revisions} n SET n.teaser ='%s' WHERE n.vid='%d'"), $teaser, $node['vid']);
    echo
$node['nid']." / ".$node['vid']."<br/>";
    }
?>

 
 

Drupal is a registered trademark of Dries Buytaert.