| 1 |
|
diff -Naur ../drupal/modules/search/search.module ./modules/search/search.module |
| 2 |
|
--- ../drupal/modules/search/search.module 2007-07-26 15:16:48.000000000 -0400 |
| 3 |
|
+++ ./modules/search/search.module 2007-12-10 12:36:29.493866736 -0500 |
| 4 |
|
@@ -272,6 +272,8 @@ |
| 5 |
|
// When re-indexing, keep link references |
| 6 |
|
db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'". ($reindex ? " AND fromsid = 0" : ''), $sid, $type); |
| 7 |
|
} |
| 8 |
|
+ // Cached search results may no longer be valid. |
| 9 |
|
+ cache_clear_all(NULL, 'cache_search'); |
| 10 |
|
} |
| 11 |
|
|
| 12 |
|
/** |
| 13 |
|
@@ -304,6 +306,34 @@ |
| 14 |
|
foreach (module_list() as $module) { |
| 15 |
|
module_invoke($module, 'update_index'); |
| 16 |
|
} |
| 17 |
|
+ |
| 18 |
|
+ // Don't allow search result caching if any node access modules such as organic |
| 19 |
|
+ // groups have placed viewing restrictions on any nodes. Even if you have had |
| 20 |
|
+ // organic groups enabled and have since disabled it, search caching will |
| 21 |
|
+ // be prevented. If you find yourself in this position, you can restore the |
| 22 |
|
+ // original un-restricted state of node_access like this: |
| 23 |
|
+ // TRUNCATE node_access; |
| 24 |
|
+ // INSERT INTO node_access VALUES (0,0,'all',1,0,0); |
| 25 |
|
+ $r1 = db_result(db_query("SELECT count(*) FROM {node_access}")); |
| 26 |
|
+ $r2 = db_result(db_query("SELECT count(*) FROM {node_access} WHERE grant_view = 1")); |
| 27 |
|
+ if (($r1 == 1) && ($r2 == 1)) { |
| 28 |
|
+ // Recalculate the list of popular search terms |
| 29 |
|
+ $tops = array(); |
| 30 |
|
+ $result = db_query("SELECT location, count(*) AS count FROM {watchdog} WHERE type = 'search' GROUP BY location HAVING count(*) > 2"); |
| 31 |
|
+ while ($row = db_fetch_object($result)) { |
| 32 |
|
+ $tops[] = strtolower($row->location); |
| 33 |
|
+ } |
| 34 |
|
+ // Since we just potentially indexed new or updated content, clear the cache. |
| 35 |
|
+ cache_clear_all(NULL, 'cache_search'); |
| 36 |
|
+ |
| 37 |
|
+ // Set the search_cache_queries to the new top queries. |
| 38 |
|
+ cache_set('search_cache_queries', 'cache_search', serialize($tops)); |
| 39 |
|
+ } |
| 40 |
|
+ else { |
| 41 |
|
+ // No guarantee that the cache complies with node access restrictions, so |
| 42 |
|
+ // wipe it. |
| 43 |
|
+ cache_clear_all(NULL, 'cache_search'); |
| 44 |
|
+ } |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
/** |
| 48 |
|
@@ -890,6 +920,7 @@ |
| 49 |
|
* Menu callback; presents the search form and/or search results. |
| 50 |
|
*/ |
| 51 |
|
function search_view() { |
| 52 |
|
+ global $base_root, $user; |
| 53 |
|
$type = arg(1); |
| 54 |
|
|
| 55 |
|
// Search form submits with POST but redirects to GET. This way we can keep |
| 56 |
|
@@ -909,8 +940,29 @@ |
| 57 |
|
// Log the search keys: |
| 58 |
|
watchdog('search', t('%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name'))), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys)); |
| 59 |
|
|
| 60 |
|
- // Collect the search results: |
| 61 |
|
- $results = search_data($keys, $type); |
| 62 |
|
+ // Look to the cache to see if this is a popular (thus cached) query |
| 63 |
|
+ $request_uri = strtolower($base_root . request_uri()); |
| 64 |
|
+ $results = NULL; |
| 65 |
|
+ // Only cache results if the user is not an admin, is authenticated, has |
| 66 |
|
+ // exactly one role, and the query is in the popular search queries array. |
| 67 |
|
+ if ($cachable = ($user->uid > 1 && count($user->roles) === 1 && in_array('authenticated user', $user->roles))) { |
| 68 |
|
+ $queries_cache = cache_get('search_cache_queries', 'cache_search'); |
| 69 |
|
+ } |
| 70 |
|
+ if ($cachable && $queries_cache) { |
| 71 |
|
+ $search_cache_queries = unserialize($queries_cache->data); |
| 72 |
|
+ if (in_array($request_uri, $search_cache_queries)) { |
| 73 |
|
+ if ($search_cache = cache_get($request_uri, 'cache_search')) { |
| 74 |
|
+ $results = $search_cache->data; |
| 75 |
|
+ } |
| 76 |
|
+ } |
| 77 |
|
+ } |
| 78 |
|
+ if (empty($results)) { |
| 79 |
|
+ // Collect the search results: |
| 80 |
|
+ $results = search_data($keys, $type); |
| 81 |
|
+ if ($cachable) { |
| 82 |
|
+ cache_set($request_uri, 'cache_search', $results); |
| 83 |
|
+ } |
| 84 |
|
+ } |
| 85 |
|
|
| 86 |
|
if ($results) { |
| 87 |
|
$results = theme('box', t('Search results'), $results); |