Same name and namespace in other branches
  1. 4.6.x modules/node.module \node_save()
  2. 4.7.x modules/node.module \node_save()
  3. 6.x modules/node/node.module \node_save()
  4. 7.x modules/node/node.module \node_save()

Save a node object into the database.

7 calls to node_save()
blogapi_blogger_edit_post in modules/blogapi/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
blogapi_mt_publish_post in modules/blogapi/blogapi.module
Blogging API callback. Publishes the given node
blogapi_mt_set_post_categories in modules/blogapi/blogapi.module
Blogging API callback. Assigns taxonomy terms to a particular node.
book_admin_edit_submit in modules/book/book.module

... See full list

File

modules/node/node.module, line 602
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.

Code

function node_save(&$node) {
  global $user;
  $node->is_new = FALSE;

  // Apply filters to some default node fields:
  if (empty($node->nid)) {

    // Insert a new node.
    $node->is_new = TRUE;
    $node->nid = db_next_id('{node}_nid');
    $node->vid = db_next_id('{node_revisions}_vid');
  }
  else {

    // We need to ensure that all node fields are filled.
    $node_current = node_load($node->nid);
    foreach ($node as $field => $data) {
      $node_current->{$field} = $data;
    }
    $node = $node_current;
    if ($node->revision) {
      $node->old_vid = $node->vid;
      $node->vid = db_next_id('{node_revisions}_vid');
    }
  }

  // Set some required fields:
  if (empty($node->created)) {
    $node->created = time();
  }

  // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
  $node->changed = time();

  // Split off revisions data to another structure
  $revisions_table_values = array(
    'nid' => $node->nid,
    'vid' => $node->vid,
    'title' => $node->title,
    'body' => $node->body,
    'teaser' => $node->teaser,
    'timestamp' => $node->changed,
    'uid' => $user->uid,
    'format' => $node->format,
  );
  $revisions_table_types = array(
    'nid' => '%d',
    'vid' => '%d',
    'title' => "'%s'",
    'body' => "'%s'",
    'teaser' => "'%s'",
    'timestamp' => '%d',
    'uid' => '%d',
    'format' => '%d',
  );
  if (!empty($node->log) || $node->is_new || $node->revision) {

    // Only store the log message if there's something to store; this prevents
    // existing log messages from being unintentionally overwritten by a blank
    // message. A new revision will have an empty log message (or $node->log).
    $revisions_table_values['log'] = $node->log;
    $revisions_table_types['log'] = "'%s'";
  }
  $node_table_values = array(
    'nid' => $node->nid,
    'vid' => $node->vid,
    'title' => $node->title,
    'type' => $node->type,
    'uid' => $node->uid,
    'status' => $node->status,
    'created' => $node->created,
    'changed' => $node->changed,
    'comment' => $node->comment,
    'promote' => $node->promote,
    'sticky' => $node->sticky,
  );
  $node_table_types = array(
    'nid' => '%d',
    'vid' => '%d',
    'title' => "'%s'",
    'type' => "'%s'",
    'uid' => '%d',
    'status' => '%d',
    'created' => '%d',
    'changed' => '%d',
    'comment' => '%d',
    'promote' => '%d',
    'sticky' => '%d',
  );

  //Generate the node table query and the

  //the node_revisions table query
  if ($node->is_new) {
    $node_query = 'INSERT INTO {node} (' . implode(', ', array_keys($node_table_types)) . ') VALUES (' . implode(', ', $node_table_types) . ')';
    $revisions_query = 'INSERT INTO {node_revisions} (' . implode(', ', array_keys($revisions_table_types)) . ') VALUES (' . implode(', ', $revisions_table_types) . ')';
  }
  else {
    $arr = array();
    foreach ($node_table_types as $key => $value) {
      $arr[] = $key . ' = ' . $value;
    }
    $node_table_values[] = $node->nid;
    $node_query = 'UPDATE {node} SET ' . implode(', ', $arr) . ' WHERE nid = %d';
    if ($node->revision) {
      $revisions_query = 'INSERT INTO {node_revisions} (' . implode(', ', array_keys($revisions_table_types)) . ') VALUES (' . implode(', ', $revisions_table_types) . ')';
    }
    else {
      $arr = array();
      foreach ($revisions_table_types as $key => $value) {
        $arr[] = $key . ' = ' . $value;
      }
      $revisions_table_values[] = $node->vid;
      $revisions_query = 'UPDATE {node_revisions} SET ' . implode(', ', $arr) . ' WHERE vid = %d';
    }
  }

  // Insert the node into the database:
  db_query($node_query, $node_table_values);
  db_query($revisions_query, $revisions_table_values);

  // Call the node specific callback (if any):
  if ($node->is_new) {
    node_invoke($node, 'insert');
    node_invoke_nodeapi($node, 'insert');
  }
  else {
    node_invoke($node, 'update');
    node_invoke_nodeapi($node, 'update');
  }

  // Update the node access table for this node.
  node_access_acquire_grants($node);

  // Clear the cache so an anonymous poster can see the node being added or updated.
  cache_clear_all();
}