From 234c658294bbbffa7eda4600defd258a24058675 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 16 Jan 2012 17:11:22 -0500
Subject: [PATCH 01/14] Issue #1407196: Refactor Ooyala ping functions into
 their own file.

---
 includes/ooyala.pages.inc |  154 -------------------------------------------
 includes/ooyala.ping.inc  |  161 +++++++++++++++++++++++++++++++++++++++++++++
 ooyala.module             |    2 +-
 3 files changed, 162 insertions(+), 155 deletions(-)
 create mode 100644 includes/ooyala.ping.inc

diff --git a/includes/ooyala.pages.inc b/includes/ooyala.pages.inc
index 11cf4be..bfc9ce5 100644
--- a/includes/ooyala.pages.inc
+++ b/includes/ooyala.pages.inc
@@ -339,157 +339,3 @@ function ooyala_refresh_thumbnail() {
   drupal_json_output($data);
 }
 
-/**
- * Menu callback; Respond to Ooyala pings when a video has finished processing.
- *
- * This callback responds to the URL "ooyala/ping" within the Drupal site.
- * Typically this URL needs to be configured to receive pings from Ooyala.com
- * when a video has finished processing and is ready to be displayed. The
- * configuration for setting up a ping URL is at
- * https://www.ooyala.com/backlot/web, underneath the Account => Developers
- * tab. Enter a URL in the API Ping URL such as http://example.com/ooyala/ping,
- * if Drupal is installed at the base of the domain.
- */
-
-function ooyala_ping() {
-  module_load_include('inc', 'ooyala', 'includes/ooyala.api');
-  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
-
-  if (isset($_GET['embedCode'])) {
-    $embedcode = $_GET['embedCode'];
-    $embed_args = array('%embedcode' => $embedcode);
-    if ($verbose) {
-      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embedcode with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
-    }
-
-    if ($video = ooyala_api_video_load($embedcode)) {
-      _ooyala_ping_fetch_image($embedcode, $embed_args);
-
-      if ($nodes = ooyala_load_nodes($embedcode)) {
-        foreach ($nodes as $node) {
-          _ooyala_ping_autopublish($node, $embed_args);
-          _ooyala_ping_metadata($node, $video, $embedcode, $embed_args);
-        }
-      }
-      else {
-        watchdog('ooyala_ping', 'Content matching the embed code %embedcode was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
-      }
-    }
-    else {
-      watchdog('ooyala_ping', 'A matching video for the %embedcode could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
-    }
-  }
-  else {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'Received embedcode-less ping from Ooyala Backlot with request <pre>@request</pre>.', array('@request' => print_r($_REQUEST, TRUE)));
-    }
-    else {
-      watchdog('ooyala_ping', 'Recieved ping without an embed code. No action has been taken.', array(), WATCHDOG_ERROR);
-    }
-  }
-
-  // Disable the page cache for this request.
-  if (function_exists('drupal_page_is_cacheable')) {
-    // PressFlow or Drupal 7 mechanism:
-    drupal_page_is_cacheable(FALSE);
-  }
-  else {
-    // Drupal 6 cache disabling hack.
-    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
-  }
-}
-
-/**
- * Fetch an image in response to an Ooyala ping.
- *
- * @param $embedcode
- *   The embed code to fetch the image for.
- * @param $embed_args
- *   An array of arguments for substition into translated watchdog messages.
- *
- * @return
- *   The image path of the fetched image, or FALSE if one could not be loaded.
- */
-function _ooyala_ping_fetch_image($embedcode, $embed_args) {
-  $image_path = NULL;
-  if ($image_path = ooyala_api_fetch_image($embedcode)) {
-    if (variable_get('ooyala_ping_reporting_verbose', FALSE)) {
-      watchdog('ooyala_ping', 'The video image for %embedcode has been downloaded to <a href="!url">@filename</a>.', $embed_args + array('!url' => file_create_url($image_path), '@filename' => basename($image_path)));
-    }
-  }
-  else {
-    watchdog('ooyala_ping', 'There was an error downloading the video image for %embedcode.', $embed_args, WATCHDOG_ERROR);
-  }
-
-  return $image_path;
-}
-
-/**
- * Mark a node as published if the video has been published.
- *
- * @param &$node
- *   The node object to mark as published.
- * @param $embed_args
- *   An array of arguments for substition into translated watchdog messages.
- */
-function _ooyala_ping_autopublish(&$node, $embed_args) {
-  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
-  if (variable_get('ooyala_autopublish', 0) && !$node->status) {
-    $node->status = 1;
-    node_save($node);
-    watchdog('ooyala_ping', 'The content <a href="!url">%title</a> has been published by a ping for %embedcode.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
-  }
-  elseif (!variable_get('ooyala_autopublish', 0)) {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was not published as automatic publishing is disabled.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
-    }
-  }
-  else {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was already published.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title), WATCHDOG_WARNING);
-    }
-  }
-}
-
-/**
- * Update the saved metadata in a node's video field.
- *
- * @param &$node
- *   The node object to mark as published.
- * @param $video
- *   The video array as loaded from the Ooyala API.
- * @param $embedcode
- *   The embed code to fetch the image for.
- * @param $embed_args
- *   An array of arguments for substition into translated watchdog messages.
- */
-function _ooyala_ping_metadata(&$node, $video, $embedcode, $embed_args) {
-  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
-  foreach ($node->ooyala_field_names as $field_name) {
-    foreach ($node->{$field_name} as $key => $value) {
-      if ($value['value'] == $embedcode) {
-        // Update the meta data in the node if changed.
-        if (empty($value['status']) || $value['status'] != $video['status']) {
-          $node->{$field_name}[$key]['status'] = $video['status'];
-          $node->pulled_from_ooyala = TRUE;
-        }
-        if (empty($value['length']) || $value['length'] != $video['length']) {
-          $node->{$field_name}[$key]['length'] = $video['length'];
-          $node->pulled_from_ooyala = TRUE;
-        }
-      }
-    }
-  }
-  if (!empty($node->pulled_from_ooyala)) {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'Updated video %embedcode status (@status) and length (@length) values.', $embed_args + array('@status' => $video['status'], '@length' => $video['length']));
-    }
-    node_save($node);
-  }
-  else {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'No changes necessary to video status or length for embed code %embedcode.', $embed_args);
-    }
-  }
-}
-
diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
new file mode 100644
index 0000000..b5c2bcb
--- /dev/null
+++ b/includes/ooyala.ping.inc
@@ -0,0 +1,161 @@
+<?php
+
+/**
+ * @file
+ * Functions for handling pings notifying us of Ooyala backlot updates.
+ */
+
+
+/**
+ * Menu callback; Respond to Ooyala pings when a video has finished processing.
+ *
+ * This callback responds to the URL "ooyala/ping" within the Drupal site.
+ * Typically this URL needs to be configured to receive pings from Ooyala.com
+ * when a video has finished processing and is ready to be displayed. The
+ * configuration for setting up a ping URL is at
+ * https://www.ooyala.com/backlot/web, underneath the Account => Developers
+ * tab. Enter a URL in the API Ping URL such as http://example.com/ooyala/ping,
+ * if Drupal is installed at the base of the domain.
+ */
+function ooyala_ping() {
+  module_load_include('inc', 'ooyala', 'includes/ooyala.api');
+  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
+
+  if (isset($_GET['embedCode'])) {
+    $embedcode = $_GET['embedCode'];
+    $embed_args = array('%embedcode' => $embedcode);
+    if ($verbose) {
+      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embedcode with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
+    }
+
+    if ($video = ooyala_api_video_load($embedcode)) {
+      _ooyala_ping_fetch_image($embedcode, $embed_args);
+
+      if ($nodes = ooyala_load_nodes($embedcode)) {
+        foreach ($nodes as $node) {
+          _ooyala_ping_autopublish($node, $embed_args);
+          _ooyala_ping_metadata($node, $video, $embedcode, $embed_args);
+        }
+      }
+      else {
+        watchdog('ooyala_ping', 'Content matching the embed code %embedcode was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
+      }
+    }
+    else {
+      watchdog('ooyala_ping', 'A matching video for the %embedcode could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
+    }
+  }
+  else {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'Received embedcode-less ping from Ooyala Backlot with request <pre>@request</pre>.', array('@request' => print_r($_REQUEST, TRUE)));
+    }
+    else {
+      watchdog('ooyala_ping', 'Recieved ping without an embed code. No action has been taken.', array(), WATCHDOG_ERROR);
+    }
+  }
+
+  // Disable the page cache for this request.
+  if (function_exists('drupal_page_is_cacheable')) {
+    // PressFlow or Drupal 7 mechanism:
+    drupal_page_is_cacheable(FALSE);
+  }
+  else {
+    // Drupal 6 cache disabling hack.
+    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
+  }
+}
+
+/**
+ * Fetch an image in response to an Ooyala ping.
+ *
+ * @param $embedcode
+ *   The embed code to fetch the image for.
+ * @param $embed_args
+ *   An array of arguments for substition into translated watchdog messages.
+ *
+ * @return
+ *   The image path of the fetched image, or FALSE if one could not be loaded.
+ */
+function _ooyala_ping_fetch_image($embedcode, $embed_args) {
+  $image_path = NULL;
+  if ($image_path = ooyala_api_fetch_image($embedcode)) {
+    if (variable_get('ooyala_ping_reporting_verbose', FALSE)) {
+      watchdog('ooyala_ping', 'The video image for %embedcode has been downloaded to <a href="!url">@filename</a>.', $embed_args + array('!url' => file_create_url($image_path), '@filename' => basename($image_path)));
+    }
+  }
+  else {
+    watchdog('ooyala_ping', 'There was an error downloading the video image for %embedcode.', $embed_args, WATCHDOG_ERROR);
+  }
+
+  return $image_path;
+}
+
+/**
+ * Mark a node as published if the video has been published.
+ *
+ * @param &$node
+ *   The node object to mark as published.
+ * @param $embed_args
+ *   An array of arguments for substition into translated watchdog messages.
+ */
+function _ooyala_ping_autopublish(&$node, $embed_args) {
+  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
+  if (variable_get('ooyala_autopublish', 0) && !$node->status) {
+    $node->status = 1;
+    node_save($node);
+    watchdog('ooyala_ping', 'The content <a href="!url">%title</a> has been published by a ping for %embedcode.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
+  }
+  elseif (!variable_get('ooyala_autopublish', 0)) {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was not published as automatic publishing is disabled.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
+    }
+  }
+  else {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was already published.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title), WATCHDOG_WARNING);
+    }
+  }
+}
+
+/**
+ * Update the saved metadata in a node's video field.
+ *
+ * @param &$node
+ *   The node object to mark as published.
+ * @param $video
+ *   The video array as loaded from the Ooyala API.
+ * @param $embedcode
+ *   The embed code to fetch the image for.
+ * @param $embed_args
+ *   An array of arguments for substition into translated watchdog messages.
+ */
+function _ooyala_ping_metadata(&$node, $video, $embedcode, $embed_args) {
+  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
+  foreach ($node->ooyala_field_names as $field_name) {
+    foreach ($node->{$field_name} as $key => $value) {
+      if ($value['value'] == $embedcode) {
+        // Update the meta data in the node if changed.
+        if (empty($value['status']) || $value['status'] != $video['status']) {
+          $node->{$field_name}[$key]['status'] = $video['status'];
+          $node->pulled_from_ooyala = TRUE;
+        }
+        if (empty($value['length']) || $value['length'] != $video['length']) {
+          $node->{$field_name}[$key]['length'] = $video['length'];
+          $node->pulled_from_ooyala = TRUE;
+        }
+      }
+    }
+  }
+  if (!empty($node->pulled_from_ooyala)) {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'Updated video %embedcode status (@status) and length (@length) values.', $embed_args + array('@status' => $video['status'], '@length' => $video['length']));
+    }
+    node_save($node);
+  }
+  else {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'No changes necessary to video status or length for embed code %embedcode.', $embed_args);
+    }
+  }
+}
+
diff --git a/ooyala.module b/ooyala.module
index 52e00e0..4ecd3ed 100755
--- a/ooyala.module
+++ b/ooyala.module
@@ -53,7 +53,7 @@ function ooyala_menu() {
   $items['ooyala/ping'] = array(
     'page callback' => 'ooyala_ping',
     'access arguments' => array('access content'),
-    'file' => 'includes/ooyala.pages.inc',
+    'file' => 'includes/ooyala.ping.inc',
     'type' => MENU_CALLBACK,
   );
 
-- 
1.7.7.4


From 3cf31e9fbe061561ba63ae933f513a2b610f5e0b Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 16 Jan 2012 17:23:28 -0500
Subject: [PATCH 02/14] Issue #1407196: Standardize variable names to use
 embed_code.

---
 includes/ooyala.ping.inc |   44 ++++++++++++++++++++++----------------------
 1 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index b5c2bcb..a67d520 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -22,27 +22,27 @@ function ooyala_ping() {
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
 
   if (isset($_GET['embedCode'])) {
-    $embedcode = $_GET['embedCode'];
-    $embed_args = array('%embedcode' => $embedcode);
+    $embed_code = $_GET['embedCode'];
+    $embed_args = array('%embed_code' => $embed_code);
     if ($verbose) {
-      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embedcode with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
+      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
     }
 
-    if ($video = ooyala_api_video_load($embedcode)) {
-      _ooyala_ping_fetch_image($embedcode, $embed_args);
+    if ($video = ooyala_api_video_load($embed_code)) {
+      _ooyala_ping_fetch_image($embed_code, $embed_args);
 
-      if ($nodes = ooyala_load_nodes($embedcode)) {
+      if ($nodes = ooyala_load_nodes($embed_code)) {
         foreach ($nodes as $node) {
           _ooyala_ping_autopublish($node, $embed_args);
-          _ooyala_ping_metadata($node, $video, $embedcode, $embed_args);
+          _ooyala_ping_metadata($node, $video, $embed_code, $embed_args);
         }
       }
       else {
-        watchdog('ooyala_ping', 'Content matching the embed code %embedcode was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
+        watchdog('ooyala_ping', 'Content matching the embed code %embed_code was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
       }
     }
     else {
-      watchdog('ooyala_ping', 'A matching video for the %embedcode could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
+      watchdog('ooyala_ping', 'A matching video for the %embed_code could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
     }
   }
   else {
@@ -68,7 +68,7 @@ function ooyala_ping() {
 /**
  * Fetch an image in response to an Ooyala ping.
  *
- * @param $embedcode
+ * @param $embed_code
  *   The embed code to fetch the image for.
  * @param $embed_args
  *   An array of arguments for substition into translated watchdog messages.
@@ -76,15 +76,15 @@ function ooyala_ping() {
  * @return
  *   The image path of the fetched image, or FALSE if one could not be loaded.
  */
-function _ooyala_ping_fetch_image($embedcode, $embed_args) {
+function _ooyala_ping_fetch_image($embed_code, $embed_args) {
   $image_path = NULL;
-  if ($image_path = ooyala_api_fetch_image($embedcode)) {
+  if ($image_path = ooyala_api_fetch_image($embed_code)) {
     if (variable_get('ooyala_ping_reporting_verbose', FALSE)) {
-      watchdog('ooyala_ping', 'The video image for %embedcode has been downloaded to <a href="!url">@filename</a>.', $embed_args + array('!url' => file_create_url($image_path), '@filename' => basename($image_path)));
+      watchdog('ooyala_ping', 'The video image for %embed_code has been downloaded to <a href="!url">@filename</a>.', $embed_args + array('!url' => file_create_url($image_path), '@filename' => basename($image_path)));
     }
   }
   else {
-    watchdog('ooyala_ping', 'There was an error downloading the video image for %embedcode.', $embed_args, WATCHDOG_ERROR);
+    watchdog('ooyala_ping', 'There was an error downloading the video image for %embed_code.', $embed_args, WATCHDOG_ERROR);
   }
 
   return $image_path;
@@ -103,16 +103,16 @@ function _ooyala_ping_autopublish(&$node, $embed_args) {
   if (variable_get('ooyala_autopublish', 0) && !$node->status) {
     $node->status = 1;
     node_save($node);
-    watchdog('ooyala_ping', 'The content <a href="!url">%title</a> has been published by a ping for %embedcode.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
+    watchdog('ooyala_ping', 'The content <a href="!url">%title</a> has been published by a ping for %embed_code.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
   }
   elseif (!variable_get('ooyala_autopublish', 0)) {
     if ($verbose) {
-      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was not published as automatic publishing is disabled.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
+      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embed_code was not published as automatic publishing is disabled.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title));
     }
   }
   else {
     if ($verbose) {
-      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embedcode was already published.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title), WATCHDOG_WARNING);
+      watchdog('ooyala_ping', 'The content <a href="!url">%title</a> with embed code %embed_code was already published.', $embed_args + array('!url' => url('node/' . $node->nid), '%title' => $node->title), WATCHDOG_WARNING);
     }
   }
 }
@@ -124,16 +124,16 @@ function _ooyala_ping_autopublish(&$node, $embed_args) {
  *   The node object to mark as published.
  * @param $video
  *   The video array as loaded from the Ooyala API.
- * @param $embedcode
+ * @param $embed_code
  *   The embed code to fetch the image for.
  * @param $embed_args
  *   An array of arguments for substition into translated watchdog messages.
  */
-function _ooyala_ping_metadata(&$node, $video, $embedcode, $embed_args) {
+function _ooyala_ping_metadata(&$node, $video, $embed_code, $embed_args) {
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
   foreach ($node->ooyala_field_names as $field_name) {
     foreach ($node->{$field_name} as $key => $value) {
-      if ($value['value'] == $embedcode) {
+      if ($value['value'] == $embed_code) {
         // Update the meta data in the node if changed.
         if (empty($value['status']) || $value['status'] != $video['status']) {
           $node->{$field_name}[$key]['status'] = $video['status'];
@@ -148,13 +148,13 @@ function _ooyala_ping_metadata(&$node, $video, $embedcode, $embed_args) {
   }
   if (!empty($node->pulled_from_ooyala)) {
     if ($verbose) {
-      watchdog('ooyala_ping', 'Updated video %embedcode status (@status) and length (@length) values.', $embed_args + array('@status' => $video['status'], '@length' => $video['length']));
+      watchdog('ooyala_ping', 'Updated video %embed_code status (@status) and length (@length) values.', $embed_args + array('@status' => $video['status'], '@length' => $video['length']));
     }
     node_save($node);
   }
   else {
     if ($verbose) {
-      watchdog('ooyala_ping', 'No changes necessary to video status or length for embed code %embedcode.', $embed_args);
+      watchdog('ooyala_ping', 'No changes necessary to video status or length for embed code %embed_code.', $embed_args);
     }
   }
 }
-- 
1.7.7.4


From c78fa6bab83afae35deeded8fa99eaa58a6166d5 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 16 Jan 2012 17:24:32 -0500
Subject: [PATCH 03/14] Issue #1407196: Remove legacy D6 cache disabling code.

---
 includes/ooyala.ping.inc |    9 +--------
 1 files changed, 1 insertions(+), 8 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index a67d520..60bf1bd 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -55,14 +55,7 @@ function ooyala_ping() {
   }
 
   // Disable the page cache for this request.
-  if (function_exists('drupal_page_is_cacheable')) {
-    // PressFlow or Drupal 7 mechanism:
-    drupal_page_is_cacheable(FALSE);
-  }
-  else {
-    // Drupal 6 cache disabling hack.
-    $GLOBALS['conf']['cache'] = CACHE_DISABLED;
-  }
+  drupal_page_is_cacheable(FALSE);
 }
 
 /**
-- 
1.7.7.4


From a62ced804af1ae7e5f3d28cb9d6335df8f034266 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 16 Jan 2012 17:37:21 -0500
Subject: [PATCH 04/14] Issue #1407196: Add basic queue insert functions.

---
 includes/ooyala.ping.inc |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 60bf1bd..9fff832 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -5,6 +5,40 @@
  * Functions for handling pings notifying us of Ooyala backlot updates.
  */
 
+/**
+ * Insert an embed code to the ooyala_ping queue be processed.
+ */
+function ooyala_ping_insert() {
+  $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
+
+  if (isset($_GET['embedCode'])) {
+    $data = array(
+      'embed_code' => $_GET['embedCode'],
+      'fetch_attempts' => 0,
+    );
+
+    if ($verbose) {
+      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
+      $data['request'] = $_REQUEST;
+    }
+
+    $queue = ooyala_ping_queue();
+    $queue->createItem($data);
+  }
+}
+
+/**
+ * Return the Ooyala Ping queue, creating it if needed.
+ *
+ * @return
+ *   An instance of the default queuing mechanism used for storing Ooyala pings
+ *   to process.
+ */
+function ooyala_ping_queue() {
+  $queue = DrupalQueue::get('ooyala_ping');
+  $queue->createQueue();
+  return $queue;
+}
 
 /**
  * Menu callback; Respond to Ooyala pings when a video has finished processing.
-- 
1.7.7.4


From 0dcd3c6c5fb08f51a220f257afc1729c52b03c05 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 16 Jan 2012 18:08:59 -0500
Subject: [PATCH 05/14] Issue #1407196: Move all request logging to the ping
 page callback.

---
 includes/ooyala.ping.inc |   20 ++++++++------------
 1 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 9fff832..f3e194f 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -19,12 +19,19 @@ function ooyala_ping_insert() {
 
     if ($verbose) {
       watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
-      $data['request'] = $_REQUEST;
     }
 
     $queue = ooyala_ping_queue();
     $queue->createItem($data);
   }
+  else {
+    if ($verbose) {
+      watchdog('ooyala_ping', 'Received embedcode-less ping from Ooyala Backlot with request <pre>@request</pre>.', array('@request' => print_r($_REQUEST, TRUE)));
+    }
+    else {
+      watchdog('ooyala_ping', 'Recieved ping without an embed code. No action has been taken.', array(), WATCHDOG_ERROR);
+    }
+  }
 }
 
 /**
@@ -58,9 +65,6 @@ function ooyala_ping() {
   if (isset($_GET['embedCode'])) {
     $embed_code = $_GET['embedCode'];
     $embed_args = array('%embed_code' => $embed_code);
-    if ($verbose) {
-      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
-    }
 
     if ($video = ooyala_api_video_load($embed_code)) {
       _ooyala_ping_fetch_image($embed_code, $embed_args);
@@ -79,14 +83,6 @@ function ooyala_ping() {
       watchdog('ooyala_ping', 'A matching video for the %embed_code could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
     }
   }
-  else {
-    if ($verbose) {
-      watchdog('ooyala_ping', 'Received embedcode-less ping from Ooyala Backlot with request <pre>@request</pre>.', array('@request' => print_r($_REQUEST, TRUE)));
-    }
-    else {
-      watchdog('ooyala_ping', 'Recieved ping without an embed code. No action has been taken.', array(), WATCHDOG_ERROR);
-    }
-  }
 
   // Disable the page cache for this request.
   drupal_page_is_cacheable(FALSE);
-- 
1.7.7.4


From 5d020c0b10907b167ed012d3224a0404dc5adb19 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 13:39:38 -0500
Subject: [PATCH 06/14] Issue #1407196: Move the cache disable to the ping
 page function.

---
 includes/ooyala.ping.inc |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index f3e194f..36a8cf1 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -9,6 +9,9 @@
  * Insert an embed code to the ooyala_ping queue be processed.
  */
 function ooyala_ping_insert() {
+  // Disable the page cache for this request.
+  drupal_page_is_cacheable(FALSE);
+
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
 
   if (isset($_GET['embedCode'])) {
@@ -84,8 +87,6 @@ function ooyala_ping() {
     }
   }
 
-  // Disable the page cache for this request.
-  drupal_page_is_cacheable(FALSE);
 }
 
 /**
-- 
1.7.7.4


From 570db46282184488d0a2bce9e86e412eecda0f89 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 13:40:45 -0500
Subject: [PATCH 07/14] Issue #1407196: Add missing return in
 _ooyala_ping_fetch_image().

---
 includes/ooyala.ping.inc |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 36a8cf1..5475a4e 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -109,6 +109,7 @@ function _ooyala_ping_fetch_image($embed_code, $embed_args) {
   }
   else {
     watchdog('ooyala_ping', 'There was an error downloading the video image for %embed_code.', $embed_args, WATCHDOG_ERROR);
+    return FALSE;
   }
 
   return $image_path;
-- 
1.7.7.4


From a1dec7b51fc0e380cdedb0f6c53d6479a39f2015 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 13:52:19 -0500
Subject: [PATCH 08/14] Issue #1407196: Refactor ping processing into an
 explicit process function.

---
 includes/ooyala.ping.inc |   49 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 5475a4e..b0c6cac 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -51,6 +51,36 @@ function ooyala_ping_queue() {
 }
 
 /**
+ * Process items in the Ooyala ping queue.
+ *
+ * @param $limit
+ *   Optional parameter indicating the number of items to process at once.
+ */
+function ooyala_ping_queue_process($limit = NULL) {
+  if (!$limit) {
+    $limit = 10;
+  }
+
+  $queue = ooyala_ping_queue();
+  $processed = array();
+  while (count($processed) < $limit && $item = $queue->claimItem()) {
+
+    if (!ooyala_ping_process($item['embed_code'])) {
+      if ($item['fetch_attempts'] < variable_get('ooyala_ping_fetch_attempts', 10)) {
+        $item['fetch_attempts']++;
+        $queue->createItem($item);
+      }
+      else {
+        watchdog('ooyala_ping', 'Unable to process ping request for embed code %embed_code after !count attempts. Discarding the embed code from the queue.', array('%embed_code' => $item['embed_code'], '!count' => $item['fetch_attempts']), WATCHDOG_ERROR);
+      }
+    }
+  }
+  else {
+    $queue->deleteItem($item);
+  }
+}
+
+/**
  * Menu callback; Respond to Ooyala pings when a video has finished processing.
  *
  * This callback responds to the URL "ooyala/ping" within the Drupal site.
@@ -60,8 +90,15 @@ function ooyala_ping_queue() {
  * https://www.ooyala.com/backlot/web, underneath the Account => Developers
  * tab. Enter a URL in the API Ping URL such as http://example.com/ooyala/ping,
  * if Drupal is installed at the base of the domain.
+ *
+ * @param $embed_code
+ *   The embed code to process by syncing data such as video metadata and node
+ *   statuses.
+ *
+ * @return
+ *   TRUE if the embed code was processed successfully, or FALSE otherwise.
  */
-function ooyala_ping() {
+function ooyala_ping_process($embed_code) {
   module_load_include('inc', 'ooyala', 'includes/ooyala.api');
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
 
@@ -70,7 +107,12 @@ function ooyala_ping() {
     $embed_args = array('%embed_code' => $embed_code);
 
     if ($video = ooyala_api_video_load($embed_code)) {
-      _ooyala_ping_fetch_image($embed_code, $embed_args);
+      // If we can't fetch an image for a video, odds are the rest of the
+      // request will fail too. So, we bail here and don't publish the node so
+      // anything dependent on the thumbnail image (such as views) won't break.
+      if (!_ooyala_ping_fetch_image($embed_code, $embed_args)) {
+        return FALSE;
+      }
 
       if ($nodes = ooyala_load_nodes($embed_code)) {
         foreach ($nodes as $node) {
@@ -80,13 +122,16 @@ function ooyala_ping() {
       }
       else {
         watchdog('ooyala_ping', 'Content matching the embed code %embed_code was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
+        return FALSE;
       }
     }
     else {
       watchdog('ooyala_ping', 'A matching video for the %embed_code could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
+      return FALSE;
     }
   }
 
+  return TRUE;
 }
 
 /**
-- 
1.7.7.4


From be314c8fd6dc7fe4e8ddfebd3373fa056f64239e Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 13:57:37 -0500
Subject: [PATCH 09/14] Issue #1407196: Queue pings when they are recieved and
 process them with cron.

---
 ooyala.module |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/ooyala.module b/ooyala.module
index 4ecd3ed..ca2d8f5 100755
--- a/ooyala.module
+++ b/ooyala.module
@@ -51,7 +51,7 @@ function ooyala_menu() {
     'type' => MENU_CALLBACK,
   );
   $items['ooyala/ping'] = array(
-    'page callback' => 'ooyala_ping',
+    'page callback' => 'ooyala_ping_insert',
     'access arguments' => array('access content'),
     'file' => 'includes/ooyala.ping.inc',
     'type' => MENU_CALLBACK,
@@ -239,6 +239,10 @@ function ooyala_cron() {
 
   // Set the sync time so that we don't need to process unchanged videos.
   variable_set('ooyala_last_sync', $sync_time);
+
+  // Fire off the processing of queued embed codes.
+  module_load_include('inc', 'ooyala', 'includes/ooyala.ping');
+  ooyala_ping_queue_process(variable_get('ooyala_ping_queue_limit', 10));
 }
 
 /**
-- 
1.7.7.4


From a06be19e7daabfaef5eb199fa89a619adb3ac986 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 14:13:14 -0500
Subject: [PATCH 10/14] Issue #1407196: Fix else statement outside of the
 while loop.

---
 includes/ooyala.ping.inc |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index b0c6cac..6c7f13f 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -74,9 +74,9 @@ function ooyala_ping_queue_process($limit = NULL) {
         watchdog('ooyala_ping', 'Unable to process ping request for embed code %embed_code after !count attempts. Discarding the embed code from the queue.', array('%embed_code' => $item['embed_code'], '!count' => $item['fetch_attempts']), WATCHDOG_ERROR);
       }
     }
-  }
-  else {
-    $queue->deleteItem($item);
+    else {
+      $queue->deleteItem($item);
+    }
   }
 }
 
-- 
1.7.7.4


From 144e302227b1186cc68b2e6c971c177b5b4aeaf4 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 14:49:30 -0500
Subject: [PATCH 11/14] Issue #1407196: Fix wrong variable when logging ping
 requests.

---
 includes/ooyala.ping.inc |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 6c7f13f..da46018 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -21,7 +21,7 @@ function ooyala_ping_insert() {
     );
 
     if ($verbose) {
-      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', $embed_args + array('@request' => print_r($_REQUEST, TRUE)));
+      watchdog('ooyala_ping', 'Received ping from Ooyala Backlot for embedCode %embed_code with request <pre>@request</pre>.', array('%embed_code' => $_GET['embedCode'], '@request' => print_r($_REQUEST, TRUE)));
     }
 
     $queue = ooyala_ping_queue();
-- 
1.7.7.4


From 516e0b7293c911b5af906629eaccdf5dc0f41871 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 14:50:24 -0500
Subject: [PATCH 12/14] Issue #1407196: Fix processing the same item multiple
 times and retrying failed items.

---
 includes/ooyala.ping.inc |   28 +++++++++++++++++-----------
 1 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index da46018..4606f53 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -57,26 +57,32 @@ function ooyala_ping_queue() {
  *   Optional parameter indicating the number of items to process at once.
  */
 function ooyala_ping_queue_process($limit = NULL) {
+  $queue = ooyala_ping_queue();
+  $processed = 0;
+
   if (!$limit) {
     $limit = 10;
   }
 
-  $queue = ooyala_ping_queue();
-  $processed = array();
-  while (count($processed) < $limit && $item = $queue->claimItem()) {
+  // Limit to at most the number of items in the queue. This prevents us from
+  // repeatedly fetching the same embed code during the same processing phase
+  // if the embed code is encountering errors processing.
+  $limit = min($limit, $queue->numberOfItems());
 
-    if (!ooyala_ping_process($item['embed_code'])) {
-      if ($item['fetch_attempts'] < variable_get('ooyala_ping_fetch_attempts', 10)) {
-        $item['fetch_attempts']++;
-        $queue->createItem($item);
+  while ($processed < $limit && $item = $queue->claimItem()) {
+    $processed++;
+    $data = $item->data;
+
+    if (!ooyala_ping_process($data['embed_code'])) {
+      if ($data['fetch_attempts'] < variable_get('ooyala_ping_fetch_attempts', 10)) {
+        $data['fetch_attempts']++;
+        $queue->createItem($data);
       }
       else {
-        watchdog('ooyala_ping', 'Unable to process ping request for embed code %embed_code after !count attempts. Discarding the embed code from the queue.', array('%embed_code' => $item['embed_code'], '!count' => $item['fetch_attempts']), WATCHDOG_ERROR);
+        watchdog('ooyala_ping', 'Unable to process ping request for embed code %embed_code after !count attempts. Discarding the embed code from the queue.', array('%embed_code' => $data['embed_code'], '!count' => $data['fetch_attempts']), WATCHDOG_ERROR);
       }
     }
-    else {
-      $queue->deleteItem($item);
-    }
+    $queue->deleteItem($item);
   }
 }
 
-- 
1.7.7.4


From 2e3a5bda5f401bf8068479cd354c07c95617044d Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 14:52:28 -0500
Subject: [PATCH 13/14] Issue #1407196: Acutally use the passed in $embed_code
 parameter.

---
 includes/ooyala.ping.inc |   39 ++++++++++++++++++---------------------
 1 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index 4606f53..f0265da 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -108,34 +108,31 @@ function ooyala_ping_process($embed_code) {
   module_load_include('inc', 'ooyala', 'includes/ooyala.api');
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
 
-  if (isset($_GET['embedCode'])) {
-    $embed_code = $_GET['embedCode'];
-    $embed_args = array('%embed_code' => $embed_code);
-
-    if ($video = ooyala_api_video_load($embed_code)) {
-      // If we can't fetch an image for a video, odds are the rest of the
-      // request will fail too. So, we bail here and don't publish the node so
-      // anything dependent on the thumbnail image (such as views) won't break.
-      if (!_ooyala_ping_fetch_image($embed_code, $embed_args)) {
-        return FALSE;
-      }
+  $embed_args = array('%embed_code' => $embed_code);
 
-      if ($nodes = ooyala_load_nodes($embed_code)) {
-        foreach ($nodes as $node) {
-          _ooyala_ping_autopublish($node, $embed_args);
-          _ooyala_ping_metadata($node, $video, $embed_code, $embed_args);
-        }
-      }
-      else {
-        watchdog('ooyala_ping', 'Content matching the embed code %embed_code was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
-        return FALSE;
+  if ($video = ooyala_api_video_load($embed_code)) {
+    // If we can't fetch an image for a video, odds are the rest of the
+    // request will fail too. So, we bail here and don't publish the node so
+    // anything dependent on the thumbnail image (such as views) won't break.
+    if (!_ooyala_ping_fetch_image($embed_code, $embed_args)) {
+      return FALSE;
+    }
+
+    if ($nodes = ooyala_load_nodes($embed_code)) {
+      foreach ($nodes as $node) {
+        _ooyala_ping_autopublish($node, $embed_args);
+        _ooyala_ping_metadata($node, $video, $embed_code, $embed_args);
       }
     }
     else {
-      watchdog('ooyala_ping', 'A matching video for the %embed_code could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
+      watchdog('ooyala_ping', 'Content matching the embed code %embed_code was not found. No action has been taken.', $embed_args, WATCHDOG_WARNING);
       return FALSE;
     }
   }
+  else {
+    watchdog('ooyala_ping', 'A matching video for the %embed_code could not be loaded from Ooyala.', $embed_args, WATCHDOG_ERROR);
+    return FALSE;
+  }
 
   return TRUE;
 }
-- 
1.7.7.4


From 65335b6094f29f6b2ceffa3300a19f62fad1c25c Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Wed, 18 Jan 2012 15:52:20 -0500
Subject: [PATCH 14/14] Issue #1407196: Fix missing array keys for D7 field
 array formats.

---
 includes/ooyala.ping.inc |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/includes/ooyala.ping.inc b/includes/ooyala.ping.inc
index f0265da..bd2ff8c 100644
--- a/includes/ooyala.ping.inc
+++ b/includes/ooyala.ping.inc
@@ -205,7 +205,9 @@ function _ooyala_ping_autopublish(&$node, $embed_args) {
 function _ooyala_ping_metadata(&$node, $video, $embed_code, $embed_args) {
   $verbose = variable_get('ooyala_ping_reporting_verbose', FALSE);
   foreach ($node->ooyala_field_names as $field_name) {
-    foreach ($node->{$field_name} as $key => $value) {
+    // We explicitly only allow a delta of 1 for fields, so we can hardcode the
+    // array index.
+    foreach ($node->{$field_name}['und'][0] as $key => $value) {
       if ($value['value'] == $embed_code) {
         // Update the meta data in the node if changed.
         if (empty($value['status']) || $value['status'] != $video['status']) {
-- 
1.7.7.4

