? LICENSE.txt
Index: multisite_search.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multisite_search/multisite_search.info,v
retrieving revision 1.1.4.2
diff -u -p -r1.1.4.2 multisite_search.info
--- multisite_search.info	11 Jun 2008 08:55:18 -0000	1.1.4.2
+++ multisite_search.info	18 Aug 2010 12:58:41 -0000
@@ -5,4 +5,10 @@ description = "Search across all web sit
 dependencies[] = search
 package = Multisite Search
 
-core = 6.x
\ No newline at end of file
+core = 6.x
+; Information added by drupal.org packaging script on 2008-06-11
+version = "6.x-1.1"
+core = "6.x"
+project = "multisite_search"
+datestamp = "1213176011"
+
Index: multisite_search.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multisite_search/multisite_search.install,v
retrieving revision 1.1.4.3
diff -u -p -r1.1.4.3 multisite_search.install
--- multisite_search.install	13 Nov 2008 06:20:48 -0000	1.1.4.3
+++ multisite_search.install	18 Aug 2010 12:58:41 -0000
@@ -1,12 +1,7 @@
 <?php
-// $Id: multisite_search.install,v 1.1.4.3 2008/11/13 06:20:48 gloscon Exp $
+// $Id: multisite_search.install,v 1.1.4.2 2008/02/18 04:37:19 gloscon Exp $
 
 /**
- * @file
- * Drupal Multisite search installation.
- */
- 
-/**
  * Implementation of hook_install().
  */
 function multisite_search_install() {
@@ -17,59 +12,70 @@ function multisite_search_install() {
 /**
  * Implementation of hook_schema().
  */
-function multisite_search_schema() {  
+function multisite_search_schema(){  
   $schema['multisite_drupal_search_dataset'] = array(
     'fields' => array(
-      'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
-      'type' => array('type' => 'varchar', 'default' => NULL, 'length' => 16),
-      'data' => array('type' => 'text', 'not null' => TRUE, 'size' => 'big'),
-      'subdmn_id' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 255)
+      'sid'=>array('type'=>'int','unsigned'=>TRUE,'not null' => TRUE,'default'=>0),
+      'type'=>array('type'=>'varchar','default'=>NULL,'length'=>16),
+      'data'=>array('type'=>'text','not null' => TRUE,'size'=>'big'),
+      'subdmn_id'=>array('type'=>'varchar','not null' => TRUE,'length'=>255)
     ),
     'indexes' => array(
-       'sid_type' => array('sid', 'type', 'subdmn_id')
+       'sid_type' => array('sid','type','subdmn_id')
     ),
   );
+
   $schema['multisite_drupal_search_index'] = array(
     'fields' => array(
-      'word' => array('type' => 'varchar', 'not null' => TRUE, 'default' => '', 'length' => 50),
-      'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
-      'type' => array('type' => 'varchar', 'default' => NULL, 'length' => 16),
-      'subdmn_id' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 255),
-      'fromsid' => array('type' => 'int', 'unsigned' => TRUE , 'not null' => TRUE, 'default' => 0),
-      'fromtype' => array('type' => 'varchar', 'default' => NULL, 'length' => 16),
-      'fromsubdmn_id' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 255),
-      'score' => array('type' => 'float', 'default' => NULL)
+      'word'=>array('type'=>'varchar','not null' => TRUE,'default'=>'','length'=>50),
+      'sid'=>array('type'=>'int','unsigned'=>TRUE,'not null' => TRUE,'default'=>0),
+      'type'=>array('type'=>'varchar','default'=>NULL,'length'=>16),
+      'subdmn_id'=>array('type'=>'varchar','not null' => TRUE,'length'=>255),
+      'fromsid'=>array('type'=>'int','unsigned'=>TRUE,'not null' => TRUE,'default'=>0),
+      'fromtype'=>array('type'=>'varchar','default'=>NULL,'length'=>16),
+      'fromsubdmn_id'=>array('type'=>'varchar','not null' => TRUE,'length'=>255),
+      'score'=>array('type'=>'float','default'=>NULL)
     ),
     'indexes' => array(
-      'sid_type' => array('sid', 'type', 'subdmn_id'),
-      'from_sid_type' => array('fromsid', 'fromtype', 'fromsubdmn_id'),
-      'word' => array('word')
+       'sid_type' => array('sid','type','subdmn_id'),
+       'from_sid_type' => array('fromsid','fromtype','fromsubdmn_id'),
+       'word' => array('word')
     ),
   );
+
   $schema['multisite_drupal_search_total'] = array(
     'fields' => array(
-      'word' => array('type' => 'varchar', 'not null' => TRUE, 'default' => '', 'length' => 50),
-      'count' => array('type' => 'float', 'default' => NULL)
+      'word'=>array('type'=>'varchar','not null' => TRUE,'default'=>'','length'=>50),
+      'count'=>array('type'=>'float','default'=>NULL)
     ),
-    'primary key' => array('word'),
+      'primary key' => array('word'),
   );
   // store multisite information 
   $schema['multisite_drupal_search_sites'] = array(
     'fields' => array(
-      'site_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 11),
-      'table_prefix' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 225),
-      'site_url' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 225)
+      'site_id'=>array('type'=>'serial','unsigned'=>TRUE,'not null'=>TRUE,'length'=>11),
+      'table_prefix'=>array('type'=>'varchar','not null'=>TRUE,'length'=>225),
+      'site_url'=>array('type'=>'varchar','not null'=>TRUE,'length'=>225)
     ),
-      'primary key' => array('site_id'),
+      'primary key'=>array('site_id'),
   );
+
   return $schema;
 }
 
+
+function multisite_search_update_6001() {
+  $items = array();
+  $items[] = update_sql("ALTER TABLE {multisite_drupal_search_sites} ADD local_site tinyint(1) NOT NULL");
+  return $items;
+}
+
 /**
  * Implementation of hook_uninstall().
  */
 function multisite_search_uninstall() {
   drupal_uninstall_schema('multisite_search');
+
   // Drop tables.
   db_query('DROP TABLE IF EXISTS multisite_drupal_search_dataset');
   db_query('DROP TABLE IF EXISTS multisite_drupal_search_index');
Index: multisite_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multisite_search/multisite_search.module,v
retrieving revision 1.1.4.2
diff -u -p -r1.1.4.2 multisite_search.module
--- multisite_search.module	13 Nov 2008 06:20:49 -0000	1.1.4.2
+++ multisite_search.module	18 Aug 2010 12:58:42 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: multisite_search.module,v 1.1.4.2 2008/11/13 06:20:49 gloscon Exp $
+// $Id: multisite_search.module,v 1.1.4.1 2008/02/15 08:58:17 gloscon Exp $
 
 /**
  * @file
@@ -9,8 +9,7 @@
 /**
 * Implementation hook_help();
 */
-
-function multisite_search_help($path, $arg) {
+function multisite_search_help($path,$arg) {
   global $user;
   switch ($path) {
     case 'admin/modules#description':
@@ -19,20 +18,22 @@ function multisite_search_help($path, $a
   }
 }
 
-function multisite_search_theme() {
+function multisite_search_theme(){
+
   return array(
-    'multisite_search_theme_form' => array(
-      'arguments' => array('form' => NULL),
+    'multisite_search_theme_form'=>array(
+      'arguments'=>array('form'=>NULL),
     ),
-    'multisite_search_block_form' => array(
-      'arguments' => array('form' => NULL),
+    'multisite_search_block_form'=>array(
+      'arguments'=>array('form'=>NULL),
     ),
   );
 }
 
 function multisite_search_menu() {
   $items = array();
-    $items['admin/mssearch'] = array(
+	
+    $items['admin/settings/mssearch'] = array(
       'title' => 'Multisite Configuration',
       'description' => 'Manage sub site\'s info',
       'position' => 'left',
@@ -40,88 +41,159 @@ function multisite_search_menu() {
       'page callback' => 'add_multisite_for_mssearch',
       'access callback' => 'user_access',
       'access arguments' => array('administer site configuration'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+
+    );
+
+        $items['admin/settings/mssearch/general'] = array(
+      'title' => 'Multisite Configuration',
+      'description' => 'Manage sub site\'s info',
+      'position' => 'left',
+      'weight' => -10,
+      'page callback' => 'add_multisite_for_mssearch',
+      'access callback' => 'user_access',
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 0,
+    );
+    
+    $items['admin/settings/mssearch/cron'] = array(
+      'title' => 'Multisite Cron Configuration',
+      'description' => 'Manage cron execution for multisite indexing process',
+      'position' => 'left',
+      'weight' => -10,
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('multisite_search_cron_settings'),        
+      'access callback' => 'user_access',
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 1,
+    );
+
+  return $items;	
+}
+
+
+/**
+ * Form builder. Configure Cron
+ *
+ * @ingroup forms
+ * @see system_settings_form()
+ */
+
+function multisite_search_cron_settings() {
+
+    $form['multisite_search_cron_ttl'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Cron TTL'),
+		'#required' => TRUE,
+		'#description' => 'Define the limit in seconds of multisite re-indexing process, after NNN configured the multisite search will run a reindex',
+		'#default_value' => variable_get('multisite_search_cron_ttl', '3600')
     );
-  return $items;
+
+    return system_settings_form($form);
 }
 
 /**
  * Menu callback -- ask for confirmation of node deletion
  */
-function multisite_for_mssearch_delete_confirm(&$form_state, $siteid) {
+function multisite_for_mssearch_delete_confirm(&$form_state,$siteid) {
   $form['site_id'] = array('#type' => 'value', '#value' => $siteid);
-  return confirm_form($form, t('Are you sure you want to delete sub site id '. $siteid), $_GET['destination'] ? $_GET['destination'] : 'admin/mssearch', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
+
+  return confirm_form($form,t('Are you sure you want to delete sub site id '.$siteid),$_GET['destination'] ?      $_GET['destination'] : 'admin/mssearch',t('This action cannot be undone.'),t('Delete'), t('Cancel'));
 }
 
 function multisite_for_mssearch_delete_confirm_submit($form, &$form_state) {
+
   if ($form_state['values']['confirm']) {
     multisite_for_mssearch_delete_site($form_state['values']['site_id']);
   }
   $form_state['redirect'] = 'admin/mssearch';
+
 }
 
-function multisite_for_mssearch_delete_site($site_id) { 
+function multisite_for_mssearch_delete_site($site_id){ 
   // site id is given 
   // get details of this site ?? 
   // delete all data related to this site from 3 search table ?? 
   $subdmn_id = get_subdmn_id($site_id);
-  db_query("DELETE FROM multisite_drupal_search_sites WHERE site_id = %d", $site_id);
-  db_query("DELETE FROM multisite_drupal_search_dataset WHERE subdmn_id = '%s'", $subdmn_id);
-  db_query("DELETE FROM multisite_drupal_search_index WHERE subdmn_id = '%s'", $subdmn_id);
+  db_query("DELETE FROM {multisite_drupal_search_sites} WHERE site_id = %d", $site_id);
+  db_query("DELETE FROM {multisite_drupal_search_dataset} WHERE subdmn_id = '%s'",$subdmn_id);
+  db_query("DELETE FROM {multisite_drupal_search_index} WHERE subdmn_id = '%s'",$subdmn_id);
   drupal_set_message(t("Sub-site deleted successfully."));
 }
 
-function get_subdmn_id($site_id) {
-  $object =  db_fetch_object(db_query("SELECT table_prefix FROM multisite_drupal_search_sites WHERE site_id=%d", $site_id));
+function get_subdmn_id($site_id){
+
+  $object =  db_fetch_object(db_query("SELECT table_prefix FROM {multisite_drupal_search_sites} WHERE site_id=%d",$site_id));
   return $object->table_prefix;
+
 }
+function add_multisite_for_mssearch(){
 
-function add_multisite_for_mssearch() {
   if (arg(3)== 'delete') { 
-    return drupal_get_form('multisite_for_mssearch_delete_confirm', arg(2));
+       return drupal_get_form('multisite_for_mssearch_delete_confirm',arg(2));
   }
+
   $output = "";
   // show all existing sub-site 
-  $sql = db_query("SELECT * FROM multisite_drupal_search_sites ORDER BY site_id");
-  $total  = db_result(db_query("SELECT COUNT(*) FROM multisite_drupal_search_sites ORDER BY site_id"));
-  $header = array(t("Site id"), t("Table prefix"), t("Site URL"), t("Operations"));
+  
+  $sql = db_query("SELECT * FROM {multisite_drupal_search_sites} ORDER BY site_id");
+  $total  = db_result(db_query("SELECT COUNT(*) FROM {multisite_drupal_search_sites} ORDER BY site_id"));
+  
+  $header = array("Site id", "Table prefix", "Site URL","Local Site","Operations");
   $rows = array();
-  if ($total == 0) {
-    $rows[] = array('data' => array( array('data' => t('No site found'), 'colspan' => 4, 'align' => 'center')));
+    
+  if($total == 0) {
+  	$rows[] = array('data'=>array( array('data'=>'No site found', 'colspan'=>4, 'align'=>'center')));
   }
-  while ($res = db_fetch_array($sql)) {
-    $rows[] = array($res['site_id'], $res['table_prefix'], $res['site_url'], l("delete", 'admin/mssearch/'. $res['site_id'] .'/delete'));
+  while($res = db_fetch_array($sql)){
+      	$rows[] = array($res['site_id'], $res['table_prefix'], $res['site_url'],($res['local_site'])?'Yes':'No', l("delete", 'admin/mssearch/'.$res['site_id'].'/delete'));
   }
-  $output .= theme('table', $header, $rows);
-  $output .= '<h2>'. t('Add New Sub-Site') .'</h2>';
+
+  $output .= theme('table',$header,$rows);
+  $output .= "<br /><h2>Add New Sub-Site</h2>";
   $output .= drupal_get_form("add_multisite_for_mssearch_form");
-  return $output;
+
+  return $output;	
 }
 
-function add_multisite_for_mssearch_form() {
+function add_multisite_for_mssearch_form(){
   $form = array();
-  $form['table_prefix'] = array(
-    '#title' => 'Enter sub-site table prefix',
-    '#type' => 'textfield',
-    '#size' => 15,
-    '#default_value' => '',
-    '#attributes' => array('title' => t('Enter the table prefix of subsite.')),
-  );
-  $form['site_url'] = array(
-    '#title' => 'Enter sub-site URL',
-    '#type' => 'textfield',
-    '#size' => 15,
-    '#required' => TRUE,
-    '#default_value' => '',
-    '#description' => 'eg: http://subdomainname.domainname.com',
-    '#attributes' => array('title' => t('Enter complete subsite URL')),
-  );
+
+    $form['table_prefix'] = array(
+        '#title' => 'Enter sub-site table prefix',
+        '#type' => 'textfield',
+        '#size' => 15,
+        '#default_value' => '',
+        '#attributes' => array('title' => t('Enter the table prefix of subsite.')),
+    );
+    $form['site_url'] = array(
+        '#title' => 'Enter sub-site URL',
+        '#type' => 'textfield',
+        '#size' => 15,
+        '#required' => TRUE,
+        '#default_value' => '',
+        '#description' => 'eg: http://subdomainname.domainname.com',
+        '#attributes' => array('title' => t('Enter complete subsite URL')),
+    );
+
+    $form['local_site'] = array(
+        '#type' => 'radios',
+        '#title' => t('Local sub-site'),
+        '#default_value' => t('No'),
+        '#description' => 'If Yes, this site is where multisite search module is installed',
+        '#options' => array(t('No'), t('Yes')),
+    );
+
   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
+  
   return $form;
 }
 
 function add_multisite_for_mssearch_form_submit($form, &$form_state) {
-  // save subsite 
-  db_query("INSERT INTO multisite_drupal_search_sites VALUES(NULL, '%s', '%s')", $form_state['values']['table_prefix'], $form_state['values']['site_url']); 
+  // save subsite 	
+  db_query("INSERT INTO {multisite_drupal_search_sites} VALUES(NULL, '%s', '%s',%s)", $form_state['values']['table_prefix'], $form_state['values']['site_url'],$form_state['values']['local_site']);
   return;
 }
 
@@ -154,8 +226,7 @@ function multisite_search_block($op = 'l
         // Have Drupal save the string to the database.
       }
       return;
-    case 'view':
-    default:
+    case 'view': default:
       // If $op is "view", then we need to generate the block for display
       // purposes. The $delta parameter tells us which block is being requested.
       if ($op == 'view' && user_access('search content')) { 
@@ -174,14 +245,16 @@ function multisite_search_block($op = 'l
   }
 } 
 
+
 /**
  * Process a block search form submission.
  */
-function multisite_search_box_form_submit($form, &$form_state) {
+function multisite_search_box_form_submit($form,&$form_state) {	
   $form_id = $form['form_id']['#value'];
-  $form_state['redirect'] = 'search/multisite_search/'. trim($form_state['values'][$form_id .'_keys']);
+  $form_state['redirect'] = 'search/multisite_search/'. trim($form_state['values'][$form_id .'_keys']);	
 }
 
+
 /**
  * Theme the theme search form.
  */
@@ -193,10 +266,12 @@ function theme_multisite_search_theme_fo
  * Theme the block search form.
  */
 function theme_multisite_search_block_form($form) {
-  $fst = drupal_render($form['multisite_search_block_form_keys']);
-  $sec = drupal_render($form['submit']);
-  $out = '<div class="container-inline">'. $fst . $sec .'</div>';
-  return '<div>'. $out .'<div class="container-inline">'. drupal_render($form) .'</div></div>';
+
+	$fst = drupal_render($form['multisite_search_block_form_keys']);
+	$sec = drupal_render($form['submit']);
+	$out = '<div class="container-inline">'. $fst.$sec .'</div>';
+	
+  return '<div>'.$out. '<div class="container-inline">'. drupal_render($form).'</div></div>';
 }
 
 function multisite_search_forms() {
@@ -211,15 +286,17 @@ function multisite_search_forms() {
   return $forms;
 }
 
+
 /**
  * Output a search form for the search block and the theme's search box.
  */
-function multisite_search_box(&$form_state, $form_id) {
+function multisite_search_box(&$form_state,$form_id) {
+
   // Use search_keys instead of keys to avoid ID conflicts with the search block.
   $form[$form_id .'_keys'] = array(
     '#title' => t('Search Multi-site'),
     '#type' => 'textfield',
-    '#size' => 15,
+    '#size' => 15,	
     '#default_value' => '',
     '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
   );
@@ -237,57 +314,73 @@ function multisite_search_box(&$form_sta
  * search_dirty).
  */
 function multisite_search_cron() {
+
+  //Fetch last execution
+  $msearch_last_cron_execution = variable_get('multisite_search_last_cron_execution', FALSE);
+
+  //Validate if we are in the umbral to maintain the index search
+  if($msearch_last_cron_execution && time() < ( $msearch_last_cron_execution + variable_get('multisite_search_cron_ttl', '3600')) ) {
+      return false;
+  } else {
+      variable_set('multisite_search_last_cron_execution', time());
+  }
+
   // We register a shutdown function to ensure that search_total is always up
   // to date.
+
   // set this module weight to 10 
   db_query("UPDATE {system} SET weight = 10 WHERE name = 'multisite_search'");
+
   // Synchronise tables 
   // delete all from all & insert new 
-  db_query('TRUNCATE TABLE multisite_drupal_search_dataset');
-  db_query('TRUNCATE TABLE multisite_drupal_search_index');
-  db_query('TRUNCATE TABLE multisite_drupal_search_total');
+  db_query('TRUNCATE TABLE {multisite_drupal_search_dataset}');
+  db_query('TRUNCATE TABLE {multisite_drupal_search_index}');
+  db_query('TRUNCATE TABLE {multisite_drupal_search_total}');
+   
   // get all sites 
   // get prefix 
-  $res = db_query("SELECT * FROM multisite_drupal_search_sites ");
-  while ($result = db_fetch_array($res)) {
-    // get table prefix 
-    $tblpf = $result['table_prefix'];
-    // insert into search dataset 
-    $res1 = db_query("SELECT * FROM ". $tblpf ."search_dataset");
-    while ($result1 = db_fetch_array($res1)) { 
-      // insert into new multisite table 
-      db_query("INSERT INTO multisite_drupal_search_dataset (sid, type, data, subdmn_id) VALUES (%d, '%s', '%s', '%s')", $result1['sid'], $result1['type'], $result1['data'], $tblpf);
-    }
-    // insert into search index 
-    $res2 = db_query("SELECT * FROM ". $tblpf ."search_index");
-    while ($result2 = db_fetch_array($res2)) { 
-      // insert into new multisite table 
-      db_query("INSERT INTO multisite_drupal_search_index (word, sid, type, subdmn_id, fromsid, fromtype, fromsubdmn_id, score) VALUES ('%s', %d, '%s', '%s', '%s', %d, '%s', %f)", $result2['word'], $result2['sid'], $result2['type'], $tblpf, $result2['fromsid'], $result2['fromtype'], $tblpf, $result2['score']);
-    }
-    // ??????  how to proceed with this in a better way 
-    // insert into search total -- need to have done without cron job 
-    $res3 = db_query("SELECT DISTINCT (word) FROM multisite_drupal_search_index");
-    while ($result3 = db_fetch_array($res3)) { 
-      // for each word update search total
-      $word = $result3['word'];
-      $total = db_result(db_query("SELECT SUM(score) FROM multisite_drupal_search_index WHERE word = '%s'", $word));
-      // Apply Zipf's law to equalize the probability distribution
-      $total = log10(1 + 1/(max(1, $total)));
-      db_query("UPDATE multisite_drupal_search_total SET count = %f WHERE word = '%s'", $total, $word);
-      if (!db_affected_rows()) {
-        db_query("INSERT INTO multisite_drupal_search_total (word, count) VALUES ('%s', %f)", $word, $total);
-      }
-    }
-  }
+  
+  $res = db_query("SELECT * FROM {multisite_drupal_search_sites}");
+  while($result = db_fetch_array($res)){
+  	// get table prefix 
+  	$tblpf = $result['table_prefix'];
+  	// insert into search dataset 
+  	$res1 = db_query("SELECT * FROM ".$tblpf."search_dataset");
+  	while($result1 = db_fetch_array($res1)){ 
+  		// insert into new multisite table 
+  		db_query("INSERT INTO {multisite_drupal_search_dataset} (sid, type, data, subdmn_id) VALUES (%d, '%s', '%s', '%s')", $result1['sid'], $result1['type'], $result1['data'], $tblpf);
+  	}
+  	// insert into search index 
+  	$res2 = db_query("SELECT * FROM ".$tblpf."search_index");
+  	while($result2 = db_fetch_array($res2)){ 
+  		// insert into new multisite table 
+  		db_query("INSERT INTO {multisite_drupal_search_index} (word, sid, type, subdmn_id, fromsid, fromtype, fromsubdmn_id, score) VALUES ('%s', %d, '%s', '%s', '%s', %d, '%s', %f)", $result2['word'], $result2['sid'], $result2['type'], $tblpf, $result2['fromsid'], $result2['fromtype'], $tblpf, $result2['score']);
+  	}
+  	// ??????  how to proceed with this in a better way 
+  	// insert into search total -- need to have done without cron job 
+  	$res3 = db_query("SELECT DISTINCT (word) FROM {multisite_drupal_search_index}");
+  	while($result3 = db_fetch_array($res3)){ 
+  	  // for each word update search total
+  	  $word = $result3['word'];
+  	  $total = db_result(db_query("SELECT SUM(score) FROM {multisite_drupal_search_index} WHERE word = '%s'", $word));
+  	  // Apply Zipf's law to equalize the probability distribution
+  	  $total = log10(1 + 1/(max(1, $total)));
+  	  db_query("UPDATE {multisite_drupal_search_total} SET count = %f WHERE word = '%s'", $total, $word);
+              if (!db_affected_rows()) {
+                db_query("INSERT INTO {multisite_drupal_search_total} (word, count) VALUES ('%s', %f)", $word, $total);
+              }
+  	}
+  }		
 }
 
 /**
 * Implementation of hook_search().
 */
 function multisite_search_search($op = 'search', $keys = NULL) {
+	
   switch ($op) {  
     case 'name':
-      return t('web site'); // Used on search tab. 
+      return t('Multi Site'); // Used on search tab.
     case 'reset':
     return;
     case 'search': 
@@ -296,6 +389,7 @@ function multisite_search_search($op = '
       list($join1, $where1) = _db_rewrite_sql();
       $arguments1 = array();
       $conditions1 = 'n.status = 1';
+
       if ($type = search_query_extract($keys, 'type')) {
         $types = array();
         foreach (explode(',', $type) as $t) {
@@ -305,6 +399,7 @@ function multisite_search_search($op = '
         $conditions1 .= ' AND ('. implode(' OR ', $types) .')';
         $keys = search_query_insert($keys, 'type');
       }
+
       if ($category = search_query_extract($keys, 'category')) {
         $categories = array();
         foreach (explode(',', $category) as $c) {
@@ -315,6 +410,7 @@ function multisite_search_search($op = '
         $join1 .= ' INNER JOIN {term_node} tn ON n.nid = tn.nid';
         $keys = search_query_insert($keys, 'category');
       }
+
       // Build ranking expression (we try to map each parameter to a
       // uniform distribution in the range 0..1).
       $ranking = array();
@@ -323,6 +419,7 @@ function multisite_search_search($op = '
       // Used to avoid joining on node_comment_statistics twice
       $stats_join = FALSE;
       $total = 0;
+
       // 1. factor 
       if ($weight = (int)variable_get('node_rank_relevance', 5)) {
         // Average relevance values hover around 0.15
@@ -330,6 +427,7 @@ function multisite_search_search($op = '
         $arguments2[] = $weight;
         $total += $weight;
       }
+      
       // 2.factor 
       if ($weight = (int)variable_get('node_rank_recent', 5)) {
         // Exponential decay with half-life of 6 months, starting at last indexed node
@@ -340,6 +438,7 @@ function multisite_search_search($op = '
         $stats_join = TRUE;
         $total += $weight;
       }
+      
       // 3.factor 
       if (module_exists('comment') && $weight = (int)variable_get('node_rank_comments', 5)) {
         // Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
@@ -352,6 +451,7 @@ function multisite_search_search($op = '
         }
         $total += $weight;
       }
+      
       // 4.factor 
       if (module_exists('statistics') && variable_get('statistics_count_content_views', 0) &&
           $weight = (int)variable_get('node_rank_views', 5)) {
@@ -364,53 +464,133 @@ function multisite_search_search($op = '
         $total += $weight;
       }
       $select2 = (count($ranking) ? implode(' + ', $ranking) : 'i.relevance') .' AS score';
+      
       // study things before this and is this needed assumption no 
       // Do search simple no need other table 
       $find = do_multisite_drupal_search($keys, 'node');  
+
       // Load results
       $results = array();
       foreach ($find as $item) { 
-        // get site table prefix 
-        // load some other site node how ?? 
-        $tbl_prefix = $item->subdmn_id; 
-        // get this table prefix item site url 
-        $msite_sql = db_query("SELECT * FROM multisite_drupal_search_sites WHERE table_prefix='%s'", $tbl_prefix);
-        $msite_res = db_fetch_array($msite_sql);
-        $msite_url = $msite_res['site_url'];
-        global $db_prefix;
-        global $base_url;
-        global $user;
+      	// get site table prefix 
+      	// load some other site node how ?? 
+      	$tbl_prefix = $item->subdmn_id; 
+      	// get this table prefix item site url 
+      	$msite_sql = db_query("SELECT * FROM {multisite_drupal_search_sites} WHERE table_prefix='%s'", $tbl_prefix);
+      	$msite_res = db_fetch_array($msite_sql);
+      	$msite_url = $msite_res['site_url'];
+        $msite_local_site = $msite_res['local_site'];
+        $msite_table_prefix = $msite_res['table_prefix'];
+      	
+      	global $db_prefix;
+      	global $base_url;
+      	global $user;
+
         // Build the node body. 
-        $node = db_fetch_object(db_query("SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM ". $tbl_prefix ."node n INNER JOIN ". $tbl_prefix ."users u ON u.uid = n.uid INNER JOIN ". $tbl_prefix ."node_revisions r ON r.vid = n.vid WHERE n.nid=%d", $item->sid));
+        $node = db_fetch_object(db_query("SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM ".$tbl_prefix."node n INNER JOIN ".$tbl_prefix."users u ON u.uid = n.uid INNER JOIN ".$tbl_prefix."node_revisions r ON r.vid = n.vid WHERE n.nid=%d", $item->sid));
+
         // Fetch comments for snippet 
         // Fetch terms for snippet
         $extra = array(); 
         // assuming have to show only total comment 
-        $comments = db_result(db_query('SELECT comment_count FROM '. $tbl_prefix .'node_comment_statistics WHERE nid = %d', $node->nid));
+        $comments = db_result(db_query('SELECT comment_count FROM '.$tbl_prefix.'node_comment_statistics WHERE nid = %d', $node->nid));
         $extra[] = format_plural($comments, '1 comment', '@count comments');
+
         // attachment module ???
-        if (user_access('access user profiles')) { 
-          $tuname = "<a href='". $msite_url . url('user/'. $node->uid) ."'>". $node->name ."</a>";
+        if( user_access('access user profiles') ){ 
+          $tuname = "<a href='".$msite_url.url('user/'. $node->uid)."'>".$node->name."</a>";	
+        }else{
+          $tuname = $node->name;	
         }
-        else {
-          $tuname = $node->name;
+        // check access permission and do accordingly
+        if($msite_local_site){
+            $results[] = array('link' => ''.$msite_url.url('node/'. $item->sid),
+                               'type' => node_get_types('name', $node),
+                               'title' => $node->title,
+                               'user' => $tuname,
+                               'date' => $node->changed,
+                               'node' => $node,
+                               'extra' => $extra,
+                               'score' => $item->score / $total,
+                               'snippet' => search_excerpt($keys, $node->body));
+        } else {
+            $results[] = array('link' => ''.$msite_url."/".multisite_search_get_path_alias('node/'. $item->sid,'',$msite_table_prefix),
+                   'type' => node_get_types('name', $node),
+                   'title' => $node->title,
+                   'user' => $tuname,
+                   'date' => $node->changed,
+                   'node' => $node,
+                   'extra' => $extra,
+                   'score' => $item->score / $total,
+                   'snippet' => search_excerpt($keys, $node->body));
         }
-        // check access permission and do accordingly 
-        $results[] = array(
-          'link' => ''. $msite_url . url('node/'. $item->sid),
-          'type' => node_get_types('name', $node),
-          'title' => $node->title,
-          'user' => $tuname,
-          'date' => $node->changed,
-          'node' => $node,
-          'extra' => $extra,
-          'score' => $item->score / $total,
-          'snippet' => search_excerpt($keys, $node->body));
       }
       return $results;
   }
 }
 
+function multisite_search_get_path_alias($path, $path_language = '',$prefix) {
+  $result = $path;
+  if ($alias = multisite_search_lookup_path('alias', $path, $path_language,$prefix)) {
+    $result = $alias;
+  }
+  return $result;
+}
+
+function multisite_search_lookup_path($action, $path = '', $path_language = '',$prefix ='') {
+  global $language;
+  // $map is an array with language keys, holding arrays of Drupal paths to alias relations
+  static $map = array(), $no_src = array(), $count;
+
+  $path_language = $path_language ? $path_language : $language->language;
+
+  // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
+  if (!isset($count)) {
+    $count = db_result(db_query('SELECT COUNT(pid) FROM ' . $prefix . 'url_alias'));
+  }
+
+  if ($action == 'wipe') {
+    $map = array();
+    $no_src = array();
+    $count = NULL;
+  }
+  elseif ($count > 0 && $path != '') {
+    if ($action == 'alias') {
+      if (isset($map[$path_language][$path])) {
+        return $map[$path_language][$path];
+      }
+      // Get the most fitting result falling back with alias without language
+      //drupal_set_message(sprint_f("SELECT dst FROM " . $prefix . "url_alias WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $path, $path_language));
+      $alias = db_result(db_query("SELECT dst FROM " . $prefix . "url_alias WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $path, $path_language));
+      $map[$path_language][$path] = $alias;
+      return $alias;
+    }
+    // Check $no_src for this $path in case we've already determined that there
+    // isn't a path that has this alias
+    elseif ($action == 'source' && !isset($no_src[$path_language][$path])) {
+      // Look for the value $path within the cached $map
+      $src = '';
+      if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
+        // Get the most fitting result falling back with alias without language
+        if ($src = db_result(db_query("SELECT src FROM " . $prefix . "url_alias WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $path, $path_language))) {
+          $map[$path_language][$src] = $path;
+        }
+        else {
+          // We can't record anything into $map because we do not have a valid
+          // index and there is no need because we have not learned anything
+          // about any Drupal path. Thus cache to $no_src.
+          $no_src[$path_language][$path] = TRUE;
+        }
+      }
+      return $src;
+    }
+  }
+
+  return FALSE;
+}
+
+
+
 /**
  * Do a query on the full-text search index for a word or words.
  *
@@ -480,31 +660,36 @@ function multisite_search_search($op = '
  */
 function do_multisite_drupal_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
   $query = search_parse_query($keywords);
+
   if ($query[2] == '') {
     form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
   }
   if ($query === NULL || $query[0] == '' || $query[2] == '') {
     return array();
   }
+
   // First pass: select all possible matching sids, doing a simple index-based OR matching on the keywords.
   // 'matches' is used to reject those items that cannot possibly match the query.
   $conditions = $where1 .' AND ('. $query[2] .") AND i.type = '%s'";
   $arguments = array_merge($arguments1, $query[3], array($type, $query[4]));
-  $result = db_query_temporary("SELECT i.type, i.sid, i.subdmn_id, SUM(i.score * t.count) AS relevance, COUNT(*) AS matches FROM multisite_drupal_search_index i INNER JOIN multisite_drupal_search_total t ON i.word = t.word $join1 WHERE $conditions GROUP BY i.subdmn_id, i.type, i.sid HAVING COUNT(*) >= %d", $arguments, 'temp_search_sids');
+  $result = db_query_temporary("SELECT i.type, i.sid, i.subdmn_id, SUM(i.score * t.count) AS relevance, COUNT(*) AS matches FROM {multisite_drupal_search_index} i INNER JOIN {multisite_drupal_search_total} t ON i.word = t.word $join1 WHERE $conditions GROUP BY i.subdmn_id, i.type, i.sid HAVING COUNT(*) >= %d", $arguments, 'temp_search_sids');
+
   // Calculate maximum relevance, to normalize it
   $normalize = db_result(db_query('SELECT MAX(relevance) FROM temp_search_sids'));
   if (!$normalize) {
     return array();
   }
   $select2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * i.relevance)', $select2);
+
   // Second pass: only keep items that match the complicated keywords conditions (phrase search, negative keywords, ...)
   $conditions = '('. $query[0] .')';
   $arguments = array_merge($arguments2, $query[1]);
-  $result = db_query_temporary("SELECT i.type, i.sid, i.subdmn_id, $select2 FROM temp_search_sids i INNER JOIN multisite_drupal_search_dataset d ON i.sid = d.sid AND i.type = d.type AND i.subdmn_id = d.subdmn_id $join2 WHERE $conditions $sort_parameters", $arguments, 'temp_search_results');
+  $result = db_query_temporary("SELECT i.type, i.sid, i.subdmn_id, $select2 FROM temp_search_sids i INNER JOIN {multisite_drupal_search_dataset} d ON i.sid = d.sid AND i.type = d.type AND i.subdmn_id = d.subdmn_id $join2 WHERE $conditions $sort_parameters", $arguments, 'temp_search_results');
   if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) == 0) {
     return array();
   }
   $count_query = "SELECT $count";
+
   // Do actual search query
   $result = pager_query("SELECT * FROM temp_search_results", 10, 0, $count_query);
   $results = array();

