node.module
<?php
define('NODE_NEW_LIMIT', time() - 30 * 24 * 60 * 60);
define('NODE_BUILD_NORMAL', 0);
define('NODE_BUILD_PREVIEW', 1);
define('NODE_BUILD_SEARCH_INDEX', 2);
define('NODE_BUILD_SEARCH_RESULT', 3);
define('NODE_BUILD_RSS', 4);
define('NODE_BUILD_PRINT', 5);
function node_help($path, $arg) {
if ($path != 'admin/content/node-settings/rebuild' && $path != 'batch' && strpos($path, '#') === FALSE
&& user_access('access administration pages') && node_access_needs_rebuild()) {
if ($path == 'admin/content/node-settings') {
$message = t('The content access permissions need to be rebuilt.');
}
else {
$message = t('The content access permissions need to be rebuilt. Please visit <a href="@node_access_rebuild">this page</a>.', array('@node_access_rebuild' => url('admin/content/node-settings/rebuild')));
}
drupal_set_message($message, 'error');
}
switch ($path) {
case 'admin/help#node':
$output = '<p>'. t('The node module manages content on your site, and stores all posts (regardless of type) as a "node". In addition to basic publishing settings, including whether the post has been published, promoted to the site front page, or should remain present (or sticky) at the top of lists, the node module also records basic information about the author of a post. Optional revision control over edits is available. For additional functionality, the node module is often extended by other modules.') .'</p>';
$output .= '<p>'. t('Though each post on your site is a node, each post is also of a particular <a href="@content-type">content type</a>. <a href="@content-type">Content types</a> are used to define the characteristics of a post, including the title and description of the fields displayed on its add and edit pages. Each content type may have different default settings for <em>Publishing options</em> and other workflow controls. By default, the two content types in a standard Drupal installation are <em>Page</em> and <em>Story</em>. Use the <a href="@content-type">content types page</a> to add new or edit existing content types. Additional content types also become available as you enable additional core, contributed and custom modules.', array('@content-type' => url('admin/content/types'))) .'</p>';
$output .= '<p>'. t('The administrative <a href="@content">content page</a> allows you to review and manage your site content. The <a href="@post-settings">post settings page</a> sets certain options for the display of posts. The node module makes a number of permissions available for each content type, which may be set by role on the <a href="@permissions">permissions page</a>.', array('@content' => url('admin/content/node'), '@post-settings' => url('admin/content/node-settings'), '@permissions' => url('admin/user/permissions'))) .'</p>';
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@node">Node module</a>.', array('@node' => 'http://drupal.org/handbook/modules/node/')) .'</p>';
return $output;
case 'admin/content/node':
return ' '; case 'admin/content/types':
return '<p>'. t('Below is a list of all the content types on your site. All posts that exist on your site are instances of one of these content types.') .'</p>';
case 'admin/content/types/add':
return '<p>'. t('To create a new content type, enter the human-readable name, the machine-readable name, and all other relevant fields that are on this page. Once created, users of your site will be able to create posts that are instances of this content type.') .'</p>';
case 'node/%/revisions':
return '<p>'. t('The revisions let you track differences between multiple versions of a post.') .'</p>';
case 'node/%/edit':
$node = node_load($arg[1]);
$type = node_get_types('type', $node->type);
return (!empty($type->help) ? '<p>'. filter_xss_admin($type->help) .'</p>' : '');
}
if ($arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) {
$type = node_get_types('type', str_replace('-', '_', $arg[2]));
return (!empty($type->help) ? '<p>'. filter_xss_admin($type->help) .'</p>' : '');
}
}
function node_theme() {
return array(
'node' => array(
'arguments' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE),
'template' => 'node',
),
'node_list' => array(
'arguments' => array('items' => NULL, 'title' => NULL),
),
'node_search_admin' => array(
'arguments' => array('form' => NULL),
),
'node_filter_form' => array(
'arguments' => array('form' => NULL),
'file' => 'node.admin.inc',
),
'node_filters' => array(
'arguments' => array('form' => NULL),
'file' => 'node.admin.inc',
),
'node_admin_nodes' => array(
'arguments' => array('form' => NULL),
'file' => 'node.admin.inc',
),
'node_add_list' => array(
'arguments' => array('content' => NULL),
'file' => 'node.pages.inc',
),
'node_form' => array(
'arguments' => array('form' => NULL),
'file' => 'node.pages.inc',
),
'node_preview' => array(
'arguments' => array('node' => NULL),
'file' => 'node.pages.inc',
),
'node_log_message' => array(
'arguments' => array('log' => NULL),
),
'node_submitted' => array(
'arguments' => array('node' => NULL),
),
);
}
function node_cron() {
db_query('DELETE FROM {history} WHERE timestamp < %d', NODE_NEW_LIMIT);
}
function node_title_list($result, $title = NULL) {
$items = array();
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$items[] = l($node->title, 'node/'. $node->nid, !empty($node->comment_count) ? array('attributes' => array('title' => format_plural($node->comment_count, '1 comment', '@count comments'))) : array());
$num_rows = TRUE;
}
return $num_rows ? theme('node_list', $items, $title) : FALSE;
}
function theme_node_list($items, $title = NULL) {
return theme('item_list', $items, $title);
}
function node_tag_new($nid) {
global $user;
if ($user->uid) {
if (node_last_viewed($nid)) {
db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid);
}
else {
@db_query('INSERT INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)', $user->uid, $nid, time());
}
}
}
function node_last_viewed($nid) {
global $user;
static $history;
if (!isset($history[$nid])) {
$history[$nid] = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d", $user->uid, $nid));
}
return (isset($history[$nid]->timestamp) ? $history[$nid]->timestamp : 0);
}
function node_mark($nid, $timestamp) {
global $user;
static $cache;
if (!$user->uid) {
return MARK_READ;
}
if (!isset($cache[$nid])) {
$cache[$nid] = node_last_viewed($nid);
}
if ($cache[$nid] == 0 && $timestamp > NODE_NEW_LIMIT) {
return MARK_NEW;
}
elseif ($timestamp > $cache[$nid] && $timestamp > NODE_NEW_LIMIT) {
return MARK_UPDATED;
}
return MARK_READ;
}
function node_teaser_js(&$form, &$form_state) {
if (isset($form['#post']['teaser_js'])) {
if (trim($form_state['values']['teaser_js'])) {
$body = trim($form_state['values']['teaser_js']) ."\r\n<!--break-->\r\n". trim($form_state['values']['body']);
}
else {
$body = '<!--break-->'. $form_state['values']['body'];
}
form_set_value($form['body'], $body, $form_state);
$form['body']['#value'] = $body;
}
return $form;
}
function node_teaser_include_verify(&$form, &$form_state) {
$message = '';
if (isset($form['#post']['body']) && isset($form_state['values']['teaser_include']) && !$form_state['values']['teaser_include']) {
if (strpos($form_state['values']['body'], '<!--break-->') === 0) {
$message = t('You specified that the summary should not be shown when this post is displayed in full view. This setting is ignored when the summary is empty.');
}
elseif (strpos($form_state['values']['body'], '<!--break-->') === FALSE) {
$message = t('You specified that the summary should not be shown when this post is displayed in full view. This setting has been ignored since you have not defined a summary for the post. (To define a summary, insert the delimiter "<!--break-->" (without the quotes) in the Body of the post to indicate the end of the summary and the start of the main content.)');
}
if (!empty($message)) {
drupal_set_message($message, 'warning');
form_set_value($form['teaser_include'], 1, $form_state);
$form['teaser_include']['#value'] = 1;
}
}
return $form;
}
function node_teaser($body, $format = NULL, $size = NULL) {
if (!isset($size)) {
$size = variable_get('teaser_length', 600);
}
$delimiter = strpos($body, '<!--break-->');
if ($size == 0 && $delimiter === FALSE) {
return $body;
}
if ($delimiter !== FALSE) {
return substr($body, 0, $delimiter);
}
if (isset($format)) {
$filters = filter_list_format($format);
if (isset($filters['php/0']) && strpos($body, '<?') !== FALSE) {
return $body;
}
}
if (drupal_strlen($body) <= $size) {
return $body;
}
$teaser = truncate_utf8($body, $size);
$max_rpos = strlen($teaser);
$min_rpos = $max_rpos;
$reversed = strrev($teaser);
$break_points = array();
$break_points[] = array('</p>' => 0);
$line_breaks = array('<br />' => 6, '<br>' => 4);
if (isset($filters['filter/1'])) {
$line_breaks["\n"] = 1;
}
$break_points[] = $line_breaks;
$break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
foreach ($break_points as $points) {
foreach ($points as $point => $offset) {
$rpos = strpos($reversed, strrev($point));
if ($rpos !== FALSE) {
$min_rpos = min($rpos + $offset, $min_rpos);
}
}
if ($min_rpos !== $max_rpos) {
return ($min_rpos === 0) ? $teaser : substr($teaser, 0, 0 - $min_rpos);
}
}
return $teaser;
}
function node_get_types($op = 'types', $node = NULL, $reset = FALSE) {
static $_node_types, $_node_names;
if ($reset || !isset($_node_types)) {
list($_node_types, $_node_names) = _node_types_build();
}
if ($node) {
if (is_array($node)) {
$type = $node['type'];
}
elseif (is_object($node)) {
$type = $node->type;
}
elseif (is_string($node)) {
$type = $node;
}
if (!isset($_node_types[$type])) {
return FALSE;
}
}
switch ($op) {
case 'types':
return $_node_types;
case 'type':
return isset($_node_types[$type]) ? $_node_types[$type] : FALSE;
case 'module':
return isset($_node_types[$type]->module) ? $_node_types[$type]->module : FALSE;
case 'names':
return $_node_names;
case 'name':
return isset($_node_names[$type]) ? $_node_names[$type] : FALSE;
}
}
function node_types_rebuild() {
_node_types_build();
$node_types = node_get_types('types', NULL, TRUE);
foreach ($node_types as $type => $info) {
if (!empty($info->is_new)) {
node_type_save($info);
}
if (!empty($info->disabled)) {
node_type_delete($info->type);
}
}
_node_types_build();
}
function node_type_save($info) {
$is_existing = FALSE;
$existing_type = !empty($info->old_type) ? $info->old_type : $info->type;
$is_existing = db_result(db_query("SELECT COUNT(*) FROM {node_type} WHERE type = '%s'", $existing_type));
if (!isset($info->help)) {
$info->help = '';
}
if (!isset($info->min_word_count)) {
$info->min_word_count = 0;
}
if (!isset($info->body_label)) {
$info->body_label = '';
}
if ($is_existing) {
db_query("UPDATE {node_type} SET type = '%s', name = '%s', module = '%s', has_title = %d, title_label = '%s', has_body = %d, body_label = '%s', description = '%s', help = '%s', min_word_count = %d, custom = %d, modified = %d, locked = %d WHERE type = '%s'", $info->type, $info->name, $info->module, $info->has_title, $info->title_label, $info->has_body, $info->body_label, $info->description, $info->help, $info->min_word_count, $info->custom, $info->modified, $info->locked, $existing_type);
module_invoke_all('node_type', 'update', $info);
return SAVED_UPDATED;
}
else {
db_query("INSERT INTO {node_type} (type, name, module, has_title, title_label, has_body, body_label, description, help, min_word_count, custom, modified, locked, orig_type) VALUES ('%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%s', %d, %d, %d, %d, '%s')", $info->type, $info->name, $info->module, $info->has_title, $info->title_label, $info->has_body, $info->body_label, $info->description, $info->help, $info->min_word_count, $info->custom, $info->modified, $info->locked, $info->orig_type);
module_invoke_all('node_type', 'insert', $info);
return SAVED_NEW;
}
}
function node_type_delete($type) {
$info = node_get_types('type', $type);
db_query("DELETE FROM {node_type} WHERE type = '%s'", $type);
module_invoke_all('node_type', 'delete', $info);
}
function node_type_update_nodes($old_type, $type) {
db_query("UPDATE {node} SET type = '%s' WHERE type = '%s'", $type, $old_type);
return db_affected_rows();
}
function _node_types_build() {
$_node_types = array();
$_node_names = array();
$info_array = module_invoke_all('node_info');
foreach ($info_array as $type => $info) {
$info['type'] = $type;
$_node_types[$type] = (object) _node_type_set_defaults($info);
$_node_names[$type] = $info['name'];
}
$type_result = db_query(db_rewrite_sql('SELECT nt.type, nt.* FROM {node_type} nt ORDER BY nt.type ASC', 'nt', 'type'));
while ($type_object = db_fetch_object($type_result)) {
if ($type_object->module != 'node' && empty($info_array[$type_object->type])) {
$type_object->disabled = TRUE;
}
if (!isset($_node_types[$type_object->type]) || $type_object->modified) {
$_node_types[$type_object->type] = $type_object;
$_node_names[$type_object->type] = $type_object->name;
if ($type_object->type != $type_object->orig_type) {
unset($_node_types[$type_object->orig_type]);
unset($_node_names[$type_object->orig_type]);
}
}
}
asort($_node_names);
return array($_node_types, $_node_names);
}
function _node_type_set_defaults($info) {
if (!isset($info['has_title'])) {
$info['has_title'] = TRUE;
}
if ($info['has_title'] && !isset($info['title_label'])) {
$info['title_label'] = t('Title');
}
if (!isset($info['has_body'])) {
$info['has_body'] = TRUE;
}
if ($info['has_body'] && !isset($info['body_label'])) {
$info['body_label'] = t('Body');
}
if (!isset($info['help'])) {
$info['help'] = '';
}
if (!isset($info['min_word_count'])) {
$info['min_word_count'] = 0;
}
if (!isset($info['custom'])) {
$info['custom'] = FALSE;
}
if (!isset($info['modified'])) {
$info['modified'] = FALSE;
}
if (!isset($info['locked'])) {
$info['locked'] = TRUE;
}
$info['orig_type'] = $info['type'];
$info['is_new'] = TRUE;
return $info;
}
function node_hook(&$node, $hook) {
$module = node_get_types('module', $node);
if ($module == 'node') {
$module = 'node_content'; }
return module_hook($module, $hook);
}
function node_invoke(&$node, $hook, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
if (node_hook($node, $hook)) {
$module = node_get_types('module', $node);
if ($module == 'node') {
$module = 'node_content'; }
$function = $module .'_'. $hook;
return ($function($node, $a2, $a3, $a4));
}
}
function node_invoke_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$return = array();
foreach (module_implements('nodeapi') as $name) {
$function = $name .'_nodeapi';
$result = $function($node, $op, $a3, $a4);
if (isset($result) && is_array($result)) {
$return = array_merge($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
function node_load($param = array(), $revision = NULL, $reset = NULL) {
static $nodes = array();
if ($reset) {
$nodes = array();
}
$cachable = ($revision == NULL);
$arguments = array();
if (is_numeric($param)) {
if ($cachable) {
if (isset($nodes[$param])) {
return is_object($nodes[$param]) ? drupal_clone($nodes[$param]) : $nodes[$param];
}
}
$cond = 'n.nid = %d';
$arguments[] = $param;
}
elseif (is_array($param)) {
foreach ($param as $key => $value) {
$cond[] = 'n.'. db_escape_table($key) ." = '%s'";
$arguments[] = $value;
}
$cond = implode(' AND ', $cond);
}
else {
return FALSE;
}
$fields = drupal_schema_fields_sql('node', 'n');
$fields = array_merge($fields, drupal_schema_fields_sql('node_revisions', 'r'));
$fields = array_merge($fields, array('u.name', 'u.picture', 'u.data'));
$fields = array_diff($fields, array('n.vid', 'n.title', 'r.nid'));
$fields = implode(', ', $fields);
$fields = str_replace('r.timestamp', 'r.timestamp AS revision_timestamp', $fields);
$fields = str_replace('r.uid', 'r.uid AS revision_uid', $fields);
if ($revision) {
array_unshift($arguments, $revision);
$node = db_fetch_object(db_query('SELECT '. $fields .' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE '. $cond, $arguments));
}
else {
$node = db_fetch_object(db_query('SELECT '. $fields .' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE '. $cond, $arguments));
}
if ($node && $node->nid) {
if ($extra = node_invoke($node, 'load')) {
foreach ($extra as $key => $value) {
$node->$key = $value;
}
}
if ($extra = node_invoke_nodeapi($node, 'load')) {
foreach ($extra as $key => $value) {
$node->$key = $value;
}
}
if ($cachable) {
$nodes[$node->nid] = is_object($node) ? drupal_clone($node) : $node;
}
}
return $node;
}
function node_validate($node, $form = array()) {
$node = (object)$node;
$type = node_get_types('type', $node);
if (!empty($type->min_word_count) && isset($node->body) && count(explode(' ', $node->body)) < $type->min_word_count) {
form_set_error('body', t('The body of your @type is too short. You need at least %words words.', array('%words' => $type->min_word_count, '@type' => $type->name)));
}
if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
form_set_error('changed', t('This content has been modified by another user, changes cannot be saved.'));
}
if (user_access('administer nodes')) {
if (!empty($node->name) && !($account = user_load(array('name' => $node->name)))) {
form_set_error('name', t('The username %name does not exist.', array('%name' => $node->name)));
}
if (!empty($node->date) && strtotime($node->date) <= 0) {
form_set_error('date', t('You have to specify a valid date.'));
}
}
node_invoke($node, 'validate', $form);
node_invoke_nodeapi($node, 'validate', $form);
}
function node_submit($node) {
global $user;
$node = (object)$node;
if (!isset($node->teaser)) {
if (isset($node->body)) {
$node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
if (isset($node->teaser_include) && !$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
$node->body = substr($node->body, strlen($node->teaser));
}
}
else {
$node->teaser = '';
}
}
if (user_access('administer nodes')) {
if ($account = user_load(array('name' => $node->name))) {
$node->uid = $account->uid;
}
else {
$node->uid = 0;
}
}
$node->created = !empty($node->date) ? strtotime($node->date) : time();
$node->validated = TRUE;
return $node;
}
function node_save(&$node) {
node_invoke_nodeapi($node, 'presave');
global $user;
$node->is_new = FALSE;
if (empty($node->nid)) {
$node->is_new = TRUE;
if (!isset($node->log)) {
$node->log = '';
}
if (!isset($node->teaser)) {
$node->teaser = '';
}
if (!isset($node->body)) {
$node->body = '';
}
}
elseif (!empty($node->revision)) {
$node->old_vid = $node->vid;
}
else {
if (empty($node->log)) {
unset($node->log);
}
}
if (empty($node->created)) {
$node->created = time();
}
$node->changed = time();
$node->timestamp = time();
$node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT;
$update_node = TRUE;
if ($node->is_new) {
_node_save_revision($node, $user->uid);
drupal_write_record('node', $node);
db_query('UPDATE {node_revisions} SET nid = %d WHERE vid = %d', $node->nid, $node->vid);
$op = 'insert';
}
else {
drupal_write_record('node', $node, 'nid');
if (!empty($node->revision)) {
_node_save_revision($node, $user->uid);
db_query('UPDATE {node} SET vid = %d WHERE nid = %d', $node->vid, $node->nid);
}
else {
_node_save_revision($node, $user->uid, 'vid');
$update_node = FALSE;
}
$op = 'update';
}
node_invoke($node, $op);
node_invoke_nodeapi($node, $op);
node_access_acquire_grants($node);
cache_clear_all();
}
function _node_save_revision(&$node, $uid, $update = NULL) {
$temp_uid = $node->uid;
$node->uid = $uid;
if (isset($update)) {
drupal_write_record('node_revisions', $node, $update);
}
else {
drupal_write_record('node_revisions', $node);
}
$node->uid = $temp_uid;
}
function node_delete($nid) {
$node = node_load($nid);
if (node_access('delete', $node)) {
db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
node_invoke($node, 'delete');
node_invoke_nodeapi($node, 'delete');
cache_clear_all();
if (function_exists('search_wipe')) {
search_wipe($node->nid, 'node');
}
watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title));
drupal_set_message(