| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: xmlsitemap_node.module,v 1.19.2.20.2.103 2010/04/30 03:31:21 davereid Exp $ 3 4 /** 5 * Implements hook_xmlsitemap_link_info(). 6 */ 7 function xmlsitemap_node_xmlsitemap_link_info() { 8 $types['node'] = array( 9 'label' => t('Content'), 10 'bundle label' => t('Content type'), 11 'base table' => 'node', 12 'entity keys' => array( 13 'id' => 'nid', 14 'bundle' => 'type', 15 ), 16 'uri callback' => 'xmlsitemap_node_path', 17 'load hook' => 'node_load', 18 'xmlsitemap' => array( 19 'process callback' => 'xmlsitemap_node_xmlsitemap_process_node_links', 20 ), 21 ); 22 foreach (node_get_types('names') as $type => $name) { 23 $types['node']['bundles'][$type] = array( 24 'label' => $name, 25 'admin' => array( 26 'real path' => 'admin/content/node-type/' . str_replace('_', '-', $type), 27 'access arguments' => array('administer content types'), 28 ), 29 ); 30 } 31 return $types; 32 } 33 34 /** 35 * @todo Replace with node_path in Drupal 7. 36 */ 37 function xmlsitemap_node_path($node) { 38 return 'node/' . $node->nid; 39 } 40 41 /** 42 * Implements hook_cron(). 43 * 44 * Process old nodes not found in the {xmlsitemap} table. 45 */ 46 function xmlsitemap_node_cron() { 47 xmlsitemap_node_xmlsitemap_index_links(xmlsitemap_var('batch_limit')); 48 } 49 50 /** 51 * Implements hook_xmlsitemap_index_links(). 52 */ 53 function xmlsitemap_node_xmlsitemap_index_links($limit) { 54 if ($types = xmlsitemap_get_link_type_enabled_bundles('node')) { 55 $query = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {xmlsitemap} x ON x.type = 'node' AND n.nid = x.id WHERE x.id IS NULL AND n.type IN (" . db_placeholders($types, 'varchar') . ") ORDER BY n.nid DESC", $types, 0, $limit); 56 $nids = array(); 57 while ($nid = db_result($query)) { 58 $nids[] = $nid; 59 } 60 xmlsitemap_node_xmlsitemap_process_node_links($nids); 61 } 62 } 63 64 /** 65 * Process node sitemap links. 66 * 67 * @param $nids 68 * An array of node IDs. 69 */ 70 function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) { 71 foreach ($nids as $nid) { 72 if ($node = node_load($nid, NULL, TRUE)) { 73 $link = xmlsitemap_node_create_link($node); 74 xmlsitemap_link_save($link); 75 } 76 } 77 } 78 79 /** 80 * Implements hook_nodeapi(). 81 */ 82 function xmlsitemap_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 83 switch ($op) { 84 case 'insert': 85 case 'update': 86 $link = xmlsitemap_node_create_link($node); 87 xmlsitemap_link_save($link); 88 break; 89 case 'delete': 90 xmlsitemap_link_delete('node', $node->nid); 91 break; 92 } 93 } 94 95 /** 96 * Implements hook_comment(). 97 */ 98 function xmlsitemap_node_comment($comment, $op) { 99 switch ($op) { 100 case 'update': 101 case 'publish': 102 case 'unpublish': 103 case 'delete': 104 $comment = (object) $comment; 105 if ($node = node_load($comment->nid, NULL, TRUE)) { 106 $link = xmlsitemap_node_create_link($node); 107 xmlsitemap_link_save($link); 108 } 109 break; 110 } 111 } 112 113 /** 114 * Implements hook_node_type(). 115 */ 116 function xmlsitemap_node_node_type($op, $info) { 117 if ($op == 'update') { 118 // Cannot perform xmlsitemap_link_bundle_settings_save() here since 119 // node_type_form_submit() strips all non-essential data from $info. 120 if (!empty($info->old_type) && $info->old_type != $info->type) { 121 xmlsitemap_link_bundle_rename('node', $info->old_type, $info->type); 122 } 123 } 124 if ($op == 'delete') { 125 xmlsitemap_link_bundle_delete('node', $info->type); 126 } 127 } 128 129 /** 130 * Implements hook_content_extra_fields(). 131 * 132 * This is so that the XML sitemap fieldset is sortable on the node edit form. 133 */ 134 function xmlsitemap_node_content_extra_fields() { 135 $extras['xmlsitemap'] = array( 136 'label' => t('XML sitemap'), 137 'description' => t('XML sitemap module form'), 138 'weight' => 30, 139 ); 140 return $extras; 141 } 142 143 /** 144 * Implements hook_form_FORM_ID_alter(). 145 * 146 * @see node_type_form() 147 * @see xmlsitemap_add_link_bundle_settings() 148 */ 149 function xmlsitemap_node_form_node_type_form_alter(&$form, $form_state) { 150 $node_type = isset($form['#node_type']->type) ? $form['#node_type']->type : ''; 151 152 module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin'); 153 xmlsitemap_add_link_bundle_settings($form, $form_state, 'node', $node_type); 154 155 // Add our submit handler before node_type_form_submit() so we can compare 156 // the old and new values. 157 array_unshift($form['#submit'], 'xmlsitemap_node_type_form_submit'); 158 } 159 160 function xmlsitemap_node_type_form_submit($form, &$form_state) { 161 $node_type = $form_state['values']['old_type']; 162 xmlsitemap_link_bundle_settings_save('node', $node_type, $form_state['values']['xmlsitemap']); 163 164 // Do not allow node_type_form_submit() to run variable_set() on this. 165 unset($form_state['values']['xmlsitemap']); 166 } 167 168 /** 169 * Implements hook_form_alter(). 170 * 171 * Add the XML sitemap individual link options for a node. 172 * 173 * @see xmlsitemap_add_form_link_options() 174 */ 175 function xmlsitemap_node_form_alter(&$form, $form_state, $form_id) { 176 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { 177 // Add the link options. 178 module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin'); 179 xmlsitemap_add_form_link_options($form, 'node', $form['type']['#value'], $form['nid']['#value']); 180 $form['xmlsitemap']['#access'] |= user_access('administer nodes'); 181 $form['xmlsitemap']['#weight'] = 30; 182 } 183 } 184 185 /** 186 * Fetch all the timestamps for when a node was changed. 187 * 188 * @param $node 189 * A node object. 190 * @return 191 * An array of UNIX timestamp integers. 192 */ 193 function xmlsitemap_node_get_timestamps($node) { 194 static $timestamps = array(); 195 196 if (!isset($timestamps[$node->nid])) { 197 $timestamps[$node->nid] = array(); 198 $query = db_query("SELECT c.timestamp FROM {comments} c WHERE c.nid = %d AND c.status = %d UNION ALL SELECT nr.timestamp FROM {node_revisions} nr WHERE nr.nid = %d", $node->nid, COMMENT_PUBLISHED, $node->nid); 199 while ($timestamp = db_result($query)) { 200 $timestamps[$node->nid][] = (int) $timestamp; 201 } 202 } 203 204 return $timestamps[$node->nid]; 205 } 206 207 /** 208 * Create a sitemap link from a node. 209 * 210 * The link will be saved as $node->xmlsitemap. 211 * 212 * @param $node 213 * A node object. 214 */ 215 function xmlsitemap_node_create_link(stdClass &$node) { 216 if (!isset($node->xmlsitemap) || !is_array($node->xmlsitemap)) { 217 $node->xmlsitemap = array(); 218 if ($node->nid && $link = xmlsitemap_link_load('node', $node->nid)) { 219 $node->xmlsitemap = $link; 220 } 221 } 222 223 $settings = xmlsitemap_link_bundle_load('node', $node->type); 224 225 $node->xmlsitemap += array( 226 'type' => 'node', 227 'id' => $node->nid, 228 'subtype' => $node->type, 229 'loc' => 'node/'. $node->nid, 230 'status' => $settings['status'], 231 'status_default' => $settings['status'], 232 'status_override' => 0, 233 'priority' => $settings['priority'], 234 'priority_default' => $settings['priority'], 235 'priority_override' => 0, 236 ); 237 238 // Always recalculate changefreq and changecount. 239 $timestamps = xmlsitemap_node_get_timestamps($node); 240 $node->xmlsitemap['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0; 241 $node->xmlsitemap['changecount'] = $node->nid ? count($timestamps) - 1 : 0; 242 243 // The following values must always be checked because they are volatile. 244 $node->xmlsitemap['lastmod'] = isset($node->changed) ? $node->changed : REQUEST_TIME; 245 $node->xmlsitemap['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1; 246 $node->xmlsitemap['language'] = isset($node->language) ? $node->language : ''; 247 248 return $node->xmlsitemap; 249 } 250 251 /** 252 * Determine whether a user may view the specified node. 253 * 254 * @param $node 255 * The node object on which the operation is to be performed, or node type 256 * (e.g. 'forum') for "create" operation. 257 * @param $account 258 * Optional, a user object representing the user for whom the operation is to 259 * be performed. Determines access for a user other than the current user. 260 * @return 261 * TRUE if the operation may be performed, or FALSE otherwise. 262 * 263 * This is for all intesive purposes a copy of Drupal 7's node_access() function. 264 * It invokes a backport of Drupal 7's hook_node_grants_alter() specifically 265 * for use with XML sitemap. 266 */ 267 function xmlsitemap_node_view_access($node, $account = NULL) { 268 global $user; 269 270 $op = 'view'; 271 $rights = &xmlsitemap_static(__FUNCTION__, array()); 272 273 if (!$node || !in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) { 274 // If there was no node to check against, or the $op was not one of the 275 // supported ones, we return access denied. 276 return FALSE; 277 } 278 // If no user object is supplied, the access check is for the current user. 279 if (empty($account)) { 280 $account = $user; 281 } 282 283 // $node may be either an object or a node type. Since node types cannot be 284 // an integer, use either nid or type as the static cache id. 285 //$cid = is_object($node) ? $node->nid : $node; 286 287 // If we've already checked access for this node, user and op, return from 288 // cache. 289 if (isset($rights[$account->uid][$node->nid])) { 290 return $rights[$account->uid][$node->nid]; 291 } 292 293 if (user_access('administer nodes', $account)) { 294 $rights[$account->uid][$node->nid] = TRUE; 295 return TRUE; 296 } 297 298 if (!user_access('access content', $account)) { 299 $rights[$account->uid][$node->nid] = FALSE; 300 return FALSE; 301 } 302 303 // Can't use node_invoke(), because the access hook takes the $op parameter 304 // before the $node parameter. 305 $module = node_get_types('module', $node); 306 if ($module == 'node') { 307 $module = 'node_content'; // Avoid function name collisions. 308 } 309 $access = module_invoke($module, 'access', $op, $node, $account); 310 if (!is_null($access)) { 311 $rights[$account->uid][$node->nid] = $access; 312 return $access; 313 } 314 315 // If the module did not override the access rights, use those set in the 316 // node_access table. 317 if ($op != 'create' && $node->nid) { 318 if (module_implements('node_grants')) { 319 // Fetch the node grants and allow other modules to alter them (D7 backport). 320 $grants = &xmlsitemap_static(__FUNCTION__ . ':grants', array()); 321 if (!isset($grants[$account->uid][$op])) { 322 // Indicate that this is our special function in the grants. 323 $account->xmlsitemap_node_access = TRUE; 324 $grants[$account->uid][$op] = node_access_grants($op, $account); 325 // Call hook_node_grants_alter(). This is a backport from Drupal 7. 326 drupal_alter('node_grants', $grants[$account->uid][$op], $account, $op); 327 // Remove the special indicator. 328 unset($account->xmlsitemap_node_access); 329 } 330 331 $grant_condition = array(); 332 foreach ($grants[$account->uid][$op] as $realm => $gids) { 333 foreach ($gids as $gid) { 334 $grant_condition[] = "(gid = $gid AND realm = '$realm')"; 335 } 336 } 337 if (count($grant_condition)) { 338 $grant_condition = 'AND ('. implode(' OR ', $grant_condition) .')'; 339 } 340 else { 341 $grant_condition = ''; 342 } 343 344 $sql = "SELECT 1 FROM {node_access} WHERE (nid = 0 OR nid = %d) $grant_condition AND grant_$op >= 1"; 345 $result = (bool) db_result(db_query_range($sql, $node->nid, 0, 1)); 346 $rights[$account->uid][$node->nid] = $result; 347 return $result; 348 } 349 elseif ($op == 'view' && $node->status) { 350 // If no modules implement hook_node_grants(), the default behaviour is to 351 // allow all users to view published nodes, so reflect that here. 352 $rights[$account->uid][$op] = TRUE; 353 return TRUE; 354 } 355 } 356 357 // Check if authors can view their own unpublished nodes. 358 if ($op == 'view' && !$node->status && $account->uid == $node->uid && $account->uid) { 359 $rights[$account->uid][$op] = TRUE; 360 return TRUE; 361 } 362 363 return FALSE; 364 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |