punbb/include/search_idx.php
changeset 2 a8a21e1c7afa
parent 0 f9ffdbd96607
equal deleted inserted replaced
1:8f6143115bf5 2:a8a21e1c7afa
    84 //
    84 //
    85 // Updates the search index with the contents of $post_id (and $subject)
    85 // Updates the search index with the contents of $post_id (and $subject)
    86 //
    86 //
    87 function update_search_index($mode, $post_id, $message, $subject = null)
    87 function update_search_index($mode, $post_id, $message, $subject = null)
    88 {
    88 {
    89 	global $db_type, $db;
    89 	global $db_type, $pun_db;
    90 
    90 
    91 	// Split old and new post/subject to obtain array of 'words'
    91 	// Split old and new post/subject to obtain array of 'words'
    92 	$words_message = split_words($message);
    92 	$words_message = split_words($message);
    93 	$words_subject = ($subject) ? split_words($subject) : array();
    93 	$words_subject = ($subject) ? split_words($subject) : array();
    94 
    94 
    95 	if ($mode == 'edit')
    95 	if ($mode == 'edit')
    96 	{
    96 	{
    97 		$result = $db->query('SELECT w.id, w.word, m.subject_match FROM '.$db->prefix.'search_words AS w INNER JOIN '.$db->prefix.'search_matches AS m ON w.id=m.word_id WHERE m.post_id='.$post_id, true) or error('Unable to fetch search index words', __FILE__, __LINE__, $db->error());
    97 		$result = $pun_db->query('SELECT w.id, w.word, m.subject_match FROM '.$pun_db->prefix.'search_words AS w INNER JOIN '.$pun_db->prefix.'search_matches AS m ON w.id=m.word_id WHERE m.post_id='.$post_id, true) or error('Unable to fetch search index words', __FILE__, __LINE__, $pun_db->error());
    98 
    98 
    99 		// Declare here to stop array_keys() and array_diff() from complaining if not set
    99 		// Declare here to stop array_keys() and array_diff() from complaining if not set
   100 		$cur_words['post'] = array();
   100 		$cur_words['post'] = array();
   101 		$cur_words['subject'] = array();
   101 		$cur_words['subject'] = array();
   102 
   102 
   103 		while ($row = $db->fetch_row($result))
   103 		while ($row = $pun_db->fetch_row($result))
   104 		{
   104 		{
   105 			$match_in = ($row[2]) ? 'subject' : 'post';
   105 			$match_in = ($row[2]) ? 'subject' : 'post';
   106 			$cur_words[$match_in][$row[1]] = $row[0];
   106 			$cur_words[$match_in][$row[1]] = $row[0];
   107 		}
   107 		}
   108 
   108 
   109 		$db->free_result($result);
   109 		$pun_db->free_result($result);
   110 
   110 
   111 		$words['add']['post'] = array_diff($words_message, array_keys($cur_words['post']));
   111 		$words['add']['post'] = array_diff($words_message, array_keys($cur_words['post']));
   112 		$words['add']['subject'] = array_diff($words_subject, array_keys($cur_words['subject']));
   112 		$words['add']['subject'] = array_diff($words_subject, array_keys($cur_words['subject']));
   113 		$words['del']['post'] = array_diff(array_keys($cur_words['post']), $words_message);
   113 		$words['del']['post'] = array_diff(array_keys($cur_words['post']), $words_message);
   114 		$words['del']['subject'] = array_diff(array_keys($cur_words['subject']), $words_subject);
   114 		$words['del']['subject'] = array_diff(array_keys($cur_words['subject']), $words_subject);
   118 		$words['add']['post'] = $words_message;
   118 		$words['add']['post'] = $words_message;
   119 		$words['add']['subject'] = $words_subject;
   119 		$words['add']['subject'] = $words_subject;
   120 		$words['del']['post'] = array();
   120 		$words['del']['post'] = array();
   121 		$words['del']['subject'] = array();
   121 		$words['del']['subject'] = array();
   122 	}
   122 	}
   123 
   123   
   124 	unset($words_message);
   124 	unset($words_message);
   125 	unset($words_subject);
   125 	unset($words_subject);
   126 
   126 
   127 	// Get unique words from the above arrays
   127 	// Get unique words from the above arrays
   128 	$unique_words = array_unique(array_merge($words['add']['post'], $words['add']['subject']));
   128 	$unique_words = array_unique(array_merge($words['add']['post'], $words['add']['subject']));
   129 
   129 
   130 	if (!empty($unique_words))
   130 	if (!empty($unique_words))
   131 	{
   131 	{
   132 		$result = $db->query('SELECT id, word FROM '.$db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $unique_words)).')', true) or error('Unable to fetch search index words', __FILE__, __LINE__, $db->error());
   132 		$result = $pun_db->query('SELECT id, word FROM '.$pun_db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $unique_words)).')', true) or error('Unable to fetch search index words', __FILE__, __LINE__, $pun_db->error());
   133 
   133     
   134 		$word_ids = array();
   134 		$word_ids = array();
   135 		while ($row = $db->fetch_row($result))
   135 		while ($row = $pun_db->fetch_row($result))
   136 			$word_ids[$row[1]] = $row[0];
   136 			$word_ids[$row[1]] = $row[0];
   137 
   137     
   138 		$db->free_result($result);
   138 		$pun_db->free_result($result);
   139 
   139     
   140 		$new_words = array_diff($unique_words, array_keys($word_ids));
   140 		$new_words = array_diff($unique_words, array_keys($word_ids));
   141 		unset($unique_words);
   141 		unset($unique_words);
   142 
   142     
   143 		if (!empty($new_words))
   143 		if (!empty($new_words))
   144 		{
   144 		{
   145 			switch ($db_type)
   145 			switch ($db_type)
   146 			{
   146 			{
   147 				case 'mysql':
   147 				case 'mysql':
   148 				case 'mysqli':
   148 				case 'mysqli':
   149 					$db->query('INSERT INTO '.$db->prefix.'search_words (word) VALUES'.implode(',', preg_replace('#^(.*)$#', '(\'\1\')', $new_words))) or error('Unable to insert search index words', __FILE__, __LINE__, $db->error());
   149 					$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_words (word) VALUES'.implode(',', preg_replace('#^(.*)$#', '(\'\1\')', $new_words))) or error('Unable to insert search index words', __FILE__, __LINE__, $pun_db->error());
   150 					break;
   150 					break;
   151 
   151 
   152 				default:
   152 				default:
   153 					while (list(, $word) = @each($new_words))
   153 					while (list(, $word) = @each($new_words))
   154 						$db->query('INSERT INTO '.$db->prefix.'search_words (word) VALUES(\''.$word.'\')') or error('Unable to insert search index words', __FILE__, __LINE__, $db->error());
   154 						$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_words (word) VALUES(\''.$word.'\')') or error('Unable to insert search index words', __FILE__, __LINE__, $pun_db->error());
   155 					break;
   155 					break;
   156 			}
   156 			}
   157 		}
   157 		}
   158 
   158 
   159 		unset($new_words);
   159 		unset($new_words);
   160 	}
   160 	}
   161 
   161   
   162 	// Delete matches (only if editing a post)
   162 	// Delete matches (only if editing a post)
   163 	while (list($match_in, $wordlist) = @each($words['del']))
   163 	while (list($match_in, $wordlist) = @each($words['del']))
   164 	{
   164 	{
   165 		$subject_match = ($match_in == 'subject') ? 1 : 0;
   165 		$subject_match = ($match_in == 'subject') ? 1 : 0;
   166 
   166 
   168 		{
   168 		{
   169 			$sql = '';
   169 			$sql = '';
   170 			while (list(, $word) = @each($wordlist))
   170 			while (list(, $word) = @each($wordlist))
   171 				$sql .= (($sql != '') ? ',' : '').$cur_words[$match_in][$word];
   171 				$sql .= (($sql != '') ? ',' : '').$cur_words[$match_in][$word];
   172 
   172 
   173 			$db->query('DELETE FROM '.$db->prefix.'search_matches WHERE word_id IN('.$sql.') AND post_id='.$post_id.' AND subject_match='.$subject_match) or error('Unable to delete search index word matches', __FILE__, __LINE__, $db->error());
   173 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_matches WHERE word_id IN('.$sql.') AND post_id='.$post_id.' AND subject_match='.$subject_match) or error('Unable to delete search index word matches', __FILE__, __LINE__, $pun_db->error());
   174 		}
   174 		}
   175 	}
   175 	}
   176 
   176 
   177 	// Add new matches
   177 	// Add new matches
   178 	while (list($match_in, $wordlist) = @each($words['add']))
   178 	while (list($match_in, $wordlist) = @each($words['add']))
   179 	{
   179 	{
   180 		$subject_match = ($match_in == 'subject') ? 1 : 0;
   180 		$subject_match = ($match_in == 'subject') ? 1 : 0;
   181 
   181 
   182 		if (!empty($wordlist))
   182 		if (!empty($wordlist))
   183 			$db->query('INSERT INTO '.$db->prefix.'search_matches (post_id, word_id, subject_match) SELECT '.$post_id.', id, '.$subject_match.' FROM '.$db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $wordlist)).')') or error('Unable to insert search index word matches', __FILE__, __LINE__, $db->error());
   183 			$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_matches (post_id, word_id, subject_match) SELECT '.$post_id.', id, '.$subject_match.' FROM '.$pun_db->prefix.'search_words WHERE word IN('.implode(',', preg_replace('#^(.*)$#', '\'\1\'', $wordlist)).')') or error('Unable to insert search index word matches', __FILE__, __LINE__, $pun_db->error());
   184 	}
   184 	}
   185 
   185 
   186 	unset($words);
   186 	unset($words);
   187 }
   187 }
   188 
   188 
   190 //
   190 //
   191 // Strip search index of indexed words in $post_ids
   191 // Strip search index of indexed words in $post_ids
   192 //
   192 //
   193 function strip_search_index($post_ids)
   193 function strip_search_index($post_ids)
   194 {
   194 {
   195 	global $db_type, $db;
   195 	global $db_type, $pun_db;
   196 
   196 
   197 	switch ($db_type)
   197 	switch ($db_type)
   198 	{
   198 	{
   199 		case 'mysql':
   199 		case 'mysql':
   200 		case 'mysqli':
   200 		case 'mysqli':
   201 		{
   201 		{
   202 			$result = $db->query('SELECT word_id FROM '.$db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id') or error('Unable to fetch search index word match', __FILE__, __LINE__, $db->error());
   202 			$result = $pun_db->query('SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id') or error('Unable to fetch search index word match', __FILE__, __LINE__, $pun_db->error());
   203 
   203 
   204 			if ($db->num_rows($result))
   204 			if ($pun_db->num_rows($result))
   205 			{
   205 			{
   206 				$word_ids = '';
   206 				$word_ids = '';
   207 				while ($row = $db->fetch_row($result))
   207 				while ($row = $pun_db->fetch_row($result))
   208 					$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
   208 					$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
   209 
   209 
   210 				$result = $db->query('SELECT word_id FROM '.$db->prefix.'search_matches WHERE word_id IN('.$word_ids.') GROUP BY word_id HAVING COUNT(word_id)=1') or error('Unable to fetch search index word match', __FILE__, __LINE__, $db->error());
   210 				$result = $pun_db->query('SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE word_id IN('.$word_ids.') GROUP BY word_id HAVING COUNT(word_id)=1') or error('Unable to fetch search index word match', __FILE__, __LINE__, $pun_db->error());
   211 
   211 
   212 				if ($db->num_rows($result))
   212 				if ($pun_db->num_rows($result))
   213 				{
   213 				{
   214 					$word_ids = '';
   214 					$word_ids = '';
   215 					while ($row = $db->fetch_row($result))
   215 					while ($row = $pun_db->fetch_row($result))
   216 						$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
   216 						$word_ids .= ($word_ids != '') ? ','.$row[0] : $row[0];
   217 
   217 
   218 					$db->query('DELETE FROM '.$db->prefix.'search_words WHERE id IN('.$word_ids.')') or error('Unable to delete search index word', __FILE__, __LINE__, $db->error());
   218 					$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_words WHERE id IN('.$word_ids.')') or error('Unable to delete search index word', __FILE__, __LINE__, $pun_db->error());
   219 				}
   219 				}
   220 			}
   220 			}
   221 
   221 
   222 			break;
   222 			break;
   223 		}
   223 		}
   224 
   224 
   225 		default:
   225 		default:
   226 			$db->query('DELETE FROM '.$db->prefix.'search_words WHERE id IN(SELECT word_id FROM '.$db->prefix.'search_matches WHERE word_id IN(SELECT word_id FROM '.$db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id) GROUP BY word_id HAVING COUNT(word_id)=1)') or error('Unable to delete from search index', __FILE__, __LINE__, $db->error());
   226 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_words WHERE id IN(SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE word_id IN(SELECT word_id FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.') GROUP BY word_id) GROUP BY word_id HAVING COUNT(word_id)=1)') or error('Unable to delete from search index', __FILE__, __LINE__, $pun_db->error());
   227 			break;
   227 			break;
   228 	}
   228 	}
   229 
   229 
   230 	$db->query('DELETE FROM '.$db->prefix.'search_matches WHERE post_id IN('.$post_ids.')') or error('Unable to delete search index word match', __FILE__, __LINE__, $db->error());
   230 	$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_matches WHERE post_id IN('.$post_ids.')') or error('Unable to delete search index word match', __FILE__, __LINE__, $pun_db->error());
   231 }
   231 }