? rewrite-d6-1.patch
Index: profilesearch.info
===================================================================
RCS file: /cvs/drupal/contributions/modules/profilesearch/profilesearch.info,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 profilesearch.info
--- profilesearch.info	18 Jun 2008 15:50:26 -0000	1.2.2.1
+++ profilesearch.info	22 Dec 2008 00:41:49 -0000
@@ -1,14 +1,5 @@
-; $Id: profilesearch.info,v 1.2.2.1 2008/06/18 15:50:26 crell Exp $
+; $Id$
 name = ProfileSearch
-description = Adds capability to search user profiles.
-; Information added by drupal.org packaging script on 2007-10-19
-version = "5.x-1.0"
-project = "profilesearch"
-datestamp = "1192795818"
-
-
-; Information added by drupal.org packaging script on 2008-02-23
-version = "5.x-1.x-dev"
-project = "profilesearch"
-datestamp = "1203768502"
-
+description = Maintains a serach index for users and user profiles.
+core = 6.x
+dependencies[] = search
Index: profilesearch.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/profilesearch/profilesearch.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 profilesearch.module
--- profilesearch.module	18 Jun 2008 15:50:26 -0000	1.1.2.1
+++ profilesearch.module	22 Dec 2008 00:41:49 -0000
@@ -1,110 +1,110 @@
 <?php
-// $Id: profilesearch.module,v 1.1.2.1 2008/06/18 15:50:26 crell Exp $
-
+// $Id$
 /**
  * @file
- * ProfileSearch lets users search the user profiles.
+ * This module maintains a serach index for users and user profiles.
+ *
+ * This module doesn't add any search functionallity by it's own. You need to
+ * use Views with a 'Search Terms' filter or a custom module that implements
+ * hook_search().
  */
 
 /**
- * Implementation of hook_menu().
+ * Implementation of hook_user().
  */
-function profilesearch_menu($may_cache) {
-  
-  if ($may_cache) {
-    $items[] = array(
-      'title' => t('Profile search results'),
-      'path' => 'profilesearch',
-      'callback' => 'profilesearch_dosearch',
-      'access' => user_access('access user profiles'),
-      'type' => MENU_CALLBACK,
-    );
+function profilesearch_user($op, &$edit, &$user) {
+  switch ($op) {
+    case 'update':
+      db_query('UPDATE {search_dataset} SET reindex = %d WHERE sid = %d AND type = "users"', time(), $user->uid);
+      return;
+
+    case 'delete':
+      search_wipe($user->uid, 'users');
+      return; 
   }
-
-  return $items;
 }
 
 /**
- * Implementation of hook_block().
+ * Implementation of hook_search().
  */
-function profilesearch_block($op = 'list', $delta = 0, $edit = array()) {
-  if ($op == 'list') {
-    $blocks = array();
-      $blocks['search'] = array('info' => t('Profile search block'));
-    return $blocks;
+function profilesearch_search($op, $keys = NULL) {
+  switch ($op) {
+    case 'reset':
+      db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'users'", time());
+      return;
+
+    case 'status':
+      $total = db_result(db_query('SELECT COUNT(*) FROM {users} WHERE status = 1'));
+      $remaining = db_result(db_query("SELECT COUNT(*) FROM {users} u LEFT JOIN {search_dataset} d ON d.type = 'users' AND d.sid = u.uid WHERE u.status = 1 AND (d.sid IS NULL OR d.reindex <> 0)"));
+      return array('remaining' => $remaining, 'total' => $total);
   }
-  elseif ($op == 'view' && $delta = 'search') {
-    return array(
-      'title' => t('Search users'),
-      'content' => drupal_get_form('profilesearch_search_form'),
-    );
+}
+
+/**
+ * Implementation of hook_update_index().
+ */
+function profilesearch_update_index() {
+  $limit = (int)variable_get('search_cron_limit', 100);
+  $result = db_query_range('SELECT u.uid FROM {users} u LEFT JOIN {search_dataset} d ON d.type = "users" AND d.sid = u.uid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, u.uid ASC', 0, $limit);
+
+  while ($user = db_fetch_object($result)) {
+    _profilesearch_index_user($user);
   }
 }
 
-function profilesearch_search_form() {
-   $form['search'] = array(
-     '#type' => 'textfield',
-     '#size' => 20,
-     '#title' => 'Search',
-   );
-   $form['submit'] = array(
-     '#type' => 'submit',
-     '#value' => 'Search',
-   );
-
-   return $form;
-}
-
-function profilesearch_search_form_submit($form_id, $form_values) {
-
-  $searchstring = str_replace(" ", "+", $form_values['search']);
-  
-  return 'profilesearch/'. $searchstring;
-}
-
-function profilesearch_dosearch($searchstring) {
-
-  $result = db_query("SELECT DISTINCT u.*, pf.title AS title, pv.value AS value
-    FROM {profile_values} pv
-      LEFT JOIN {users} u ON pv.uid = u.uid 
-      LEFT JOIN {profile_fields} pf ON pf.fid=pv.fid
-    WHERE pv.value LIKE '%s' 
-      OR u.name LIKE '%s'
-      OR u.mail LIKE '%s'
-    ORDER BY u.name", array('%'. $searchstring .'%', '%'. $searchstring .'%', '%'. $searchstring .'%'));
-  
-  $records = array();
-  while ($record = db_fetch_object($result)) {
-    if (empty($records[$record->uid])) {
-      $records[$record->uid] = $record;
+/**
+ * Internal help function to index a single user and optionally it's profile
+ * fields too.
+ *
+ * @todo
+ * Add the possibility for other modules to add to the searchable text by
+ * invoking something like 'update index' in hook_user().
+ */
+function _profilesearch_index_user($user) {
+  $user = user_load($user->uid);
+
+  $text = '<h1>'. check_plain($user->name) .'</h1>';
+
+  if (module_exists('profile')) {
+    $fields = _profilesearch_get_profile_fields();
+
+    foreach ($fields as $field) {
+      $text .= ' '. $user->$field;
     }
   }
-  
-  return theme('profilesearch_results', $records);
+
+  search_index($user->uid, 'users', $text);
 }
 
-function theme_profilesearch_results($records) {
-  
-  $header = array(t('User'), t('E-mail'), t('Field'), t('Value'));
-  
-  foreach ($records as $user) {
-    $row = array();
-    
-    $row[] = theme('username', $user);
-    $row[] = $user->mail;
-    // We could be matching on the username or email address, in 
-    // which case skip the field.
-    if (empty($user->value)) {
-      $row[] = t('n/a');
-      $row[] = t('n/a');
-    }
-    else {
-      $row[] = $user->title;
-      $row[] = $user->value;
+/**
+ * Internal helper function that returns information about all availabe
+ * profile fields.
+ *
+ * @return
+ * Returns an array of all profile fields which values should be indexed.
+ */
+function _profilesearch_get_profile_fields() {
+  static $fields;
+
+  if (!isset($fields)) {
+    // We dont select date and checkbox fields as their values are hard to
+    // index in a convenient way.
+    $results = db_query('SELECT name FROM {profile_fields} WHERE visibility BETWEEN 2 AND 4 AND type NOT IN ("checkbox", "date")');
+
+    while ($field = db_result($results)) {
+      $fields[] = $field;
     }
-    
-    $rows[] = $row;
   }
-  
-  return theme('table', $header, $rows);
+
+  return (array)$fields;
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function profilesearch_uninstall() {
+  // We can't use 'search_wipe()' here because that function requires 'sid'.
+  db_query('DELETE FROM {search_dataset} WHERE type = "users"');
+  db_query('DELETE FROM {search_index} WHERE type = "users"');
+  db_query('DELETE FROM {search_node_links} WHERE type = "users"');
 }
