You are here

function uuid_node_features_rebuild in UUID Features Integration 6

Same name and namespace in other branches
  1. 7 includes/uuid_node.features.inc \uuid_node_features_rebuild()

Implementation of hook_features_rebuild(). Rebuilds nodes based on UUID from code defaults.

1 call to uuid_node_features_rebuild()
uuid_node_features_revert in includes/uuid_node.features.inc
Implementation of hook_features_revert().

File

includes/uuid_node.features.inc, line 109
Features hooks for the uuid_node features component.

Code

function uuid_node_features_rebuild($module) {
  $nodes = module_invoke($module, 'uuid_features_default_content');
  $manually_set = array(
    'created' => array(
      'table' => 'node',
      'column' => 'created',
    ),
    'changed' => array(
      'table' => 'node',
      'column' => 'changed',
    ),
    'revision_timestamp' => array(
      'table' => 'node_revisions',
      'column' => 'timestamp',
    ),
    'last_comment_timestamp' => array(
      'table' => 'node_comment_statistics',
      'column' => 'last_comment_timestamp',
    ),
  );
  if (!empty($nodes)) {
    module_load_include('inc', 'node', 'node.pages');
    foreach ($nodes as $data) {
      $node = (object) $data;

      // Note which of these values have been set in the export
      foreach ($manually_set as $key => $values) {
        if (isset($node->{$key})) {
          $manually_set[$key]['value'] = $node->{$key};
        }
      }

      // Perform tasks from node_object_prepare() without overwriting data
      // in the exported node.
      $node_options = variable_get('node_options_' . $node->type, array(
        'status',
        'promote',
      ));
      foreach (array(
        'status',
        'promote',
        'sticky',
      ) as $key) {
        if (!isset($node->{$key})) {
          $node->{$key} = in_array($key, $node_options);
        }
      }
      node_invoke($node, 'prepare');
      node_invoke_nodeapi($node, 'prepare');
      $existing = node_get_by_uuid($node->uuid);
      if (!empty($existing)) {
        $node->nid = $existing->nid;
        $node->vid = $existing->vid;
      }

      // The hook_alter signature is:
      // hook_uuid_node_features_rebuild_alter(&node, $module);
      drupal_alter('uuid_node_features_rebuild', $node, $module);
      $node = node_submit($node);
      node_save($node);

      // The Node API is hard-coded to override certain values set in the node.
      // Set them here.
      foreach ($manually_set as $key => $values) {
        if (isset($values['value'])) {
          $table = $values['table'];
          $column = $values['column'];
          $value = $values['value'];
          if ($table == 'node_comment_statistics') {
            $where = 'nid = %d';
            $args = array(
              $value,
              $node->nid,
            );
          }
          else {
            $where = 'nid = %d AND vid = %d';
            $args = array(
              $value,
              $node->nid,
              $node->vid,
            );
          }
          db_query("UPDATE {$table} SET {$column} = %d WHERE {$where}", $args);
        }
      }
    }
  }
}