Only update node if status has actually changed

Campsoupster - November 7, 2009 - 23:14
Project:Video Upload
Version:6.x-1.5
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

Currently, when cron runs all nodes with a video upload field are resaved by the following function in video_upload.module:

<?php
function _video_upload_update_video($video) {
 
$node = node_load($video->nid);
 
$field = $video->field;
 
$field_name = $video->field['field_name'];
 
$delta = isset($video->delta) ? $video->delta : 1;
 
$node->{$field_name}[$delta - 1]['video_status_ts'] = $_SERVER['REQUEST_TIME'];
 
$node->{$field_name}[$delta - 1]['video_status'] = $video->video_status;
 
$node->{$field_name}[$delta - 1]['video_id'] = $video->video_id;
 
node_save($node);

  return
$video->video_id;
}
?>

The node is resaved regardless of whether or not the video's status has changed. If node revisions are turned on for a content type using a video upload field, this builds up additional revisions unnecessarily. If notifications are being triggered on node updates, unnecessary notification emails are also sent out.

It seems like you could check the existing status of a video upload and only resave the node if the video has actually changed status. Being a bit of a n00b, it seems like the following patch would work, but unfortunately, it doesn't.

<?php
function _video_upload_update_video($video) {
 
$node = node_load($video->nid);
 
$field = $video->field;
 
$field_name = $video->field['field_name'];
 
$delta = isset($video->delta) ? $video->delta : 1;
 
$current_status = $node->{$field_name}[$delta - 1]['video_status'];
  If (
$current_status != $video->video_status) {
     
$node->{$field_name}[$delta - 1]['video_status_ts'] = $_SERVER['REQUEST_TIME'];
     
$node->{$field_name}[$delta - 1]['video_status'] = $video->video_status;
     
$node->{$field_name}[$delta - 1]['video_id'] = $video->video_id;
   
node_save($node);
  }
  return
$video->video_id;
}
?>

Kudos to anyone who wants to patch this properly. And thanks, Jonathan, for this great module.

Cheers,
Sean

 
 

Drupal is a registered trademark of Dries Buytaert.