/[drupal]/drupal/modules/node.module
ViewVC logotype

Contents of /drupal/modules/node.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.657 - (hide annotations) (download) (as text)
Wed Jul 12 14:30:01 2006 UTC (3 years, 4 months ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-6-0-BETA-4, DRUPAL-6-0-RC-2, DRUPAL-6-0-RC-3, DRUPAL-6-0-BETA-1, DRUPAL-5-0-BETA-1, DRUPAL-5-0-BETA-2, DRUPAL-6-0-BETA-2, DRUPAL-6-0-BETA-3, DRUPAL-5-0-RC-1, DRUPAL-5-0-RC-2, DRUPAL-6-0-RC-1, HEAD
Changes since 1.656: +1 -1 lines
File MIME type: text/x-php
FILE REMOVED
#64280: Renamed from node.module to node/node.module
1 dries 1.1 <?php
2 dries 1.657 // $Id: node.module,v 1.656 2006/07/10 19:27:52 dries Exp $
3 dries 1.389
4     /**
5     * @file
6     * The core that allows content to be submitted to the site.
7     */
8 kjartan 1.321
9     define('NODE_NEW_LIMIT', time() - 30 * 24 * 60 * 60);
10 dries 1.1
11 dries 1.362 /**
12     * Implementation of hook_help().
13     */
14     function node_help($section) {
15 dries 1.248 switch ($section) {
16 dries 1.277 case 'admin/help#node':
17 dries 1.544 $output = '<p>'. t('All content in a website is stored and treated as <b>nodes</b>. Therefore nodes are any postings such as blogs, stories, polls and forums. The node module manages these content types and is one of the strengths of Drupal over other content management systems.') .'</p>';
18     $output .= '<p>'. t('Treating all content as nodes allows the flexibility of creating new types of content. It also allows you to painlessly apply new features or changes to all content. Comments are not stored as nodes but are always associated with a node.') .'</p>';
19     $output .= t('<p>Node module features</p>
20     <ul>
21     <li>The list tab provides an interface to search and sort all content on your site.</li>
22     <li>The configure settings tab has basic settings for content on your site.</li>
23     <li>The configure content types tab lists all content types for your site and lets you configure their default workflow.</li>
24     <li>The search tab lets you search all content on your site</li>
25     </ul>
26     ');
27     $output .= t('<p>You can</p>
28     <ul>
29     <li>search for content at <a href="%search">search</a>.</li>
30 dries 1.593 <li>administer nodes at <a href="%admin-settings-content-types">administer &gt;&gt; settings &gt;&gt; content types</a>.</li>
31 dries 1.544 </ul>
32 dries 1.593 ', array('%search' => url('search'), '%admin-settings-content-types' => url('admin/settings/content-types')));
33 dries 1.606 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%node">Node page</a>.', array('%node' => 'http://drupal.org/handbook/modules/node/')) .'</p>';
34 dries 1.362 return $output;
35 dries 1.355 case 'admin/modules#description':
36 dries 1.484 return t('Allows content to be submitted to the site and displayed on pages.');
37 dries 1.248 case 'admin/node':
38 unconed 1.457 return t('<p>Below is a list of all of the posts on your site. Other forms of content are listed elsewhere (e.g. <a href="%comments">comments</a>).</p><p>Clicking a title views the post, while clicking an author\'s name views their user information.</p>', array('%comments' => url('admin/comment')));
39 dries 1.248 case 'admin/node/search':
40 dries 1.424 return t('<p>Enter a simple pattern to search for a post. This can include the wildcard character *.<br />For example, a search for "br*" might return "bread bakers", "our daily bread" and "brenda".</p>');
41 dries 1.426 }
42    
43     if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'revisions') {
44     return t('The revisions let you track differences between multiple versions of a post.');
45 dries 1.28 }
46 dries 1.451
47     if (arg(0) == 'node' && arg(1) == 'add' && $type = arg(2)) {
48 unconed 1.626 return filter_xss_admin(variable_get($type .'_help', ''));
49 dries 1.451 }
50 kjartan 1.138 }
51    
52 dries 1.362 /**
53     * Implementation of hook_cron().
54     */
55 kjartan 1.321 function node_cron() {
56 dries 1.324 db_query('DELETE FROM {history} WHERE timestamp < %d', NODE_NEW_LIMIT);
57 dries 1.293 }
58    
59 dries 1.362 /**
60     * Gather a listing of links to nodes.
61     *
62     * @param $result
63 drumm 1.646 * A DB result object from a query to fetch node objects. If your query joins the <code>node_comment_statistics</code> table so that the <code>comment_count</code> field is available, a title attribute will be added to show the number of comments.
64 dries 1.362 * @param $title
65     * A heading for the resulting list.
66     *
67     * @return
68     * An HTML list suitable as content for a block.
69     */
70 dries 1.155 function node_title_list($result, $title = NULL) {
71     while ($node = db_fetch_object($result)) {
72 dries 1.417 $items[] = l($node->title, 'node/'. $node->nid, $node->comment_count ? array('title' => format_plural($node->comment_count, '1 comment', '%count comments')) : '');
73 dries 1.155 }
74 dries 1.158
75 dries 1.324 return theme('node_list', $items, $title);
76 dries 1.206 }
77    
78 dries 1.362 /**
79     * Format a listing of links to nodes.
80     */
81 dries 1.207 function theme_node_list($items, $title = NULL) {
82 dries 1.324 return theme('item_list', $items, $title);
83 dries 1.102 }
84    
85 dries 1.362 /**
86     * Update the 'last viewed' timestamp of the specified node for current user.
87     */
88 dries 1.198 function node_tag_new($nid) {
89     global $user;
90    
91     if ($user->uid) {
92 dries 1.369 if (node_last_viewed($nid)) {
93 dries 1.324 db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid);
94 dries 1.198 }
95     else {
96 dries 1.406 @db_query('INSERT INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)', $user->uid, $nid, time());
97 dries 1.198 }
98     }
99     }
100    
101 dries 1.362 /**
102     * Retrieves the timestamp at which the current user last viewed the
103     * specified node.
104     */
105 dries 1.198 function node_last_viewed($nid) {
106     global $user;
107 dries 1.369 static $history;
108 dries 1.198
109 dries 1.369 if (!isset($history[$nid])) {
110     $history[$nid] = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = '$user->uid' AND nid = %d", $nid));
111     }
112    
113 dries 1.538 return (isset($history[$nid]->timestamp) ? $history[$nid]->timestamp : 0);
114 dries 1.198 }
115    
116     /**
117 dries 1.454 * Decide on the type of marker to be displayed for a given node.
118 dries 1.198 *
119 dries 1.362 * @param $nid
120     * Node ID whose history supplies the "last viewed" timestamp.
121     * @param $timestamp
122     * Time which is compared against node's "last viewed" timestamp.
123 dries 1.454 * @return
124     * One of the MARK constants.
125 dries 1.299 */
126 dries 1.454 function node_mark($nid, $timestamp) {
127 dries 1.198 global $user;
128     static $cache;
129    
130 dries 1.454 if (!$user->uid) {
131     return MARK_READ;
132     }
133 dries 1.202 if (!isset($cache[$nid])) {
134 dries 1.454 $cache[$nid] = node_last_viewed($nid);
135     }
136     if ($cache[$nid] == 0 && $timestamp > NODE_NEW_LIMIT) {
137     return MARK_NEW;
138     }
139     elseif ($timestamp > $cache[$nid] && $timestamp > NODE_NEW_LIMIT) {
140     return MARK_UPDATED;
141 dries 1.198 }
142 dries 1.454 return MARK_READ;
143 dries 1.198 }
144    
145 dries 1.362 /**
146 unconed 1.494 * Automatically generate a teaser for a node body in a given format.
147 dries 1.362 */
148 unconed 1.494 function node_teaser($body, $format = NULL) {
149 dries 1.102
150 dries 1.324 $size = variable_get('teaser_length', 600);
151 dries 1.160
152 dries 1.373 // find where the delimiter is in the body
153     $delimiter = strpos($body, '<!--break-->');
154    
155 unconed 1.494 // If the size is zero, and there is no delimiter, the entire body is the teaser.
156 drumm 1.615 if ($size == 0 && $delimiter === FALSE) {
157 dries 1.160 return $body;
158     }
159 dries 1.102
160 unconed 1.494 // We check for the presence of the PHP evaluator filter in the current
161     // format. If the body contains PHP code, we do not split it up to prevent
162     // parse errors.
163     if (isset($format)) {
164     $filters = filter_list_format($format);
165 drumm 1.615 if (isset($filters['filter/1']) && strpos($body, '<?') !== FALSE) {
166 unconed 1.494 return $body;
167     }
168 unconed 1.377 }
169    
170 dries 1.362 // If a valid delimiter has been specified, use it to chop of the teaser.
171 drumm 1.615 if ($delimiter !== FALSE) {
172 dries 1.160 return substr($body, 0, $delimiter);
173     }
174    
175 unconed 1.494 // If we have a short body, the entire body is the teaser.
176 dries 1.238 if (strlen($body) < $size) {
177     return $body;
178     }
179    
180 dries 1.362 // In some cases, no delimiter has been specified (e.g. when posting using
181     // the Blogger API). In this case, we try to split at paragraph boundaries.
182 unconed 1.494 // When even the first paragraph is too long, we try to split at the end of
183 dries 1.362 // the next sentence.
184 unconed 1.639 $breakpoints = array('</p>' => 4, '<br />' => 0, '<br>' => 0, "\n" => 0, '. ' => 1, '! ' => 1, '? ' => 1, '。' => 3, '؟ ' => 1);
185 unconed 1.494 foreach ($breakpoints as $point => $charnum) {
186     if ($length = strpos($body, $point, $size)) {
187     return substr($body, 0, $length + $charnum);
188     }
189 dries 1.160 }
190 dries 1.496
191 unconed 1.494 // If all else fails, we simply truncate the string.
192 unconed 1.346 return truncate_utf8($body, $size);
193 dries 1.102 }
194    
195 dries 1.524 function _node_names($op = '', $node = NULL) {
196 dries 1.563 static $node_names = array();
197     static $node_list = array();
198 dries 1.524
199 dries 1.563 if (empty($node_names)) {
200 dries 1.525 $node_names = module_invoke_all('node_info');
201 dries 1.524 foreach ($node_names as $type => $value) {
202     $node_list[$type] = $value['name'];
203     }
204     }
205     if ($node) {
206     if (is_array($node)) {
207     $type = $node['type'];
208     }
209     elseif (is_object($node)) {
210     $type = $node->type;
211     }
212     elseif (is_string($node)) {
213     $type = $node;
214     }
215     if (!isset($node_names[$type])) {
216     return FALSE;
217     }
218     }
219     switch ($op) {
220     case 'base':
221     return $node_names[$type]['base'];
222     case 'list':
223     return $node_list;
224     case 'name':
225     return $node_list[$type];
226     }
227     }
228    
229 kjartan 1.327 /**
230 dries 1.524 * Determine the basename for hook_load etc.
231 dries 1.318 *
232 dries 1.524 * @param $node
233 dries 1.362 * Either a node object, a node array, or a string containing the node type.
234 dries 1.318 * @return
235 dries 1.524 * The basename for hook_load, hook_nodeapi etc.
236 dries 1.318 */
237 dries 1.524 function node_get_base($node) {
238     return _node_names('base', $node);
239     }
240 dries 1.505
241 dries 1.524 /**
242     * Determine the human readable name for a given type.
243     *
244     * @param $node
245     * Either a node object, a node array, or a string containing the node type.
246     * @return
247     * The human readable name of the node type.
248     */
249     function node_get_name($node) {
250     return _node_names('name', $node);
251 dries 1.320 }
252 dries 1.318
253 kjartan 1.327 /**
254 dries 1.524 * Return the list of available node types.
255 dries 1.318 *
256 dries 1.524 * @param $node
257     * Either a node object, a node array, or a string containing the node type.
258 dries 1.318 * @return
259 dries 1.535 * An array consisting ('#type' => name) pairs.
260 dries 1.318 */
261 dries 1.524 function node_get_types() {
262     return _node_names('list');
263 dries 1.320 }
264 dries 1.318
265 kjartan 1.327 /**
266 dries 1.318 * Determine whether a node hook exists.
267     *
268     * @param &$node
269     * Either a node object, node array, or a string containing the node type.
270     * @param $hook
271     * A string containing the name of the hook.
272     * @return
273     * TRUE iff the $hook exists in the node type of $node.
274     */
275     function node_hook(&$node, $hook) {
276 dries 1.524 return module_hook(node_get_base($node), $hook);
277 dries 1.318 }
278    
279 kjartan 1.327 /**
280 dries 1.318 * Invoke a node hook.
281     *
282     * @param &$node
283     * Either a node object, node array, or a string containing the node type.
284     * @param $hook
285     * A string containing the name of the hook.
286     * @param $a2, $a3, $a4
287     * Arguments to pass on to the hook, after the $node argument.
288     * @return
289 dries 1.362 * The returned value of the invoked hook.
290 dries 1.318 */
291     function node_invoke(&$node, $hook, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
292 dries 1.524 if (node_hook($node, $hook)) {
293     $function = node_get_base($node) ."_$hook";
294 dries 1.258 return ($function($node, $a2, $a3, $a4));
295 dries 1.102 }
296     }
297    
298 dries 1.362 /**
299     * Invoke a hook_nodeapi() operation in all modules.
300     *
301     * @param &$node
302 dries 1.476 * A node object.
303 dries 1.362 * @param $op
304     * A string containing the name of the nodeapi operation.
305     * @param $a3, $a4
306     * Arguments to pass on to the hook, after the $node and $op arguments.
307     * @return
308     * The returned value of the invoked hooks.
309     */
310 dries 1.350 function node_invoke_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
311 dries 1.264 $return = array();
312 dries 1.524 foreach (module_implements('nodeapi') as $name) {
313 dries 1.324 $function = $name .'_nodeapi';
314 dries 1.524 $result = $function($node, $op, $a3, $a4);
315 dries 1.565 if (isset($result) && is_array($result)) {
316 dries 1.524 $return = array_merge($return, $result);
317     }
318     else if (isset($result)) {
319     $return[] = $result;
320 dries 1.264 }
321     }
322     return $return;
323     }
324    
325 dries 1.362 /**
326     * Load a node object from the database.
327     *
328 dries 1.504 * @param $param
329     * Either the nid of the node or an array of conditions to match against in the database query
330 dries 1.362 * @param $revision
331     * Which numbered revision to load. Defaults to the current version.
332 dries 1.420 * @param $reset
333     * Whether to reset the internal node_load cache.
334 dries 1.362 *
335     * @return
336     * A fully-populated node object.
337     */
338 dries 1.504 function node_load($param = array(), $revision = NULL, $reset = NULL) {
339 dries 1.420 static $nodes = array();
340    
341     if ($reset) {
342     $nodes = array();
343     }
344    
345 unconed 1.579 $arguments = array();
346 dries 1.504 if (is_numeric($param)) {
347     $cachable = $revision == NULL;
348 dries 1.538 if ($cachable && isset($nodes[$param])) {
349 dries 1.504 return $nodes[$param];
350     }
351 unconed 1.579 $cond = 'n.nid = %d';
352     $arguments[] = $param;
353 dries 1.420 }
354 dries 1.504 else {
355     // Turn the conditions into a query.
356 unconed 1.508 foreach ($param as $key => $value) {
357 unconed 1.579 $cond[] = 'n.'. db_escape_string($key) ." = '%s'";
358     $arguments[] = $value;
359 dries 1.504 }
360     $cond = implode(' AND ', $cond);
361 dries 1.102 }
362    
363 dries 1.362 // Retrieve the node.
364 dries 1.526 if ($revision) {
365 unconed 1.579 array_unshift($arguments, $revision);
366 dries 1.655 $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, r.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data 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));
367 dries 1.526 }
368     else {
369 dries 1.655 $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data 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));
370 dries 1.102 }
371    
372 dries 1.526 if ($node->nid) {
373     // Call the node specific callback (if any) and piggy-back the
374     // results to the node or overwrite some values.
375     if ($extra = node_invoke($node, 'load')) {
376     foreach ($extra as $key => $value) {
377     $node->$key = $value;
378     }
379 dries 1.102 }
380    
381 dries 1.526 if ($extra = node_invoke_nodeapi($node, 'load')) {
382     foreach ($extra as $key => $value) {
383     $node->$key = $value;
384     }
385 dries 1.350 }
386     }
387    
388 dries 1.420 if ($cachable) {
389 dries 1.504 $nodes[$param] = $node;
390 dries 1.220 }
391    
392 dries 1.102 return $node;
393     }
394    
395 dries 1.362 /**
396     * Save a node object into the database.
397     */
398 unconed 1.527 function node_save(&$node) {
399 dries 1.526 global $user;
400 dries 1.102
401 dries 1.654 $node->is_new = FALSE;
402 dries 1.102
403 dries 1.362 // Apply filters to some default node fields:
404 dries 1.102 if (empty($node->nid)) {
405 dries 1.362 // Insert a new node.
406 dries 1.654 $node->is_new = TRUE;
407 dries 1.102
408 dries 1.526 $node->nid = db_next_id('{node}_nid');
409     $node->vid = db_next_id('{node_revisions}_vid');;
410     }
411     else {
412     // We need to ensure that all node fields are filled.
413     $node_current = node_load($node->nid);
414     foreach ($node as $field => $data) {
415     $node_current->$field = $data;
416     }
417     $node = $node_current;
418    
419     if ($node->revision) {
420     $node->old_vid = $node->vid;
421     $node->vid = db_next_id('{node_revisions}_vid');
422 dries 1.349 }
423 dries 1.526 }
424    
425 unconed 1.629 // Set some required fields:
426     if (empty($node->created)) {
427     $node->created = time();
428     }
429 unconed 1.569 // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
430 unconed 1.629 $node->changed = time();
431 dries 1.102
432 dries 1.526 // Split off revisions data to another structure
433     $revisions_table_values = array('nid' => $node->nid, 'vid' => $node->vid,
434     'title' => $node->title, 'body' => $node->body,
435     'teaser' => $node->teaser, 'log' => $node->log, 'timestamp' => $node->changed,
436     'uid' => $user->uid, 'format' => $node->format);
437     $revisions_table_types = array('nid' => '%d', 'vid' => '%d',
438     'title' => "'%s'", 'body' => "'%s'",
439     'teaser' => "'%s'", 'log' => "'%s'", 'timestamp' => '%d',
440     'uid' => '%d', 'format' => '%d');
441     $node_table_values = array('nid' => $node->nid, 'vid' => $node->vid,
442     'title' => $node->title, 'type' => $node->type, 'uid' => $node->uid,
443     'status' => $node->status, 'created' => $node->created,
444     'changed' => $node->changed, 'comment' => $node->comment,
445 dries 1.655 'promote' => $node->promote, 'sticky' => $node->sticky);
446 dries 1.526 $node_table_types = array('nid' => '%d', 'vid' => '%d',
447     'title' => "'%s'", 'type' => "'%s'", 'uid' => '%d',
448     'status' => '%d', 'created' => '%d',
449     'changed' => '%d', 'comment' => '%d',
450 dries 1.655 'promote' => '%d', 'sticky' => '%d');
451 dries 1.526
452     //Generate the node table query and the
453     //the node_revisions table query
454     if ($node->is_new) {
455     $node_query = 'INSERT INTO {node} ('. implode(', ', array_keys($node_table_types)) .') VALUES ('. implode(', ', $node_table_types) .')';
456     $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')';
457     }
458     else {
459     $arr = array();
460     foreach ($node_table_types as $key => $value) {
461     $arr[] = $key .' = '. $value;
462     }
463     $node_table_values[] = $node->nid;
464     $node_query = 'UPDATE {node} SET '. implode(', ', $arr) .' WHERE nid = %d';
465     if ($node->revision) {
466     $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')';
467     }
468     else {
469     $arr = array();
470     foreach ($revisions_table_types as $key => $value) {
471     $arr[] = $key .' = '. $value;
472 dries 1.102 }
473 dries 1.526 $revisions_table_values[] = $node->vid;
474     $revisions_query = 'UPDATE {node_revisions} SET '. implode(', ', $arr) .' WHERE vid = %d';
475 dries 1.102 }
476 dries 1.526 }
477 dries 1.102
478 dries 1.526 // Insert the node into the database:
479     db_query($node_query, $node_table_values);
480     db_query($revisions_query, $revisions_table_values);
481 dries 1.102
482 dries 1.526 // Call the node specific callback (if any):
483     if ($node->is_new) {
484 dries 1.324 node_invoke($node, 'insert');
485     node_invoke_nodeapi($node, 'insert');
486 dries 1.102 }
487     else {
488 dries 1.324 node_invoke($node, 'update');
489     node_invoke_nodeapi($node, 'update');
490 dries 1.102 }
491 dries 1.156
492 dries 1.362 // Clear the cache so an anonymous poster can see the node being added or updated.
493 dries 1.156 cache_clear_all();
494 dries 1.102 }
495    
496 dries 1.362 /**
497     * Generate a display of the given node.
498     *
499     * @param $node
500     * A node array or node object.
501     * @param $teaser
502 dries 1.517 * Whether to display the teaser only, as on the main page.
503 dries 1.362 * @param $page
504     * Whether the node is being displayed by itself as a page.
505 dries 1.425 * @param $links
506     * Whether or not to display node links. Links are omitted for node previews.
507 dries 1.362 *
508     * @return
509     * An HTML representation of the themed node.
510     */
511 dries 1.425 function node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) {
512 dries 1.576 $node = (object)$node;
513 dries 1.102
514 dries 1.411 // Remove the delimiter (if any) that separates the teaser from the body.
515 dries 1.362 // TODO: this strips legitimate uses of '<!--break-->' also.
516 dries 1.324 $node->body = str_replace('<!--break-->', '', $node->body);
517 dries 1.160
518 dries 1.655 if ($node->log != '' && !$teaser) {
519 dries 1.585 $node->body .= '<div class="log"><div class="title">'. t('Log') .':</div>'. filter_xss($node->log) .'</div>';
520 dries 1.526 }
521    
522 dries 1.362 // The 'view' hook can be implemented to overwrite the default function
523     // to display nodes.
524 dries 1.324 if (node_hook($node, 'view')) {
525 dries 1.370 node_invoke($node, 'view', $teaser, $page);
526 dries 1.102 }
527     else {
528 dries 1.370 $node = node_prepare($node, $teaser);
529 dries 1.309 }
530 dries 1.370 // Allow modules to change $node->body before viewing.
531     node_invoke_nodeapi($node, 'view', $teaser, $page);
532 dries 1.425 if ($links) {
533     $node->links = module_invoke_all('link', 'node', $node, !$page);
534 dries 1.650
535     foreach (module_implements('link_alter') AS $module) {
536     $function = $module .'_link_alter';
537     $function($node, $node->links);
538     }
539 dries 1.425 }
540 dries 1.501 // unset unused $node part so that a bad theme can not open a security hole
541     if ($teaser) {
542     unset($node->body);
543     }
544     else {
545     unset($node->teaser);
546     }
547 dries 1.370
548     return theme('node', $node, $teaser, $page);
549 dries 1.309 }
550 dries 1.102
551 dries 1.362 /**
552     * Apply filters to a node in preparation for theming.
553     */
554 dries 1.355 function node_prepare($node, $teaser = FALSE) {
555 dries 1.341 $node->readmore = (strlen($node->teaser) < strlen($node->body));
556 dries 1.355 if ($teaser == FALSE) {
557 unconed 1.512 $node->body = check_markup($node->body, $node->format, FALSE);
558 dries 1.309 }
559     else {
560 unconed 1.512 $node->teaser = check_markup($node->teaser, $node->format, FALSE);
561 dries 1.259 }
562 dries 1.309 return $node;
563 dries 1.102 }
564    
565 dries 1.362 /**
566     * Generate a page displaying a single node, along with its comments.
567     */
568 dries 1.219 function node_show($node, $cid) {
569 dries 1.371 $output = node_view($node, FALSE, TRUE);
570 dries 1.174
571 dries 1.371 if (function_exists('comment_render') && $node->comment) {
572     $output .= comment_render($node, $cid);
573 dries 1.300 }
574 dries 1.174
575 dries 1.371 // Update the history table, stating that this user viewed this node.
576     node_tag_new($node->nid);
577 dries 1.76
578 dries 1.371 return $output;
579 dries 1.74 }
580    
581 dries 1.362 /**
582     * Implementation of hook_perm().
583     */
584 dries 1.39 function node_perm() {
585 dries 1.584 return array('administer nodes', 'access content', 'view revisions', 'revert revisions');
586 dries 1.39 }
587    
588 dries 1.362 /**
589     * Implementation of hook_search().
590     */
591 dries 1.654 function node_search($op = 'search', $keys = NULL) {
592 dries 1.413 switch ($op) {
593     case 'name':
594     return t('content');
595 unconed 1.537
596 dries 1.415 case 'reset':
597     variable_del('node_cron_last');
598 unconed 1.588 variable_del('node_cron_last_nid');
599 dries 1.415 return;
600 unconed 1.537
601 unconed 1.439 case 'status':
602     $last = variable_get('node_cron_last', 0);
603 unconed 1.588 $last_nid = variable_get('node_cron_last_nid', 0);
604 dries 1.594 $total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1'));
605     $remaining = db_result(db_query('SELECT COUNT(*) FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND ((GREATEST(n.created, n.changed, c.last_comment_timestamp) = %d AND n.nid > %d ) OR (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d))', $last, $last_nid, $last, $last, $last));
606 unconed 1.439 return array('remaining' => $remaining, 'total' => $total);
607 unconed 1.537
608     case 'admin':
609     $form = array();
610     // Output form for defining rank factor weights.
611     $form['content_ranking'] = array('#type' => 'fieldset', '#title' => t('Content ranking'));
612     $form['content_ranking']['#theme'] = 'node_search_admin';
613 drumm 1.646 $form['content_ranking']['info'] = array('#type' => 'markup', '#value' => '<em>'. t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') .'</em>');
614 unconed 1.537
615     $ranking = array('node_rank_relevance' => t('Keyword relevance'),
616     'node_rank_recent' => t('Recently posted'));
617     if (module_exist('comment')) {
618     $ranking['node_rank_comments'] = t('Number of comments');
619     }
620     if (module_exist('statistics') && variable_get('statistics_count_content_views', 0)) {
621     $ranking['node_rank_views'] = t('Number of views');
622     }
623    
624     // Note: reversed to reflect that higher number = higher ranking.
625     $options = drupal_map_assoc(range(0, 10));
626     foreach ($ranking as $var => $title) {
627     $form['content_ranking']['factors'][$var] = array('#title' => $title, '#type' => 'select', '#options' => $options, '#default_value' => variable_get($var, 5));
628     }
629     return $form;
630    
631 dries 1.413 case 'search':
632 unconed 1.537 // Build matching conditions
633     list($join1, $where1) = _db_rewrite_sql();
634     $arguments1 = array();
635     $conditions1 = 'n.status = 1';
636    
637     if ($type = search_query_extract($keys, 'type')) {
638     $types = array();
639     foreach (explode(',', $type) as $t) {
640     $types[] = "n.type = '%s'";
641     $arguments1[] = $t;
642     }
643     $conditions1 .= ' AND ('. implode(' OR ', $types) .')';
644     $keys = search_query_insert($keys, 'type');
645     }
646    
647     if ($category = search_query_extract($keys, 'category')) {
648     $categories = array();
649     foreach (explode(',', $category) as $c) {
650     $categories[] = "tn.tid = %d";
651     $arguments1[] = $c;
652     }
653     $conditions1 .= ' AND ('. implode(' OR ', $categories) .')';
654     $join1 .= ' INNER JOIN {term_node} tn ON n.nid = tn.nid';
655     $keys = search_query_insert($keys, 'category');
656     }
657    
658     // Build ranking expression (we try to map each parameter to a
659     // uniform distribution in the range 0..1).
660     $ranking = array();
661     $arguments2 = array();
662     $join2 = '';
663     // Used to avoid joining on node_comment_statistics twice
664 dries 1.654 $stats_join = FALSE;
665 unconed 1.537 if ($weight = (int)variable_get('node_rank_relevance', 5)) {
666     // Average relevance values hover around 0.15
667     $ranking[] = '%d * i.relevance';
668     $arguments2[] = $weight;
669     }
670     if ($weight = (int)variable_get('node_rank_recent', 5)) {
671     // Exponential decay with half-life of 6 months, starting at last indexed node
672     $ranking[] = '%d * POW(2, (GREATEST(n.created, n.changed, c.last_comment_timestamp) - %d) * 6.43e-8)';
673     $arguments2[] = $weight;
674     $arguments2[] = (int)variable_get('node_cron_last', 0);
675     $join2 .= ' INNER JOIN {node} n ON n.nid = i.sid LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
676 dries 1.654 $stats_join = TRUE;
677 unconed 1.537 }
678     if (module_exist('comment') && $weight = (int)variable_get('node_rank_comments', 5)) {
679     // Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
680     $scale = variable_get('node_cron_comments_scale', 0.0);
681     $ranking[] = '%d * (2.0 - 2.0 / (1.0 + c.comment_count * %f))';
682     $arguments2[] = $weight;
683     $arguments2[] = $scale;
684     if (!$stats_join) {
685     $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
686     }
687     }
688     if (module_exist('statistics') && variable_get('statistics_count_content_views', 0) &&
689     $weight = (int)variable_get('node_rank_views', 5)) {
690     // Inverse law that maps the highest view count on the site to 1 and 0 to 0.
691     $scale = variable_get('node_cron_views_scale', 0.0);
692     $ranking[] = '%d * (2.0 - 2.0 / (1.0 + nc.totalcount * %f))';
693     $arguments2[] = $weight;
694     $arguments2[] = $scale;
695 unconed 1.638 $join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid';
696 unconed 1.537 }
697     $select2 = (count($ranking) ? implode(' + ', $ranking) : 'i.relevance') . ' AS score';
698    
699     // Do search
700     $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join1 .' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2);
701    
702     // Load results
703 dries 1.413 $results = array();
704     foreach ($find as $item) {
705 unconed 1.609 $node = node_load($item->sid);
706 unconed 1.510
707     // Get node output (filtered and with module-specific fields).
708     if (node_hook($node, 'view')) {
709 dries 1.654 node_invoke($node, 'view', FALSE, FALSE);
710 unconed 1.510 }
711     else {
712 dries 1.654 $node = node_prepare($node, FALSE);
713 unconed 1.510 }
714     // Allow modules to change $node->body before viewing.
715 dries 1.654 node_invoke_nodeapi($node, 'view', FALSE, FALSE);
716 unconed 1.510
717 unconed 1.537 // Fetch comments for snippet
718     $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
719 unconed 1.641 // Fetch terms for snippet
720     $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');
721 unconed 1.537
722 dries 1.436 $extra = node_invoke_nodeapi($node, 'search result');
723 unconed 1.609 $results[] = array('link' => url('node/'. $item->sid),
724 dries 1.524 'type' => node_get_name($node),
725 dries 1.413 'title' => $node->title,
726 dries 1.514 'user' => theme('username', $node),
727 dries 1.413 'date' => $node->changed,
728 unconed 1.537 'node' => $node,
729 dries 1.436 'extra' => $extra,
730 unconed 1.511 'snippet' => search_excerpt($keys, $node->body));
731 dries 1.413 }
732     return $results;
733     }
734 dries 1.73 }
735    
736 killes 1.622 /**
737     * Implementation of hook_user().
738     */
739     function node_user($op, &$edit, &$user) {
740     if ($op == 'delete') {
741     db_query('UPDATE {node} SET uid = 0 WHERE uid = %d', $user->uid);
742     db_query('UPDATE {node_revisions} SET uid = 0 WHERE uid = %d', $user->uid);
743     }
744     }
745    
746 unconed 1.537 function theme_node_search_admin($form) {
747     $output = form_render($form['info']);
748    
749     $header = array(t('Factor'), t('Weight'));
750     foreach (element_children($form['factors']) as $key) {
751     $row = array();
752     $row[] = $form['factors'][$key]['#title'];
753     unset($form['factors'][$key]['#title']);
754     $row[] = form_render($form['factors'][$key]);
755     $rows[] = $row;
756     }
757     $output .= theme('table', $header, $rows);
758    
759     $output .= form_render($form);
760     return $output;
761     }
762    
763 dries 1.355 /**
764 dries 1.362 * Menu callback; presents general node configuration options.
765 dries 1.355 */
766     function node_configure() {
767    
768 dries 1.532 $form['default_nodes_main'] = array(
769 dries 1.535 '#type' => 'select', '#title' => t('Number of posts on main page'), '#default_value' => variable_get('default_nodes_main', 10),
770     '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
771     '#description' => t('The default maximum number of posts to display per page on overview pages such as the main page.')
772 dries 1.532 );
773    
774     $form['teaser_length'] = array(
775 dries 1.535 '#type' => 'select', '#title' => t('Length of trimmed posts'), '#default_value' => variable_get('teaser_length', 600),
776     '#options' => array(0 => t('Unlimited'), 200 => t('200 characters'), 400 => t('400 characters'), 600 => t('600 characters'),
777 dries 1.532 800 => t('800 characters'), 1000 => t('1000 characters'), 1200 => t('1200 characters'), 1400 => t('1400 characters'),
778     1600 => t('1600 characters'), 1800 => t('1800 characters'), 2000 => t('2000 characters')),
779 drumm 1.646 '#description' => t("The maximum number of characters used in the trimmed version of a post. Drupal will use this setting to determine at which offset long posts should be trimmed. The trimmed version of a post is typically used as a teaser when displaying the post on the main page, in XML feeds, etc. To disable teasers, set to 'Unlimited'. Note that this setting will only affect new or updated content and will not affect existing teasers.")
780 dries 1.532 );
781    
782     $form['node_preview'] = array(
783 dries 1.535 '#type' => 'radios', '#title' => t('Preview post'), '#default_value' => variable_get('node_preview', 0),
784     '#options' => array(t('Optional'), t('Required')), '#description' => t('Must users preview posts before submitting?')
785 dries 1.532 );
786 dries 1.237
787 dries 1.532 return system_settings_form('node_configure', $form);
788 dries 1.37 }
789    
790 dries 1.362 /**
791     * Retrieve the comment mode for the given node ID (none, read, or read/write).
792     */
793 dries 1.120 function node_comment_mode($nid) {
794 dries 1.142 static $comment_mode;
795     if (!isset($comment_mode[$nid])) {
796 dries 1.324 $comment_mode[$nid] = db_result(db_query('SELECT comment FROM {node} WHERE nid = %d', $nid));
797 dries 1.142 }
798     return $comment_mode[$nid];
799 dries 1.120 }
800    
801 dries 1.348 /**
802     * Implementation of hook_link().
803     */
804 kjartan 1.89 function node_link($type, $node = 0, $main = 0) {
805 dries 1.205 $links = array();
806    
807 dries 1.324 if ($type == 'node') {
808 dries 1.363 if (array_key_exists('links', $node)) {
809 kjartan 1.50 $links = $node->links;
810     }
811 dries 1.73
812 dries 1.341 if ($main == 1 && $node->teaser && $node->readmore) {
813 dries 1.650 $links['node_read_more'] = array(
814 drumm 1.653 'title' => t('read more'),
815     'href' => "node/$node->nid",
816     'attributes' => array('title'