From f761b8745c306703e0f78b5abdcc900044f5b602 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?G=C3=A1bor=20Hojtsy?= <gabor@hojtsy.hu>
Date: Thu, 20 Dec 2007 09:08:35 +0000
Subject: [PATCH] #164510 by yched, KarenS: move node batch operations to
 node.admin.inc for performance and fix a buggy message

---
 modules/node/node.admin.inc | 101 ++++++++++++++++++++++++++++++++++++
 modules/node/node.module    |  95 ---------------------------------
 2 files changed, 101 insertions(+), 95 deletions(-)

diff --git a/modules/node/node.admin.inc b/modules/node/node.admin.inc
index db9cebe9b6e1..bc33c3c19089 100644
--- a/modules/node/node.admin.inc
+++ b/modules/node/node.admin.inc
@@ -344,6 +344,107 @@ function node_admin_nodes_validate($form, &$form_state) {
   }
 }
 
+
+/**
+ * Make mass update of nodes, changing all nodes in the $nodes array
+ * to update them with the field values in $updates.
+ *
+ * IMPORTANT NOTE: This function is intended to work when called
+ * from a form submit handler. Calling it outside of the form submission
+ * process may not work correctly.
+ *
+ * @param array $nodes
+ *   Array of node nids to update.
+ * @param array $updates
+ *   Array of key/value pairs with node field names and the
+ *   value to update that field to.
+ */
+function node_mass_update($nodes, $updates) {
+  // We use batch processing to prevent timeout when updating a large number
+  // of nodes.
+  if (count($nodes) > 10) {
+    $batch = array(
+      'operations' => array(
+        array('_node_mass_update_batch_process', array($nodes, $updates))
+      ),
+      'finished' => '_node_mass_update_batch_finished',
+      'title' => t('Processing'),
+      // We use a single multi-pass operation, so the default
+      // 'Remaining x of y operations' message will be confusing here.
+      'progress_message' => '',
+      'error_message' => t('The update has encountered an error.'),
+      // The operations do not live in the .module file, so we need to
+      // tell the batch engine which file to load before calling them.
+      'file' => drupal_get_path('module', 'node'). '/node.admin.inc',
+    );
+    batch_set($batch);
+  }
+  else {
+    foreach($nodes as $nid) {
+      _node_mass_update_helper($nid, $updates);
+    }
+    drupal_set_message(t('The update has been performed.'));
+  }
+}
+
+/**
+ * Node Mass Update - helper function.
+ */
+function _node_mass_update_helper($nid, $updates) {
+  $node = node_load($nid, NULL, TRUE);
+  foreach ($updates as $name => $value) {
+    $node->$name = $value;
+  }
+  node_save($node);
+  return $node;
+}
+
+/**
+ * Node Mass Update Batch operation
+ */
+function _node_mass_update_batch_process($nodes, $updates, &$context) {
+  if (!isset($context['sandbox']['progress'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['max'] = count($nodes);
+    $context['sandbox']['nodes'] = $nodes;
+  }
+
+  // Process nodes by groups of 5.
+  $count = min(5, count($context['sandbox']['nodes']));
+  for ($i = 1; $i <= $count; $i++) {
+    // For each nid, load the node, reset the values, and save it.
+    $nid = array_shift($context['sandbox']['nodes']);
+    $node = _node_mass_update_helper($nid, $updates);
+
+    // Store result for post-processing in the finished callback.
+    $context['results'][] = l($node->title, 'node/'. $node->nid);
+
+    // Update our progress information.
+    $context['sandbox']['progress']++;
+  }
+
+  // Inform the batch engine that we are not finished,
+  // and provide an estimation of the completion level we reached.
+  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
+    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  }
+}
+
+/**
+ * Node Mass Update Batch 'finished' callback.
+ */
+function _node_mass_update_batch_finished($success, $results, $operations) {
+  if ($success) {
+    drupal_set_message(t('The update has been performed.'));
+  }
+  else {
+    drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
+    $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
+    $message .= theme('item_list', $results);
+    drupal_set_message($message);
+  }
+}
+
 /**
  * Menu callback: content administration.
  */
diff --git a/modules/node/node.module b/modules/node/node.module
index 60c9f7816c1e..76c000a5014d 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -2630,98 +2630,3 @@ function node_unpublish_by_keyword_action($node, $context) {
     }
   }
 }
-
-/**
- * Make mass update of nodes, changing all nodes in the $nodes array
- * to update them with the field values in $updates.
- *
- * IMPORTANT NOTE: This function is intended to work when called
- * from a form submit handler. Calling it outside of the form submission
- * process may not work correctly.
- *
- * @param array $nodes
- *   Array of node nids to update.
- * @param array $updates
- *   Array of key/value pairs with node field names and the
- *   value to update that field to.
- */
-function node_mass_update($nodes, $updates) {
-  // We use batch processing to prevent timeout when updating a large number
-  // of nodes.
-  if (count($nodes) > 10) {
-    $batch = array(
-      'operations' => array(
-        array('_node_mass_update_batch_process', array($nodes, $updates))
-      ),
-      'finished' => '_node_mass_update_batch_finished',
-      'title' => t('Processing'),
-      'error_message' => t('The update has encountered an error.'),
-    );
-    batch_set($batch);
-  }
-  else {
-    foreach ($nodes as $nid) {
-      _node_mass_update_helper($nid, $updates);
-    }
-    drupal_set_message(t('The update has been performed.'));
-  }
-}
-
-/**
- * Node Mass Update - helper function.
- */
-function _node_mass_update_helper($nid, $updates) {
-  $node = node_load($nid, NULL, TRUE);
-  foreach ($updates as $name => $value) {
-    $node->$name = $value;
-  }
-  node_save($node);
-  return $node;
-}
-
-/**
- * Node Mass Update Batch operation
- */
-function _node_mass_update_batch_process($nodes, $updates, &$context) {
-  if (!isset($context['sandbox']['progress'])) {
-    $context['sandbox']['progress'] = 0;
-    $context['sandbox']['max'] = count($nodes);
-    $context['sandbox']['nodes'] = $nodes;
-  }
-
-  // Process nodes by groups of 5.
-  $count = min(5, count($context['sandbox']['nodes']));
-  for ($i = 1; $i <= $count; $i++) {
-    // For each nid, load the node, reset the values, and save it.
-    $nid = array_shift($context['sandbox']['nodes']);
-    $node = _node_mass_update_helper($nid, $updates);
-
-    // Store result for post-processing in the finished callback.
-    $context['results'][] = l($node->title, 'node/'. $node->nid);
-
-    // Update our progress information.
-    $context['sandbox']['progress']++;
-    $context['message'] = t('Processing %title', array('%title' => $node->title));
-  }
-
-  // Inform the batch engine that we are not finished,
-  // and provide an estimation of the completion level we reached.
-  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
-    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
-  }
-}
-
-/**
- * Node Mass Update Batch 'finished' callback.
- */
-function _node_mass_update_batch_finished($success, $results, $operations) {
-  if ($success) {
-    drupal_set_message(t('The update has been performed.'));
-  }
-  else {
-    drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
-    $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
-    $message .= theme('item_list', $results);
-    drupal_set_message($message);
-  }
-}
-- 
GitLab