Esempio n. 1
0
function sp_save_index_entry($postID)
{
    global $wpdb, $table_prefix;
    $table_name = $table_prefix . 'similar_posts';
    $post = $wpdb->get_row("SELECT post_content, post_title FROM {$wpdb->posts} WHERE ID = {$postID}", ARRAY_A);
    //extract its terms
    $options = get_option('similar-posts');
    $utf8 = $options['utf8'] === 'true';
    $use_stemmer = $options['use_stemmer'] === 'true';
    $content = sp_get_post_terms($post['post_content'], $utf8, $use_stemmer);
    $title = sp_get_title_terms($post['post_title'], $utf8, $use_stemmer);
    $tags = sp_get_tag_terms($postID, $utf8);
    //check to see if the field is set
    $pid = $wpdb->get_var("SELECT pID FROM {$table_name} WHERE pID={$postID} limit 1");
    //then insert if empty
    if (is_null($pid)) {
        $wpdb->query("INSERT INTO {$table_name} (pID, content, title, tags) VALUES ({$postID}, \"{$content}\", \"{$title}\", \"{$tags}\")");
    } else {
        $wpdb->query("UPDATE {$table_name} SET content=\"{$content}\", title=\"{$title}\", tags=\"{$tags}\" WHERE pID={$postID}");
    }
    return $postID;
}
function save_index_entries($utf8 = false, $use_stemmer = false, $batch = 100)
{
    global $wpdb, $table_prefix;
    // empty the index table
    $table_name = $table_prefix . 'similar_posts';
    //$start_time = ppl_microtime();
    $wpdb->query("TRUNCATE `{$table_name}`");
    $termcount = 0;
    $start = 0;
    // in batches to conserve memory
    while ($posts = $wpdb->get_results("SELECT `ID`, `post_title`, `post_content` FROM {$wpdb->posts} LIMIT {$start}, {$batch}", ARRAY_A)) {
        reset($posts);
        while (list($dummy, $post) = each($posts)) {
            $content = sp_get_post_terms($post['post_content'], $utf, $use_stemmer);
            $title = sp_get_title_terms($post['post_title'], $utf, $use_stemmer);
            $tags = sp_get_tag_terms($postID, $utf);
            $postID = $post['ID'];
            $wpdb->query("INSERT INTO `{$table_name}` (pID, content, title, tags) VALUES ({$postID}, \"{$content}\", \"{$title}\", \"{$tags}\")");
            $termcount = $termcount + 1;
        }
        $start += $batch;
        set_time_limit(30);
    }
    //echo (ppl_microtime() - $start_time);
    unset($posts);
    return $termcount;
}