Mercurial > defr > drupal > core
comparison modules/node/node.module @ 1:c1f4ac30525a 6.0
Drupal 6.0
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Tue, 23 Dec 2008 14:28:28 +0100 |
parents | |
children | 165d43f946a8 |
comparison
equal
deleted
inserted
replaced
0:5a113a1c4740 | 1:c1f4ac30525a |
---|---|
1 <?php | |
2 // $Id: node.module,v 1.947.2.2 2008/02/13 14:10:22 goba Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * The core that allows content to be submitted to the site. Modules and scripts may | |
7 * programmatically submit nodes using the usual form API pattern. | |
8 */ | |
9 | |
10 define('NODE_NEW_LIMIT', time() - 30 * 24 * 60 * 60); | |
11 | |
12 define('NODE_BUILD_NORMAL', 0); | |
13 define('NODE_BUILD_PREVIEW', 1); | |
14 define('NODE_BUILD_SEARCH_INDEX', 2); | |
15 define('NODE_BUILD_SEARCH_RESULT', 3); | |
16 define('NODE_BUILD_RSS', 4); | |
17 define('NODE_BUILD_PRINT', 5); | |
18 | |
19 /** | |
20 * Implementation of hook_help(). | |
21 */ | |
22 function node_help($path, $arg) { | |
23 // Remind site administrators about the {node_access} table being flagged | |
24 // for rebuild. We don't need to issue the message on the confirm form, or | |
25 // while the rebuild is being processed. | |
26 if ($path != 'admin/content/node-settings/rebuild' && $path != 'batch' && strpos($path, '#') === FALSE | |
27 && user_access('access administration pages') && node_access_needs_rebuild()) { | |
28 if ($path == 'admin/content/node-settings') { | |
29 $message = t('The content access permissions need to be rebuilt.'); | |
30 } | |
31 else { | |
32 $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'))); | |
33 } | |
34 drupal_set_message($message, 'error'); | |
35 } | |
36 | |
37 switch ($path) { | |
38 case 'admin/help#node': | |
39 $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>'; | |
40 $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>'; | |
41 $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>'; | |
42 $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>'; | |
43 return $output; | |
44 case 'admin/content/node': | |
45 return ' '; // Return a non-null value so that the 'more help' link is shown. | |
46 case 'admin/content/types': | |
47 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>'; | |
48 case 'admin/content/types/add': | |
49 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>'; | |
50 case 'node/%/revisions': | |
51 return '<p>'. t('The revisions let you track differences between multiple versions of a post.') .'</p>'; | |
52 case 'node/%/edit': | |
53 $node = node_load($arg[1]); | |
54 $type = node_get_types('type', $node->type); | |
55 return (!empty($type->help) ? '<p>'. filter_xss_admin($type->help) .'</p>' : ''); | |
56 } | |
57 | |
58 if ($arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) { | |
59 $type = node_get_types('type', str_replace('-', '_', $arg[2])); | |
60 return (!empty($type->help) ? '<p>'. filter_xss_admin($type->help) .'</p>' : ''); | |
61 } | |
62 } | |
63 | |
64 /** | |
65 * Implementation of hook_theme() | |
66 */ | |
67 function node_theme() { | |
68 return array( | |
69 'node' => array( | |
70 'arguments' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE), | |
71 'template' => 'node', | |
72 ), | |
73 'node_list' => array( | |
74 'arguments' => array('items' => NULL, 'title' => NULL), | |
75 ), | |
76 'node_search_admin' => array( | |
77 'arguments' => array('form' => NULL), | |
78 ), | |
79 'node_filter_form' => array( | |
80 'arguments' => array('form' => NULL), | |
81 'file' => 'node.admin.inc', | |
82 ), | |
83 'node_filters' => array( | |
84 'arguments' => array('form' => NULL), | |
85 'file' => 'node.admin.inc', | |
86 ), | |
87 'node_admin_nodes' => array( | |
88 'arguments' => array('form' => NULL), | |
89 'file' => 'node.admin.inc', | |
90 ), | |
91 'node_add_list' => array( | |
92 'arguments' => array('content' => NULL), | |
93 'file' => 'node.pages.inc', | |
94 ), | |
95 'node_form' => array( | |
96 'arguments' => array('form' => NULL), | |
97 'file' => 'node.pages.inc', | |
98 ), | |
99 'node_preview' => array( | |
100 'arguments' => array('node' => NULL), | |
101 'file' => 'node.pages.inc', | |
102 ), | |
103 'node_log_message' => array( | |
104 'arguments' => array('log' => NULL), | |
105 ), | |
106 'node_submitted' => array( | |
107 'arguments' => array('node' => NULL), | |
108 ), | |
109 ); | |
110 } | |
111 | |
112 /** | |
113 * Implementation of hook_cron(). | |
114 */ | |
115 function node_cron() { | |
116 db_query('DELETE FROM {history} WHERE timestamp < %d', NODE_NEW_LIMIT); | |
117 } | |
118 | |
119 /** | |
120 * Gather a listing of links to nodes. | |
121 * | |
122 * @param $result | |
123 * A DB result object from a query to fetch node objects. If your query | |
124 * joins the <code>node_comment_statistics</code> table so that the | |
125 * <code>comment_count</code> field is available, a title attribute will | |
126 * be added to show the number of comments. | |
127 * @param $title | |
128 * A heading for the resulting list. | |
129 * | |
130 * @return | |
131 * An HTML list suitable as content for a block, or FALSE if no result can | |
132 * fetch from DB result object. | |
133 */ | |
134 function node_title_list($result, $title = NULL) { | |
135 $items = array(); | |
136 $num_rows = FALSE; | |
137 while ($node = db_fetch_object($result)) { | |
138 $items[] = l($node->title, 'node/'. $node->nid, !empty($node->comment_count) ? array('title' => format_plural($node->comment_count, '1 comment', '@count comments')) : array()); | |
139 $num_rows = TRUE; | |
140 } | |
141 | |
142 return $num_rows ? theme('node_list', $items, $title) : FALSE; | |
143 } | |
144 | |
145 /** | |
146 * Format a listing of links to nodes. | |
147 * | |
148 * @ingroup themeable | |
149 */ | |
150 function theme_node_list($items, $title = NULL) { | |
151 return theme('item_list', $items, $title); | |
152 } | |
153 | |
154 /** | |
155 * Update the 'last viewed' timestamp of the specified node for current user. | |
156 */ | |
157 function node_tag_new($nid) { | |
158 global $user; | |
159 | |
160 if ($user->uid) { | |
161 if (node_last_viewed($nid)) { | |
162 db_query('UPDATE {history} SET timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid); | |
163 } | |
164 else { | |
165 @db_query('INSERT INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)', $user->uid, $nid, time()); | |
166 } | |
167 } | |
168 } | |
169 | |
170 /** | |
171 * Retrieves the timestamp at which the current user last viewed the | |
172 * specified node. | |
173 */ | |
174 function node_last_viewed($nid) { | |
175 global $user; | |
176 static $history; | |
177 | |
178 if (!isset($history[$nid])) { | |
179 $history[$nid] = db_fetch_object(db_query("SELECT timestamp FROM {history} WHERE uid = %d AND nid = %d", $user->uid, $nid)); | |
180 } | |
181 | |
182 return (isset($history[$nid]->timestamp) ? $history[$nid]->timestamp : 0); | |
183 } | |
184 | |
185 /** | |
186 * Decide on the type of marker to be displayed for a given node. | |
187 * | |
188 * @param $nid | |
189 * Node ID whose history supplies the "last viewed" timestamp. | |
190 * @param $timestamp | |
191 * Time which is compared against node's "last viewed" timestamp. | |
192 * @return | |
193 * One of the MARK constants. | |
194 */ | |
195 function node_mark($nid, $timestamp) { | |
196 global $user; | |
197 static $cache; | |
198 | |
199 if (!$user->uid) { | |
200 return MARK_READ; | |
201 } | |
202 if (!isset($cache[$nid])) { | |
203 $cache[$nid] = node_last_viewed($nid); | |
204 } | |
205 if ($cache[$nid] == 0 && $timestamp > NODE_NEW_LIMIT) { | |
206 return MARK_NEW; | |
207 } | |
208 elseif ($timestamp > $cache[$nid] && $timestamp > NODE_NEW_LIMIT) { | |
209 return MARK_UPDATED; | |
210 } | |
211 return MARK_READ; | |
212 } | |
213 | |
214 /** | |
215 * See if the user used JS to submit a teaser. | |
216 */ | |
217 function node_teaser_js(&$form, &$form_state) { | |
218 if (isset($form['#post']['teaser_js'])) { | |
219 // Glue the teaser to the body. | |
220 if (trim($form_state['values']['teaser_js'])) { | |
221 // Space the teaser from the body | |
222 $body = trim($form_state['values']['teaser_js']) ."\r\n<!--break-->\r\n". trim($form_state['values']['body']); | |
223 } | |
224 else { | |
225 // Empty teaser, no spaces. | |
226 $body = '<!--break-->'. $form_state['values']['body']; | |
227 } | |
228 // Pass updated body value on to preview/submit form processing. | |
229 form_set_value($form['body'], $body, $form_state); | |
230 // Pass updated body value back onto form for those cases | |
231 // in which the form is redisplayed. | |
232 $form['body']['#value'] = $body; | |
233 } | |
234 return $form; | |
235 } | |
236 | |
237 /** | |
238 * Ensure value of "teaser_include" checkbox is consistent with other form data. | |
239 * | |
240 * This handles two situations in which an unchecked checkbox is rejected: | |
241 * | |
242 * 1. The user defines a teaser (summary) but it is empty; | |
243 * 2. The user does not define a teaser (summary) (in this case an | |
244 * unchecked checkbox would cause the body to be empty, or missing | |
245 * the auto-generated teaser). | |
246 * | |
247 * If JavaScript is active then it is used to force the checkbox to be | |
248 * checked when hidden, and so the second case will not arise. | |
249 * | |
250 * In either case a warning message is output. | |
251 */ | |
252 function node_teaser_include_verify(&$form, &$form_state) { | |
253 $message = ''; | |
254 | |
255 // $form['#post'] is set only when the form is built for preview/submit. | |
256 if (isset($form['#post']['body']) && isset($form_state['values']['teaser_include']) && !$form_state['values']['teaser_include']) { | |
257 // "teaser_include" checkbox is present and unchecked. | |
258 if (strpos($form_state['values']['body'], '<!--break-->') === 0) { | |
259 // Teaser is empty string. | |
260 $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.'); | |
261 } | |
262 elseif (strpos($form_state['values']['body'], '<!--break-->') === FALSE) { | |
263 // Teaser delimiter is not present in the body. | |
264 $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.)'); | |
265 } | |
266 | |
267 if (!empty($message)) { | |
268 drupal_set_message($message, 'warning'); | |
269 // Pass new checkbox value on to preview/submit form processing. | |
270 form_set_value($form['teaser_include'], 1, $form_state); | |
271 // Pass new checkbox value back onto form for those cases | |
272 // in which form is redisplayed. | |
273 $form['teaser_include']['#value'] = 1; | |
274 } | |
275 } | |
276 | |
277 return $form; | |
278 } | |
279 | |
280 /** | |
281 * Generate a teaser for a node body. | |
282 * | |
283 * If the end of the teaser is not indicated using the <!--break--> delimiter | |
284 * then we generate the teaser automatically, trying to end it at a sensible | |
285 * place such as the end of a paragraph, a line break, or the end of a | |
286 * sentence (in that order of preference). | |
287 * | |
288 * @param $body | |
289 * The content for which a teaser will be generated. | |
290 * @param $format | |
291 * The format of the content. If the content contains PHP code, we do not | |
292 * split it up to prevent parse errors. If the line break filter is present | |
293 * then we treat newlines embedded in $body as line breaks. | |
294 * @param $size | |
295 * The desired character length of the teaser. If omitted, the default | |
296 * value will be used. Ignored if the special delimiter is present | |
297 * in $body. | |
298 * @return | |
299 * The generated teaser. | |
300 */ | |
301 function node_teaser($body, $format = NULL, $size = NULL) { | |
302 | |
303 if (!isset($size)) { | |
304 $size = variable_get('teaser_length', 600); | |
305 } | |
306 | |
307 // Find where the delimiter is in the body | |
308 $delimiter = strpos($body, '<!--break-->'); | |
309 | |
310 // If the size is zero, and there is no delimiter, the entire body is the teaser. | |
311 if ($size == 0 && $delimiter === FALSE) { | |
312 return $body; | |
313 } | |
314 | |
315 // If a valid delimiter has been specified, use it to chop off the teaser. | |
316 if ($delimiter !== FALSE) { | |
317 return substr($body, 0, $delimiter); | |
318 } | |
319 | |
320 // We check for the presence of the PHP evaluator filter in the current | |
321 // format. If the body contains PHP code, we do not split it up to prevent | |
322 // parse errors. | |
323 if (isset($format)) { | |
324 $filters = filter_list_format($format); | |
325 if (isset($filters['php/0']) && strpos($body, '<?') !== FALSE) { | |
326 return $body; | |
327 } | |
328 } | |
329 | |
330 // If we have a short body, the entire body is the teaser. | |
331 if (drupal_strlen($body) <= $size) { | |
332 return $body; | |
333 } | |
334 | |
335 // If the delimiter has not been specified, try to split at paragraph or | |
336 // sentence boundaries. | |
337 | |
338 // The teaser may not be longer than maximum length specified. Initial slice. | |
339 $teaser = truncate_utf8($body, $size); | |
340 | |
341 // Store the actual length of the UTF8 string -- which might not be the same | |
342 // as $size. | |
343 $max_rpos = strlen($teaser); | |
344 | |
345 // How much to cut off the end of the teaser so that it doesn't end in the | |
346 // middle of a paragraph, sentence, or word. | |
347 // Initialize it to maximum in order to find the minimum. | |
348 $min_rpos = $max_rpos; | |
349 | |
350 // Store the reverse of the teaser. We use strpos on the reversed needle and | |
351 // haystack for speed and convenience. | |
352 $reversed = strrev($teaser); | |
353 | |
354 // Build an array of arrays of break points grouped by preference. | |
355 $break_points = array(); | |
356 | |
357 // A paragraph near the end of sliced teaser is most preferable. | |
358 $break_points[] = array('</p>' => 0); | |
359 | |
360 // If no complete paragraph then treat line breaks as paragraphs. | |
361 $line_breaks = array('<br />' => 6, '<br>' => 4); | |
362 // Newline only indicates a line break if line break converter | |
363 // filter is present. | |
364 if (isset($filters['filter/1'])) { | |
365 $line_breaks["\n"] = 1; | |
366 } | |
367 $break_points[] = $line_breaks; | |
368 | |
369 // If the first paragraph is too long, split at the end of a sentence. | |
370 $break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1); | |
371 | |
372 // Iterate over the groups of break points until a break point is found. | |
373 foreach ($break_points as $points) { | |
374 // Look for each break point, starting at the end of the teaser. | |
375 foreach ($points as $point => $offset) { | |
376 // The teaser is already reversed, but the break point isn't. | |
377 $rpos = strpos($reversed, strrev($point)); | |
378 if ($rpos !== FALSE) { | |
379 $min_rpos = min($rpos + $offset, $min_rpos); | |
380 } | |
381 } | |
382 | |
383 // If a break point was found in this group, slice and return the teaser. | |
384 if ($min_rpos !== $max_rpos) { | |
385 // Don't slice with length 0. Length must be <0 to slice from RHS. | |
386 return ($min_rpos === 0) ? $teaser : substr($teaser, 0, 0 - $min_rpos); | |
387 } | |
388 } | |
389 | |
390 // If a break point was not found, still return a teaser. | |
391 return $teaser; | |
392 } | |
393 | |
394 /** | |
395 * Builds a list of available node types, and returns all of part of this list | |
396 * in the specified format. | |
397 * | |
398 * @param $op | |
399 * The format in which to return the list. When this is set to 'type', | |
400 * 'module', or 'name', only the specified node type is returned. When set to | |
401 * 'types' or 'names', all node types are returned. | |
402 * @param $node | |
403 * A node object, array, or string that indicates the node type to return. | |
404 * Leave at default value (NULL) to return a list of all node types. | |
405 * @param $reset | |
406 * Whether or not to reset this function's internal cache (defaults to | |
407 * FALSE). | |
408 * | |
409 * @return | |
410 * Either an array of all available node types, or a single node type, in a | |
411 * variable format. Returns FALSE if the node type is not found. | |
412 */ | |
413 function node_get_types($op = 'types', $node = NULL, $reset = FALSE) { | |
414 static $_node_types, $_node_names; | |
415 | |
416 if ($reset || !isset($_node_types)) { | |
417 list($_node_types, $_node_names) = _node_types_build(); | |
418 } | |
419 | |
420 if ($node) { | |
421 if (is_array($node)) { | |
422 $type = $node['type']; | |
423 } | |
424 elseif (is_object($node)) { | |
425 $type = $node->type; | |
426 } | |
427 elseif (is_string($node)) { | |
428 $type = $node; | |
429 } | |
430 if (!isset($_node_types[$type])) { | |
431 return FALSE; | |
432 } | |
433 } | |
434 switch ($op) { | |
435 case 'types': | |
436 return $_node_types; | |
437 case 'type': | |
438 return isset($_node_types[$type]) ? $_node_types[$type] : FALSE; | |
439 case 'module': | |
440 return isset($_node_types[$type]->module) ? $_node_types[$type]->module : FALSE; | |
441 case 'names': | |
442 return $_node_names; | |
443 case 'name': | |
444 return isset($_node_names[$type]) ? $_node_names[$type] : FALSE; | |
445 } | |
446 } | |
447 | |
448 /** | |
449 * Resets the database cache of node types, and saves all new or non-modified | |
450 * module-defined node types to the database. | |
451 */ | |
452 function node_types_rebuild() { | |
453 _node_types_build(); | |
454 | |
455 $node_types = node_get_types('types', NULL, TRUE); | |
456 | |
457 foreach ($node_types as $type => $info) { | |
458 if (!empty($info->is_new)) { | |
459 node_type_save($info); | |
460 } | |
461 if (!empty($info->disabled)) { | |
462 node_type_delete($info->type); | |
463 } | |
464 } | |
465 | |
466 _node_types_build(); | |
467 } | |
468 | |
469 /** | |
470 * Saves a node type to the database. | |
471 * | |
472 * @param $info | |
473 * The node type to save, as an object. | |
474 * | |
475 * @return | |
476 * Status flag indicating outcome of the operation. | |
477 */ | |
478 function node_type_save($info) { | |
479 $is_existing = FALSE; | |
480 $existing_type = !empty($info->old_type) ? $info->old_type : $info->type; | |
481 $is_existing = db_result(db_query("SELECT COUNT(*) FROM {node_type} WHERE type = '%s'", $existing_type)); | |
482 if (!isset($info->help)) { | |
483 $info->help = ''; | |
484 } | |
485 if (!isset($info->min_word_count)) { | |
486 $info->min_word_count = 0; | |
487 } | |
488 if (!isset($info->body_label)) { | |
489 $info->body_label = ''; | |
490 } | |
491 | |
492 if ($is_existing) { | |
493 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); | |
494 | |
495 module_invoke_all('node_type', 'update', $info); | |
496 return SAVED_UPDATED; | |
497 } | |
498 else { | |
499 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); | |
500 | |
501 module_invoke_all('node_type', 'insert', $info); | |
502 return SAVED_NEW; | |
503 } | |
504 } | |
505 | |
506 /** | |
507 * Deletes a node type from the database. | |
508 * | |
509 * @param $type | |
510 * The machine-readable name of the node type to be deleted. | |
511 */ | |
512 function node_type_delete($type) { | |
513 $info = node_get_types('type', $type); | |
514 db_query("DELETE FROM {node_type} WHERE type = '%s'", $type); | |
515 module_invoke_all('node_type', 'delete', $info); | |
516 } | |
517 | |
518 /** | |
519 * Updates all nodes of one type to be of another type. | |
520 * | |
521 * @param $old_type | |
522 * The current node type of the nodes. | |
523 * @param $type | |
524 * The new node type of the nodes. | |
525 * | |
526 * @return | |
527 * The number of nodes whose node type field was modified. | |
528 */ | |
529 function node_type_update_nodes($old_type, $type) { | |
530 db_query("UPDATE {node} SET type = '%s' WHERE type = '%s'", $type, $old_type); | |
531 return db_affected_rows(); | |
532 } | |
533 | |
534 /** | |
535 * Builds and returns the list of available node types. | |
536 * | |
537 * The list of types is built by querying hook_node_info() in all modules, and | |
538 * by comparing this information with the node types in the {node_type} table. | |
539 * | |
540 */ | |
541 function _node_types_build() { | |
542 $_node_types = array(); | |
543 $_node_names = array(); | |
544 | |
545 $info_array = module_invoke_all('node_info'); | |
546 foreach ($info_array as $type => $info) { | |
547 $info['type'] = $type; | |
548 $_node_types[$type] = (object) _node_type_set_defaults($info); | |
549 $_node_names[$type] = $info['name']; | |
550 } | |
551 | |
552 $type_result = db_query(db_rewrite_sql('SELECT nt.type, nt.* FROM {node_type} nt ORDER BY nt.type ASC', 'nt', 'type')); | |
553 while ($type_object = db_fetch_object($type_result)) { | |
554 // Check for node types from disabled modules and mark their types for removal. | |
555 // Types defined by the node module in the database (rather than by a separate | |
556 // module using hook_node_info) have a module value of 'node'. | |
557 if ($type_object->module != 'node' && empty($info_array[$type_object->type])) { | |
558 $type_object->disabled = TRUE; | |
559 } | |
560 if (!isset($_node_types[$type_object->type]) || $type_object->modified) { | |
561 $_node_types[$type_object->type] = $type_object; | |
562 $_node_names[$type_object->type] = $type_object->name; | |
563 | |
564 if ($type_object->type != $type_object->orig_type) { | |
565 unset($_node_types[$type_object->orig_type]); | |
566 unset($_node_names[$type_object->orig_type]); | |
567 } | |
568 } | |
569 } | |
570 | |
571 asort($_node_names); | |
572 | |
573 return array($_node_types, $_node_names); | |
574 } | |
575 | |
576 /** | |
577 * Set default values for a node type defined through hook_node_info(). | |
578 */ | |
579 function _node_type_set_defaults($info) { | |
580 if (!isset($info['has_title'])) { | |
581 $info['has_title'] = TRUE; | |
582 } | |
583 if ($info['has_title'] && !isset($info['title_label'])) { | |
584 $info['title_label'] = t('Title'); | |
585 } | |
586 | |
587 if (!isset($info['has_body'])) { | |
588 $info['has_body'] = TRUE; | |
589 } | |
590 if ($info['has_body'] && !isset($info['body_label'])) { | |
591 $info['body_label'] = t('Body'); | |
592 } | |
593 | |
594 if (!isset($info['help'])) { | |
595 $info['help'] = ''; | |
596 } | |
597 if (!isset($info['min_word_count'])) { | |
598 $info['min_word_count'] = 0; | |
599 } | |
600 if (!isset($info['custom'])) { | |
601 $info['custom'] = FALSE; | |
602 } | |
603 if (!isset($info['modified'])) { | |
604 $info['modified'] = FALSE; | |
605 } | |
606 if (!isset($info['locked'])) { | |
607 $info['locked'] = TRUE; | |
608 } | |
609 | |
610 $info['orig_type'] = $info['type']; | |
611 $info['is_new'] = TRUE; | |
612 | |
613 return $info; | |
614 } | |
615 | |
616 /** | |
617 * Determine whether a node hook exists. | |
618 * | |
619 * @param &$node | |
620 * Either a node object, node array, or a string containing the node type. | |
621 * @param $hook | |
622 * A string containing the name of the hook. | |
623 * @return | |
624 * TRUE iff the $hook exists in the node type of $node. | |
625 */ | |
626 function node_hook(&$node, $hook) { | |
627 $module = node_get_types('module', $node); | |
628 if ($module == 'node') { | |
629 $module = 'node_content'; // Avoid function name collisions. | |
630 } | |
631 return module_hook($module, $hook); | |
632 } | |
633 | |
634 /** | |
635 * Invoke a node hook. | |
636 * | |
637 * @param &$node | |
638 * Either a node object, node array, or a string containing the node type. | |
639 * @param $hook | |
640 * A string containing the name of the hook. | |
641 * @param $a2, $a3, $a4 | |
642 * Arguments to pass on to the hook, after the $node argument. | |
643 * @return | |
644 * The returned value of the invoked hook. | |
645 */ | |
646 function node_invoke(&$node, $hook, $a2 = NULL, $a3 = NULL, $a4 = NULL) { | |
647 if (node_hook($node, $hook)) { | |
648 $module = node_get_types('module', $node); | |
649 if ($module == 'node') { | |
650 $module = 'node_content'; // Avoid function name collisions. | |
651 } | |
652 $function = $module .'_'. $hook; | |
653 return ($function($node, $a2, $a3, $a4)); | |
654 } | |
655 } | |
656 | |
657 /** | |
658 * Invoke a hook_nodeapi() operation in all modules. | |
659 * | |
660 * @param &$node | |
661 * A node object. | |
662 * @param $op | |
663 * A string containing the name of the nodeapi operation. | |
664 * @param $a3, $a4 | |
665 * Arguments to pass on to the hook, after the $node and $op arguments. | |
666 * @return | |
667 * The returned value of the invoked hooks. | |
668 */ | |
669 function node_invoke_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { | |
670 $return = array(); | |
671 foreach (module_implements('nodeapi') as $name) { | |
672 $function = $name .'_nodeapi'; | |
673 $result = $function($node, $op, $a3, $a4); | |
674 if (isset($result) && is_array($result)) { | |
675 $return = array_merge($return, $result); | |
676 } | |
677 else if (isset($result)) { | |
678 $return[] = $result; | |
679 } | |
680 } | |
681 return $return; | |
682 } | |
683 | |
684 /** | |
685 * Load a node object from the database. | |
686 * | |
687 * @param $param | |
688 * Either the nid of the node or an array of conditions to match against in the database query | |
689 * @param $revision | |
690 * Which numbered revision to load. Defaults to the current version. | |
691 * @param $reset | |
692 * Whether to reset the internal node_load cache. | |
693 * | |
694 * @return | |
695 * A fully-populated node object. | |
696 */ | |
697 function node_load($param = array(), $revision = NULL, $reset = NULL) { | |
698 static $nodes = array(); | |
699 | |
700 if ($reset) { | |
701 $nodes = array(); | |
702 } | |
703 | |
704 $cachable = ($revision == NULL); | |
705 $arguments = array(); | |
706 if (is_numeric($param)) { | |
707 if ($cachable) { | |
708 // Is the node statically cached? | |
709 if (isset($nodes[$param])) { | |
710 return is_object($nodes[$param]) ? drupal_clone($nodes[$param]) : $nodes[$param]; | |
711 } | |
712 } | |
713 $cond = 'n.nid = %d'; | |
714 $arguments[] = $param; | |
715 } | |
716 elseif (is_array($param)) { | |
717 // Turn the conditions into a query. | |
718 foreach ($param as $key => $value) { | |
719 $cond[] = 'n.'. db_escape_string($key) ." = '%s'"; | |
720 $arguments[] = $value; | |
721 } | |
722 $cond = implode(' AND ', $cond); | |
723 } | |
724 else { | |
725 return FALSE; | |
726 } | |
727 | |
728 // Retrieve a field list based on the site's schema. | |
729 $fields = drupal_schema_fields_sql('node', 'n'); | |
730 $fields = array_merge($fields, drupal_schema_fields_sql('node_revisions', 'r')); | |
731 $fields = array_merge($fields, array('u.name', 'u.picture', 'u.data')); | |
732 // Remove fields not needed in the query: n.vid and r.nid are redundant, | |
733 // n.title is unnecessary because the node title comes from the | |
734 // node_revisions table. We'll keep r.vid, r.title, and n.nid. | |
735 $fields = array_diff($fields, array('n.vid', 'n.title', 'r.nid')); | |
736 $fields = implode(', ', $fields); | |
737 // Rename timestamp field for clarity. | |
738 $fields = str_replace('r.timestamp', 'r.timestamp AS revision_timestamp', $fields); | |
739 // Change name of revision uid so it doesn't conflict with n.uid. | |
740 $fields = str_replace('r.uid', 'r.uid AS revision_uid', $fields); | |
741 | |
742 // Retrieve the node. | |
743 // No db_rewrite_sql is applied so as to get complete indexing for search. | |
744 if ($revision) { | |
745 array_unshift($arguments, $revision); | |
746 $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)); | |
747 } | |
748 else { | |
749 $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)); | |
750 } | |
751 | |
752 if ($node && $node->nid) { | |
753 // Call the node specific callback (if any) and piggy-back the | |
754 // results to the node or overwrite some values. | |
755 if ($extra = node_invoke($node, 'load')) { | |
756 foreach ($extra as $key => $value) { | |
757 $node->$key = $value; | |
758 } | |
759 } | |
760 | |
761 if ($extra = node_invoke_nodeapi($node, 'load')) { | |
762 foreach ($extra as $key => $value) { | |
763 $node->$key = $value; | |
764 } | |
765 } | |
766 if ($cachable) { | |
767 $nodes[$node->nid] = is_object($node) ? drupal_clone($node) : $node; | |
768 } | |
769 } | |
770 | |
771 return $node; | |
772 } | |
773 | |
774 /** | |
775 * Perform validation checks on the given node. | |
776 */ | |
777 function node_validate($node, $form = array()) { | |
778 // Convert the node to an object, if necessary. | |
779 $node = (object)$node; | |
780 $type = node_get_types('type', $node); | |
781 | |
782 // Make sure the body has the minimum number of words. | |
783 // TODO : use a better word counting algorithm that will work in other languages | |
784 if (!empty($type->min_word_count) && isset($node->body) && count(explode(' ', $node->body)) < $type->min_word_count) { | |
785 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))); | |
786 } | |
787 | |
788 if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) { | |
789 form_set_error('changed', t('This content has been modified by another user, changes cannot be saved.')); | |
790 } | |
791 | |
792 if (user_access('administer nodes')) { | |
793 // Validate the "authored by" field. | |
794 if (!empty($node->name) && !($account = user_load(array('name' => $node->name)))) { | |
795 // The use of empty() is mandatory in the context of usernames | |
796 // as the empty string denotes the anonymous user. In case we | |
797 // are dealing with an anonymous user we set the user ID to 0. | |
798 form_set_error('name', t('The username %name does not exist.', array('%name' => $node->name))); | |
799 } | |
800 | |
801 // Validate the "authored on" field. As of PHP 5.1.0, strtotime returns FALSE instead of -1 upon failure. | |
802 if (!empty($node->date) && strtotime($node->date) <= 0) { | |
803 form_set_error('date', t('You have to specify a valid date.')); | |
804 } | |
805 } | |
806 | |
807 // Do node-type-specific validation checks. | |
808 node_invoke($node, 'validate', $form); | |
809 node_invoke_nodeapi($node, 'validate', $form); | |
810 } | |
811 | |
812 /** | |
813 * Prepare node for save and allow modules to make changes. | |
814 */ | |
815 function node_submit($node) { | |
816 global $user; | |
817 | |
818 // Convert the node to an object, if necessary. | |
819 $node = (object)$node; | |
820 | |
821 // Generate the teaser, but only if it hasn't been set (e.g. by a | |
822 // module-provided 'teaser' form item). | |
823 if (!isset($node->teaser)) { | |
824 if (isset($node->body)) { | |
825 $node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL); | |
826 // Chop off the teaser from the body if needed. The teaser_include | |
827 // property might not be set (eg. in Blog API postings), so only act on | |
828 // it, if it was set with a given value. | |
829 if (isset($node->teaser_include) && !$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) { | |
830 $node->body = substr($node->body, strlen($node->teaser)); | |
831 } | |
832 } | |
833 else { | |
834 $node->teaser = ''; | |
835 } | |
836 } | |
837 | |
838 if (user_access('administer nodes')) { | |
839 // Populate the "authored by" field. | |
840 if ($account = user_load(array('name' => $node->name))) { | |
841 $node->uid = $account->uid; | |
842 } | |
843 else { | |
844 $node->uid = 0; | |
845 } | |
846 } | |
847 $node->created = !empty($node->date) ? strtotime($node->date) : time(); | |
848 $node->validated = TRUE; | |
849 | |
850 return $node; | |
851 } | |
852 | |
853 /** | |
854 * Save a node object into the database. | |
855 */ | |
856 function node_save(&$node) { | |
857 // Let modules modify the node before it is saved to the database. | |
858 node_invoke_nodeapi($node, 'presave'); | |
859 global $user; | |
860 | |
861 $node->is_new = FALSE; | |
862 | |
863 // Apply filters to some default node fields: | |
864 if (empty($node->nid)) { | |
865 // Insert a new node. | |
866 $node->is_new = TRUE; | |
867 | |
868 // When inserting a node, $node->log must be set because | |
869 // {node_revisions}.log does not (and cannot) have a default | |
870 // value. If the user does not have permission to create | |
871 // revisions, however, the form will not contain an element for | |
872 // log so $node->log will be unset at this point. | |
873 if (!isset($node->log)) { | |
874 $node->log = ''; | |
875 } | |
876 | |
877 // For the same reasons, make sure we have $node->teaser and | |
878 // $node->body. We should consider making these fields nullable | |
879 // in a future version since node types are not required to use them. | |
880 if (!isset($node->teaser)) { | |
881 $node->teaser = ''; | |
882 } | |
883 if (!isset($node->body)) { | |
884 $node->body = ''; | |
885 } | |
886 } | |
887 elseif (!empty($node->revision)) { | |
888 $node->old_vid = $node->vid; | |
889 } | |
890 else { | |
891 // When updating a node, avoid clobberring an existing log entry with an empty one. | |
892 if (empty($node->log)) { | |
893 unset($node->log); | |
894 } | |
895 } | |
896 | |
897 // Set some required fields: | |
898 if (empty($node->created)) { | |
899 $node->created = time(); | |
900 } | |
901 // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...) | |
902 $node->changed = time(); | |
903 | |
904 $node->timestamp = time(); | |
905 $node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT; | |
906 $update_node = TRUE; | |
907 | |
908 // Generate the node table query and the node_revisions table query. | |
909 if ($node->is_new) { | |
910 drupal_write_record('node', $node); | |
911 _node_save_revision($node, $user->uid); | |
912 $op = 'insert'; | |
913 } | |
914 else { | |
915 drupal_write_record('node', $node, 'nid'); | |
916 if (!empty($node->revision)) { | |
917 _node_save_revision($node, $user->uid); | |
918 } | |
919 else { | |
920 _node_save_revision($node, $user->uid, 'vid'); | |
921 $update_node = FALSE; | |
922 } | |
923 $op = 'update'; | |
924 } | |
925 if ($update_node) { | |
926 db_query('UPDATE {node} SET vid = %d WHERE nid = %d', $node->vid, $node->nid); | |
927 } | |
928 | |
929 // Call the node specific callback (if any). | |
930 node_invoke($node, $op); | |
931 node_invoke_nodeapi($node, $op); | |
932 | |
933 // Update the node access table for this node. | |
934 node_access_acquire_grants($node); | |
935 | |
936 // Clear the page and block caches. | |
937 cache_clear_all(); | |
938 } | |
939 | |
940 /** | |
941 * Helper function to save a revision with the uid of the current user. | |
942 * | |
943 * Node is taken by reference, becuse drupal_write_record() updates the | |
944 * $node with the revision id, and we need to pass that back to the caller. | |
945 */ | |
946 function _node_save_revision(&$node, $uid, $update = NULL) { | |
947 $temp_uid = $node->uid; | |
948 $node->uid = $uid; | |
949 if (isset($update)) { | |
950 drupal_write_record('node_revisions', $node, $update); | |
951 } | |
952 else { | |
953 drupal_write_record('node_revisions', $node); | |
954 } | |
955 $node->uid = $temp_uid; | |
956 } | |
957 | |
958 /** | |
959 * Delete a node. | |
960 */ | |
961 function node_delete($nid) { | |
962 | |
963 $node = node_load($nid); | |
964 | |
965 if (node_access('delete', $node)) { | |
966 db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); | |
967 db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); | |
968 | |
969 // Call the node-specific callback (if any): | |
970 node_invoke($node, 'delete'); | |
971 node_invoke_nodeapi($node, 'delete'); | |
972 | |
973 // Clear the page and block caches. | |
974 cache_clear_all(); | |
975 | |
976 // Remove this node from the search index if needed. | |
977 if (function_exists('search_wipe')) { | |
978 search_wipe($node->nid, 'node'); | |
979 } | |
980 watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title)); | |
981 drupal_set_message(t('@type %title has been deleted.', array('@type' => node_get_types('name', $node), '%title' => $node->title))); | |
982 } | |
983 } | |
984 | |
985 /** | |
986 * Generate a display of the given node. | |
987 * | |
988 * @param $node | |
989 * A node array or node object. | |
990 * @param $teaser | |
991 * Whether to display the teaser only or the full form. | |
992 * @param $page | |
993 * Whether the node is being displayed by itself as a page. | |
994 * @param $links | |
995 * Whether or not to display node links. Links are omitted for node previews. | |
996 * | |
997 * @return | |
998 * An HTML representation of the themed node. | |
999 */ | |
1000 function node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) { | |
1001 $node = (object)$node; | |
1002 | |
1003 $node = node_build_content($node, $teaser, $page); | |
1004 | |
1005 if ($links) { | |
1006 $node->links = module_invoke_all('link', 'node', $node, $teaser); | |
1007 drupal_alter('link', $node->links, $node); | |
1008 } | |
1009 | |
1010 // Set the proper node part, then unset unused $node part so that a bad | |
1011 // theme can not open a security hole. | |
1012 $content = drupal_render($node->content); | |
1013 if ($teaser) { | |
1014 $node->teaser = $content; | |
1015 unset($node->body); | |
1016 } | |
1017 else { | |
1018 $node->body = $content; | |
1019 unset($node->teaser); | |
1020 } | |
1021 | |
1022 // Allow modules to modify the fully-built node. | |
1023 node_invoke_nodeapi($node, 'alter', $teaser, $page); | |
1024 | |
1025 return theme('node', $node, $teaser, $page); | |
1026 } | |
1027 | |
1028 /** | |
1029 * Apply filters and build the node's standard elements. | |
1030 */ | |
1031 function node_prepare($node, $teaser = FALSE) { | |
1032 // First we'll overwrite the existing node teaser and body with | |
1033 // the filtered copies! Then, we'll stick those into the content | |
1034 // array and set the read more flag if appropriate. | |
1035 $node->readmore = $node->teaser != $node->body; | |
1036 | |
1037 if ($teaser == FALSE) { | |
1038 $node->body = check_markup($node->body, $node->format, FALSE); | |
1039 } | |
1040 else { | |
1041 $node->teaser = check_markup($node->teaser, $node->format, FALSE); | |
1042 } | |
1043 | |
1044 $node->content['body'] = array( | |
1045 '#value' => $teaser ? $node->teaser : $node->body, | |
1046 '#weight' => 0, | |
1047 ); | |
1048 | |
1049 return $node; | |
1050 } | |
1051 | |
1052 /** | |
1053 * Builds a structured array representing the node's content. | |
1054 * | |
1055 * @param $node | |
1056 * A node object. | |
1057 * @param $teaser | |
1058 * Whether to display the teaser only, as on the main page. | |
1059 * @param $page | |
1060 * Whether the node is being displayed by itself as a page. | |
1061 * | |
1062 * @return | |
1063 * An structured array containing the individual elements | |
1064 * of the node's body. | |
1065 */ | |
1066 function node_build_content($node, $teaser = FALSE, $page = FALSE) { | |
1067 | |
1068 // The build mode identifies the target for which the node is built. | |
1069 if (!isset($node->build_mode)) { | |
1070 $node->build_mode = NODE_BUILD_NORMAL; | |
1071 } | |
1072 | |
1073 // Remove the delimiter (if any) that separates the teaser from the body. | |
1074 $node->body = isset($node->body) ? str_replace('<!--break-->', '', $node->body) : ''; | |
1075 | |
1076 // The 'view' hook can be implemented to overwrite the default function | |
1077 // to display nodes. | |
1078 if (node_hook($node, 'view')) { | |
1079 $node = node_invoke($node, 'view', $teaser, $page); | |
1080 } | |
1081 else { | |
1082 $node = node_prepare($node, $teaser); | |
1083 } | |
1084 | |
1085 // Allow modules to make their own additions to the node. | |
1086 node_invoke_nodeapi($node, 'view', $teaser, $page); | |
1087 | |
1088 return $node; | |
1089 } | |
1090 | |
1091 /** | |
1092 * Generate a page displaying a single node, along with its comments. | |
1093 */ | |
1094 function node_show($node, $cid, $message = FALSE) { | |
1095 if ($message) { | |
1096 drupal_set_title(t('Revision of %title from %date', array('%title' => $node->title, '%date' => format_date($node->revision_timestamp)))); | |
1097 } | |
1098 $output = node_view($node, FALSE, TRUE); | |
1099 | |
1100 if (function_exists('comment_render') && $node->comment) { | |
1101 $output .= comment_render($node, $cid); | |
1102 } | |
1103 | |
1104 // Update the history table, stating that this user viewed this node. | |
1105 node_tag_new($node->nid); | |
1106 | |
1107 return $output; | |
1108 } | |
1109 | |
1110 /** | |
1111 * Theme a log message. | |
1112 * | |
1113 * @ingroup themeable | |
1114 */ | |
1115 function theme_node_log_message($log) { | |
1116 return '<div class="log"><div class="title">'. t('Log') .':</div>'. $log .'</div>'; | |
1117 } | |
1118 | |
1119 /** | |
1120 * Implementation of hook_perm(). | |
1121 */ | |
1122 function node_perm() { | |
1123 $perms = array('administer content types', 'administer nodes', 'access content', 'view revisions', 'revert revisions', 'delete revisions'); | |
1124 | |
1125 foreach (node_get_types() as $type) { | |
1126 if ($type->module == 'node') { | |
1127 $name = check_plain($type->type); | |
1128 $perms[] = 'create '. $name .' content'; | |
1129 $perms[] = 'delete own '. $name .' content'; | |
1130 $perms[] = 'delete any '. $name .' content'; | |
1131 $perms[] = 'edit own '. $name .' content'; | |
1132 $perms[] = 'edit any '. $name .' content'; | |
1133 } | |
1134 } | |
1135 | |
1136 return $perms; | |
1137 } | |
1138 | |
1139 /** | |
1140 * Implementation of hook_search(). | |
1141 */ | |
1142 function node_search($op = 'search', $keys = NULL) { | |
1143 switch ($op) { | |
1144 case 'name': | |
1145 return t('Content'); | |
1146 | |
1147 case 'reset': | |
1148 db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'node'", time()); | |
1149 return; | |
1150 | |
1151 case 'status': | |
1152 $total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1')); | |
1153 $remaining = db_result(db_query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0")); | |
1154 return array('remaining' => $remaining, 'total' => $total); | |
1155 | |
1156 case 'admin': | |
1157 $form = array(); | |
1158 // Output form for defining rank factor weights. | |
1159 $form['content_ranking'] = array( | |
1160 '#type' => 'fieldset', | |
1161 '#title' => t('Content ranking'), | |
1162 ); | |
1163 $form['content_ranking']['#theme'] = 'node_search_admin'; | |
1164 $form['content_ranking']['info'] = array( | |
1165 '#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>' | |
1166 ); | |
1167 | |
1168 $ranking = array('node_rank_relevance' => t('Keyword relevance'), | |
1169 'node_rank_recent' => t('Recently posted')); | |
1170 if (module_exists('comment')) { | |
1171 $ranking['node_rank_comments'] = t('Number of comments'); | |
1172 } | |
1173 if (module_exists('statistics') && variable_get('statistics_count_content_views', 0)) { | |
1174 $ranking['node_rank_views'] = t('Number of views'); | |
1175 } | |
1176 | |
1177 // Note: reversed to reflect that higher number = higher ranking. | |
1178 $options = drupal_map_assoc(range(0, 10)); | |
1179 foreach ($ranking as $var => $title) { | |
1180 $form['content_ranking']['factors'][$var] = array( | |
1181 '#title' => $title, | |
1182 '#type' => 'select', | |
1183 '#options' => $options, | |
1184 '#default_value' => variable_get($var, 5), | |
1185 ); | |
1186 } | |
1187 return $form; | |
1188 | |
1189 case 'search': | |
1190 // Build matching conditions | |
1191 list($join1, $where1) = _db_rewrite_sql(); | |
1192 $arguments1 = array(); | |
1193 $conditions1 = 'n.status = 1'; | |
1194 | |
1195 if ($type = search_query_extract($keys, 'type')) { | |
1196 $types = array(); | |
1197 foreach (explode(',', $type) as $t) { | |
1198 $types[] = "n.type = '%s'"; | |
1199 $arguments1[] = $t; | |
1200 } | |
1201 $conditions1 .= ' AND ('. implode(' OR ', $types) .')'; | |
1202 $keys = search_query_insert($keys, 'type'); | |
1203 } | |
1204 | |
1205 if ($category = search_query_extract($keys, 'category')) { | |
1206 $categories = array(); | |
1207 foreach (explode(',', $category) as $c) { | |
1208 $categories[] = "tn.tid = %d"; | |
1209 $arguments1[] = $c; | |
1210 } | |
1211 $conditions1 .= ' AND ('. implode(' OR ', $categories) .')'; | |
1212 $join1 .= ' INNER JOIN {term_node} tn ON n.vid = tn.vid'; | |
1213 $keys = search_query_insert($keys, 'category'); | |
1214 } | |
1215 | |
1216 // Build ranking expression (we try to map each parameter to a | |
1217 // uniform distribution in the range 0..1). | |
1218 $ranking = array(); | |
1219 $arguments2 = array(); | |
1220 $join2 = ''; | |
1221 // Used to avoid joining on node_comment_statistics twice | |
1222 $stats_join = FALSE; | |
1223 $total = 0; | |
1224 if ($weight = (int)variable_get('node_rank_relevance', 5)) { | |
1225 // Average relevance values hover around 0.15 | |
1226 $ranking[] = '%d * i.relevance'; | |
1227 $arguments2[] = $weight; | |
1228 $total += $weight; | |
1229 } | |
1230 if ($weight = (int)variable_get('node_rank_recent', 5)) { | |
1231 // Exponential decay with half-life of 6 months, starting at last indexed node | |
1232 $ranking[] = '%d * POW(2, (GREATEST(MAX(n.created), MAX(n.changed), MAX(c.last_comment_timestamp)) - %d) * 6.43e-8)'; | |
1233 $arguments2[] = $weight; | |
1234 $arguments2[] = (int)variable_get('node_cron_last', 0); | |
1235 $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid'; | |
1236 $stats_join = TRUE; | |
1237 $total += $weight; | |
1238 } | |
1239 if (module_exists('comment') && $weight = (int)variable_get('node_rank_comments', 5)) { | |
1240 // Inverse law that maps the highest reply count on the site to 1 and 0 to 0. | |
1241 $scale = variable_get('node_cron_comments_scale', 0.0); | |
1242 $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(c.comment_count) * %f))'; | |
1243 $arguments2[] = $weight; | |
1244 $arguments2[] = $scale; | |
1245 if (!$stats_join) { | |
1246 $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid'; | |
1247 } | |
1248 $total += $weight; | |
1249 } | |
1250 if (module_exists('statistics') && variable_get('statistics_count_content_views', 0) && | |
1251 $weight = (int)variable_get('node_rank_views', 5)) { | |
1252 // Inverse law that maps the highest view count on the site to 1 and 0 to 0. | |
1253 $scale = variable_get('node_cron_views_scale', 0.0); | |
1254 $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(nc.totalcount) * %f))'; | |
1255 $arguments2[] = $weight; | |
1256 $arguments2[] = $scale; | |
1257 $join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid'; | |
1258 $total += $weight; | |
1259 } | |
1260 $select2 = (count($ranking) ? implode(' + ', $ranking) : 'i.relevance') .' AS score'; | |
1261 | |
1262 // Do search | |
1263 $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); | |
1264 | |
1265 // Load results | |
1266 $results = array(); | |
1267 foreach ($find as $item) { | |
1268 // Build the node body. | |
1269 $node = node_load($item->sid); | |
1270 $node->build_mode = NODE_BUILD_SEARCH_RESULT; | |
1271 $node = node_build_content($node, FALSE, FALSE); | |
1272 $node->body = drupal_render($node->content); | |
1273 | |
1274 // Fetch comments for snippet | |
1275 $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index'); | |
1276 // Fetch terms for snippet | |
1277 $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index'); | |
1278 | |
1279 $extra = node_invoke_nodeapi($node, 'search result'); | |
1280 $results[] = array( | |
1281 'link' => url('node/'. $item->sid, array('absolute' => TRUE)), | |
1282 'type' => check_plain(node_get_types('name', $node)), | |
1283 'title' => $node->title, | |
1284 'user' => theme('username', $node), | |
1285 'date' => $node->changed, | |
1286 'node' => $node, | |
1287 'extra' => $extra, | |
1288 'score' => $item->score / $total, | |
1289 'snippet' => search_excerpt($keys, $node->body), | |
1290 ); | |
1291 } | |
1292 return $results; | |
1293 } | |
1294 } | |
1295 | |
1296 /** | |
1297 * Implementation of hook_user(). | |
1298 */ | |
1299 function node_user($op, &$edit, &$user) { | |
1300 if ($op == 'delete') { | |
1301 db_query('UPDATE {node} SET uid = 0 WHERE uid = %d', $user->uid); | |
1302 db_query('UPDATE {node_revisions} SET uid = 0 WHERE uid = %d', $user->uid); | |
1303 } | |
1304 } | |
1305 | |
1306 /** | |
1307 * Theme the content ranking part of the search settings admin page. | |
1308 * | |
1309 * @ingroup themeable | |
1310 */ | |
1311 function theme_node_search_admin($form) { | |
1312 $output = drupal_render($form['info']); | |
1313 | |
1314 $header = array(t('Factor'), t('Weight')); | |
1315 foreach (element_children($form['factors']) as $key) { | |
1316 $row = array(); | |
1317 $row[] = $form['factors'][$key]['#title']; | |
1318 unset($form['factors'][$key]['#title']); | |
1319 $row[] = drupal_render($form['factors'][$key]); | |
1320 $rows[] = $row; | |
1321 } | |
1322 $output .= theme('table', $header, $rows); | |
1323 | |
1324 $output .= drupal_render($form); | |
1325 return $output; | |
1326 } | |
1327 | |
1328 /** | |
1329 * Retrieve the comment mode for the given node ID (none, read, or read/write). | |
1330 */ | |
1331 function node_comment_mode($nid) { | |
1332 static $comment_mode; | |
1333 if (!isset($comment_mode[$nid])) { | |
1334 $comment_mode[$nid] = db_result(db_query('SELECT comment FROM {node} WHERE nid = %d', $nid)); | |
1335 } | |
1336 return $comment_mode[$nid]; | |
1337 } | |
1338 | |
1339 /** | |
1340 * Implementation of hook_link(). | |
1341 */ | |
1342 function node_link($type, $node = NULL, $teaser = FALSE) { | |
1343 $links = array(); | |
1344 | |
1345 if ($type == 'node') { | |
1346 if ($teaser == 1 && $node->teaser && !empty($node->readmore)) { | |
1347 $links['node_read_more'] = array( | |
1348 'title' => t('Read more'), | |
1349 'href' => "node/$node->nid", | |
1350 // The title attribute gets escaped when the links are processed, so | |
1351 // there is no need to escape here. | |
1352 'attributes' => array('title' => t('Read the rest of !title.', array('!title' => $node->title))) | |
1353 ); | |
1354 } | |
1355 } | |
1356 | |
1357 return $links; | |
1358 } | |
1359 | |
1360 function _node_revision_access($node, $op = 'view') { | |
1361 static $access = array(); | |
1362 if (!isset($access[$node->vid])) { | |
1363 $node_current_revision = node_load($node->nid); | |
1364 $is_current_revision = $node_current_revision->vid == $node->vid; | |
1365 // There should be at least two revisions. If the vid of the given node | |
1366 // and the vid of the current revision differs, then we already have two | |
1367 // different revisions so there is no need for a separate database check. | |
1368 // Also, if you try to revert to or delete the current revision, that's | |
1369 // not good. | |
1370 if ($is_current_revision && (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $node->nid)) == 1 || $op == 'update' || $op == 'delete')) { | |
1371 $access[$node->vid] = FALSE; | |
1372 } | |
1373 elseif (user_access('administer nodes')) { | |
1374 $access[$node->vid] = TRUE; | |
1375 } | |
1376 else { | |
1377 $map = array('view' => 'view revisions', 'update' => 'revert revisions', 'delete' => 'delete revisions'); | |
1378 // First check the user permission, second check the access to the | |
1379 // current revision and finally, if the node passed in is not the current | |
1380 // revision then access to that, too. | |
1381 $access[$node->vid] = isset($map[$op]) && user_access($map[$op]) && node_access($op, $node_current_revision) && ($is_current_revision || node_access($op, $node)); | |
1382 } | |
1383 } | |
1384 return $access[$node->vid]; | |
1385 } | |
1386 | |
1387 function _node_add_access() { | |
1388 $types = node_get_types(); | |
1389 foreach ($types as $type) { | |
1390 if (node_hook($type->type, 'form') && node_access('create', $type->type)) { | |
1391 return TRUE; | |
1392 } | |
1393 } | |
1394 return FALSE; | |
1395 } | |
1396 | |
1397 /** | |
1398 * Implementation of hook_menu(). | |
1399 */ | |
1400 function node_menu() { | |
1401 $items['admin/content/node'] = array( | |
1402 'title' => 'Content', | |
1403 'description' => "View, edit, and delete your site's content.", | |
1404 'page callback' => 'drupal_get_form', | |
1405 'page arguments' => array('node_admin_content'), | |
1406 'access arguments' => array('administer nodes'), | |
1407 'file' => 'node.admin.inc', | |
1408 ); | |
1409 | |
1410 $items['admin/content/node/overview'] = array( | |
1411 'title' => 'List', | |
1412 'type' => MENU_DEFAULT_LOCAL_TASK, | |
1413 'weight' => -10, | |
1414 ); | |
1415 | |
1416 $items['admin/content/node-settings'] = array( | |
1417 'title' => 'Post settings', | |
1418 'description' => 'Control posting behavior, such as teaser length, requiring previews before posting, and the number of posts on the front page.', | |
1419 'page callback' => 'drupal_get_form', | |
1420 'page arguments' => array('node_configure'), | |
1421 'access arguments' => array('administer nodes'), | |
1422 'file' => 'node.admin.inc', | |
1423 ); | |
1424 $items['admin/content/node-settings/rebuild'] = array( | |
1425 'title' => 'Rebuild permissions', | |
1426 'page arguments' => array('node_configure_rebuild_confirm'), | |
1427 'file' => 'node.admin.inc', | |
1428 // Any user than can potentially trigger a node_acess_needs_rebuild(TRUE) | |
1429 // has to be allowed access to the 'node access rebuild' confirm form. | |
1430 'access arguments' => array('access administration pages'), | |
1431 'type' => MENU_CALLBACK, | |
1432 ); | |
1433 | |
1434 $items['admin/content/types'] = array( | |
1435 'title' => 'Content types', | |
1436 'description' => 'Manage posts by content type, including default status, front page promotion, etc.', | |
1437 'page callback' => 'node_overview_types', | |
1438 'access arguments' => array('administer content types'), | |
1439 'file' => 'content_types.inc', | |
1440 ); | |
1441 $items['admin/content/types/list'] = array( | |
1442 'title' => 'List', | |
1443 'type' => MENU_DEFAULT_LOCAL_TASK, | |
1444 'weight' => -10, | |
1445 ); | |
1446 $items['admin/content/types/add'] = array( | |
1447 'title' => 'Add content type', | |
1448 'page callback' => 'drupal_get_form', | |
1449 'page arguments' => array('node_type_form'), | |
1450 'file' => 'content_types.inc', | |
1451 'type' => MENU_LOCAL_TASK, | |
1452 ); | |
1453 $items['node'] = array( | |
1454 'title' => 'Content', | |
1455 'page callback' => 'node_page_default', | |
1456 'access arguments' => array('access content'), | |
1457 'type' => MENU_CALLBACK, | |
1458 ); | |
1459 $items['node/add'] = array( | |
1460 'title' => 'Create content', | |
1461 'page callback' => 'node_add_page', | |
1462 'access callback' => '_node_add_access', | |
1463 'weight' => 1, | |
1464 'file' => 'node.pages.inc', | |
1465 ); | |
1466 $items['rss.xml'] = array( | |
1467 'title' => 'RSS feed', | |
1468 'page callback' => 'node_feed', | |
1469 'access arguments' => array('access content'), | |
1470 'type' => MENU_CALLBACK, | |
1471 ); | |
1472 foreach (node_get_types('types', NULL, TRUE) as $type) { | |
1473 $type_url_str = str_replace('_', '-', $type->type); | |
1474 $items['node/add/'. $type_url_str] = array( | |
1475 'title' => drupal_ucfirst($type->name), | |
1476 'title callback' => 'check_plain', | |
1477 'page callback' => 'node_add', | |
1478 'page arguments' => array(2), | |
1479 'access callback' => 'node_access', | |
1480 'access arguments' => array('create', $type->type), | |
1481 'description' => $type->description, | |
1482 'file' => 'node.pages.inc', | |
1483 ); | |
1484 $items['admin/content/node-type/'. $type_url_str] = array( | |
1485 'title' => $type->name, | |
1486 'page callback' => 'drupal_get_form', | |
1487 'page arguments' => array('node_type_form', $type), | |
1488 'file' => 'content_types.inc', | |
1489 'type' => MENU_CALLBACK, | |
1490 ); | |
1491 $items['admin/content/node-type/'. $type_url_str .'/edit'] = array( | |
1492 'title' => 'Edit', | |
1493 'type' => MENU_DEFAULT_LOCAL_TASK, | |
1494 ); | |
1495 $items['admin/content/node-type/'. $type_url_str .'/delete'] = array( | |
1496 'title' => 'Delete', | |
1497 'page arguments' => array('node_type_delete_confirm', $type), | |
1498 'file' => 'content_types.inc', | |
1499 'type' => MENU_CALLBACK, | |
1500 ); | |
1501 } | |
1502 $items['node/%node'] = array( | |
1503 'title callback' => 'node_page_title', | |
1504 'title arguments' => array(1), | |
1505 'page callback' => 'node_page_view', | |
1506 'page arguments' => array(1), | |
1507 'access callback' => 'node_access', | |
1508 'access arguments' => array('view', 1), | |
1509 'type' => MENU_CALLBACK); | |
1510 $items['node/%node/view'] = array( | |
1511 'title' => 'View', | |
1512 'type' => MENU_DEFAULT_LOCAL_TASK, | |
1513 'weight' => -10); | |
1514 $items['node/%node/edit'] = array( | |
1515 'title' => 'Edit', | |
1516 'page callback' => 'node_page_edit', | |
1517 'page arguments' => array(1), | |
1518 'access callback' => 'node_access', | |
1519 'access arguments' => array('update', 1), | |
1520 'weight' => 1, | |
1521 'file' => 'node.pages.inc', | |
1522 'type' => MENU_LOCAL_TASK, | |
1523 ); | |
1524 $items['node/%node/delete'] = array( | |
1525 'title' => 'Delete', | |
1526 'page callback' => 'drupal_get_form', | |
1527 'page arguments' => array('node_delete_confirm', 1), | |
1528 'access callback' => 'node_access', | |
1529 'access arguments' => array('delete', 1), | |
1530 'file' => 'node.pages.inc', | |
1531 'weight' => 1, | |
1532 'type' => MENU_CALLBACK); | |
1533 $items['node/%node/revisions'] = array( | |
1534 'title' => 'Revisions', | |
1535 'page callback' => 'node_revision_overview', | |
1536 'page arguments' => array(1), | |
1537 'access callback' => '_node_revision_access', | |
1538 'access arguments' => array(1), | |
1539 'weight' => 2, | |
1540 'file' => 'node.pages.inc', | |
1541 'type' => MENU_LOCAL_TASK, | |
1542 ); | |
1543 $items['node/%node/revisions/%/view'] = array( | |
1544 'title' => 'Revisions', | |
1545 'load arguments' => array(3), | |
1546 'page callback' => 'node_show', | |
1547 'page arguments' => array(1, NULL, TRUE), | |
1548 'type' => MENU_CALLBACK, | |
1549 ); | |
1550 $items['node/%node/revisions/%/revert'] = array( | |
1551 'title' => 'Revert to earlier revision', | |
1552 'load arguments' => array(3), | |
1553 'page callback' => 'drupal_get_form', | |
1554 'page arguments' => array('node_revision_revert_confirm', 1), | |
1555 'access callback' => '_node_revision_access', | |
1556 'access arguments' => array(1, 'update'), | |
1557 'file' => 'node.pages.inc', | |
1558 'type' => MENU_CALLBACK, | |
1559 ); | |
1560 $items['node/%node/revisions/%/delete'] = array( | |
1561 'title' => 'Delete earlier revision', | |
1562 'load arguments' => array(3), | |
1563 'page callback' => 'drupal_get_form', | |
1564 'page arguments' => array('node_revision_delete_confirm', 1), | |
1565 'access callback' => '_node_revision_access', | |
1566 'access arguments' => array(1, 'delete'), | |
1567 'file' => 'node.pages.inc', | |
1568 'type' => MENU_CALLBACK, | |
1569 ); | |
1570 return $items; | |
1571 } | |
1572 | |
1573 /** | |
1574 * Title callback. | |
1575 */ | |
1576 function node_page_title($node) { | |
1577 return $node->title; | |
1578 } | |
1579 | |
1580 /** | |
1581 * Implementation of hook_init(). | |
1582 */ | |
1583 function node_init() { | |
1584 drupal_add_css(drupal_get_path('module', 'node') .'/node.css'); | |
1585 } | |
1586 | |
1587 function node_last_changed($nid) { | |
1588 $node = db_fetch_object(db_query('SELECT changed FROM {node} WHERE nid = %d', $nid)); | |
1589 return ($node->changed); | |
1590 } | |
1591 | |
1592 /** | |
1593 * Return a list of all the existing revision numbers. | |
1594 */ | |
1595 function node_revision_list($node) { | |
1596 $revisions = array(); | |
1597 $result = db_query('SELECT r.vid, r.title, r.log, r.uid, n.vid AS current_vid, r.timestamp, u.name FROM {node_revisions} r LEFT JOIN {node} n ON n.vid = r.vid INNER JOIN {users} u ON u.uid = r.uid WHERE r.nid = %d ORDER BY r.timestamp DESC', $node->nid); | |
1598 while ($revision = db_fetch_object($result)) { | |
1599 $revisions[$revision->vid] = $revision; | |
1600 } | |
1601 | |
1602 return $revisions; | |
1603 } | |
1604 | |
1605 /** | |
1606 * Implementation of hook_block(). | |
1607 */ | |
1608 function node_block($op = 'list', $delta = 0) { | |
1609 if ($op == 'list') { | |
1610 $blocks[0]['info'] = t('Syndicate'); | |
1611 // Not worth caching. | |
1612 $blocks[0]['cache'] = BLOCK_NO_CACHE; | |
1613 return $blocks; | |
1614 } | |
1615 else if ($op == 'view') { | |
1616 $block['subject'] = t('Syndicate'); | |
1617 $block['content'] = theme('feed_icon', url('rss.xml'), t('Syndicate')); | |
1618 | |
1619 return $block; | |
1620 } | |
1621 } | |
1622 | |
1623 /** | |
1624 * A generic function for generating RSS feeds from a set of nodes. | |
1625 * | |
1626 * @param $nids | |
1627 * An array of node IDs (nid). Defaults to FALSE so empty feeds can be | |
1628 * generated with passing an empty array, if no items are to be added | |
1629 * to the feed. | |
1630 * @param $channel | |
1631 * An associative array containing title, link, description and other keys. | |
1632 * The link should be an absolute URL. | |
1633 */ | |
1634 function node_feed($nids = FALSE, $channel = array()) { | |
1635 global $base_url, $language; | |
1636 | |
1637 if ($nids === FALSE) { | |
1638 $nids = array(); | |
1639 $result = db_query_range(db_rewrite_sql('SELECT n.nid, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.created DESC'), 0, variable_get('feed_default_items', 10)); | |
1640 while ($row = db_fetch_object($result)) { | |
1641 $nids[] = $row->nid; | |
1642 } | |
1643 } | |
1644 | |
1645 $item_length = variable_get('feed_item_length', 'teaser'); | |
1646 $namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'); | |
1647 | |
1648 $items = ''; | |
1649 foreach ($nids as $nid) { | |
1650 // Load the specified node: | |
1651 $item = node_load($nid); | |
1652 $item->build_mode = NODE_BUILD_RSS; | |
1653 $item->link = url("node/$nid", array('absolute' => TRUE)); | |
1654 | |
1655 if ($item_length != 'title') { | |
1656 $teaser = ($item_length == 'teaser') ? TRUE : FALSE; | |
1657 | |
1658 // Filter and prepare node teaser | |
1659 if (node_hook($item, 'view')) { | |
1660 $item = node_invoke($item, 'view', $teaser, FALSE); | |
1661 } | |
1662 else { | |
1663 $item = node_prepare($item, $teaser); | |
1664 } | |
1665 | |
1666 // Allow modules to change $node->teaser before viewing. | |
1667 node_invoke_nodeapi($item, 'view', $teaser, FALSE); | |
1668 } | |
1669 | |
1670 // Allow modules to add additional item fields and/or modify $item | |
1671 $extra = node_invoke_nodeapi($item, 'rss item'); | |
1672 $extra = array_merge($extra, array(array('key' => 'pubDate', 'value' => format_date($item->created, 'custom', 'r')), array('key' => 'dc:creator', 'value' => $item->name), array('key' => 'guid', 'value' => $item->nid .' at '. $base_url, 'attributes' => array('isPermaLink' => 'false')))); | |
1673 foreach ($extra as $element) { | |
1674 if (isset($element['namespace'])) { | |
1675 $namespaces = array_merge($namespaces, $element['namespace']); | |
1676 } | |
1677 } | |
1678 | |
1679 // Prepare the item description | |
1680 switch ($item_length) { | |
1681 case 'fulltext': | |
1682 $item_text = $item->body; | |
1683 break; | |
1684 case 'teaser': | |
1685 $item_text = $item->teaser; | |
1686 if (!empty($item->readmore)) { | |
1687 $item_text .= '<p>'. l(t('read more'), 'node/'. $item->nid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) .'</p>'; | |
1688 } | |
1689 break; | |
1690 case 'title': | |
1691 $item_text = ''; | |
1692 break; | |
1693 } | |
1694 | |
1695 $items .= format_rss_item($item->title, $item->link, $item_text, $extra); | |
1696 } | |
1697 | |
1698 $channel_defaults = array( | |
1699 'version' => '2.0', | |
1700 'title' => variable_get('site_name', 'Drupal'), | |
1701 'link' => $base_url, | |
1702 'description' => variable_get('site_mission', ''), | |
1703 'language' => $language->language | |
1704 ); | |
1705 $channel = array_merge($channel_defaults, $channel); | |
1706 | |
1707 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; | |
1708 $output .= "<rss version=\"". $channel["version"] ."\" xml:base=\"". $base_url ."\" ". drupal_attributes($namespaces) .">\n"; | |
1709 $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']); | |
1710 $output .= "</rss>\n"; | |
1711 | |
1712 drupal_set_header('Content-Type: application/rss+xml; charset=utf-8'); | |
1713 print $output; | |
1714 } | |
1715 | |
1716 /** | |
1717 * Menu callback; Generate a listing of promoted nodes. | |
1718 */ | |
1719 function node_page_default() { | |
1720 $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10)); | |
1721 | |
1722 $output = ''; | |
1723 $num_rows = FALSE; | |
1724 while ($node = db_fetch_object($result)) { | |
1725 $output .= node_view(node_load($node->nid), 1); | |
1726 $num_rows = TRUE; | |
1727 } | |
1728 | |
1729 if ($num_rows) { | |
1730 $feed_url = url('rss.xml', array('absolute' => TRUE)); | |
1731 drupal_add_feed($feed_url, variable_get('site_name', 'Drupal') .' '. t('RSS')); | |
1732 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); | |
1733 } | |
1734 else { | |
1735 $default_message = t('<h1 class="title">Welcome to your new Drupal website!</h1><p>Please follow these steps to set up and start using your website:</p>'); | |
1736 $default_message .= '<ol>'; | |
1737 | |
1738 $default_message .= '<li>'. t('<strong>Configure your website</strong> Once logged in, visit the <a href="@admin">administration section</a>, where you can <a href="@config">customize and configure</a> all aspects of your website.', array('@admin' => url('admin'), '@config' => url('admin/settings'))) .'</li>'; | |
1739 $default_message .= '<li>'. t('<strong>Enable additional functionality</strong> Next, visit the <a href="@modules">module list</a> and enable features which suit your specific needs. You can find additional modules in the <a href="@download_modules">Drupal modules download section</a>.', array('@modules' => url('admin/build/modules'), '@download_modules' => 'http://drupal.org/project/modules')) .'</li>'; | |
1740 $default_message .= '<li>'. t('<strong>Customize your website design</strong> To change the "look and feel" of your website, visit the <a href="@themes">themes section</a>. You may choose from one of the included themes or download additional themes from the <a href="@download_themes">Drupal themes download section</a>.', array('@themes' => url('admin/build/themes'), '@download_themes' => 'http://drupal.org/project/themes')) .'</li>'; | |
1741 $default_message .= '<li>'. t('<strong>Start posting content</strong> Finally, you can <a href="@content">create content</a> for your website. This message will disappear once you have promoted a post to the front page.', array('@content' => url('node/add'))) .'</li>'; | |
1742 $default_message .= '</ol>'; | |
1743 $default_message .= '<p>'. t('For more information, please refer to the <a href="@help">help section</a>, or the <a href="@handbook">online Drupal handbooks</a>. You may also post at the <a href="@forum">Drupal forum</a>, or view the wide range of <a href="@support">other support options</a> available.', array('@help' => url('admin/help'), '@handbook' => 'http://drupal.org/handbooks', '@forum' => 'http://drupal.org/forum', '@support' => 'http://drupal.org/support')) .'</p>'; | |
1744 | |
1745 $output = '<div id="first-time">'. $default_message .'</div>'; | |
1746 } | |
1747 drupal_set_title(''); | |
1748 | |
1749 return $output; | |
1750 } | |
1751 | |
1752 /** | |
1753 * Menu callback; view a single node. | |
1754 */ | |
1755 function node_page_view($node, $cid = NULL) { | |
1756 drupal_set_title(check_plain($node->title)); | |
1757 return node_show($node, $cid); | |
1758 } | |
1759 | |
1760 /** | |
1761 * Implementation of hook_update_index(). | |
1762 */ | |
1763 function node_update_index() { | |
1764 $limit = (int)variable_get('search_cron_limit', 100); | |
1765 | |
1766 // Store the maximum possible comments per thread (used for ranking by reply count) | |
1767 variable_set('node_cron_comments_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}')))); | |
1768 variable_set('node_cron_views_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(totalcount) FROM {node_counter}')))); | |
1769 | |
1770 $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit); | |
1771 | |
1772 while ($node = db_fetch_object($result)) { | |
1773 _node_index_node($node); | |
1774 } | |
1775 } | |
1776 | |
1777 /** | |
1778 * Index a single node. | |
1779 * | |
1780 * @param $node | |
1781 * The node to index. | |
1782 */ | |
1783 function _node_index_node($node) { | |
1784 $node = node_load($node->nid); | |
1785 | |
1786 // save the changed time of the most recent indexed node, for the search results half-life calculation | |
1787 variable_set('node_cron_last', $node->changed); | |
1788 | |
1789 // Build the node body. | |
1790 $node->build_mode = NODE_BUILD_SEARCH_INDEX; | |
1791 $node = node_build_content($node, FALSE, FALSE); | |
1792 $node->body = drupal_render($node->content); | |
1793 | |
1794 $text = '<h1>'. check_plain($node->title) .'</h1>'. $node->body; | |
1795 | |
1796 // Fetch extra data normally not visible | |
1797 $extra = node_invoke_nodeapi($node, 'update index'); | |
1798 foreach ($extra as $t) { | |
1799 $text .= $t; | |
1800 } | |
1801 | |
1802 // Update index | |
1803 search_index($node->nid, 'node', $text); | |
1804 } | |
1805 | |
1806 /** | |
1807 * Implementation of hook_form_alter(). | |
1808 */ | |
1809 function node_form_alter(&$form, $form_state, $form_id) { | |
1810 // Advanced node search form | |
1811 if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) { | |
1812 // Keyword boxes: | |
1813 $form['advanced'] = array( | |
1814 '#type' => 'fieldset', | |
1815 '#title' => t('Advanced search'), | |
1816 '#collapsible' => TRUE, | |
1817 '#collapsed' => TRUE, | |
1818 '#attributes' => array('class' => 'search-advanced'), | |
1819 ); | |
1820 $form['advanced']['keywords'] = array( | |
1821 '#prefix' => '<div class="criterion">', | |
1822 '#suffix' => '</div>', | |
1823 ); | |
1824 $form['advanced']['keywords']['or'] = array( | |
1825 '#type' => 'textfield', | |
1826 '#title' => t('Containing any of the words'), | |
1827 '#size' => 30, | |
1828 '#maxlength' => 255, | |
1829 ); | |
1830 $form['advanced']['keywords']['phrase'] = array( | |
1831 '#type' => 'textfield', | |
1832 '#title' => t('Containing the phrase'), | |
1833 '#size' => 30, | |
1834 '#maxlength' => 255, | |
1835 ); | |
1836 $form['advanced']['keywords']['negative'] = array( | |
1837 '#type' => 'textfield', | |
1838 '#title' => t('Containing none of the words'), | |
1839 '#size' => 30, | |
1840 '#maxlength' => 255, | |
1841 ); | |
1842 | |
1843 // Taxonomy box: | |
1844 if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) { | |
1845 $form['advanced']['category'] = array( | |
1846 '#type' => 'select', | |
1847 '#title' => t('Only in the category(s)'), | |
1848 '#prefix' => '<div class="criterion">', | |
1849 '#size' => 10, | |
1850 '#suffix' => '</div>', | |
1851 '#options' => $taxonomy, | |
1852 '#multiple' => TRUE, | |
1853 ); | |
1854 } | |
1855 | |
1856 // Node types: | |
1857 $types = array_map('check_plain', node_get_types('names')); | |
1858 $form['advanced']['type'] = array( | |
1859 '#type' => 'checkboxes', | |
1860 '#title' => t('Only of the type(s)'), | |
1861 '#prefix' => '<div class="criterion">', | |
1862 '#suffix' => '</div>', | |
1863 '#options' => $types, | |
1864 ); | |
1865 $form['advanced']['submit'] = array( | |
1866 '#type' => 'submit', | |
1867 '#value' => t('Advanced search'), | |
1868 '#prefix' => '<div class="action">', | |
1869 '#suffix' => '</div>', | |
1870 ); | |
1871 | |
1872 $form['#validate'][] = 'node_search_validate'; | |
1873 } | |
1874 } | |
1875 | |
1876 /** | |
1877 * Form API callback for the search form. Registered in node_form_alter(). | |
1878 */ | |
1879 function node_search_validate($form, &$form_state) { | |
1880 // Initialise using any existing basic search keywords. | |
1881 $keys = $form_state['values']['processed_keys']; | |
1882 | |
1883 // Insert extra restrictions into the search keywords string. | |
1884 if (isset($form_state['values']['type']) && is_array($form_state['values']['type'])) { | |
1885 // Retrieve selected types - Forms API sets the value of unselected checkboxes to 0. | |
1886 $form_state['values']['type'] = array_filter($form_state['values']['type']); | |
1887 if (count($form_state['values']['type'])) { | |
1888 $keys = search_query_insert($keys, 'type', implode(',', array_keys($form_state['values']['type']))); | |
1889 } | |
1890 } | |
1891 | |
1892 if (isset($form_state['values']['category']) && is_array($form_state['values']['category'])) { | |
1893 $keys = search_query_insert($keys, 'category', implode(',', $form_state['values']['category'])); | |
1894 } | |
1895 if ($form_state['values']['or'] != '') { | |
1896 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' '. $form_state['values']['or'], $matches)) { | |
1897 $keys .= ' '. implode(' OR ', $matches[1]); | |
1898 } | |
1899 } | |
1900 if ($form_state['values']['negative'] != '') { | |
1901 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' '. $form_state['values']['negative'], $matches)) { | |
1902 $keys .= ' -'. implode(' -', $matches[1]); | |
1903 } | |
1904 } | |
1905 if ($form_state['values']['phrase'] != '') { | |
1906 $keys .= ' "'. str_replace('"', ' ', $form_state['values']['phrase']) .'"'; | |
1907 } | |
1908 if (!empty($keys)) { | |
1909 form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state); | |
1910 } | |
1911 } | |
1912 | |
1913 /** | |
1914 * @defgroup node_access Node access rights | |
1915 * @{ | |
1916 * The node access system determines who can do what to which nodes. | |
1917 * | |
1918 * In determining access rights for a node, node_access() first checks | |
1919 * whether the user has the "administer nodes" permission. Such users have | |
1920 * unrestricted access to all nodes. Then the node module's hook_access() | |
1921 * is called, and a TRUE or FALSE return value will grant or deny access. | |
1922 * This allows, for example, the blog module to always grant access to the | |
1923 * blog author, and for the book module to always deny editing access to | |
1924 * PHP pages. | |
1925 * | |
1926 * If node module does not intervene (returns NULL), then the | |
1927 * node_access table is used to determine access. All node access | |
1928 * modules are queried using hook_node_grants() to assemble a list of | |
1929 * "grant IDs" for the user. This list is compared against the table. | |
1930 * If any row contains the node ID in question (or 0, which stands for "all | |
1931 * nodes"), one of the grant IDs returned, and a value of TRUE for the | |
1932 * operation in question, then access is granted. Note that this table is a | |
1933 * list of grants; any matching row is sufficient to grant access to the | |
1934 * node. | |
1935 * | |
1936 * In node listings, the process above is followed except that | |
1937 * hook_access() is not called on each node for performance reasons and for | |
1938 * proper functioning of the pager system. When adding a node listing to your | |
1939 * module, be sure to use db_rewrite_sql() to add | |
1940 * the appropriate clauses to your query for access checks. | |
1941 * | |
1942 * To see how to write a node access module of your own, see | |
1943 * node_access_example.module. | |
1944 */ | |
1945 | |
1946 /** | |
1947 * Determine whether the current user may perform the given operation on the | |
1948 * specified node. | |
1949 * | |
1950 * @param $op | |
1951 * The operation to be performed on the node. Possible values are: | |
1952 * - "view" | |
1953 * - "update" | |
1954 * - "delete" | |
1955 * - "create" | |
1956 * @param $node | |
1957 * The node object (or node array) on which the operation is to be performed, | |
1958 * or node type (e.g. 'forum') for "create" operation. | |
1959 * @param $account | |
1960 * Optional, a user object representing the user for whom the operation is to | |
1961 * be performed. Determines access for a user other than the current user. | |
1962 * @return | |
1963 * TRUE if the operation may be performed. | |
1964 */ | |
1965 function node_access($op, $node, $account = NULL) { | |
1966 global $user; | |
1967 | |
1968 if (!$node) { | |
1969 return FALSE; | |
1970 } | |
1971 // Convert the node to an object if necessary: | |
1972 if ($op != 'create') { | |
1973 $node = (object)$node; | |
1974 } | |
1975 // If no user object is supplied, the access check is for the current user. | |
1976 if (empty($account)) { | |
1977 $account = $user; | |
1978 } | |
1979 // If the node is in a restricted format, disallow editing. | |
1980 if ($op == 'update' && !filter_access($node->format)) { | |
1981 return FALSE; | |
1982 } | |
1983 | |
1984 if (user_access('administer nodes', $account)) { | |
1985 return TRUE; | |
1986 } | |
1987 | |
1988 if (!user_access('access content', $account)) { | |
1989 return FALSE; | |
1990 } | |
1991 | |
1992 // Can't use node_invoke(), because the access hook takes the $op parameter | |
1993 // before the $node parameter. | |
1994 $module = node_get_types('module', $node); | |
1995 if ($module == 'node') { | |
1996 $module = 'node_content'; // Avoid function name collisions. | |
1997 } | |
1998 $access = module_invoke($module, 'access', $op, $node, $account); | |
1999 if (!is_null($access)) { | |
2000 return $access; | |
2001 } | |
2002 | |
2003 // If the module did not override the access rights, use those set in the | |
2004 // node_access table. | |
2005 if ($op != 'create' && $node->nid && $node->status) { | |
2006 $grants = array(); | |
2007 foreach (node_access_grants($op, $account) as $realm => $gids) { | |
2008 foreach ($gids as $gid) { | |
2009 $grants[] = "(gid = $gid AND realm = '$realm')"; | |
2010 } | |
2011 } | |
2012 | |
2013 $grants_sql = ''; | |
2014 if (count($grants)) { | |
2015 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; | |
2016 } | |
2017 | |
2018 $sql = "SELECT COUNT(*) FROM {node_access} WHERE (nid = 0 OR nid = %d) $grants_sql AND grant_$op >= 1"; | |
2019 $result = db_query($sql, $node->nid); | |
2020 return (db_result($result)); | |
2021 } | |
2022 | |
2023 // Let authors view their own nodes. | |
2024 if ($op == 'view' && $account->uid == $node->uid && $account->uid != 0) { | |
2025 return TRUE; | |
2026 } | |
2027 | |
2028 return FALSE; | |
2029 } | |
2030 | |
2031 /** | |
2032 * Generate an SQL join clause for use in fetching a node listing. | |
2033 * | |
2034 * @param $node_alias | |
2035 * If the node table has been given an SQL alias other than the default | |
2036 * "n", that must be passed here. | |
2037 * @param $node_access_alias | |
2038 * If the node_access table has been given an SQL alias other than the default | |
2039 * "na", that must be passed here. | |
2040 * @return | |
2041 * An SQL join clause. | |
2042 */ | |
2043 function _node_access_join_sql($node_alias = 'n', $node_access_alias = 'na') { | |
2044 if (user_access('administer nodes')) { | |
2045 return ''; | |
2046 } | |
2047 | |
2048 return 'INNER JOIN {node_access} '. $node_access_alias .' ON '. $node_access_alias .'.nid = '. $node_alias .'.nid'; | |
2049 } | |
2050 | |
2051 /** | |
2052 * Generate an SQL where clause for use in fetching a node listing. | |
2053 * | |
2054 * @param $op | |
2055 * The operation that must be allowed to return a node. | |
2056 * @param $node_access_alias | |
2057 * If the node_access table has been given an SQL alias other than the default | |
2058 * "na", that must be passed here. | |
2059 * @param $account | |
2060 * The user object for the user performing the operation. If omitted, the | |
2061 * current user is used. | |
2062 * @return | |
2063 * An SQL where clause. | |
2064 */ | |
2065 function _node_access_where_sql($op = 'view', $node_access_alias = 'na', $account = NULL) { | |
2066 if (user_access('administer nodes')) { | |
2067 return; | |
2068 } | |
2069 | |
2070 $grants = array(); | |
2071 foreach (node_access_grants($op, $account) as $realm => $gids) { | |
2072 foreach ($gids as $gid) { | |
2073 $grants[] = "($node_access_alias.gid = $gid AND $node_access_alias.realm = '$realm')"; | |
2074 } | |
2075 } | |
2076 | |
2077 $grants_sql = ''; | |
2078 if (count($grants)) { | |
2079 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; | |
2080 } | |
2081 | |
2082 $sql = "$node_access_alias.grant_$op >= 1 $grants_sql"; | |
2083 return $sql; | |
2084 } | |
2085 | |
2086 /** | |
2087 * Fetch an array of permission IDs granted to the given user ID. | |
2088 * | |
2089 * The implementation here provides only the universal "all" grant. A node | |
2090 * access module should implement hook_node_grants() to provide a grant | |
2091 * list for the user. | |
2092 * | |
2093 * @param $op | |
2094 * The operation that the user is trying to perform. | |
2095 * @param $account | |
2096 * The user object for the user performing the operation. If omitted, the | |
2097 * current user is used. | |
2098 * @return | |
2099 * An associative array in which the keys are realms, and the values are | |
2100 * arrays of grants for those realms. | |
2101 */ | |
2102 function node_access_grants($op, $account = NULL) { | |
2103 | |
2104 if (!isset($account)) { | |
2105 $account = $GLOBALS['user']; | |
2106 } | |
2107 | |
2108 return array_merge(array('all' => array(0)), module_invoke_all('node_grants', $account, $op)); | |
2109 } | |
2110 | |
2111 /** | |
2112 * Determine whether the user has a global viewing grant for all nodes. | |
2113 */ | |
2114 function node_access_view_all_nodes() { | |
2115 static $access; | |
2116 | |
2117 if (!isset($access)) { | |
2118 $grants = array(); | |
2119 foreach (node_access_grants('view') as $realm => $gids) { | |
2120 foreach ($gids as $gid) { | |
2121 $grants[] = "(gid = $gid AND realm = '$realm')"; | |
2122 } | |
2123 } | |
2124 | |
2125 $grants_sql = ''; | |
2126 if (count($grants)) { | |
2127 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; | |
2128 } | |
2129 | |
2130 $sql = "SELECT COUNT(*) FROM {node_access} WHERE nid = 0 $grants_sql AND grant_view >= 1"; | |
2131 $result = db_query($sql); | |
2132 $access = db_result($result); | |
2133 } | |
2134 | |
2135 return $access; | |
2136 } | |
2137 | |
2138 /** | |
2139 * Implementation of hook_db_rewrite_sql | |
2140 */ | |
2141 function node_db_rewrite_sql($query, $primary_table, $primary_field) { | |
2142 if ($primary_field == 'nid' && !node_access_view_all_nodes()) { | |
2143 $return['join'] = _node_access_join_sql($primary_table); | |
2144 $return['where'] = _node_access_where_sql(); | |
2145 $return['distinct'] = 1; | |
2146 return $return; | |
2147 } | |
2148 } | |
2149 | |
2150 /** | |
2151 * This function will call module invoke to get a list of grants and then | |
2152 * write them to the database. It is called at node save, and should be | |
2153 * called by modules whenever something other than a node_save causes | |
2154 * the permissions on a node to change. | |
2155 * | |
2156 * This function is the only function that should write to the node_access | |
2157 * table. | |
2158 * | |
2159 * @param $node | |
2160 * The $node to acquire grants for. | |
2161 */ | |
2162 function node_access_acquire_grants($node) { | |
2163 $grants = module_invoke_all('node_access_records', $node); | |
2164 if (empty($grants)) { | |
2165 $grants[] = array('realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0); | |
2166 } | |
2167 else { | |
2168 // retain grants by highest priority | |
2169 $grant_by_priority = array(); | |
2170 foreach ($grants as $g) { | |
2171 $grant_by_priority[intval($g['priority'])][] = $g; | |
2172 } | |
2173 krsort($grant_by_priority); | |
2174 $grants = array_shift($grant_by_priority); | |
2175 } | |
2176 | |
2177 node_access_write_grants($node, $grants); | |
2178 } | |
2179 | |
2180 /** | |
2181 * This function will write a list of grants to the database, deleting | |
2182 * any pre-existing grants. If a realm is provided, it will only | |
2183 * delete grants from that realm, but it will always delete a grant | |
2184 * from the 'all' realm. Modules which utilize node_access can | |
2185 * use this function when doing mass updates due to widespread permission | |
2186 * changes. | |
2187 * | |
2188 * @param $node | |
2189 * The $node being written to. All that is necessary is that it contain a nid. | |
2190 * @param $grants | |
2191 * A list of grants to write. Each grant is an array that must contain the | |
2192 * following keys: realm, gid, grant_view, grant_update, grant_delete. | |
2193 * The realm is specified by a particular module; the gid is as well, and | |
2194 * is a module-defined id to define grant privileges. each grant_* field | |
2195 * is a boolean value. | |
2196 * @param $realm | |
2197 * If provided, only read/write grants for that realm. | |
2198 * @param $delete | |
2199 * If false, do not delete records. This is only for optimization purposes, | |
2200 * and assumes the caller has already performed a mass delete of some form. | |
2201 */ | |
2202 function node_access_write_grants($node, $grants, $realm = NULL, $delete = TRUE) { | |
2203 if ($delete) { | |
2204 $query = 'DELETE FROM {node_access} WHERE nid = %d'; | |
2205 if ($realm) { | |
2206 $query .= " AND realm in ('%s', 'all')"; | |
2207 } | |
2208 db_query($query, $node->nid, $realm); | |
2209 } | |
2210 | |
2211 // Only perform work when node_access modules are active. | |
2212 if (count(module_implements('node_grants'))) { | |
2213 foreach ($grants as $grant) { | |
2214 if ($realm && $realm != $grant['realm']) { | |
2215 continue; | |
2216 } | |
2217 // Only write grants; denies are implicit. | |
2218 if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) { | |
2219 db_query("INSERT INTO {node_access} (nid, realm, gid, grant_view, grant_update, grant_delete) VALUES (%d, '%s', %d, %d, %d, %d)", $node->nid, $grant['realm'], $grant['gid'], $grant['grant_view'], $grant['grant_update'], $grant['grant_delete']); | |
2220 } | |
2221 } | |
2222 } | |
2223 } | |
2224 | |
2225 /** | |
2226 * Flag / unflag the node access grants for rebuilding, or read the current | |
2227 * value of the flag. | |
2228 * | |
2229 * When the flag is set, a message is displayed to users with 'access | |
2230 * administration pages' permission, pointing to the 'rebuild' confirm form. | |
2231 * This can be used as an alternative to direct node_access_rebuild calls, | |
2232 * allowing administrators to decide when they want to perform the actual | |
2233 * (possibly time consuming) rebuild. | |
2234 * When unsure the current user is an adminisrator, node_access_rebuild | |
2235 * should be used instead. | |
2236 * | |
2237 * @param $rebuild | |
2238 * (Optional) The boolean value to be written. | |
2239 * @return | |
2240 * (If no value was provided for $rebuild) The current value of the flag. | |
2241 */ | |
2242 function node_access_needs_rebuild($rebuild = NULL) { | |
2243 if (!isset($rebuild)) { | |
2244 return variable_get('node_access_needs_rebuild', FALSE); | |
2245 } | |
2246 elseif ($rebuild) { | |
2247 variable_set('node_access_needs_rebuild', TRUE); | |
2248 } | |
2249 else { | |
2250 variable_del('node_access_needs_rebuild'); | |
2251 } | |
2252 } | |
2253 | |
2254 /** | |
2255 * Rebuild the node access database. This is occasionally needed by modules | |
2256 * that make system-wide changes to access levels. | |
2257 * | |
2258 * When the rebuild is required by an admin-triggered action (e.g module | |
2259 * settings form), calling node_access_needs_rebuild(TRUE) instead of | |
2260 * node_access_rebuild() lets the user perform his changes and actually | |
2261 * rebuild only once he is done. | |
2262 * | |
2263 * Note : As of Drupal 6, node access modules are not required to (and actually | |
2264 * should not) call node_access_rebuild() in hook_enable/disable anymore. | |
2265 * | |
2266 * @see node_access_needs_rebuild() | |
2267 * | |
2268 * @param $batch_mode | |
2269 * Set to TRUE to process in 'batch' mode, spawning processing over several | |
2270 * HTTP requests (thus avoiding the risk of PHP timeout if the site has a | |
2271 * large number of nodes). | |
2272 * hook_update_N and any form submit handler are safe contexts to use the | |
2273 * 'batch mode'. Less decidable cases (such as calls from hook_user, | |
2274 * hook_taxonomy, hook_node_type...) might consider using the non-batch mode. | |
2275 */ | |
2276 function node_access_rebuild($batch_mode = FALSE) { | |
2277 db_query("DELETE FROM {node_access}"); | |
2278 // Only recalculate if the site is using a node_access module. | |
2279 if (count(module_implements('node_grants'))) { | |
2280 if ($batch_mode) { | |
2281 $batch = array( | |
2282 'title' => t('Rebuilding content access permissions'), | |
2283 'operations' => array( | |
2284 array('_node_access_rebuild_batch_operation', array()), | |
2285 ), | |
2286 'finished' => '_node_access_rebuild_batch_finished' | |
2287 ); | |
2288 batch_set($batch); | |
2289 } | |
2290 else { | |
2291 // If not in 'safe mode', increase the maximum execution time. | |
2292 if (!ini_get('safe_mode')) { | |
2293 set_time_limit(240); | |
2294 } | |
2295 $result = db_query("SELECT nid FROM {node}"); | |
2296 while ($node = db_fetch_object($result)) { | |
2297 $loaded_node = node_load($node->nid, NULL, TRUE); | |
2298 // To preserve database integrity, only aquire grants if the node | |
2299 // loads successfully. | |
2300 if (!empty($loaded_node)) { | |
2301 node_access_acquire_grants($loaded_node); | |
2302 } | |
2303 } | |
2304 } | |
2305 } | |
2306 else { | |
2307 // Not using any node_access modules. Add the default grant. | |
2308 db_query("INSERT INTO {node_access} VALUES (0, 0, 'all', 1, 0, 0)"); | |
2309 } | |
2310 | |
2311 if (!isset($batch)) { | |
2312 drupal_set_message(t('Content permissions have been rebuilt.')); | |
2313 node_access_needs_rebuild(FALSE); | |
2314 cache_clear_all(); | |
2315 } | |
2316 } | |
2317 | |
2318 /** | |
2319 * Batch operation for node_access_rebuild_batch. | |
2320 * | |
2321 * This is a mutlistep operation : we go through all nodes by packs of 20. | |
2322 * The batch processing engine interrupts processing and sends progress | |
2323 * feedback after 1 second execution time. | |
2324 */ | |
2325 function _node_access_rebuild_batch_operation(&$context) { | |
2326 if (empty($context['sandbox'])) { | |
2327 // Initiate multistep processing. | |
2328 $context['sandbox']['progress'] = 0; | |
2329 $context['sandbox']['current_node'] = 0; | |
2330 $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}')); | |
2331 } | |
2332 | |
2333 // Process the next 20 nodes. | |
2334 $limit = 20; | |
2335 $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit); | |
2336 while ($row = db_fetch_array($result)) { | |
2337 $loaded_node = node_load($row['nid'], NULL, TRUE); | |
2338 // To preserve database integrity, only aquire grants if the node | |
2339 // loads successfully. | |
2340 if (!empty($loaded_node)) { | |
2341 node_access_acquire_grants($loaded_node); | |
2342 } | |
2343 $context['sandbox']['progress']++; | |
2344 $context['sandbox']['current_node'] = $loaded_node->nid; | |
2345 } | |
2346 | |
2347 // Multistep processing : report progress. | |
2348 if ($context['sandbox']['progress'] != $context['sandbox']['max']) { | |
2349 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; | |
2350 } | |
2351 } | |
2352 | |
2353 /** | |
2354 * Post-processing for node_access_rebuild_batch. | |
2355 */ | |
2356 function _node_access_rebuild_batch_finished($success, $results, $operations) { | |
2357 if ($success) { | |
2358 drupal_set_message(t('The content access permissions have been rebuilt.')); | |
2359 node_access_needs_rebuild(FALSE); | |
2360 } | |
2361 else { | |
2362 drupal_set_message(t('The content access permissions have not been properly rebuilt.'), 'error'); | |
2363 } | |
2364 cache_clear_all(); | |
2365 } | |
2366 | |
2367 /** | |
2368 * @} End of "defgroup node_access". | |
2369 */ | |
2370 | |
2371 | |
2372 /** | |
2373 * @defgroup node_content Hook implementations for user-created content types. | |
2374 * @{ | |
2375 */ | |
2376 | |
2377 /** | |
2378 * Implementation of hook_access(). | |
2379 * | |
2380 * Named so as not to conflict with node_access() | |
2381 */ | |
2382 function node_content_access($op, $node, $account) { | |
2383 $type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type); | |
2384 | |
2385 if ($op == 'create') { | |
2386 return user_access('create '. $type .' content', $account); | |
2387 } | |
2388 | |
2389 if ($op == 'update') { | |
2390 if (user_access('edit any '. $type .' content', $account) || (user_access('edit own '. $type .' content', $account) && ($account->uid == $node->uid))) { | |
2391 return TRUE; | |
2392 } | |
2393 } | |
2394 | |
2395 if ($op == 'delete') { | |
2396 if (user_access('delete any '. $type .' content', $account) || (user_access('delete own '. $type .' content', $account) && ($account->uid == $node->uid))) { | |
2397 return TRUE; | |
2398 } | |
2399 } | |
2400 } | |
2401 | |
2402 /** | |
2403 * Implementation of hook_form(). | |
2404 */ | |
2405 function node_content_form($node, $form_state) { | |
2406 $type = node_get_types('type', $node); | |
2407 $form = array(); | |
2408 | |
2409 if ($type->has_title) { | |
2410 $form['title'] = array( | |
2411 '#type' => 'textfield', | |
2412 '#title' => check_plain($type->title_label), | |
2413 '#required' => TRUE, | |
2414 '#default_value' => $node->title, | |
2415 '#maxlength' => 255, | |
2416 '#weight' => -5, | |
2417 ); | |
2418 } | |
2419 | |
2420 if ($type->has_body) { | |
2421 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count); | |
2422 } | |
2423 | |
2424 return $form; | |
2425 } | |
2426 | |
2427 /** | |
2428 * @} End of "defgroup node_content". | |
2429 */ | |
2430 | |
2431 /** | |
2432 * Implementation of hook_forms(). All node forms share the same form handler | |
2433 */ | |
2434 function node_forms() { | |
2435 $forms = array(); | |
2436 if ($types = node_get_types()) { | |
2437 foreach (array_keys($types) as $type) { | |
2438 $forms[$type .'_node_form']['callback'] = 'node_form'; | |
2439 } | |
2440 } | |
2441 return $forms; | |
2442 } | |
2443 | |
2444 /** | |
2445 * Format the "Submitted by username on date/time" for each node | |
2446 * | |
2447 * @ingroup themeable | |
2448 */ | |
2449 function theme_node_submitted($node) { | |
2450 return t('Submitted by !username on @datetime', | |
2451 array( | |
2452 '!username' => theme('username', $node), | |
2453 '@datetime' => format_date($node->created), | |
2454 )); | |
2455 } | |
2456 | |
2457 /** | |
2458 * Implementation of hook_hook_info(). | |
2459 */ | |
2460 function node_hook_info() { | |
2461 return array( | |
2462 'node' => array( | |
2463 'nodeapi' => array( | |
2464 'presave' => array( | |
2465 'runs when' => t('When either saving a new post or updating an existing post'), | |
2466 ), | |
2467 'insert' => array( | |
2468 'runs when' => t('After saving a new post'), | |
2469 ), | |
2470 'update' => array( | |
2471 'runs when' => t('After saving an updated post'), | |
2472 ), | |
2473 'delete' => array( | |
2474 'runs when' => t('After deleting a post') | |
2475 ), | |
2476 'view' => array( | |
2477 'runs when' => t('When content is viewed by an authenticated user') | |
2478 ), | |
2479 ), | |
2480 ), | |
2481 ); | |
2482 } | |
2483 | |
2484 /** | |
2485 * Implementation of hook_action_info(). | |
2486 */ | |
2487 function node_action_info() { | |
2488 return array( | |
2489 'node_publish_action' => array( | |
2490 'type' => 'node', | |
2491 'description' => t('Publish post'), | |
2492 'configurable' => FALSE, | |
2493 'behavior' => array('changes_node_property'), | |
2494 'hooks' => array( | |
2495 'nodeapi' => array('presave'), | |
2496 'comment' => array('insert', 'update'), | |
2497 ), | |
2498 ), | |
2499 'node_unpublish_action' => array( | |
2500 'type' => 'node', | |
2501 'description' => t('Unpublish post'), | |
2502 'configurable' => FALSE, | |
2503 'behavior' => array('changes_node_property'), | |
2504 'hooks' => array( | |
2505 'nodeapi' => array('presave'), | |
2506 'comment' => array('delete', 'insert', 'update'), | |
2507 ), | |
2508 ), | |
2509 'node_make_sticky_action' => array( | |
2510 'type' => 'node', | |
2511 'description' => t('Make post sticky'), | |
2512 'configurable' => FALSE, | |
2513 'behavior' => array('changes_node_property'), | |
2514 'hooks' => array( | |
2515 'nodeapi' => array('presave'), | |
2516 'comment' => array('insert', 'update'), | |
2517 ), | |
2518 ), | |
2519 'node_make_unsticky_action' => array( | |
2520 'type' => 'node', | |
2521 'description' => t('Make post unsticky'), | |
2522 'configurable' => FALSE, | |
2523 'behavior' => array('changes_node_property'), | |
2524 'hooks' => array( | |
2525 'nodeapi' => array('presave'), | |
2526 'comment' => array('delete', 'insert', 'update'), | |
2527 ), | |
2528 ), | |
2529 'node_promote_action' => array( | |
2530 'type' => 'node', | |
2531 'description' => t('Promote post to front page'), | |
2532 'configurable' => FALSE, | |
2533 'behavior' => array('changes_node_property'), | |
2534 'hooks' => array( | |
2535 'nodeapi' => array('presave'), | |
2536 'comment' => array('insert', 'update'), | |
2537 ), | |
2538 ), | |
2539 'node_unpromote_action' => array( | |
2540 'type' => 'node', | |
2541 'description' => t('Remove post from front page'), | |
2542 'configurable' => FALSE, | |
2543 'behavior' => array('changes_node_property'), | |
2544 'hooks' => array( | |
2545 'nodeapi' => array('presave'), | |
2546 'comment' => array('delete', 'insert', 'update'), | |
2547 ), | |
2548 ), | |
2549 'node_assign_owner_action' => array( | |
2550 'type' => 'node', | |
2551 'description' => t('Change the author of a post'), | |
2552 'configurable' => TRUE, | |
2553 'behavior' => array('changes_node_property'), | |
2554 'hooks' => array( | |
2555 'any' => TRUE, | |
2556 'nodeapi' => array('presave'), | |
2557 'comment' => array('delete', 'insert', 'update'), | |
2558 ), | |
2559 ), | |
2560 'node_save_action' => array( | |
2561 'type' => 'node', | |
2562 'description' => t('Save post'), | |
2563 'configurable' => FALSE, | |
2564 'hooks' => array( | |
2565 'comment' => array('delete', 'insert', 'update'), | |
2566 ), | |
2567 ), | |
2568 'node_unpublish_by_keyword_action' => array( | |
2569 'type' => 'node', | |
2570 'description' => t('Unpublish post containing keyword(s)'), | |
2571 'configurable' => TRUE, | |
2572 'hooks' => array( | |
2573 'nodeapi' => array('presave', 'insert', 'update'), | |
2574 ), | |
2575 ), | |
2576 ); | |
2577 } | |
2578 | |
2579 /** | |
2580 * Implementation of a Drupal action. | |
2581 * Sets the status of a node to 1, meaning published. | |
2582 */ | |
2583 function node_publish_action(&$node, $context = array()) { | |
2584 $node->status = 1; | |
2585 watchdog('action', 'Set @type %title to published.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2586 } | |
2587 | |
2588 /** | |
2589 * Implementation of a Drupal action. | |
2590 * Sets the status of a node to 0, meaning unpublished. | |
2591 */ | |
2592 function node_unpublish_action(&$node, $context = array()) { | |
2593 $node->status = 0; | |
2594 watchdog('action', 'Set @type %title to unpublished.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2595 } | |
2596 | |
2597 /** | |
2598 * Implementation of a Drupal action. | |
2599 * Sets the sticky-at-top-of-list property of a node to 1. | |
2600 */ | |
2601 function node_make_sticky_action(&$node, $context = array()) { | |
2602 $node->sticky = 1; | |
2603 watchdog('action', 'Set @type %title to sticky.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2604 } | |
2605 | |
2606 /** | |
2607 * Implementation of a Drupal action. | |
2608 * Sets the sticky-at-top-of-list property of a node to 0. | |
2609 */ | |
2610 function node_make_unsticky_action(&$node, $context = array()) { | |
2611 $node->sticky = 0; | |
2612 watchdog('action', 'Set @type %title to unsticky.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2613 } | |
2614 | |
2615 /** | |
2616 * Implementation of a Drupal action. | |
2617 * Sets the promote property of a node to 1. | |
2618 */ | |
2619 function node_promote_action(&$node, $context = array()) { | |
2620 $node->promote = 1; | |
2621 watchdog('action', 'Promoted @type %title to front page.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2622 } | |
2623 | |
2624 /** | |
2625 * Implementation of a Drupal action. | |
2626 * Sets the promote property of a node to 0. | |
2627 */ | |
2628 function node_unpromote_action(&$node, $context = array()) { | |
2629 $node->promote = 0; | |
2630 watchdog('action', 'Removed @type %title from front page.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2631 } | |
2632 | |
2633 /** | |
2634 * Implementation of a Drupal action. | |
2635 * Saves a node. | |
2636 */ | |
2637 function node_save_action($node) { | |
2638 node_save($node); | |
2639 watchdog('action', 'Saved @type %title', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2640 } | |
2641 | |
2642 /** | |
2643 * Implementation of a configurable Drupal action. | |
2644 * Assigns ownership of a node to a user. | |
2645 */ | |
2646 function node_assign_owner_action(&$node, $context) { | |
2647 $node->uid = $context['owner_uid']; | |
2648 $owner_name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $context['owner_uid'])); | |
2649 watchdog('action', 'Changed owner of @type %title to uid %name.', array('@type' => node_get_types('type', $node), '%title' => $node->title, '%name' => $owner_name)); | |
2650 } | |
2651 | |
2652 function node_assign_owner_action_form($context) { | |
2653 $description = t('The username of the user to which you would like to assign ownership.'); | |
2654 $count = db_result(db_query("SELECT COUNT(*) FROM {users}")); | |
2655 $owner_name = ''; | |
2656 if (isset($context['owner_uid'])) { | |
2657 $owner_name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $context['owner_uid'])); | |
2658 } | |
2659 | |
2660 // Use dropdown for fewer than 200 users; textbox for more than that. | |
2661 if (intval($count) < 200) { | |
2662 $options = array(); | |
2663 $result = db_query("SELECT uid, name FROM {users} WHERE uid > 0 ORDER BY name"); | |
2664 while ($data = db_fetch_object($result)) { | |
2665 $options[$data->name] = $data->name; | |
2666 } | |
2667 $form['owner_name'] = array( | |
2668 '#type' => 'select', | |
2669 '#title' => t('Username'), | |
2670 '#default_value' => $owner_name, | |
2671 '#options' => $options, | |
2672 '#description' => $description, | |
2673 ); | |
2674 } | |
2675 else { | |
2676 $form['owner_name'] = array( | |
2677 '#type' => 'textfield', | |
2678 '#title' => t('Username'), | |
2679 '#default_value' => $owner_name, | |
2680 '#autocomplete_path' => 'user/autocomplete', | |
2681 '#size' => '6', | |
2682 '#maxlength' => '7', | |
2683 '#description' => $description, | |
2684 ); | |
2685 } | |
2686 return $form; | |
2687 } | |
2688 | |
2689 function node_assign_owner_action_validate($form, $form_state) { | |
2690 $count = db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s'", $form_state['values']['owner_name'])); | |
2691 if (intval($count) != 1) { | |
2692 form_set_error('owner_name', t('Please enter a valid username.')); | |
2693 } | |
2694 } | |
2695 | |
2696 function node_assign_owner_action_submit($form, $form_state) { | |
2697 // Username can change, so we need to store the ID, not the username. | |
2698 $uid = db_result(db_query("SELECT uid from {users} WHERE name = '%s'", $form_state['values']['owner_name'])); | |
2699 return array('owner_uid' => $uid); | |
2700 } | |
2701 | |
2702 function node_unpublish_by_keyword_action_form($context) { | |
2703 $form['keywords'] = array( | |
2704 '#title' => t('Keywords'), | |
2705 '#type' => 'textarea', | |
2706 '#description' => t('The post will be unpublished if it contains any of the character sequences above. Use a comma-separated list of character sequences. Example: funny, bungee jumping, "Company, Inc.". Character sequences are case-sensitive.'), | |
2707 '#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '', | |
2708 ); | |
2709 return $form; | |
2710 } | |
2711 | |
2712 function node_unpublish_by_keyword_action_submit($form, $form_state) { | |
2713 return array('keywords' => drupal_explode_tags($form_state['values']['keywords'])); | |
2714 } | |
2715 | |
2716 /** | |
2717 * Implementation of a configurable Drupal action. | |
2718 * Unpublish a node if it contains a certain string. | |
2719 * | |
2720 * @param $context | |
2721 * An array providing more information about the context of the call to this action. | |
2722 * @param $comment | |
2723 * A node object. | |
2724 */ | |
2725 function node_unpublish_by_keyword_action($node, $context) { | |
2726 foreach ($context['keywords'] as $keyword) { | |
2727 if (strstr(node_view(drupal_clone($node)), $keyword) || strstr($node->title, $keyword)) { | |
2728 $node->status = 0; | |
2729 watchdog('action', 'Set @type %title to unpublished.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); | |
2730 break; | |
2731 } | |
2732 } | |
2733 } |