|
1 <?php |
|
2 // $Id: node.module,v 1.947.2.15 2009/02/16 14:39:40 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('attributes' => 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_table($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 $node->format = 0; |
|
836 } |
|
837 } |
|
838 |
|
839 if (user_access('administer nodes')) { |
|
840 // Populate the "authored by" field. |
|
841 if ($account = user_load(array('name' => $node->name))) { |
|
842 $node->uid = $account->uid; |
|
843 } |
|
844 else { |
|
845 $node->uid = 0; |
|
846 } |
|
847 } |
|
848 $node->created = !empty($node->date) ? strtotime($node->date) : time(); |
|
849 $node->validated = TRUE; |
|
850 |
|
851 return $node; |
|
852 } |
|
853 |
|
854 /** |
|
855 * Save a node object into the database. |
|
856 */ |
|
857 function node_save(&$node) { |
|
858 // Let modules modify the node before it is saved to the database. |
|
859 node_invoke_nodeapi($node, 'presave'); |
|
860 global $user; |
|
861 |
|
862 $node->is_new = FALSE; |
|
863 |
|
864 // Apply filters to some default node fields: |
|
865 if (empty($node->nid)) { |
|
866 // Insert a new node. |
|
867 $node->is_new = TRUE; |
|
868 |
|
869 // When inserting a node, $node->log must be set because |
|
870 // {node_revisions}.log does not (and cannot) have a default |
|
871 // value. If the user does not have permission to create |
|
872 // revisions, however, the form will not contain an element for |
|
873 // log so $node->log will be unset at this point. |
|
874 if (!isset($node->log)) { |
|
875 $node->log = ''; |
|
876 } |
|
877 |
|
878 // For the same reasons, make sure we have $node->teaser and |
|
879 // $node->body. We should consider making these fields nullable |
|
880 // in a future version since node types are not required to use them. |
|
881 if (!isset($node->teaser)) { |
|
882 $node->teaser = ''; |
|
883 } |
|
884 if (!isset($node->body)) { |
|
885 $node->body = ''; |
|
886 } |
|
887 } |
|
888 elseif (!empty($node->revision)) { |
|
889 $node->old_vid = $node->vid; |
|
890 } |
|
891 else { |
|
892 // When updating a node, avoid clobberring an existing log entry with an empty one. |
|
893 if (empty($node->log)) { |
|
894 unset($node->log); |
|
895 } |
|
896 } |
|
897 |
|
898 // Set some required fields: |
|
899 if (empty($node->created)) { |
|
900 $node->created = time(); |
|
901 } |
|
902 // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...) |
|
903 $node->changed = time(); |
|
904 |
|
905 $node->timestamp = time(); |
|
906 $node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT; |
|
907 |
|
908 // Generate the node table query and the node_revisions table query. |
|
909 if ($node->is_new) { |
|
910 _node_save_revision($node, $user->uid); |
|
911 drupal_write_record('node', $node); |
|
912 db_query('UPDATE {node_revisions} SET nid = %d WHERE vid = %d', $node->nid, $node->vid); |
|
913 $op = 'insert'; |
|
914 } |
|
915 else { |
|
916 drupal_write_record('node', $node, 'nid'); |
|
917 if (!empty($node->revision)) { |
|
918 _node_save_revision($node, $user->uid); |
|
919 db_query('UPDATE {node} SET vid = %d WHERE nid = %d', $node->vid, $node->nid); |
|
920 } |
|
921 else { |
|
922 _node_save_revision($node, $user->uid, 'vid'); |
|
923 } |
|
924 $op = 'update'; |
|
925 } |
|
926 |
|
927 // Call the node specific callback (if any). |
|
928 node_invoke($node, $op); |
|
929 node_invoke_nodeapi($node, $op); |
|
930 |
|
931 // Update the node access table for this node. |
|
932 node_access_acquire_grants($node); |
|
933 |
|
934 // Clear the page and block caches. |
|
935 cache_clear_all(); |
|
936 } |
|
937 |
|
938 /** |
|
939 * Helper function to save a revision with the uid of the current user. |
|
940 * |
|
941 * Node is taken by reference, becuse drupal_write_record() updates the |
|
942 * $node with the revision id, and we need to pass that back to the caller. |
|
943 */ |
|
944 function _node_save_revision(&$node, $uid, $update = NULL) { |
|
945 $temp_uid = $node->uid; |
|
946 $node->uid = $uid; |
|
947 if (isset($update)) { |
|
948 drupal_write_record('node_revisions', $node, $update); |
|
949 } |
|
950 else { |
|
951 drupal_write_record('node_revisions', $node); |
|
952 } |
|
953 $node->uid = $temp_uid; |
|
954 } |
|
955 |
|
956 /** |
|
957 * Delete a node. |
|
958 */ |
|
959 function node_delete($nid) { |
|
960 |
|
961 $node = node_load($nid); |
|
962 |
|
963 if (node_access('delete', $node)) { |
|
964 db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); |
|
965 db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); |
|
966 |
|
967 // Call the node-specific callback (if any): |
|
968 node_invoke($node, 'delete'); |
|
969 node_invoke_nodeapi($node, 'delete'); |
|
970 |
|
971 // Clear the page and block caches. |
|
972 cache_clear_all(); |
|
973 |
|
974 // Remove this node from the search index if needed. |
|
975 if (function_exists('search_wipe')) { |
|
976 search_wipe($node->nid, 'node'); |
|
977 } |
|
978 watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title)); |
|
979 drupal_set_message(t('@type %title has been deleted.', array('@type' => node_get_types('name', $node), '%title' => $node->title))); |
|
980 } |
|
981 } |
|
982 |
|
983 /** |
|
984 * Generate a display of the given node. |
|
985 * |
|
986 * @param $node |
|
987 * A node array or node object. |
|
988 * @param $teaser |
|
989 * Whether to display the teaser only or the full form. |
|
990 * @param $page |
|
991 * Whether the node is being displayed by itself as a page. |
|
992 * @param $links |
|
993 * Whether or not to display node links. Links are omitted for node previews. |
|
994 * |
|
995 * @return |
|
996 * An HTML representation of the themed node. |
|
997 */ |
|
998 function node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) { |
|
999 $node = (object)$node; |
|
1000 |
|
1001 $node = node_build_content($node, $teaser, $page); |
|
1002 |
|
1003 if ($links) { |
|
1004 $node->links = module_invoke_all('link', 'node', $node, $teaser); |
|
1005 drupal_alter('link', $node->links, $node); |
|
1006 } |
|
1007 |
|
1008 // Set the proper node part, then unset unused $node part so that a bad |
|
1009 // theme can not open a security hole. |
|
1010 $content = drupal_render($node->content); |
|
1011 if ($teaser) { |
|
1012 $node->teaser = $content; |
|
1013 unset($node->body); |
|
1014 } |
|
1015 else { |
|
1016 $node->body = $content; |
|
1017 unset($node->teaser); |
|
1018 } |
|
1019 |
|
1020 // Allow modules to modify the fully-built node. |
|
1021 node_invoke_nodeapi($node, 'alter', $teaser, $page); |
|
1022 |
|
1023 return theme('node', $node, $teaser, $page); |
|
1024 } |
|
1025 |
|
1026 /** |
|
1027 * Apply filters and build the node's standard elements. |
|
1028 */ |
|
1029 function node_prepare($node, $teaser = FALSE) { |
|
1030 // First we'll overwrite the existing node teaser and body with |
|
1031 // the filtered copies! Then, we'll stick those into the content |
|
1032 // array and set the read more flag if appropriate. |
|
1033 $node->readmore = $node->teaser != $node->body; |
|
1034 |
|
1035 if ($teaser == FALSE) { |
|
1036 $node->body = check_markup($node->body, $node->format, FALSE); |
|
1037 } |
|
1038 else { |
|
1039 $node->teaser = check_markup($node->teaser, $node->format, FALSE); |
|
1040 } |
|
1041 |
|
1042 $node->content['body'] = array( |
|
1043 '#value' => $teaser ? $node->teaser : $node->body, |
|
1044 '#weight' => 0, |
|
1045 ); |
|
1046 |
|
1047 return $node; |
|
1048 } |
|
1049 |
|
1050 /** |
|
1051 * Builds a structured array representing the node's content. |
|
1052 * |
|
1053 * @param $node |
|
1054 * A node object. |
|
1055 * @param $teaser |
|
1056 * Whether to display the teaser only, as on the main page. |
|
1057 * @param $page |
|
1058 * Whether the node is being displayed by itself as a page. |
|
1059 * |
|
1060 * @return |
|
1061 * An structured array containing the individual elements |
|
1062 * of the node's body. |
|
1063 */ |
|
1064 function node_build_content($node, $teaser = FALSE, $page = FALSE) { |
|
1065 |
|
1066 // The build mode identifies the target for which the node is built. |
|
1067 if (!isset($node->build_mode)) { |
|
1068 $node->build_mode = NODE_BUILD_NORMAL; |
|
1069 } |
|
1070 |
|
1071 // Remove the delimiter (if any) that separates the teaser from the body. |
|
1072 $node->body = isset($node->body) ? str_replace('<!--break-->', '', $node->body) : ''; |
|
1073 |
|
1074 // The 'view' hook can be implemented to overwrite the default function |
|
1075 // to display nodes. |
|
1076 if (node_hook($node, 'view')) { |
|
1077 $node = node_invoke($node, 'view', $teaser, $page); |
|
1078 } |
|
1079 else { |
|
1080 $node = node_prepare($node, $teaser); |
|
1081 } |
|
1082 |
|
1083 // Allow modules to make their own additions to the node. |
|
1084 node_invoke_nodeapi($node, 'view', $teaser, $page); |
|
1085 |
|
1086 return $node; |
|
1087 } |
|
1088 |
|
1089 /** |
|
1090 * Generate a page displaying a single node, along with its comments. |
|
1091 */ |
|
1092 function node_show($node, $cid, $message = FALSE) { |
|
1093 if ($message) { |
|
1094 drupal_set_title(t('Revision of %title from %date', array('%title' => $node->title, '%date' => format_date($node->revision_timestamp)))); |
|
1095 } |
|
1096 $output = node_view($node, FALSE, TRUE); |
|
1097 |
|
1098 if (function_exists('comment_render') && $node->comment) { |
|
1099 $output .= comment_render($node, $cid); |
|
1100 } |
|
1101 |
|
1102 // Update the history table, stating that this user viewed this node. |
|
1103 node_tag_new($node->nid); |
|
1104 |
|
1105 return $output; |
|
1106 } |
|
1107 |
|
1108 /** |
|
1109 * Theme a log message. |
|
1110 * |
|
1111 * @ingroup themeable |
|
1112 */ |
|
1113 function theme_node_log_message($log) { |
|
1114 return '<div class="log"><div class="title">'. t('Log') .':</div>'. $log .'</div>'; |
|
1115 } |
|
1116 |
|
1117 /** |
|
1118 * Implementation of hook_perm(). |
|
1119 */ |
|
1120 function node_perm() { |
|
1121 $perms = array('administer content types', 'administer nodes', 'access content', 'view revisions', 'revert revisions', 'delete revisions'); |
|
1122 |
|
1123 foreach (node_get_types() as $type) { |
|
1124 if ($type->module == 'node') { |
|
1125 $name = check_plain($type->type); |
|
1126 $perms[] = 'create '. $name .' content'; |
|
1127 $perms[] = 'delete own '. $name .' content'; |
|
1128 $perms[] = 'delete any '. $name .' content'; |
|
1129 $perms[] = 'edit own '. $name .' content'; |
|
1130 $perms[] = 'edit any '. $name .' content'; |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 return $perms; |
|
1135 } |
|
1136 |
|
1137 /** |
|
1138 * Implementation of hook_search(). |
|
1139 */ |
|
1140 function node_search($op = 'search', $keys = NULL) { |
|
1141 switch ($op) { |
|
1142 case 'name': |
|
1143 return t('Content'); |
|
1144 |
|
1145 case 'reset': |
|
1146 db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'node'", time()); |
|
1147 return; |
|
1148 |
|
1149 case 'status': |
|
1150 $total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1')); |
|
1151 $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 n.status = 1 AND (d.sid IS NULL OR d.reindex <> 0)")); |
|
1152 return array('remaining' => $remaining, 'total' => $total); |
|
1153 |
|
1154 case 'admin': |
|
1155 $form = array(); |
|
1156 // Output form for defining rank factor weights. |
|
1157 $form['content_ranking'] = array( |
|
1158 '#type' => 'fieldset', |
|
1159 '#title' => t('Content ranking'), |
|
1160 ); |
|
1161 $form['content_ranking']['#theme'] = 'node_search_admin'; |
|
1162 $form['content_ranking']['info'] = array( |
|
1163 '#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>' |
|
1164 ); |
|
1165 |
|
1166 $ranking = array('node_rank_relevance' => t('Keyword relevance'), |
|
1167 'node_rank_recent' => t('Recently posted')); |
|
1168 if (module_exists('comment')) { |
|
1169 $ranking['node_rank_comments'] = t('Number of comments'); |
|
1170 } |
|
1171 if (module_exists('statistics') && variable_get('statistics_count_content_views', 0)) { |
|
1172 $ranking['node_rank_views'] = t('Number of views'); |
|
1173 } |
|
1174 |
|
1175 // Note: reversed to reflect that higher number = higher ranking. |
|
1176 $options = drupal_map_assoc(range(0, 10)); |
|
1177 foreach ($ranking as $var => $title) { |
|
1178 $form['content_ranking']['factors'][$var] = array( |
|
1179 '#title' => $title, |
|
1180 '#type' => 'select', |
|
1181 '#options' => $options, |
|
1182 '#default_value' => variable_get($var, 5), |
|
1183 ); |
|
1184 } |
|
1185 return $form; |
|
1186 |
|
1187 case 'search': |
|
1188 // Build matching conditions |
|
1189 list($join1, $where1) = _db_rewrite_sql(); |
|
1190 $arguments1 = array(); |
|
1191 $conditions1 = 'n.status = 1'; |
|
1192 |
|
1193 if ($type = search_query_extract($keys, 'type')) { |
|
1194 $types = array(); |
|
1195 foreach (explode(',', $type) as $t) { |
|
1196 $types[] = "n.type = '%s'"; |
|
1197 $arguments1[] = $t; |
|
1198 } |
|
1199 $conditions1 .= ' AND ('. implode(' OR ', $types) .')'; |
|
1200 $keys = search_query_insert($keys, 'type'); |
|
1201 } |
|
1202 |
|
1203 if ($category = search_query_extract($keys, 'category')) { |
|
1204 $categories = array(); |
|
1205 foreach (explode(',', $category) as $c) { |
|
1206 $categories[] = "tn.tid = %d"; |
|
1207 $arguments1[] = $c; |
|
1208 } |
|
1209 $conditions1 .= ' AND ('. implode(' OR ', $categories) .')'; |
|
1210 $join1 .= ' INNER JOIN {term_node} tn ON n.vid = tn.vid'; |
|
1211 $keys = search_query_insert($keys, 'category'); |
|
1212 } |
|
1213 |
|
1214 // Build ranking expression (we try to map each parameter to a |
|
1215 // uniform distribution in the range 0..1). |
|
1216 $ranking = array(); |
|
1217 $arguments2 = array(); |
|
1218 $join2 = ''; |
|
1219 // Used to avoid joining on node_comment_statistics twice |
|
1220 $stats_join = FALSE; |
|
1221 $total = 0; |
|
1222 if ($weight = (int)variable_get('node_rank_relevance', 5)) { |
|
1223 // Average relevance values hover around 0.15 |
|
1224 $ranking[] = '%d * i.relevance'; |
|
1225 $arguments2[] = $weight; |
|
1226 $total += $weight; |
|
1227 } |
|
1228 if ($weight = (int)variable_get('node_rank_recent', 5)) { |
|
1229 // Exponential decay with half-life of 6 months, starting at last indexed node |
|
1230 $ranking[] = '%d * POW(2, (GREATEST(MAX(n.created), MAX(n.changed), MAX(c.last_comment_timestamp)) - %d) * 6.43e-8)'; |
|
1231 $arguments2[] = $weight; |
|
1232 $arguments2[] = (int)variable_get('node_cron_last', 0); |
|
1233 $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid'; |
|
1234 $stats_join = TRUE; |
|
1235 $total += $weight; |
|
1236 } |
|
1237 if (module_exists('comment') && $weight = (int)variable_get('node_rank_comments', 5)) { |
|
1238 // Inverse law that maps the highest reply count on the site to 1 and 0 to 0. |
|
1239 $scale = variable_get('node_cron_comments_scale', 0.0); |
|
1240 $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(c.comment_count) * %f))'; |
|
1241 $arguments2[] = $weight; |
|
1242 $arguments2[] = $scale; |
|
1243 if (!$stats_join) { |
|
1244 $join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid'; |
|
1245 } |
|
1246 $total += $weight; |
|
1247 } |
|
1248 if (module_exists('statistics') && variable_get('statistics_count_content_views', 0) && |
|
1249 $weight = (int)variable_get('node_rank_views', 5)) { |
|
1250 // Inverse law that maps the highest view count on the site to 1 and 0 to 0. |
|
1251 $scale = variable_get('node_cron_views_scale', 0.0); |
|
1252 $ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(nc.totalcount) * %f))'; |
|
1253 $arguments2[] = $weight; |
|
1254 $arguments2[] = $scale; |
|
1255 $join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid'; |
|
1256 $total += $weight; |
|
1257 } |
|
1258 |
|
1259 // When all search factors are disabled (ie they have a weight of zero), |
|
1260 // the default score is based only on keyword relevance and there is no need to |
|
1261 // adjust the score of each item. |
|
1262 if ($total == 0) { |
|
1263 $select2 = 'i.relevance AS score'; |
|
1264 $total = 1; |
|
1265 } |
|
1266 else { |
|
1267 $select2 = implode(' + ', $ranking) . ' AS score'; |
|
1268 } |
|
1269 |
|
1270 // Do search. |
|
1271 $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join1, $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2); |
|
1272 |
|
1273 // Load results. |
|
1274 $results = array(); |
|
1275 foreach ($find as $item) { |
|
1276 // Build the node body. |
|
1277 $node = node_load($item->sid); |
|
1278 $node->build_mode = NODE_BUILD_SEARCH_RESULT; |
|
1279 $node = node_build_content($node, FALSE, FALSE); |
|
1280 $node->body = drupal_render($node->content); |
|
1281 |
|
1282 // Fetch comments for snippet. |
|
1283 $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index'); |
|
1284 // Fetch terms for snippet. |
|
1285 $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index'); |
|
1286 |
|
1287 $extra = node_invoke_nodeapi($node, 'search result'); |
|
1288 $results[] = array( |
|
1289 'link' => url('node/'. $item->sid, array('absolute' => TRUE)), |
|
1290 'type' => check_plain(node_get_types('name', $node)), |
|
1291 'title' => $node->title, |
|
1292 'user' => theme('username', $node), |
|
1293 'date' => $node->changed, |
|
1294 'node' => $node, |
|
1295 'extra' => $extra, |
|
1296 'score' => $item->score / $total, |
|
1297 'snippet' => search_excerpt($keys, $node->body), |
|
1298 ); |
|
1299 } |
|
1300 return $results; |
|
1301 } |
|
1302 } |
|
1303 |
|
1304 /** |
|
1305 * Implementation of hook_user(). |
|
1306 */ |
|
1307 function node_user($op, &$edit, &$user) { |
|
1308 if ($op == 'delete') { |
|
1309 db_query('UPDATE {node} SET uid = 0 WHERE uid = %d', $user->uid); |
|
1310 db_query('UPDATE {node_revisions} SET uid = 0 WHERE uid = %d', $user->uid); |
|
1311 } |
|
1312 } |
|
1313 |
|
1314 /** |
|
1315 * Theme the content ranking part of the search settings admin page. |
|
1316 * |
|
1317 * @ingroup themeable |
|
1318 */ |
|
1319 function theme_node_search_admin($form) { |
|
1320 $output = drupal_render($form['info']); |
|
1321 |
|
1322 $header = array(t('Factor'), t('Weight')); |
|
1323 foreach (element_children($form['factors']) as $key) { |
|
1324 $row = array(); |
|
1325 $row[] = $form['factors'][$key]['#title']; |
|
1326 unset($form['factors'][$key]['#title']); |
|
1327 $row[] = drupal_render($form['factors'][$key]); |
|
1328 $rows[] = $row; |
|
1329 } |
|
1330 $output .= theme('table', $header, $rows); |
|
1331 |
|
1332 $output .= drupal_render($form); |
|
1333 return $output; |
|
1334 } |
|
1335 |
|
1336 /** |
|
1337 * Retrieve the comment mode for the given node ID (none, read, or read/write). |
|
1338 */ |
|
1339 function node_comment_mode($nid) { |
|
1340 static $comment_mode; |
|
1341 if (!isset($comment_mode[$nid])) { |
|
1342 $comment_mode[$nid] = db_result(db_query('SELECT comment FROM {node} WHERE nid = %d', $nid)); |
|
1343 } |
|
1344 return $comment_mode[$nid]; |
|
1345 } |
|
1346 |
|
1347 /** |
|
1348 * Implementation of hook_link(). |
|
1349 */ |
|
1350 function node_link($type, $node = NULL, $teaser = FALSE) { |
|
1351 $links = array(); |
|
1352 |
|
1353 if ($type == 'node') { |
|
1354 if ($teaser == 1 && $node->teaser && !empty($node->readmore)) { |
|
1355 $links['node_read_more'] = array( |
|
1356 'title' => t('Read more'), |
|
1357 'href' => "node/$node->nid", |
|
1358 // The title attribute gets escaped when the links are processed, so |
|
1359 // there is no need to escape here. |
|
1360 'attributes' => array('title' => t('Read the rest of !title.', array('!title' => $node->title))) |
|
1361 ); |
|
1362 } |
|
1363 } |
|
1364 |
|
1365 return $links; |
|
1366 } |
|
1367 |
|
1368 function _node_revision_access($node, $op = 'view') { |
|
1369 static $access = array(); |
|
1370 if (!isset($access[$node->vid])) { |
|
1371 $node_current_revision = node_load($node->nid); |
|
1372 $is_current_revision = $node_current_revision->vid == $node->vid; |
|
1373 // There should be at least two revisions. If the vid of the given node |
|
1374 // and the vid of the current revision differs, then we already have two |
|
1375 // different revisions so there is no need for a separate database check. |
|
1376 // Also, if you try to revert to or delete the current revision, that's |
|
1377 // not good. |
|
1378 if ($is_current_revision && (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $node->nid)) == 1 || $op == 'update' || $op == 'delete')) { |
|
1379 $access[$node->vid] = FALSE; |
|
1380 } |
|
1381 elseif (user_access('administer nodes')) { |
|
1382 $access[$node->vid] = TRUE; |
|
1383 } |
|
1384 else { |
|
1385 $map = array('view' => 'view revisions', 'update' => 'revert revisions', 'delete' => 'delete revisions'); |
|
1386 // First check the user permission, second check the access to the |
|
1387 // current revision and finally, if the node passed in is not the current |
|
1388 // revision then access to that, too. |
|
1389 $access[$node->vid] = isset($map[$op]) && user_access($map[$op]) && node_access($op, $node_current_revision) && ($is_current_revision || node_access($op, $node)); |
|
1390 } |
|
1391 } |
|
1392 return $access[$node->vid]; |
|
1393 } |
|
1394 |
|
1395 function _node_add_access() { |
|
1396 $types = node_get_types(); |
|
1397 foreach ($types as $type) { |
|
1398 if (node_hook($type->type, 'form') && node_access('create', $type->type)) { |
|
1399 return TRUE; |
|
1400 } |
|
1401 } |
|
1402 return FALSE; |
|
1403 } |
|
1404 |
|
1405 /** |
|
1406 * Implementation of hook_menu(). |
|
1407 */ |
|
1408 function node_menu() { |
|
1409 $items['admin/content/node'] = array( |
|
1410 'title' => 'Content', |
|
1411 'description' => "View, edit, and delete your site's content.", |
|
1412 'page callback' => 'drupal_get_form', |
|
1413 'page arguments' => array('node_admin_content'), |
|
1414 'access arguments' => array('administer nodes'), |
|
1415 'file' => 'node.admin.inc', |
|
1416 ); |
|
1417 |
|
1418 $items['admin/content/node/overview'] = array( |
|
1419 'title' => 'List', |
|
1420 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
1421 'weight' => -10, |
|
1422 ); |
|
1423 |
|
1424 $items['admin/content/node-settings'] = array( |
|
1425 'title' => 'Post settings', |
|
1426 'description' => 'Control posting behavior, such as teaser length, requiring previews before posting, and the number of posts on the front page.', |
|
1427 'page callback' => 'drupal_get_form', |
|
1428 'page arguments' => array('node_configure'), |
|
1429 'access arguments' => array('administer nodes'), |
|
1430 'file' => 'node.admin.inc', |
|
1431 ); |
|
1432 $items['admin/content/node-settings/rebuild'] = array( |
|
1433 'title' => 'Rebuild permissions', |
|
1434 'page arguments' => array('node_configure_rebuild_confirm'), |
|
1435 'file' => 'node.admin.inc', |
|
1436 // Any user than can potentially trigger a node_acess_needs_rebuild(TRUE) |
|
1437 // has to be allowed access to the 'node access rebuild' confirm form. |
|
1438 'access arguments' => array('access administration pages'), |
|
1439 'type' => MENU_CALLBACK, |
|
1440 ); |
|
1441 |
|
1442 $items['admin/content/types'] = array( |
|
1443 'title' => 'Content types', |
|
1444 'description' => 'Manage posts by content type, including default status, front page promotion, etc.', |
|
1445 'page callback' => 'node_overview_types', |
|
1446 'access arguments' => array('administer content types'), |
|
1447 'file' => 'content_types.inc', |
|
1448 ); |
|
1449 $items['admin/content/types/list'] = array( |
|
1450 'title' => 'List', |
|
1451 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
1452 'weight' => -10, |
|
1453 ); |
|
1454 $items['admin/content/types/add'] = array( |
|
1455 'title' => 'Add content type', |
|
1456 'page callback' => 'drupal_get_form', |
|
1457 'page arguments' => array('node_type_form'), |
|
1458 'access arguments' => array('administer content types'), |
|
1459 'file' => 'content_types.inc', |
|
1460 'type' => MENU_LOCAL_TASK, |
|
1461 ); |
|
1462 $items['node'] = array( |
|
1463 'title' => 'Content', |
|
1464 'page callback' => 'node_page_default', |
|
1465 'access arguments' => array('access content'), |
|
1466 'type' => MENU_CALLBACK, |
|
1467 ); |
|
1468 $items['node/add'] = array( |
|
1469 'title' => 'Create content', |
|
1470 'page callback' => 'node_add_page', |
|
1471 'access callback' => '_node_add_access', |
|
1472 'weight' => 1, |
|
1473 'file' => 'node.pages.inc', |
|
1474 ); |
|
1475 $items['rss.xml'] = array( |
|
1476 'title' => 'RSS feed', |
|
1477 'page callback' => 'node_feed', |
|
1478 'access arguments' => array('access content'), |
|
1479 'type' => MENU_CALLBACK, |
|
1480 ); |
|
1481 foreach (node_get_types('types', NULL, TRUE) as $type) { |
|
1482 $type_url_str = str_replace('_', '-', $type->type); |
|
1483 $items['node/add/'. $type_url_str] = array( |
|
1484 'title' => drupal_ucfirst($type->name), |
|
1485 'title callback' => 'check_plain', |
|
1486 'page callback' => 'node_add', |
|
1487 'page arguments' => array(2), |
|
1488 'access callback' => 'node_access', |
|
1489 'access arguments' => array('create', $type->type), |
|
1490 'description' => $type->description, |
|
1491 'file' => 'node.pages.inc', |
|
1492 ); |
|
1493 $items['admin/content/node-type/'. $type_url_str] = array( |
|
1494 'title' => $type->name, |
|
1495 'page callback' => 'drupal_get_form', |
|
1496 'page arguments' => array('node_type_form', $type), |
|
1497 'access arguments' => array('administer content types'), |
|
1498 'file' => 'content_types.inc', |
|
1499 'type' => MENU_CALLBACK, |
|
1500 ); |
|
1501 $items['admin/content/node-type/'. $type_url_str .'/edit'] = array( |
|
1502 'title' => 'Edit', |
|
1503 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
1504 ); |
|
1505 $items['admin/content/node-type/'. $type_url_str .'/delete'] = array( |
|
1506 'title' => 'Delete', |
|
1507 'page arguments' => array('node_type_delete_confirm', $type), |
|
1508 'access arguments' => array('administer content types'), |
|
1509 'file' => 'content_types.inc', |
|
1510 'type' => MENU_CALLBACK, |
|
1511 ); |
|
1512 } |
|
1513 $items['node/%node'] = array( |
|
1514 'title callback' => 'node_page_title', |
|
1515 'title arguments' => array(1), |
|
1516 'page callback' => 'node_page_view', |
|
1517 'page arguments' => array(1), |
|
1518 'access callback' => 'node_access', |
|
1519 'access arguments' => array('view', 1), |
|
1520 'type' => MENU_CALLBACK); |
|
1521 $items['node/%node/view'] = array( |
|
1522 'title' => 'View', |
|
1523 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
1524 'weight' => -10); |
|
1525 $items['node/%node/edit'] = array( |
|
1526 'title' => 'Edit', |
|
1527 'page callback' => 'node_page_edit', |
|
1528 'page arguments' => array(1), |
|
1529 'access callback' => 'node_access', |
|
1530 'access arguments' => array('update', 1), |
|
1531 'weight' => 1, |
|
1532 'file' => 'node.pages.inc', |
|
1533 'type' => MENU_LOCAL_TASK, |
|
1534 ); |
|
1535 $items['node/%node/delete'] = array( |
|
1536 'title' => 'Delete', |
|
1537 'page callback' => 'drupal_get_form', |
|
1538 'page arguments' => array('node_delete_confirm', 1), |
|
1539 'access callback' => 'node_access', |
|
1540 'access arguments' => array('delete', 1), |
|
1541 'file' => 'node.pages.inc', |
|
1542 'weight' => 1, |
|
1543 'type' => MENU_CALLBACK); |
|
1544 $items['node/%node/revisions'] = array( |
|
1545 'title' => 'Revisions', |
|
1546 'page callback' => 'node_revision_overview', |
|
1547 'page arguments' => array(1), |
|
1548 'access callback' => '_node_revision_access', |
|
1549 'access arguments' => array(1), |
|
1550 'weight' => 2, |
|
1551 'file' => 'node.pages.inc', |
|
1552 'type' => MENU_LOCAL_TASK, |
|
1553 ); |
|
1554 $items['node/%node/revisions/%/view'] = array( |
|
1555 'title' => 'Revisions', |
|
1556 'load arguments' => array(3), |
|
1557 'page callback' => 'node_show', |
|
1558 'page arguments' => array(1, NULL, TRUE), |
|
1559 'access callback' => '_node_revision_access', |
|
1560 'access arguments' => array(1), |
|
1561 'type' => MENU_CALLBACK, |
|
1562 ); |
|
1563 $items['node/%node/revisions/%/revert'] = array( |
|
1564 'title' => 'Revert to earlier revision', |
|
1565 'load arguments' => array(3), |
|
1566 'page callback' => 'drupal_get_form', |
|
1567 'page arguments' => array('node_revision_revert_confirm', 1), |
|
1568 'access callback' => '_node_revision_access', |
|
1569 'access arguments' => array(1, 'update'), |
|
1570 'file' => 'node.pages.inc', |
|
1571 'type' => MENU_CALLBACK, |
|
1572 ); |
|
1573 $items['node/%node/revisions/%/delete'] = array( |
|
1574 'title' => 'Delete earlier revision', |
|
1575 'load arguments' => array(3), |
|
1576 'page callback' => 'drupal_get_form', |
|
1577 'page arguments' => array('node_revision_delete_confirm', 1), |
|
1578 'access callback' => '_node_revision_access', |
|
1579 'access arguments' => array(1, 'delete'), |
|
1580 'file' => 'node.pages.inc', |
|
1581 'type' => MENU_CALLBACK, |
|
1582 ); |
|
1583 return $items; |
|
1584 } |
|
1585 |
|
1586 /** |
|
1587 * Title callback. |
|
1588 */ |
|
1589 function node_page_title($node) { |
|
1590 return $node->title; |
|
1591 } |
|
1592 |
|
1593 /** |
|
1594 * Implementation of hook_init(). |
|
1595 */ |
|
1596 function node_init() { |
|
1597 drupal_add_css(drupal_get_path('module', 'node') .'/node.css'); |
|
1598 } |
|
1599 |
|
1600 function node_last_changed($nid) { |
|
1601 $node = db_fetch_object(db_query('SELECT changed FROM {node} WHERE nid = %d', $nid)); |
|
1602 return ($node->changed); |
|
1603 } |
|
1604 |
|
1605 /** |
|
1606 * Return a list of all the existing revision numbers. |
|
1607 */ |
|
1608 function node_revision_list($node) { |
|
1609 $revisions = array(); |
|
1610 $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); |
|
1611 while ($revision = db_fetch_object($result)) { |
|
1612 $revisions[$revision->vid] = $revision; |
|
1613 } |
|
1614 |
|
1615 return $revisions; |
|
1616 } |
|
1617 |
|
1618 /** |
|
1619 * Implementation of hook_block(). |
|
1620 */ |
|
1621 function node_block($op = 'list', $delta = 0) { |
|
1622 if ($op == 'list') { |
|
1623 $blocks[0]['info'] = t('Syndicate'); |
|
1624 // Not worth caching. |
|
1625 $blocks[0]['cache'] = BLOCK_NO_CACHE; |
|
1626 return $blocks; |
|
1627 } |
|
1628 else if ($op == 'view') { |
|
1629 $block['subject'] = t('Syndicate'); |
|
1630 $block['content'] = theme('feed_icon', url('rss.xml'), t('Syndicate')); |
|
1631 |
|
1632 return $block; |
|
1633 } |
|
1634 } |
|
1635 |
|
1636 /** |
|
1637 * A generic function for generating RSS feeds from a set of nodes. |
|
1638 * |
|
1639 * @param $nids |
|
1640 * An array of node IDs (nid). Defaults to FALSE so empty feeds can be |
|
1641 * generated with passing an empty array, if no items are to be added |
|
1642 * to the feed. |
|
1643 * @param $channel |
|
1644 * An associative array containing title, link, description and other keys. |
|
1645 * The link should be an absolute URL. |
|
1646 */ |
|
1647 function node_feed($nids = FALSE, $channel = array()) { |
|
1648 global $base_url, $language; |
|
1649 |
|
1650 if ($nids === FALSE) { |
|
1651 $nids = array(); |
|
1652 $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)); |
|
1653 while ($row = db_fetch_object($result)) { |
|
1654 $nids[] = $row->nid; |
|
1655 } |
|
1656 } |
|
1657 |
|
1658 $item_length = variable_get('feed_item_length', 'teaser'); |
|
1659 $namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'); |
|
1660 |
|
1661 $items = ''; |
|
1662 foreach ($nids as $nid) { |
|
1663 // Load the specified node: |
|
1664 $item = node_load($nid); |
|
1665 $item->build_mode = NODE_BUILD_RSS; |
|
1666 $item->link = url("node/$nid", array('absolute' => TRUE)); |
|
1667 |
|
1668 if ($item_length != 'title') { |
|
1669 $teaser = ($item_length == 'teaser') ? TRUE : FALSE; |
|
1670 |
|
1671 // Filter and prepare node teaser |
|
1672 if (node_hook($item, 'view')) { |
|
1673 $item = node_invoke($item, 'view', $teaser, FALSE); |
|
1674 } |
|
1675 else { |
|
1676 $item = node_prepare($item, $teaser); |
|
1677 } |
|
1678 |
|
1679 // Allow modules to change $node->content before the node is rendered. |
|
1680 node_invoke_nodeapi($item, 'view', $teaser, FALSE); |
|
1681 |
|
1682 // Set the proper node property, then unset unused $node property so that a |
|
1683 // bad theme can not open a security hole. |
|
1684 $content = drupal_render($item->content); |
|
1685 if ($teaser) { |
|
1686 $item->teaser = $content; |
|
1687 unset($item->body); |
|
1688 } |
|
1689 else { |
|
1690 $item->body = $content; |
|
1691 unset($item->teaser); |
|
1692 } |
|
1693 |
|
1694 // Allow modules to modify the fully-built node. |
|
1695 node_invoke_nodeapi($item, 'alter', $teaser, FALSE); |
|
1696 } |
|
1697 |
|
1698 // Allow modules to add additional item fields and/or modify $item |
|
1699 $extra = node_invoke_nodeapi($item, 'rss item'); |
|
1700 $extra = array_merge($extra, array(array('key' => 'pubDate', 'value' => gmdate('r', $item->created)), array('key' => 'dc:creator', 'value' => $item->name), array('key' => 'guid', 'value' => $item->nid .' at '. $base_url, 'attributes' => array('isPermaLink' => 'false')))); |
|
1701 foreach ($extra as $element) { |
|
1702 if (isset($element['namespace'])) { |
|
1703 $namespaces = array_merge($namespaces, $element['namespace']); |
|
1704 } |
|
1705 } |
|
1706 |
|
1707 // Prepare the item description |
|
1708 switch ($item_length) { |
|
1709 case 'fulltext': |
|
1710 $item_text = $item->body; |
|
1711 break; |
|
1712 case 'teaser': |
|
1713 $item_text = $item->teaser; |
|
1714 if (!empty($item->readmore)) { |
|
1715 $item_text .= '<p>'. l(t('read more'), 'node/'. $item->nid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) .'</p>'; |
|
1716 } |
|
1717 break; |
|
1718 case 'title': |
|
1719 $item_text = ''; |
|
1720 break; |
|
1721 } |
|
1722 |
|
1723 $items .= format_rss_item($item->title, $item->link, $item_text, $extra); |
|
1724 } |
|
1725 |
|
1726 $channel_defaults = array( |
|
1727 'version' => '2.0', |
|
1728 'title' => variable_get('site_name', 'Drupal'), |
|
1729 'link' => $base_url, |
|
1730 'description' => variable_get('site_mission', ''), |
|
1731 'language' => $language->language |
|
1732 ); |
|
1733 $channel = array_merge($channel_defaults, $channel); |
|
1734 |
|
1735 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
|
1736 $output .= "<rss version=\"". $channel["version"] ."\" xml:base=\"". $base_url ."\" ". drupal_attributes($namespaces) .">\n"; |
|
1737 $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']); |
|
1738 $output .= "</rss>\n"; |
|
1739 |
|
1740 drupal_set_header('Content-Type: application/rss+xml; charset=utf-8'); |
|
1741 print $output; |
|
1742 } |
|
1743 |
|
1744 /** |
|
1745 * Menu callback; Generate a listing of promoted nodes. |
|
1746 */ |
|
1747 function node_page_default() { |
|
1748 $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)); |
|
1749 |
|
1750 $output = ''; |
|
1751 $num_rows = FALSE; |
|
1752 while ($node = db_fetch_object($result)) { |
|
1753 $output .= node_view(node_load($node->nid), 1); |
|
1754 $num_rows = TRUE; |
|
1755 } |
|
1756 |
|
1757 if ($num_rows) { |
|
1758 $feed_url = url('rss.xml', array('absolute' => TRUE)); |
|
1759 drupal_add_feed($feed_url, variable_get('site_name', 'Drupal') .' '. t('RSS')); |
|
1760 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); |
|
1761 } |
|
1762 else { |
|
1763 $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>'); |
|
1764 $default_message .= '<ol>'; |
|
1765 |
|
1766 $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>'; |
|
1767 $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>'; |
|
1768 $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>'; |
|
1769 $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>'; |
|
1770 $default_message .= '</ol>'; |
|
1771 $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>'; |
|
1772 |
|
1773 $output = '<div id="first-time">'. $default_message .'</div>'; |
|
1774 } |
|
1775 drupal_set_title(''); |
|
1776 |
|
1777 return $output; |
|
1778 } |
|
1779 |
|
1780 /** |
|
1781 * Menu callback; view a single node. |
|
1782 */ |
|
1783 function node_page_view($node, $cid = NULL) { |
|
1784 drupal_set_title(check_plain($node->title)); |
|
1785 return node_show($node, $cid); |
|
1786 } |
|
1787 |
|
1788 /** |
|
1789 * Implementation of hook_update_index(). |
|
1790 */ |
|
1791 function node_update_index() { |
|
1792 $limit = (int)variable_get('search_cron_limit', 100); |
|
1793 |
|
1794 // Store the maximum possible comments per thread (used for ranking by reply count) |
|
1795 variable_set('node_cron_comments_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}')))); |
|
1796 variable_set('node_cron_views_scale', 1.0 / max(1, db_result(db_query('SELECT MAX(totalcount) FROM {node_counter}')))); |
|
1797 |
|
1798 $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); |
|
1799 |
|
1800 while ($node = db_fetch_object($result)) { |
|
1801 _node_index_node($node); |
|
1802 } |
|
1803 } |
|
1804 |
|
1805 /** |
|
1806 * Index a single node. |
|
1807 * |
|
1808 * @param $node |
|
1809 * The node to index. |
|
1810 */ |
|
1811 function _node_index_node($node) { |
|
1812 $node = node_load($node->nid); |
|
1813 |
|
1814 // save the changed time of the most recent indexed node, for the search results half-life calculation |
|
1815 variable_set('node_cron_last', $node->changed); |
|
1816 |
|
1817 // Build the node body. |
|
1818 $node->build_mode = NODE_BUILD_SEARCH_INDEX; |
|
1819 $node = node_build_content($node, FALSE, FALSE); |
|
1820 $node->body = drupal_render($node->content); |
|
1821 |
|
1822 $text = '<h1>'. check_plain($node->title) .'</h1>'. $node->body; |
|
1823 |
|
1824 // Fetch extra data normally not visible |
|
1825 $extra = node_invoke_nodeapi($node, 'update index'); |
|
1826 foreach ($extra as $t) { |
|
1827 $text .= $t; |
|
1828 } |
|
1829 |
|
1830 // Update index |
|
1831 search_index($node->nid, 'node', $text); |
|
1832 } |
|
1833 |
|
1834 /** |
|
1835 * Implementation of hook_form_alter(). |
|
1836 */ |
|
1837 function node_form_alter(&$form, $form_state, $form_id) { |
|
1838 // Advanced node search form |
|
1839 if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) { |
|
1840 // Keyword boxes: |
|
1841 $form['advanced'] = array( |
|
1842 '#type' => 'fieldset', |
|
1843 '#title' => t('Advanced search'), |
|
1844 '#collapsible' => TRUE, |
|
1845 '#collapsed' => TRUE, |
|
1846 '#attributes' => array('class' => 'search-advanced'), |
|
1847 ); |
|
1848 $form['advanced']['keywords'] = array( |
|
1849 '#prefix' => '<div class="criterion">', |
|
1850 '#suffix' => '</div>', |
|
1851 ); |
|
1852 $form['advanced']['keywords']['or'] = array( |
|
1853 '#type' => 'textfield', |
|
1854 '#title' => t('Containing any of the words'), |
|
1855 '#size' => 30, |
|
1856 '#maxlength' => 255, |
|
1857 ); |
|
1858 $form['advanced']['keywords']['phrase'] = array( |
|
1859 '#type' => 'textfield', |
|
1860 '#title' => t('Containing the phrase'), |
|
1861 '#size' => 30, |
|
1862 '#maxlength' => 255, |
|
1863 ); |
|
1864 $form['advanced']['keywords']['negative'] = array( |
|
1865 '#type' => 'textfield', |
|
1866 '#title' => t('Containing none of the words'), |
|
1867 '#size' => 30, |
|
1868 '#maxlength' => 255, |
|
1869 ); |
|
1870 |
|
1871 // Taxonomy box: |
|
1872 if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) { |
|
1873 $form['advanced']['category'] = array( |
|
1874 '#type' => 'select', |
|
1875 '#title' => t('Only in the category(s)'), |
|
1876 '#prefix' => '<div class="criterion">', |
|
1877 '#size' => 10, |
|
1878 '#suffix' => '</div>', |
|
1879 '#options' => $taxonomy, |
|
1880 '#multiple' => TRUE, |
|
1881 ); |
|
1882 } |
|
1883 |
|
1884 // Node types: |
|
1885 $types = array_map('check_plain', node_get_types('names')); |
|
1886 $form['advanced']['type'] = array( |
|
1887 '#type' => 'checkboxes', |
|
1888 '#title' => t('Only of the type(s)'), |
|
1889 '#prefix' => '<div class="criterion">', |
|
1890 '#suffix' => '</div>', |
|
1891 '#options' => $types, |
|
1892 ); |
|
1893 $form['advanced']['submit'] = array( |
|
1894 '#type' => 'submit', |
|
1895 '#value' => t('Advanced search'), |
|
1896 '#prefix' => '<div class="action">', |
|
1897 '#suffix' => '</div>', |
|
1898 ); |
|
1899 |
|
1900 $form['#validate'][] = 'node_search_validate'; |
|
1901 } |
|
1902 } |
|
1903 |
|
1904 /** |
|
1905 * Form API callback for the search form. Registered in node_form_alter(). |
|
1906 */ |
|
1907 function node_search_validate($form, &$form_state) { |
|
1908 // Initialise using any existing basic search keywords. |
|
1909 $keys = $form_state['values']['processed_keys']; |
|
1910 |
|
1911 // Insert extra restrictions into the search keywords string. |
|
1912 if (isset($form_state['values']['type']) && is_array($form_state['values']['type'])) { |
|
1913 // Retrieve selected types - Forms API sets the value of unselected checkboxes to 0. |
|
1914 $form_state['values']['type'] = array_filter($form_state['values']['type']); |
|
1915 if (count($form_state['values']['type'])) { |
|
1916 $keys = search_query_insert($keys, 'type', implode(',', array_keys($form_state['values']['type']))); |
|
1917 } |
|
1918 } |
|
1919 |
|
1920 if (isset($form_state['values']['category']) && is_array($form_state['values']['category'])) { |
|
1921 $keys = search_query_insert($keys, 'category', implode(',', $form_state['values']['category'])); |
|
1922 } |
|
1923 if ($form_state['values']['or'] != '') { |
|
1924 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' '. $form_state['values']['or'], $matches)) { |
|
1925 $keys .= ' '. implode(' OR ', $matches[1]); |
|
1926 } |
|
1927 } |
|
1928 if ($form_state['values']['negative'] != '') { |
|
1929 if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' '. $form_state['values']['negative'], $matches)) { |
|
1930 $keys .= ' -'. implode(' -', $matches[1]); |
|
1931 } |
|
1932 } |
|
1933 if ($form_state['values']['phrase'] != '') { |
|
1934 $keys .= ' "'. str_replace('"', ' ', $form_state['values']['phrase']) .'"'; |
|
1935 } |
|
1936 if (!empty($keys)) { |
|
1937 form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state); |
|
1938 } |
|
1939 } |
|
1940 |
|
1941 /** |
|
1942 * @defgroup node_access Node access rights |
|
1943 * @{ |
|
1944 * The node access system determines who can do what to which nodes. |
|
1945 * |
|
1946 * In determining access rights for a node, node_access() first checks |
|
1947 * whether the user has the "administer nodes" permission. Such users have |
|
1948 * unrestricted access to all nodes. Then the node module's hook_access() |
|
1949 * is called, and a TRUE or FALSE return value will grant or deny access. |
|
1950 * This allows, for example, the blog module to always grant access to the |
|
1951 * blog author, and for the book module to always deny editing access to |
|
1952 * PHP pages. |
|
1953 * |
|
1954 * If node module does not intervene (returns NULL), then the |
|
1955 * node_access table is used to determine access. All node access |
|
1956 * modules are queried using hook_node_grants() to assemble a list of |
|
1957 * "grant IDs" for the user. This list is compared against the table. |
|
1958 * If any row contains the node ID in question (or 0, which stands for "all |
|
1959 * nodes"), one of the grant IDs returned, and a value of TRUE for the |
|
1960 * operation in question, then access is granted. Note that this table is a |
|
1961 * list of grants; any matching row is sufficient to grant access to the |
|
1962 * node. |
|
1963 * |
|
1964 * In node listings, the process above is followed except that |
|
1965 * hook_access() is not called on each node for performance reasons and for |
|
1966 * proper functioning of the pager system. When adding a node listing to your |
|
1967 * module, be sure to use db_rewrite_sql() to add |
|
1968 * the appropriate clauses to your query for access checks. |
|
1969 * |
|
1970 * To see how to write a node access module of your own, see |
|
1971 * node_access_example.module. |
|
1972 */ |
|
1973 |
|
1974 /** |
|
1975 * Determine whether the current user may perform the given operation on the |
|
1976 * specified node. |
|
1977 * |
|
1978 * @param $op |
|
1979 * The operation to be performed on the node. Possible values are: |
|
1980 * - "view" |
|
1981 * - "update" |
|
1982 * - "delete" |
|
1983 * - "create" |
|
1984 * @param $node |
|
1985 * The node object (or node array) on which the operation is to be performed, |
|
1986 * or node type (e.g. 'forum') for "create" operation. |
|
1987 * @param $account |
|
1988 * Optional, a user object representing the user for whom the operation is to |
|
1989 * be performed. Determines access for a user other than the current user. |
|
1990 * @return |
|
1991 * TRUE if the operation may be performed. |
|
1992 */ |
|
1993 function node_access($op, $node, $account = NULL) { |
|
1994 global $user; |
|
1995 |
|
1996 if (!$node || !in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) { |
|
1997 // If there was no node to check against, or the $op was not one of the |
|
1998 // supported ones, we return access denied. |
|
1999 return FALSE; |
|
2000 } |
|
2001 // Convert the node to an object if necessary: |
|
2002 if ($op != 'create') { |
|
2003 $node = (object)$node; |
|
2004 } |
|
2005 // If no user object is supplied, the access check is for the current user. |
|
2006 if (empty($account)) { |
|
2007 $account = $user; |
|
2008 } |
|
2009 // If the node is in a restricted format, disallow editing. |
|
2010 if ($op == 'update' && !filter_access($node->format)) { |
|
2011 return FALSE; |
|
2012 } |
|
2013 |
|
2014 if (user_access('administer nodes', $account)) { |
|
2015 return TRUE; |
|
2016 } |
|
2017 |
|
2018 if (!user_access('access content', $account)) { |
|
2019 return FALSE; |
|
2020 } |
|
2021 |
|
2022 // Can't use node_invoke(), because the access hook takes the $op parameter |
|
2023 // before the $node parameter. |
|
2024 $module = node_get_types('module', $node); |
|
2025 if ($module == 'node') { |
|
2026 $module = 'node_content'; // Avoid function name collisions. |
|
2027 } |
|
2028 $access = module_invoke($module, 'access', $op, $node, $account); |
|
2029 if (!is_null($access)) { |
|
2030 return $access; |
|
2031 } |
|
2032 |
|
2033 // If the module did not override the access rights, use those set in the |
|
2034 // node_access table. |
|
2035 if ($op != 'create' && $node->nid && $node->status) { |
|
2036 $grants = array(); |
|
2037 foreach (node_access_grants($op, $account) as $realm => $gids) { |
|
2038 foreach ($gids as $gid) { |
|
2039 $grants[] = "(gid = $gid AND realm = '$realm')"; |
|
2040 } |
|
2041 } |
|
2042 |
|
2043 $grants_sql = ''; |
|
2044 if (count($grants)) { |
|
2045 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; |
|
2046 } |
|
2047 |
|
2048 $sql = "SELECT COUNT(*) FROM {node_access} WHERE (nid = 0 OR nid = %d) $grants_sql AND grant_$op >= 1"; |
|
2049 $result = db_query($sql, $node->nid); |
|
2050 return (db_result($result)); |
|
2051 } |
|
2052 |
|
2053 // Let authors view their own nodes. |
|
2054 if ($op == 'view' && $account->uid == $node->uid && $account->uid != 0) { |
|
2055 return TRUE; |
|
2056 } |
|
2057 |
|
2058 return FALSE; |
|
2059 } |
|
2060 |
|
2061 /** |
|
2062 * Generate an SQL join clause for use in fetching a node listing. |
|
2063 * |
|
2064 * @param $node_alias |
|
2065 * If the node table has been given an SQL alias other than the default |
|
2066 * "n", that must be passed here. |
|
2067 * @param $node_access_alias |
|
2068 * If the node_access table has been given an SQL alias other than the default |
|
2069 * "na", that must be passed here. |
|
2070 * @return |
|
2071 * An SQL join clause. |
|
2072 */ |
|
2073 function _node_access_join_sql($node_alias = 'n', $node_access_alias = 'na') { |
|
2074 if (user_access('administer nodes')) { |
|
2075 return ''; |
|
2076 } |
|
2077 |
|
2078 return 'INNER JOIN {node_access} '. $node_access_alias .' ON '. $node_access_alias .'.nid = '. $node_alias .'.nid'; |
|
2079 } |
|
2080 |
|
2081 /** |
|
2082 * Generate an SQL where clause for use in fetching a node listing. |
|
2083 * |
|
2084 * @param $op |
|
2085 * The operation that must be allowed to return a node. |
|
2086 * @param $node_access_alias |
|
2087 * If the node_access table has been given an SQL alias other than the default |
|
2088 * "na", that must be passed here. |
|
2089 * @param $account |
|
2090 * The user object for the user performing the operation. If omitted, the |
|
2091 * current user is used. |
|
2092 * @return |
|
2093 * An SQL where clause. |
|
2094 */ |
|
2095 function _node_access_where_sql($op = 'view', $node_access_alias = 'na', $account = NULL) { |
|
2096 if (user_access('administer nodes')) { |
|
2097 return; |
|
2098 } |
|
2099 |
|
2100 $grants = array(); |
|
2101 foreach (node_access_grants($op, $account) as $realm => $gids) { |
|
2102 foreach ($gids as $gid) { |
|
2103 $grants[] = "($node_access_alias.gid = $gid AND $node_access_alias.realm = '$realm')"; |
|
2104 } |
|
2105 } |
|
2106 |
|
2107 $grants_sql = ''; |
|
2108 if (count($grants)) { |
|
2109 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; |
|
2110 } |
|
2111 |
|
2112 $sql = "$node_access_alias.grant_$op >= 1 $grants_sql"; |
|
2113 return $sql; |
|
2114 } |
|
2115 |
|
2116 /** |
|
2117 * Fetch an array of permission IDs granted to the given user ID. |
|
2118 * |
|
2119 * The implementation here provides only the universal "all" grant. A node |
|
2120 * access module should implement hook_node_grants() to provide a grant |
|
2121 * list for the user. |
|
2122 * |
|
2123 * @param $op |
|
2124 * The operation that the user is trying to perform. |
|
2125 * @param $account |
|
2126 * The user object for the user performing the operation. If omitted, the |
|
2127 * current user is used. |
|
2128 * @return |
|
2129 * An associative array in which the keys are realms, and the values are |
|
2130 * arrays of grants for those realms. |
|
2131 */ |
|
2132 function node_access_grants($op, $account = NULL) { |
|
2133 |
|
2134 if (!isset($account)) { |
|
2135 $account = $GLOBALS['user']; |
|
2136 } |
|
2137 |
|
2138 return array_merge(array('all' => array(0)), module_invoke_all('node_grants', $account, $op)); |
|
2139 } |
|
2140 |
|
2141 /** |
|
2142 * Determine whether the user has a global viewing grant for all nodes. |
|
2143 */ |
|
2144 function node_access_view_all_nodes() { |
|
2145 static $access; |
|
2146 |
|
2147 if (!isset($access)) { |
|
2148 $grants = array(); |
|
2149 foreach (node_access_grants('view') as $realm => $gids) { |
|
2150 foreach ($gids as $gid) { |
|
2151 $grants[] = "(gid = $gid AND realm = '$realm')"; |
|
2152 } |
|
2153 } |
|
2154 |
|
2155 $grants_sql = ''; |
|
2156 if (count($grants)) { |
|
2157 $grants_sql = 'AND ('. implode(' OR ', $grants) .')'; |
|
2158 } |
|
2159 |
|
2160 $sql = "SELECT COUNT(*) FROM {node_access} WHERE nid = 0 $grants_sql AND grant_view >= 1"; |
|
2161 $result = db_query($sql); |
|
2162 $access = db_result($result); |
|
2163 } |
|
2164 |
|
2165 return $access; |
|
2166 } |
|
2167 |
|
2168 /** |
|
2169 * Implementation of hook_db_rewrite_sql |
|
2170 */ |
|
2171 function node_db_rewrite_sql($query, $primary_table, $primary_field) { |
|
2172 if ($primary_field == 'nid' && !node_access_view_all_nodes()) { |
|
2173 $return['join'] = _node_access_join_sql($primary_table); |
|
2174 $return['where'] = _node_access_where_sql(); |
|
2175 $return['distinct'] = 1; |
|
2176 return $return; |
|
2177 } |
|
2178 } |
|
2179 |
|
2180 /** |
|
2181 * This function will call module invoke to get a list of grants and then |
|
2182 * write them to the database. It is called at node save, and should be |
|
2183 * called by modules whenever something other than a node_save causes |
|
2184 * the permissions on a node to change. |
|
2185 * |
|
2186 * This function is the only function that should write to the node_access |
|
2187 * table. |
|
2188 * |
|
2189 * @param $node |
|
2190 * The $node to acquire grants for. |
|
2191 */ |
|
2192 function node_access_acquire_grants($node) { |
|
2193 $grants = module_invoke_all('node_access_records', $node); |
|
2194 if (empty($grants)) { |
|
2195 $grants[] = array('realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0); |
|
2196 } |
|
2197 else { |
|
2198 // retain grants by highest priority |
|
2199 $grant_by_priority = array(); |
|
2200 foreach ($grants as $g) { |
|
2201 $grant_by_priority[intval($g['priority'])][] = $g; |
|
2202 } |
|
2203 krsort($grant_by_priority); |
|
2204 $grants = array_shift($grant_by_priority); |
|
2205 } |
|
2206 |
|
2207 node_access_write_grants($node, $grants); |
|
2208 } |
|
2209 |
|
2210 /** |
|
2211 * This function will write a list of grants to the database, deleting |
|
2212 * any pre-existing grants. If a realm is provided, it will only |
|
2213 * delete grants from that realm, but it will always delete a grant |
|
2214 * from the 'all' realm. Modules which utilize node_access can |
|
2215 * use this function when doing mass updates due to widespread permission |
|
2216 * changes. |
|
2217 * |
|
2218 * @param $node |
|
2219 * The $node being written to. All that is necessary is that it contain a nid. |
|
2220 * @param $grants |
|
2221 * A list of grants to write. Each grant is an array that must contain the |
|
2222 * following keys: realm, gid, grant_view, grant_update, grant_delete. |
|
2223 * The realm is specified by a particular module; the gid is as well, and |
|
2224 * is a module-defined id to define grant privileges. each grant_* field |
|
2225 * is a boolean value. |
|
2226 * @param $realm |
|
2227 * If provided, only read/write grants for that realm. |
|
2228 * @param $delete |
|
2229 * If false, do not delete records. This is only for optimization purposes, |
|
2230 * and assumes the caller has already performed a mass delete of some form. |
|
2231 */ |
|
2232 function node_access_write_grants($node, $grants, $realm = NULL, $delete = TRUE) { |
|
2233 if ($delete) { |
|
2234 $query = 'DELETE FROM {node_access} WHERE nid = %d'; |
|
2235 if ($realm) { |
|
2236 $query .= " AND realm in ('%s', 'all')"; |
|
2237 } |
|
2238 db_query($query, $node->nid, $realm); |
|
2239 } |
|
2240 |
|
2241 // Only perform work when node_access modules are active. |
|
2242 if (count(module_implements('node_grants'))) { |
|
2243 foreach ($grants as $grant) { |
|
2244 if ($realm && $realm != $grant['realm']) { |
|
2245 continue; |
|
2246 } |
|
2247 // Only write grants; denies are implicit. |
|
2248 if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) { |
|
2249 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']); |
|
2250 } |
|
2251 } |
|
2252 } |
|
2253 } |
|
2254 |
|
2255 /** |
|
2256 * Flag / unflag the node access grants for rebuilding, or read the current |
|
2257 * value of the flag. |
|
2258 * |
|
2259 * When the flag is set, a message is displayed to users with 'access |
|
2260 * administration pages' permission, pointing to the 'rebuild' confirm form. |
|
2261 * This can be used as an alternative to direct node_access_rebuild calls, |
|
2262 * allowing administrators to decide when they want to perform the actual |
|
2263 * (possibly time consuming) rebuild. |
|
2264 * When unsure the current user is an adminisrator, node_access_rebuild |
|
2265 * should be used instead. |
|
2266 * |
|
2267 * @param $rebuild |
|
2268 * (Optional) The boolean value to be written. |
|
2269 * @return |
|
2270 * (If no value was provided for $rebuild) The current value of the flag. |
|
2271 */ |
|
2272 function node_access_needs_rebuild($rebuild = NULL) { |
|
2273 if (!isset($rebuild)) { |
|
2274 return variable_get('node_access_needs_rebuild', FALSE); |
|
2275 } |
|
2276 elseif ($rebuild) { |
|
2277 variable_set('node_access_needs_rebuild', TRUE); |
|
2278 } |
|
2279 else { |
|
2280 variable_del('node_access_needs_rebuild'); |
|
2281 } |
|
2282 } |
|
2283 |
|
2284 /** |
|
2285 * Rebuild the node access database. This is occasionally needed by modules |
|
2286 * that make system-wide changes to access levels. |
|
2287 * |
|
2288 * When the rebuild is required by an admin-triggered action (e.g module |
|
2289 * settings form), calling node_access_needs_rebuild(TRUE) instead of |
|
2290 * node_access_rebuild() lets the user perform his changes and actually |
|
2291 * rebuild only once he is done. |
|
2292 * |
|
2293 * Note : As of Drupal 6, node access modules are not required to (and actually |
|
2294 * should not) call node_access_rebuild() in hook_enable/disable anymore. |
|
2295 * |
|
2296 * @see node_access_needs_rebuild() |
|
2297 * |
|
2298 * @param $batch_mode |
|
2299 * Set to TRUE to process in 'batch' mode, spawning processing over several |
|
2300 * HTTP requests (thus avoiding the risk of PHP timeout if the site has a |
|
2301 * large number of nodes). |
|
2302 * hook_update_N and any form submit handler are safe contexts to use the |
|
2303 * 'batch mode'. Less decidable cases (such as calls from hook_user, |
|
2304 * hook_taxonomy, hook_node_type...) might consider using the non-batch mode. |
|
2305 */ |
|
2306 function node_access_rebuild($batch_mode = FALSE) { |
|
2307 db_query("DELETE FROM {node_access}"); |
|
2308 // Only recalculate if the site is using a node_access module. |
|
2309 if (count(module_implements('node_grants'))) { |
|
2310 if ($batch_mode) { |
|
2311 $batch = array( |
|
2312 'title' => t('Rebuilding content access permissions'), |
|
2313 'operations' => array( |
|
2314 array('_node_access_rebuild_batch_operation', array()), |
|
2315 ), |
|
2316 'finished' => '_node_access_rebuild_batch_finished' |
|
2317 ); |
|
2318 batch_set($batch); |
|
2319 } |
|
2320 else { |
|
2321 // If not in 'safe mode', increase the maximum execution time. |
|
2322 if (!ini_get('safe_mode')) { |
|
2323 set_time_limit(240); |
|
2324 } |
|
2325 $result = db_query("SELECT nid FROM {node}"); |
|
2326 while ($node = db_fetch_object($result)) { |
|
2327 $loaded_node = node_load($node->nid, NULL, TRUE); |
|
2328 // To preserve database integrity, only aquire grants if the node |
|
2329 // loads successfully. |
|
2330 if (!empty($loaded_node)) { |
|
2331 node_access_acquire_grants($loaded_node); |
|
2332 } |
|
2333 } |
|
2334 } |
|
2335 } |
|
2336 else { |
|
2337 // Not using any node_access modules. Add the default grant. |
|
2338 db_query("INSERT INTO {node_access} VALUES (0, 0, 'all', 1, 0, 0)"); |
|
2339 } |
|
2340 |
|
2341 if (!isset($batch)) { |
|
2342 drupal_set_message(t('Content permissions have been rebuilt.')); |
|
2343 node_access_needs_rebuild(FALSE); |
|
2344 cache_clear_all(); |
|
2345 } |
|
2346 } |
|
2347 |
|
2348 /** |
|
2349 * Batch operation for node_access_rebuild_batch. |
|
2350 * |
|
2351 * This is a mutlistep operation : we go through all nodes by packs of 20. |
|
2352 * The batch processing engine interrupts processing and sends progress |
|
2353 * feedback after 1 second execution time. |
|
2354 */ |
|
2355 function _node_access_rebuild_batch_operation(&$context) { |
|
2356 if (empty($context['sandbox'])) { |
|
2357 // Initiate multistep processing. |
|
2358 $context['sandbox']['progress'] = 0; |
|
2359 $context['sandbox']['current_node'] = 0; |
|
2360 $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}')); |
|
2361 } |
|
2362 |
|
2363 // Process the next 20 nodes. |
|
2364 $limit = 20; |
|
2365 $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit); |
|
2366 while ($row = db_fetch_array($result)) { |
|
2367 $loaded_node = node_load($row['nid'], NULL, TRUE); |
|
2368 // To preserve database integrity, only aquire grants if the node |
|
2369 // loads successfully. |
|
2370 if (!empty($loaded_node)) { |
|
2371 node_access_acquire_grants($loaded_node); |
|
2372 } |
|
2373 $context['sandbox']['progress']++; |
|
2374 $context['sandbox']['current_node'] = $loaded_node->nid; |
|
2375 } |
|
2376 |
|
2377 // Multistep processing : report progress. |
|
2378 if ($context['sandbox']['progress'] != $context['sandbox']['max']) { |
|
2379 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; |
|
2380 } |
|
2381 } |
|
2382 |
|
2383 /** |
|
2384 * Post-processing for node_access_rebuild_batch. |
|
2385 */ |
|
2386 function _node_access_rebuild_batch_finished($success, $results, $operations) { |
|
2387 if ($success) { |
|
2388 drupal_set_message(t('The content access permissions have been rebuilt.')); |
|
2389 node_access_needs_rebuild(FALSE); |
|
2390 } |
|
2391 else { |
|
2392 drupal_set_message(t('The content access permissions have not been properly rebuilt.'), 'error'); |
|
2393 } |
|
2394 cache_clear_all(); |
|
2395 } |
|
2396 |
|
2397 /** |
|
2398 * @} End of "defgroup node_access". |
|
2399 */ |
|
2400 |
|
2401 |
|
2402 /** |
|
2403 * @defgroup node_content Hook implementations for user-created content types. |
|
2404 * @{ |
|
2405 */ |
|
2406 |
|
2407 /** |
|
2408 * Implementation of hook_access(). |
|
2409 * |
|
2410 * Named so as not to conflict with node_access() |
|
2411 */ |
|
2412 function node_content_access($op, $node, $account) { |
|
2413 $type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type); |
|
2414 |
|
2415 if ($op == 'create') { |
|
2416 return user_access('create '. $type .' content', $account); |
|
2417 } |
|
2418 |
|
2419 if ($op == 'update') { |
|
2420 if (user_access('edit any '. $type .' content', $account) || (user_access('edit own '. $type .' content', $account) && ($account->uid == $node->uid))) { |
|
2421 return TRUE; |
|
2422 } |
|
2423 } |
|
2424 |
|
2425 if ($op == 'delete') { |
|
2426 if (user_access('delete any '. $type .' content', $account) || (user_access('delete own '. $type .' content', $account) && ($account->uid == $node->uid))) { |
|
2427 return TRUE; |
|
2428 } |
|
2429 } |
|
2430 } |
|
2431 |
|
2432 /** |
|
2433 * Implementation of hook_form(). |
|
2434 */ |
|
2435 function node_content_form($node, $form_state) { |
|
2436 $type = node_get_types('type', $node); |
|
2437 $form = array(); |
|
2438 |
|
2439 if ($type->has_title) { |
|
2440 $form['title'] = array( |
|
2441 '#type' => 'textfield', |
|
2442 '#title' => check_plain($type->title_label), |
|
2443 '#required' => TRUE, |
|
2444 '#default_value' => $node->title, |
|
2445 '#maxlength' => 255, |
|
2446 '#weight' => -5, |
|
2447 ); |
|
2448 } |
|
2449 |
|
2450 if ($type->has_body) { |
|
2451 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count); |
|
2452 } |
|
2453 |
|
2454 return $form; |
|
2455 } |
|
2456 |
|
2457 /** |
|
2458 * @} End of "defgroup node_content". |
|
2459 */ |
|
2460 |
|
2461 /** |
|
2462 * Implementation of hook_forms(). All node forms share the same form handler |
|
2463 */ |
|
2464 function node_forms() { |
|
2465 $forms = array(); |
|
2466 if ($types = node_get_types()) { |
|
2467 foreach (array_keys($types) as $type) { |
|
2468 $forms[$type .'_node_form']['callback'] = 'node_form'; |
|
2469 } |
|
2470 } |
|
2471 return $forms; |
|
2472 } |
|
2473 |
|
2474 /** |
|
2475 * Format the "Submitted by username on date/time" for each node |
|
2476 * |
|
2477 * @ingroup themeable |
|
2478 */ |
|
2479 function theme_node_submitted($node) { |
|
2480 return t('Submitted by !username on @datetime', |
|
2481 array( |
|
2482 '!username' => theme('username', $node), |
|
2483 '@datetime' => format_date($node->created), |
|
2484 )); |
|
2485 } |
|
2486 |
|
2487 /** |
|
2488 * Implementation of hook_hook_info(). |
|
2489 */ |
|
2490 function node_hook_info() { |
|
2491 return array( |
|
2492 'node' => array( |
|
2493 'nodeapi' => array( |
|
2494 'presave' => array( |
|
2495 'runs when' => t('When either saving a new post or updating an existing post'), |
|
2496 ), |
|
2497 'insert' => array( |
|
2498 'runs when' => t('After saving a new post'), |
|
2499 ), |
|
2500 'update' => array( |
|
2501 'runs when' => t('After saving an updated post'), |
|
2502 ), |
|
2503 'delete' => array( |
|
2504 'runs when' => t('After deleting a post') |
|
2505 ), |
|
2506 'view' => array( |
|
2507 'runs when' => t('When content is viewed by an authenticated user') |
|
2508 ), |
|
2509 ), |
|
2510 ), |
|
2511 ); |
|
2512 } |
|
2513 |
|
2514 /** |
|
2515 * Implementation of hook_action_info(). |
|
2516 */ |
|
2517 function node_action_info() { |
|
2518 return array( |
|
2519 'node_publish_action' => array( |
|
2520 'type' => 'node', |
|
2521 'description' => t('Publish post'), |
|
2522 'configurable' => FALSE, |
|
2523 'behavior' => array('changes_node_property'), |
|
2524 'hooks' => array( |
|
2525 'nodeapi' => array('presave'), |
|
2526 'comment' => array('insert', 'update'), |
|
2527 ), |
|
2528 ), |
|
2529 'node_unpublish_action' => array( |
|
2530 'type' => 'node', |
|
2531 'description' => t('Unpublish post'), |
|
2532 'configurable' => FALSE, |
|
2533 'behavior' => array('changes_node_property'), |
|
2534 'hooks' => array( |
|
2535 'nodeapi' => array('presave'), |
|
2536 'comment' => array('delete', 'insert', 'update'), |
|
2537 ), |
|
2538 ), |
|
2539 'node_make_sticky_action' => array( |
|
2540 'type' => 'node', |
|
2541 'description' => t('Make post sticky'), |
|
2542 'configurable' => FALSE, |
|
2543 'behavior' => array('changes_node_property'), |
|
2544 'hooks' => array( |
|
2545 'nodeapi' => array('presave'), |
|
2546 'comment' => array('insert', 'update'), |
|
2547 ), |
|
2548 ), |
|
2549 'node_make_unsticky_action' => array( |
|
2550 'type' => 'node', |
|
2551 'description' => t('Make post unsticky'), |
|
2552 'configurable' => FALSE, |
|
2553 'behavior' => array('changes_node_property'), |
|
2554 'hooks' => array( |
|
2555 'nodeapi' => array('presave'), |
|
2556 'comment' => array('delete', 'insert', 'update'), |
|
2557 ), |
|
2558 ), |
|
2559 'node_promote_action' => array( |
|
2560 'type' => 'node', |
|
2561 'description' => t('Promote post to front page'), |
|
2562 'configurable' => FALSE, |
|
2563 'behavior' => array('changes_node_property'), |
|
2564 'hooks' => array( |
|
2565 'nodeapi' => array('presave'), |
|
2566 'comment' => array('insert', 'update'), |
|
2567 ), |
|
2568 ), |
|
2569 'node_unpromote_action' => array( |
|
2570 'type' => 'node', |
|
2571 'description' => t('Remove post from front page'), |
|
2572 'configurable' => FALSE, |
|
2573 'behavior' => array('changes_node_property'), |
|
2574 'hooks' => array( |
|
2575 'nodeapi' => array('presave'), |
|
2576 'comment' => array('delete', 'insert', 'update'), |
|
2577 ), |
|
2578 ), |
|
2579 'node_assign_owner_action' => array( |
|
2580 'type' => 'node', |
|
2581 'description' => t('Change the author of a post'), |
|
2582 'configurable' => TRUE, |
|
2583 'behavior' => array('changes_node_property'), |
|
2584 'hooks' => array( |
|
2585 'any' => TRUE, |
|
2586 'nodeapi' => array('presave'), |
|
2587 'comment' => array('delete', 'insert', 'update'), |
|
2588 ), |
|
2589 ), |
|
2590 'node_save_action' => array( |
|
2591 'type' => 'node', |
|
2592 'description' => t('Save post'), |
|
2593 'configurable' => FALSE, |
|
2594 'hooks' => array( |
|
2595 'comment' => array('delete', 'insert', 'update'), |
|
2596 ), |
|
2597 ), |
|
2598 'node_unpublish_by_keyword_action' => array( |
|
2599 'type' => 'node', |
|
2600 'description' => t('Unpublish post containing keyword(s)'), |
|
2601 'configurable' => TRUE, |
|
2602 'hooks' => array( |
|
2603 'nodeapi' => array('presave', 'insert', 'update'), |
|
2604 ), |
|
2605 ), |
|
2606 ); |
|
2607 } |
|
2608 |
|
2609 /** |
|
2610 * Implementation of a Drupal action. |
|
2611 * Sets the status of a node to 1, meaning published. |
|
2612 */ |
|
2613 function node_publish_action(&$node, $context = array()) { |
|
2614 $node->status = 1; |
|
2615 watchdog('action', 'Set @type %title to published.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2616 } |
|
2617 |
|
2618 /** |
|
2619 * Implementation of a Drupal action. |
|
2620 * Sets the status of a node to 0, meaning unpublished. |
|
2621 */ |
|
2622 function node_unpublish_action(&$node, $context = array()) { |
|
2623 $node->status = 0; |
|
2624 watchdog('action', 'Set @type %title to unpublished.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2625 } |
|
2626 |
|
2627 /** |
|
2628 * Implementation of a Drupal action. |
|
2629 * Sets the sticky-at-top-of-list property of a node to 1. |
|
2630 */ |
|
2631 function node_make_sticky_action(&$node, $context = array()) { |
|
2632 $node->sticky = 1; |
|
2633 watchdog('action', 'Set @type %title to sticky.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2634 } |
|
2635 |
|
2636 /** |
|
2637 * Implementation of a Drupal action. |
|
2638 * Sets the sticky-at-top-of-list property of a node to 0. |
|
2639 */ |
|
2640 function node_make_unsticky_action(&$node, $context = array()) { |
|
2641 $node->sticky = 0; |
|
2642 watchdog('action', 'Set @type %title to unsticky.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2643 } |
|
2644 |
|
2645 /** |
|
2646 * Implementation of a Drupal action. |
|
2647 * Sets the promote property of a node to 1. |
|
2648 */ |
|
2649 function node_promote_action(&$node, $context = array()) { |
|
2650 $node->promote = 1; |
|
2651 watchdog('action', 'Promoted @type %title to front page.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2652 } |
|
2653 |
|
2654 /** |
|
2655 * Implementation of a Drupal action. |
|
2656 * Sets the promote property of a node to 0. |
|
2657 */ |
|
2658 function node_unpromote_action(&$node, $context = array()) { |
|
2659 $node->promote = 0; |
|
2660 watchdog('action', 'Removed @type %title from front page.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2661 } |
|
2662 |
|
2663 /** |
|
2664 * Implementation of a Drupal action. |
|
2665 * Saves a node. |
|
2666 */ |
|
2667 function node_save_action($node) { |
|
2668 node_save($node); |
|
2669 watchdog('action', 'Saved @type %title', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2670 } |
|
2671 |
|
2672 /** |
|
2673 * Implementation of a configurable Drupal action. |
|
2674 * Assigns ownership of a node to a user. |
|
2675 */ |
|
2676 function node_assign_owner_action(&$node, $context) { |
|
2677 $node->uid = $context['owner_uid']; |
|
2678 $owner_name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $context['owner_uid'])); |
|
2679 watchdog('action', 'Changed owner of @type %title to uid %name.', array('@type' => node_get_types('type', $node), '%title' => $node->title, '%name' => $owner_name)); |
|
2680 } |
|
2681 |
|
2682 function node_assign_owner_action_form($context) { |
|
2683 $description = t('The username of the user to which you would like to assign ownership.'); |
|
2684 $count = db_result(db_query("SELECT COUNT(*) FROM {users}")); |
|
2685 $owner_name = ''; |
|
2686 if (isset($context['owner_uid'])) { |
|
2687 $owner_name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $context['owner_uid'])); |
|
2688 } |
|
2689 |
|
2690 // Use dropdown for fewer than 200 users; textbox for more than that. |
|
2691 if (intval($count) < 200) { |
|
2692 $options = array(); |
|
2693 $result = db_query("SELECT uid, name FROM {users} WHERE uid > 0 ORDER BY name"); |
|
2694 while ($data = db_fetch_object($result)) { |
|
2695 $options[$data->name] = $data->name; |
|
2696 } |
|
2697 $form['owner_name'] = array( |
|
2698 '#type' => 'select', |
|
2699 '#title' => t('Username'), |
|
2700 '#default_value' => $owner_name, |
|
2701 '#options' => $options, |
|
2702 '#description' => $description, |
|
2703 ); |
|
2704 } |
|
2705 else { |
|
2706 $form['owner_name'] = array( |
|
2707 '#type' => 'textfield', |
|
2708 '#title' => t('Username'), |
|
2709 '#default_value' => $owner_name, |
|
2710 '#autocomplete_path' => 'user/autocomplete', |
|
2711 '#size' => '6', |
|
2712 '#maxlength' => '7', |
|
2713 '#description' => $description, |
|
2714 ); |
|
2715 } |
|
2716 return $form; |
|
2717 } |
|
2718 |
|
2719 function node_assign_owner_action_validate($form, $form_state) { |
|
2720 $count = db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s'", $form_state['values']['owner_name'])); |
|
2721 if (intval($count) != 1) { |
|
2722 form_set_error('owner_name', t('Please enter a valid username.')); |
|
2723 } |
|
2724 } |
|
2725 |
|
2726 function node_assign_owner_action_submit($form, $form_state) { |
|
2727 // Username can change, so we need to store the ID, not the username. |
|
2728 $uid = db_result(db_query("SELECT uid from {users} WHERE name = '%s'", $form_state['values']['owner_name'])); |
|
2729 return array('owner_uid' => $uid); |
|
2730 } |
|
2731 |
|
2732 function node_unpublish_by_keyword_action_form($context) { |
|
2733 $form['keywords'] = array( |
|
2734 '#title' => t('Keywords'), |
|
2735 '#type' => 'textarea', |
|
2736 '#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.'), |
|
2737 '#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '', |
|
2738 ); |
|
2739 return $form; |
|
2740 } |
|
2741 |
|
2742 function node_unpublish_by_keyword_action_submit($form, $form_state) { |
|
2743 return array('keywords' => drupal_explode_tags($form_state['values']['keywords'])); |
|
2744 } |
|
2745 |
|
2746 /** |
|
2747 * Implementation of a configurable Drupal action. |
|
2748 * Unpublish a node if it contains a certain string. |
|
2749 * |
|
2750 * @param $context |
|
2751 * An array providing more information about the context of the call to this action. |
|
2752 * @param $comment |
|
2753 * A node object. |
|
2754 */ |
|
2755 function node_unpublish_by_keyword_action($node, $context) { |
|
2756 foreach ($context['keywords'] as $keyword) { |
|
2757 if (strstr(node_view(drupal_clone($node)), $keyword) || strstr($node->title, $keyword)) { |
|
2758 $node->status = 0; |
|
2759 watchdog('action', 'Set @type %title to unpublished.', array('@type' => node_get_types('name', $node), '%title' => $node->title)); |
|
2760 break; |
|
2761 } |
|
2762 } |
|
2763 } |