I want to call this function from functions_databuild.php to update the search index but im a little nervous about doing so. I've looked through this but i don't really understand a few of the queries.
So my question, am i safe to run this as long as the appropriate parameters are set in the function? Will all this do is update my search index?
thanks
eric
PHP Code:
// ###################### Start indexpost #######################
function build_post_index($postid, $foruminfo, $firstpost = -1, $post = false)
{
global $vbulletin;
if ($vbulletin->options['fulltextsearch'])
{
return;
}
if ($foruminfo['indexposts'])
{
global $vbulletin;
static $firstpst;
if (is_array($post))
{
if (isset($post['threadtitle']))
{
$threadinfo = array('title' => $post['threadtitle']);
}
}
else
{
$post = $vbulletin->db->query_first("
SELECT postid, post.title, pagetext, post.threadid, thread.title AS threadtitle
FROM " . TABLE_PREFIX . "post AS post
INNER JOIN " . TABLE_PREFIX . "thread AS thread USING(threadid)
WHERE postid = $postid
");
$threadinfo = array('title' => $post['threadtitle']);
}
if (isset($firstpst["$post[threadid]"]))
{
if ($firstpst["$post[threadid]"] == $postid)
{
$firstpost = 1;
}
else
{
$firstpost = 0;
}
}
if ($firstpost == -1)
{
$getfirstpost = $vbulletin->db->query_first("
SELECT MIN(postid) AS postid
FROM " . TABLE_PREFIX . "post
WHERE threadid = " . intval($post['threadid'])
);
if ($getfirstpost['postid'] == $postid)
{
$firstpost = 1;
}
else
{
$firstpost = 0;
}
}
$allwords = '';
if ($firstpost)
{
if (!is_array($threadinfo))
{
$threadinfo = $vbulletin->db->query_first("
SELECT title
FROM " . TABLE_PREFIX . "thread
WHERE threadid = $post[threadid]
");
}
$firstpst["$post[threadid]"] = $postid;
$words = fetch_postindex_text($threadinfo['title']);
$allwords .= $words;
$wordarray = explode(' ', $words);
foreach ($wordarray AS $word)
{
#$scores["$word"] += $vbulletin->options['threadtitlescore'];
$intitle["$word"] = 2;
$scores["$word"]++;
}
}
$words = fetch_postindex_text($post['title']);
$allwords .= ' ' . $words;
$wordarray = explode(' ', $words);
foreach ($wordarray AS $word)
{
#$scores["$word"] += $vbulletin->options['posttitlescore'];
if (empty($intitle["$word"]))
{
$intitle["$word"] = 1;
}
$scores["$word"]++;
}
$words = fetch_postindex_text($post['pagetext']);
$allwords .= ' ' . $words;
$wordarray = explode(' ', $words);
foreach ($wordarray AS $word)
{
$scores["$word"]++;
}
$getwordidsql = "title IN ('" . str_replace(" ", "','", $allwords) . "')";
$words = $vbulletin->db->query_read("SELECT wordid, title FROM " . TABLE_PREFIX . "word WHERE $getwordidsql");
while ($word = $vbulletin->db->fetch_array($words))
{
$word['title'] = vbstrtolower($word['title']);
$wordcache["$word[title]"] = $word['wordid'];
}
$vbulletin->db->free_result($words);
$insertsql = '';
$newwords = '';
$newtitlewords = '';
foreach ($scores AS $word => $score)
{
if (!is_index_word($word))
{
unset($scores["$word"]);
continue;
}
// prevent score going over 255 for overflow control
if ($score > 255)
{
$scores["$word"] = 255;
}
// make sure intitle score is set
$intitle["$word"] = intval($intitle["$word"]);
if ($word)
{
if (isset($wordcache["$word"]))
{ // Does this word already exist in the word table?
$insertsql .= ", (" . $vbulletin->db->escape_string($wordcache["$word"]) . ", $postid, $score, $intitle[$word])"; // yes so just add a postindex entry for this post/word
unset($scores["$word"], $intitle["$word"]);
}
else
{
$newwords .= $word . ' '; // No so add it to the word table
}
}
}
if (!empty($insertsql))
{
$insertsql = substr($insertsql, 1);
/*insert query*/
$vbulletin->db->query_write("
REPLACE INTO " . TABLE_PREFIX . "postindex
(wordid, postid, score, intitle)
VALUES
$insertsql
");
}
$newwords = trim($newwords);
if ($newwords)
{
$insertwords = "('" . str_replace(" ", "'),('", $newwords) . "')";
/*insert query*/
$vbulletin->db->query_write("INSERT IGNORE INTO " . TABLE_PREFIX . "word (title) VALUES $insertwords");
$selectwords = "title IN ('" . str_replace(" ", "','", $newwords) . "')";
$scoressql = 'CASE title';
foreach ($scores AS $word => $score)
{
$scoressql .= " WHEN '" . $vbulletin->db->escape_string($word) . "' THEN $score";
}
$scoressql .= ' ELSE 1 END';
$titlesql = 'CASE title';
foreach($intitle AS $word => $intitlescore)
{
$titlesql .= " WHEN '" . $vbulletin->db->escape_string($word) . "' THEN $intitlescore";
}
$titlesql .= ' ELSE 0 END';
/*insert query*/
$vbulletin->db->query_write("
INSERT IGNORE INTO " . TABLE_PREFIX . "postindex
(wordid, postid, score, intitle)
SELECT DISTINCT wordid, $postid, $scoressql, $titlesql
FROM " . TABLE_PREFIX . "word
WHERE $selectwords
");
}
}
}