Regenerate Node Teasers

Last modified: April 8, 2009 - 02:41

Need to update your teasers across your whole site? Things have changed just a little bit since 4.7 (http://drupal.org/node/43458). The below code can be put in to a block, the bottom of template.php, a module, or wherever you would like. Just be sure to pull it out when you are done and put it in some place that your users won't see this.

echo 'Fixing teasers for: ';
$query = db_rewrite_sql('SELECT n.nid, n.vid, nr.body body FROM {node} n LEFT JOIN {node_revisions} nr ON n.vid = nr.vid ');
$result = db_query($query);
while ($node = db_fetch_array($result)) {
  $teaser= node_teaser($node['body'], 1);
  $insert_query= db_rewrite_sql('UPDATE {node_revisions} n set n.teaser ="'.db_escape_string($teaser).'" where n.vid='.$node['vid'].' AND n.nid='.$node['nid']);
  db_query($insert_query);
  echo $node['nid']."\n";
}

D6 hook_node_op>erations_callback Alternative

The following into a quick custom D6 module and execute it via a hook_node_operations callback.

One main difference from the previous suggestion, don't hard code the format value in the call to node_teaser:

function mymodule_operations_rebuild_teasers($nids) {
  foreach ($nids as $nid) {
    if ($node = node_load($nid)) {
      $teaser= node_teaser($node->body, $node->format);
      if (db_query(
        'UPDATE {node_revisions}'
          .' SET teaser ="%s"'
          .' WHERE vid = %d AND nid = %d',
          $teaser,
          $node->vid,
          $node->nid) !== FALSE) {
        drupal_set_message(t("Rebuilt %node-title's teaser.", array('%node-title' => $node->title)), 'information');
      }
      else {
        drupal_set_message(t("Could not rebuild %node-title's teaser.", array('%node-title' => $node->title)), 'error');
      }
    }
  }
}

Lifesaver

jbylsma - July 28, 2009 - 13:50

I had been doing some regex replacements via PHP and was noticing that, when I pulled up a node for editing, it would be showing both the regexed content and the pre-regexed content, which at first glance looked just like the node duplicated. After investigating the db, I saw that I hadn't updated the teasers and that was where the old content was originating from. After running the snippets above, content was showing normally.

 
 

Drupal is a registered trademark of Dries Buytaert.