Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created January 27, 2015 09:42
Show Gist options
  • Save hannesl/17d6e9291c81e39e1e98 to your computer and use it in GitHub Desktop.
Save hannesl/17d6e9291c81e39e1e98 to your computer and use it in GitHub Desktop.
Drupal: Close comments after a specified time.
<?php
$breakpoint = time() - 3 * 30 * 24 * 60 * 60;
$q = db_select('node');
$q->addField('node', 'nid');
$q->addField('node', 'vid');
$q->condition('comment', 2);
$q->condition('type', 'article');
$q->condition('created', $breakpoint, '<');
$nodes = $q->execute();
$successful = 0;
$failed = 0;
$errors = array();
foreach ($nodes as $node) {
$rev_updated = db_update('node_revision')
->fields(array(
'comment' => 1,
))
->condition('vid', $node->vid)
->execute();
if ($rev_updated == 1) {
$node_updated = db_update('node')
->fields(array(
'comment' => 1,
))
->condition('nid', $node->nid)
->execute();
}
if ($rev_updated + $node_updated == 2) {
$successful++;
}
else {
$failed++;
$errors[] = $rev_updated == 1 ? "Revision updated, but not node (nid: {$node->nid})." : "Neither revision nor node was updated (nid: {$node->nid}).";
}
}
print "Processed $successful nodes.";
if ($failed > 0) {
print "\n$failed nodes failed.\n" . join("\n", $errors);
}
cache_clear_all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment