diff --git a/expire.admin.inc b/expire.admin.inc index a4c4468d3acf54c0784ad1d3476e2ec5c2053f9d..b1042519430e420cb3759683a4663f68d3ac5bcb 100644 --- a/expire.admin.inc +++ b/expire.admin.inc @@ -14,6 +14,7 @@ function expire_admin_settings_form() { '#type' => 'fieldset', '#title' => t('What to expire'), ); + $form['expire']['expire_flush_front'] = array( '#type' => 'checkbox', '#title' => t('Expire front page'), @@ -21,31 +22,39 @@ function expire_admin_settings_form() { '#description' => t('When expiring a node: if promoted to front page, expire front page.'), ); - $form['expire']['expire_flush_node_terms'] = array( - '#type' => 'checkbox', - '#title' => t('Expire node term pages'), - '#default_value' => variable_get('expire_flush_node_terms', EXPIRE_FLUSH_NODE_TERMS), - '#description' => t('When expiring a node: expire taxonomy pages for its terms.'), - ); + if (module_exists('taxonomy')) { + $form['expire']['expire_flush_node_terms'] = array( + '#type' => 'checkbox', + '#title' => t('Expire node term pages'), + '#default_value' => variable_get('expire_flush_node_terms', EXPIRE_FLUSH_NODE_TERMS), + '#description' => t('When expiring a node: expire taxonomy pages for its terms.'), + ); + } - $form['expire']['expire_flush_menu_items'] = array( - '#type' => 'radios', - '#title' => t('Expire menus'), - '#options' => array(0 => t('No'), EXPIRE_FLUSH_MENU_ITEMS => t('Family'), 2 => t('Entire menu')), - '#default_value' => variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS), - '#description' => t('When expiring a node: expire related menu items or entire menu'), - ); - $form['expire']['expire_flush_cck_references'] = array( - '#type' => 'checkbox', - '#title' => t('Expire CCK node references'), - '#default_value' => variable_get('expire_flush_cck_references', EXPIRE_FLUSH_CCK_REFERENCES), - '#description' => t('When expiring a node: expire its node references and nodes containing it in their own node references.'), - ); + if (module_exists('menu')) { + $form['expire']['expire_flush_menu_items'] = array( + '#type' => 'radios', + '#title' => t('Expire menus'), + '#options' => array(0 => t('No'), EXPIRE_FLUSH_MENU_ITEMS => t('Family'), 2 => t('Entire menu')), + '#default_value' => variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS), + '#description' => t('When expiring a node: expire related menu items or entire menu'), + ); + } + + if (module_exists('node_reference')) { + $form['expire']['expire_flush_node_references'] = array( + '#type' => 'checkbox', + '#title' => t('Expire node references'), + '#default_value' => variable_get('expire_flush_node_references', EXPIRE_FLUSH_NODE_REFERENCES), + '#description' => t('When expiring a node: expire its node references and nodes containing it in their own node references.'), + ); + } $form['format'] = array( '#type' => 'fieldset', '#title' => t('Expire protocol'), ); + $form['format']['expire_include_base_url'] = array( '#type' => 'checkbox', '#title' => t('Include base URL in expires'), diff --git a/expire.domain.inc b/expire.domain.inc new file mode 100644 index 0000000000000000000000000000000000000000..f690131ea7555d2e33c852170b42561334199706 --- /dev/null +++ b/expire.domain.inc @@ -0,0 +1,74 @@ + $gid) + */ +function expire_get_domains(&$node) { + $domains = array(); + if ($node->nid) { + $result = db_query("SELECT gid FROM {domain_access} WHERE nid = :nid", array(':nid' => $node->nid)); + while ($row = db_fetch_array($result)) { + $gid = $row['gid']; + $domains[$gid] = $gid; + } + } + elseif ($node->mail && $node->name) { + $result = db_query("SELECT domain_id FROM {domain_editor} WHERE uid = :uid", array(':uid' => $node->uid)); + while ($row = db_fetch_array($result)) { + $gid = $row['domain_id']; + $domains[$gid] = $gid; + } + } + return $domains; +} + +/** + * Get all base url's where this node can appear. + * + * @param $node + * node object + * @return array + * array(0 => array($base_url . '/')) + */ +function expire_get_base_urls($node) { + global $base_url, $base_path; + + // Get list of URL's if using domain access + $base_urls = array(); + $domains = array(); + // Get domains from node/user object + foreach ($node->domains as $key => $domain_id) { + if ($key != $domain_id) { + continue; + } + $domains[$domain_id] = $domain_id; + } + // Get domains from database + foreach (expire_get_domains($node) as $domain_id) { + $domains[$domain_id] = $domain_id; + } + // Get aliases and set base url + foreach ($domains as $domain_id) { + $domain = domain_lookup($domain_id); + if ($domain['valid'] == 1) { + if (isset($domain['path'])) { + $base_urls[$domain_id][] = $domain['path']; + } + if (is_array($domain['aliases'])) { + foreach ($domain['aliases'] as $alias) { + if ($alias['redirect'] != 1) { + $temp_domain = array('scheme' => $domain['scheme'], 'subdomain' => $alias['pattern']); + $base_urls[$domain_id][] = domain_get_path($temp_domain); + } + } + } + } + } + return $base_urls; +} + diff --git a/expire.info b/expire.info index 3423afd968d3fe07735d7a85d47aff80ca1c1b00..7f18e2558a6390e699e85f5e2b565d069bd6cd14 100644 --- a/expire.info +++ b/expire.info @@ -6,6 +6,9 @@ core = 7.x files[] = expire.module files[] = expire.admin.inc +files[] = expire.domain.inc files[] = expire.drush.inc +files[] = expire.node_reference.inc files[] = expire.rules.inc +files[] = expire.votingapi.inc configure = admin/config/development/performance/expire diff --git a/expire.install b/expire.install index 51101aee5e6f131e37e5fb3ba19ad022ff37da68..e42515450f75d59445135f2dc2b477e522abf3e2 100644 --- a/expire.install +++ b/expire.install @@ -13,7 +13,7 @@ function expire_uninstall() { 'expire_flush_front', 'expire_flush_node_terms', 'expire_flush_menu_items', - 'expire_flush_cck_references', + 'expire_flush_node_references', 'expire_include_base_url', ); foreach ($vars as $var) { diff --git a/expire.module b/expire.module index 67d82a49a9cf8561268b76bc962aff7c4097927a..3026c3b2982e5d9205a4b6f625857ed3fb8e9e58 100644 --- a/expire.module +++ b/expire.module @@ -8,7 +8,7 @@ // Defaults used if variable_get is not set. define('EXPIRE_FLUSH_NODE_TERMS', TRUE); define('EXPIRE_FLUSH_MENU_ITEMS', 1); -define('EXPIRE_FLUSH_CCK_REFERENCES', TRUE); +define('EXPIRE_FLUSH_NODE_REFERENCES', TRUE); define('EXPIRE_FLUSH_FRONT', TRUE); define('EXPIRE_INCLUDE_BASE_URL', TRUE); @@ -43,15 +43,11 @@ function expire_menu() { * Acts on new comments. */ function expire_comment_insert($comment) { - // Return if no node id is attached to the comment. - if (empty($comment->nid)) { - return; - } - - // Expire the relevant node page from the static page cache to prevent serving stale content. - $node = node_load($comment->nid); - if ($node) { - expire_node($node); + if (!empty($comment->nid)) { + $node = node_load($comment->nid); + if ($node) { + expire_node($node); + } } } @@ -61,12 +57,11 @@ function expire_comment_insert($comment) { * Acts on comments updates. */ function expire_comment_update($comment) { - if (empty($comment->nid)) { - return; - } - $node = node_load($comment->nid); - if ($node) { - expire_node($node); + if (!empty($comment->nid)) { + $node = node_load($comment->nid); + if ($node) { + expire_node($node); + } } } @@ -76,12 +71,11 @@ function expire_comment_update($comment) { * Acts when publishing comments. */ function expire_comment_publish($comment) { - if (empty($comment->nid)) { - return; - } - $node = node_load($comment->nid); - if ($node) { - expire_node($node); + if (!empty($comment->nid)) { + $node = node_load($comment->nid); + if ($node) { + expire_node($node); + } } } @@ -91,12 +85,11 @@ function expire_comment_publish($comment) { * Acts when unpublishing comments. */ function expire_comment_unpublish($comment) { - if (empty($comment->nid)) { - return; - } - $node = node_load($comment->nid); - if ($node) { - expire_node($node); + if (!empty($comment->nid)) { + $node = node_load($comment->nid); + if ($node) { + expire_node($node); + } } } @@ -107,68 +100,55 @@ function expire_comment_unpublish($comment) { * Acts when deleting comments. */ function expire_comment_delete($comment) { - if (empty($comment->nid)) { - return; - } - $node = node_load($comment->nid); - if ($node) { - expire_node($node); + if (!empty($comment->nid)) { + $node = node_load($comment->nid); + if ($node) { + expire_node($node); + } } } /** * Implements hook_node_insert(). - * + * * Acts on new nodes. */ function expire_node_insert($node) { - if (empty($node->nid)) { - return; - } - else { + if (!empty($node->nid)) { expire_node($node); } } /** * Implements hook_node_update(). - * + * * Acts on node updates. */ function expire_node_update($node) { - if (empty($node->nid)) { - return; - } - else { + if (!empty($node->nid)) { expire_node($node); } } /** * Implements hook_node_delete(). - * + * * Acts on deletion of nodes. */ function expire_node_delete($node) { - if (empty($node->nid)) { - return; - } - else { + if (!empty($node->nid)) { expire_node($node); } } /** * Implements hook_node_revision_delete(). - * + * * Acts on deletion of revisions. */ function expire_node_revision_delete($node) { - if (empty($node->nid)) { - return; - } - else { + if (!empty($node->nid)) { expire_node($node); } } @@ -231,50 +211,6 @@ function expire_user_update(&$edit, $account, $category = NULL) { )); } - -/** - * Implementation of hook_votingapi_insert(). - * - * @param $votes - * array of votes - */ -function expire_votingapi_insert($votes) { - _expire_votingapi($votes); -} - -/** - * Implementation of hook_votingapi_delete(). - * - * @param $votes - * array of votes - */ -function expire_votingapi_delete($votes) { - _expire_votingapi($votes); -} - -/** - * Common expiry logic for votingapi. - */ -function _expire_votingapi($votes) { - foreach ($votes as $vote) { - if ($vote['content_type'] == 'comment') { - $nid = db_result(db_query("SELECT nid FROM {comments} WHERE cid = %d", $vote['content_id'])); - if (is_numeric($nid)) { - $node = node_load($nid); - if ($node) { - expire_node($node); - } - } - } - if ($vote['content_type'] == 'node') { - $node = node_load($vote['content_id']); - if ($node) { - expire_node($node); - } - } - } -} - /** * Expires a node from the cache; including related pages. * @@ -283,7 +219,7 @@ function _expire_votingapi($votes) { * @param $node * node object */ -function expire_node(&$node) { +function expire_node($node) { $paths = array(); // Check node object @@ -301,36 +237,29 @@ function expire_node(&$node) { // Get taxonomy terms and flush if (module_exists('taxonomy') && variable_get('expire_flush_node_terms', EXPIRE_FLUSH_NODE_TERMS)) { - // Get old terms from DB - $tids = expire_taxonomy_node_get_tids($node->nid); - // Get old terms from static variable - $terms = taxonomy_node_get_terms($node); - if (!empty($terms)) { - foreach ($terms as $term) { - $tids[$term->tid] = $term->tid; - } - } - // Get new terms from node object - if (!empty($node->taxonomy)) { - foreach ($node->taxonomy as $vocab) { - if (is_array($vocab)) { - foreach ($vocab as $term) { - $tids[$term] = $term; - } + $terms = array(); + $info = field_info_fields(); + + foreach (field_info_instances('node', $node->type) as $name => $instance) { + if ($info[$name]['type'] == 'taxonomy_term_reference') { + $new_terms = field_get_items('node', $node, $name); + if (is_array($new_terms)) { + $terms = array_merge($new_terms, $terms); + } + $old_terms = isset($node->original) && !empty($node->original) ? field_get_items('node', $node->original, $name) : array(); + if (is_array($old_terms)) { + $terms = array_merge($old_terms, $terms); } } } - $filenames = array(); - foreach ($tids as $tid) { - if (is_numeric($tid)) { - $term = taxonomy_get_term($tid); - $paths['term' . $tid] = taxonomy_term_path($term); - } + + foreach ($terms as $term) { + $paths['term' . $term['tid']] = 'taxonomy/term/' . $term['tid']; } } // Get menu and flush related items in the menu. - if (variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS) !=0) { + if (module_exists('menu') && variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS) != 0) { if (!isset($node->menu['menu_name'])) { menu_node_prepare($node); } @@ -348,48 +277,12 @@ function expire_node(&$node) { $paths = array_merge($links, $paths); } - // Get CCK References and flush. - if (variable_get('expire_flush_cck_references', EXPIRE_FLUSH_CCK_REFERENCES) && module_exists('nodereference')) { - $nids = array(); - $type = content_types($node->type); - if ($type) { - foreach ($type['fields'] as $field) { - // Add referenced nodes to nids. This will clean up nodereferrer fields - // when the referencing node is updated. - if ($field['type'] == 'nodereference') { - $node_field = isset($node->$field['field_name']) ? $node->$field['field_name'] : array(); - foreach ($node_field as $delta => $item) { - if (is_numeric($item['nid'])) { - $paths['reference' . $item['nid']] = 'node/'. $item['nid']; - } - } - - // Look for node referers without using nodereferrer - $info = content_database_info($field); - $table = $info['table']; - $column = $info['columns']['nid']['column']; - $results = db_query("SELECT n.nid - FROM {%s} nr - INNER JOIN {node} n USING (vid) - WHERE nr.%s = %d", $table, $column, $node->nid); - while ($nid = db_result($results)) { - if (is_numeric($nid)) { - $paths['referenceparent' . $nid] = 'node/'. $nid; - } - } - } - } - } - - // Get CCK references pointing to this node and flush. - if (module_exists('nodereferrer')) { - $nids = nodereferrer_referrers($node->nid); - foreach ($nids as $nid) { - if (is_numeric($nid['nid'])) { - $paths['referrer' . $nid['nid']] = 'node/' . $nid['nid']; - } - } - } + // Get Node References and flush. + if (variable_get('expire_flush_node_references', EXPIRE_FLUSH_NODE_REFERENCES) + && module_exists('node_reference') + && module_load_include('inc', 'expire', 'expire.node_reference') != FALSE) { + $node_references = expire_get_node_references($node); + $paths = array_merge($node_references, $paths); } // Flush array of paths @@ -483,22 +376,6 @@ function expire_get_menu_structure($menu, $found, $needle, $first, &$found_globa } } -/** - * Return taxonomy terms given a nid. - * - * Needed because of a weird bug with CCK & node_load() - * http://drupal.org/node/545922 - */ -function expire_taxonomy_node_get_tids($nid) { - $vid = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $nid)); - $result = db_query(db_rewrite_sql("SELECT t.tid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = %d ORDER BY v.weight, t.weight, t.name", 't', 'tid'), $vid); - $tids = array(); - while ($term = db_result($result)) { - $tids[] = $term; - } - return $tids; -} - /** * Finds all possible paths/redirects/aliases given the root path. * @@ -534,12 +411,12 @@ function expire_cache_derivative($paths, &$node = NULL) { $expire[] = substr($path_alias, strlen($base_path)); // Path redirects - if (module_exists('path_redirect')) { - $path_redirects = expire_path_redirect_load(array('redirect' => $path)); - if (isset($path_redirects)) { - foreach ($path_redirects as $path_redirect) { - if (!empty($path_redirect['redirect'])) { - $expire[] = $path_redirect['redirect']; + if (module_exists('redirect')) { + $redirects = redirect_load_multiple(array(), array('redirect' => $path)); + if (isset($redirects) && !empty($redirects)) { + foreach ($redirects as $redirect) { + if (!empty($redirect->source)) { + $expire[] = $redirect->source; } } } @@ -558,11 +435,14 @@ function expire_cache_derivative($paths, &$node = NULL) { $urls = array(); global $base_url; if (variable_get('expire_include_base_url', EXPIRE_INCLUDE_BASE_URL)) { - foreach (expire_get_base_urls($node) as $domain_id) { - foreach ($domain_id as $base) { - foreach ($expire as $path) { - $urls[] = $base . $path; - } + $base_urls[] = $base_url; + if (module_exists('domain') + && module_load_include('inc', 'expire', 'expire.domain') != FALSE) { + $base_urls = array_merge($base_urls, expire_get_base_urls($node)); + } + foreach ($expire as $path) { + foreach ($base_urls as $base) { + $urls[] = $base . $path; } } } @@ -582,39 +462,6 @@ function expire_cache_derivative($paths, &$node = NULL) { return count($urls); } -/** - * Retrieve a specific URL redirect from the database. - * http://drupal.org/node/451790 - * - * @param $where - * Array containing 'redirect' => $path - */ -function expire_path_redirect_load($where = array(), $args = array(), $sort = array()) { - $redirects = array(); - if (is_numeric($where)) { - $where = array('rid' => $where); - } - - foreach ($where as $key => $value) { - if (is_string($key)) { - $args[] = $value; - $where[$key] = $key .' = '. (is_numeric($value) ? '%d' : "'%s'"); - } - } - - if ($where && $args) { - $sql = "SELECT * FROM {path_redirect} WHERE ". implode(' AND ', $where); - if ($sort) { - $sql .= ' ORDER BY '. implode(' ,', $sort); - } - $result = db_query($sql, $args); - while ($redirect = db_fetch_array($result)) { - $redirects[] = $redirect; - } - return $redirects; - } -} - /** * Simple print_r to html function * @@ -627,88 +474,55 @@ function expire_print_r($data) { return str_replace(' ', '    ', nl2br(htmlentities(print_r($data, TRUE)))); } +function expire_normal_path_check($path) { + $original_map = arg(NULL, $path); + $parts = array_slice($original_map, 0, MENU_MAX_PARTS); + list($ancestors, $placeholders) = menu_get_ancestors($parts); + + $router_item = db_query_range('SELECT path FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc(); + return $router_item; +} + /** - * Get all base url's where this node can appear. Domain access support. + * Implementation of hook_votingapi_insert(). * - * @param $node - * node object - * @return array - * array(0 => array($base_url . '/')) + * @param $votes + * array of votes */ -function expire_get_base_urls(&$node) { - global $base_url, $base_path; - - // Get list of URL's if using domain access - $base_urls = array(); - $domains = array(); - if (module_exists('domain') && isset($node->domains)) { - // Get domains from node/user object - foreach ($node->domains as $key => $domain_id) { - if ($key != $domain_id) { - continue; - } - $domains[$domain_id] = $domain_id; - } - // Get domains from database - foreach (expire_get_domains($node) as $domain_id) { - $domains[$domain_id] = $domain_id; - } - // Get aliases and set base url - foreach ($domains as $domain_id) { - $domain = domain_lookup($domain_id); - if ($domain['valid'] == 1) { - if (isset($domain['path'])) { - $base_urls[$domain_id][] = $domain['path']; - } - if (is_array($domain['aliases'])) { - foreach ($domain['aliases'] as $alias) { - if ($alias['redirect'] != 1) { - $temp_domain = array('scheme' => $domain['scheme'], 'subdomain' => $alias['pattern']); - $base_urls[$domain_id][] = domain_get_path($temp_domain); - } - } - } - } - } - } - else { - $base_urls[0][] = $base_url . '/'; - } - return $base_urls; +function expire_votingapi_insert($votes) { + _expire_votingapi($votes); } /** - * Get domains the node is currently published to + * Implementation of hook_votingapi_delete(). * - * @param $node - * node object - * @return array - * array('$gid' => $gid) + * @param $votes + * array of votes + */ +function expire_votingapi_delete($votes) { + _expire_votingapi($votes); +} + +/** + * Common expiry logic for votingapi. */ -function expire_get_domains(&$node) { - $domains = array(); - if ($node->nid) { - $result = db_query("SELECT gid FROM {domain_access} WHERE nid = %d", $node->nid); - while ($row = db_fetch_array($result)) { - $gid = $row['gid']; - $domains[$gid] = $gid; +function _expire_votingapi($votes) { + foreach ($votes as $vote) { + if ($vote['entity_type'] == 'comment') { + $nid = db_query("SELECT nid FROM {comments} WHERE cid = :cid", array(':cid' => $vote['entity_id']))->fetchField(); + if (is_numeric($nid)) { + $node = node_load($nid); + if ($node) { + expire_node($node); + } + } } - } - elseif ($node->mail && $node->name) { - $result = db_query("SELECT domain_id FROM {domain_editor} WHERE uid = %d", $node->uid); - while ($row = db_fetch_array($result)) { - $gid = $row['domain_id']; - $domains[$gid] = $gid; + if ($vote['entity_type'] == 'node') { + $node = node_load($vote['entity_id']); + if ($node) { + expire_node($node); + } } } - return $domains; } -function expire_normal_path_check($path) { - $original_map = arg(NULL, $path); - $parts = array_slice($original_map, 0, MENU_MAX_PARTS); - list($ancestors, $placeholders) = menu_get_ancestors($parts); - - $router_item = db_query_range('SELECT path FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(':ancestors' => $ancestors))->fetchAssoc(); - return $router_item; -} diff --git a/expire.node_reference.inc b/expire.node_reference.inc new file mode 100644 index 0000000000000000000000000000000000000000..394d32a324535e515cf1c393f2d70948137e2f86 --- /dev/null +++ b/expire.node_reference.inc @@ -0,0 +1,20 @@ +type); + $field_types = field_info_fields(); + foreach ($fields as $field) { + // Add referenced node paths and expire referenced nodes + // when the referencing node is updated. + if ($field_types[$field['field_name']]['type'] == 'node_reference') { + $node_field = isset($node->$field['field_name']) && !empty($node->$field['field_name']) ? $node->$field['field_name'] : array(); + foreach($node_field[LANGUAGE_NONE] as $reference) { + $paths['reference' . $reference['nid']] = 'node/'. $reference['nid']; + } + + $types = entity_load_multiple_by_name('node'); + } + } + return $paths; +} diff --git a/expire.votingapi.inc b/expire.votingapi.inc new file mode 100644 index 0000000000000000000000000000000000000000..bf9ba306429c5772d9bd64208780bbf6748fb2f3 --- /dev/null +++ b/expire.votingapi.inc @@ -0,0 +1,46 @@ + $vote['entity_id']))->fetchField(); + if (is_numeric($nid)) { + $node = node_load($nid); + if ($node) { + expire_node($node); + } + } + } + if ($vote['entity_type'] == 'node') { + $node = node_load($vote['entity_id']); + if ($node) { + expire_node($node); + } + } + } +} + +