Fix Node Teasers
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 to do it in stages if this is the case.
<?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 ="'.db_escape_string($teaser).'" where n.nid='.$node['nid']);
db_query($insert_query);
echo $node['nid']."\n";
}
?>
Replace "node" with "node_revisions" for 4.7
Replace "node" with "node_revisions" for 4.7
<?phpecho 'Fixing teasers for: ';
$query = db_rewrite_sql('SELECT n.nid, n.body FROM {node_revisions} n');
$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.nid='.$node['nid']);
db_query($insert_query);
echo $node['nid']."\n";
}
?>
Great script, thanks,
Alex
Insufficient as-is but a great lead...
Neither of these would work for me in a 4.7 installation without real problems. The first did not take into consideration any revisions and the second would consider too many revisions while simultaneously barfing on db_escape_string. My solution was to do the following:
<?phpecho '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/>";
}
?>