Finished migrating legacy control record to new config format
[citadel.git] / citadel / modules / fulltext / serv_fulltext.c
index 681262b4e67aa4aca5c99667d61f94bbed35eb29..05ab77906e3004e374616d5e22c4b74940417c13 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This module handles fulltext indexing of the message base.
- * Copyright (c) 2005-2011 by the citadel.org team
+ * Copyright (c) 2005-2015 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
@@ -126,9 +126,9 @@ void ft_index_message(long msgnum, int op) {
                return;
        }
 
-       if (msg->cm_fields['1'] != NULL) {
+       if (!CM_IsEmpty(msg, eSuppressIdx)) {
                syslog(LOG_DEBUG, "ft_index_message() excluded msg %ld", msgnum);
-               CtdlFreeMessage(msg);
+               CM_Free(msg);
                return;
        }
 
@@ -139,12 +139,11 @@ void ft_index_message(long msgnum, int op) {
         */
        CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
        CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        msgtext = CC->redirect_buffer;
        CC->redirect_buffer = NULL;
-       syslog(LOG_DEBUG, "Wordbreaking message %ld...", msgnum);
-       if ((msgtext == NULL) || (StrLength(msgtext) == 0)) {
-               syslog(LOG_ALERT, "This message has a zero length.  Probable data corruption.");
+       if (msgtext != NULL) {
+               syslog(LOG_DEBUG, "Wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
        }
        txt = SmashStrBuf(&msgtext);
        wordbreaker(txt, &num_tokens, &tokens);
@@ -211,7 +210,7 @@ void ft_index_message(long msgnum, int op) {
  */
 void ft_index_msg(long msgnum, void *userdata) {
 
-       if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
+       if ((msgnum > CtdlGetConfigLong("MMfulltext")) && (msgnum <= ft_newhighest)) {
                ++ft_num_msgs;
                if (ft_num_msgs > ft_num_alloc) {
                        ft_num_alloc += 1024;
@@ -252,7 +251,7 @@ void do_fulltext_indexing(void) {
        /*
         * Don't do this if the site doesn't have it enabled.
         */
-       if (!config.c_enable_fulltext) {
+       if (!CtdlGetConfigInt("c_enable_fulltext")) {
                return;
        }
 
@@ -269,7 +268,10 @@ void do_fulltext_indexing(void) {
         * Check to see whether the fulltext index is up to date; if there
         * are no messages to index, don't waste any more time trying.
         */
-       if ((CitControl.MMfulltext >= CitControl.MMhighest) && (CitControl.fulltext_wordbreaker == FT_WORDBREAKER_ID)) {
+       if (
+               (CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))
+               && (CtdlGetConfigInt("MM_fulltext_wordbreaker") == FT_WORDBREAKER_ID)
+       ) {
                return;         /* nothing to do! */
        }
        
@@ -281,21 +283,20 @@ void do_fulltext_indexing(void) {
         * over.
         */
        begin_critical_section(S_CONTROL);
-       if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
+       if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
                syslog(LOG_DEBUG, "wb ver on disk = %d, code ver = %d",
-                       CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID
+                       CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
                );
                syslog(LOG_INFO, "(re)initializing full text index");
                cdb_trunc(CDB_FULLTEXT);
-               CitControl.MMfulltext = 0L;
-               put_control();
+               CtdlSetConfigLong("MMfulltext", 0);
        }
        end_critical_section(S_CONTROL);
 
        /*
         * Now go through each room and find messages to index.
         */
-       ft_newhighest = CitControl.MMhighest;
+       ft_newhighest = CtdlGetConfigLong("MMhighest");
        CtdlForEachRoom(ft_index_room, NULL);   /* load all msg pointers */
 
        if (ft_num_msgs > 0) {
@@ -354,9 +355,8 @@ void do_fulltext_indexing(void) {
        /* Save our place so we don't have to do this again */
        ft_flush_cache();
        begin_critical_section(S_CONTROL);
-       CitControl.MMfulltext = ft_newhighest;
-       CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
-       put_control();
+       CtdlSetConfigLong("MMfulltext", ft_newhighest);
+       CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
        end_critical_section(S_CONTROL);
        last_index = time(NULL);
 
@@ -415,28 +415,29 @@ void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
 
                }
                free(tokens);
-               qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
-
-               /*
-                * At this point, if a message appears num_tokens times in the
-                * list, then it contains all of the search tokens.
-                */
-               if (num_all_msgs >= num_tokens)
-                  for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
-                       if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
-
-                               ++num_ret_msgs;
-                               if (num_ret_msgs > num_ret_alloc) {
-                                       num_ret_alloc += 64;
-                                       ret_msgs = realloc(ret_msgs,
-                                               (num_ret_alloc*sizeof(long)) );
+               if (all_msgs != NULL) {
+                       qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
+
+                       /*
+                        * At this point, if a message appears num_tokens times in the
+                        * list, then it contains all of the search tokens.
+                        */
+                       if (num_all_msgs >= num_tokens)
+                               for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
+                                       if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
+                                               
+                                               ++num_ret_msgs;
+                                               if (num_ret_msgs > num_ret_alloc) {
+                                                       num_ret_alloc += 64;
+                                                       ret_msgs = realloc(ret_msgs,
+                                                                          (num_ret_alloc*sizeof(long)) );
+                                               }
+                                               ret_msgs[num_ret_msgs - 1] = all_msgs[j];
+                                               
+                                       }
                                }
-                               ret_msgs[num_ret_msgs - 1] = all_msgs[j];
-
-                       }
+                       free(all_msgs);
                }
-
-               free(all_msgs);
        }
 
        *fts_num_msgs = num_ret_msgs;
@@ -455,7 +456,7 @@ void cmd_srch(char *argbuf) {
 
        if (CtdlAccessCheck(ac_logged_in)) return;
 
-       if (!config.c_enable_fulltext) {
+       if (!CtdlGetConfigInt("c_enable_fulltext")) {
                cprintf("%d Full text index is not enabled on this server.\n",
                        ERROR + CMD_NOT_SUPPORTED);
                return;
@@ -489,7 +490,7 @@ void ft_delete_remove(char *room, long msgnum)
        if (room) return;
        
        /* Remove from fulltext index */
-       if (config.c_enable_fulltext) {
+       if (CtdlGetConfigInt("c_enable_fulltext")) {
                ft_index_message(msgnum, 0);
        }
 }
@@ -506,7 +507,7 @@ CTDL_MODULE_INIT(fulltext)
                CtdlRegisterDeleteHook(ft_delete_remove);
                CtdlRegisterSearchFuncHook(ft_search, "fulltext");
                CtdlRegisterCleanupHook(noise_word_cleanup);
-               CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
+               CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
        }
        /* return our module name for the log */
        return "fulltext";