#include "gb-include.h" #include "HttpServer.h" #include "Conf.h" #include "Pages.h" #include "Thesaurus.h" void trimWhite(char *beginning); // AutoBan.cpp, this should really be // a utility function somewhere bool sendPageThesaurus( TcpSocket *s, HttpRequest *r ) { SafeBuf p; char getBuf[64]; // holds extra values for GET method char formBuf[256]; // holds extra values for forms snprintf(getBuf, 64, "c=%s", r->getString("c", 0, "")); snprintf(formBuf, 256, "", //"", r->getString("c", 0, "")); g_pages.printAdminTop( &p, s, r); if (r->getLong("cancel", 0) != 0) { g_thesaurus.cancelRebuild(); p.safePrintf("

\n"); p.safePrintf( "
" "rebuild canceled" "
"); } if (r->getLong("rebuild", 0) != 0) { bool full = r->getLong("full", 0); p.safePrintf("

\n"); if (g_thesaurus.rebuild(0, full)) { p.safePrintf( "
" "error starting rebuild, check log for details" "
"); } else { p.safePrintf( "
" "rebuild started" "
"); } } if (r->getLong("rebuildaff", 0) != 0) { bool full = r->getLong("full", 0); p.safePrintf("

\n"); if (g_thesaurus.rebuildAffinity(0, full)) { p.safePrintf( "
" "error starting rebuild, check log for details" "
"); } else { p.safePrintf( "
" "rebuild started" "
"); } } if (r->getLong("distribute", 0) != 0) { char cmd[1024]; p.safePrintf("

\n"); if (g_thesaurus.m_affinityState) { p.safePrintf( "
" "cannot distribute during rebuild" "
"); } else { for ( long i = 0; i < g_hostdb.getNumHosts() ; i++ ) { Host *h = g_hostdb.getHost(i); snprintf(cmd, 512, "rcp -r " "./dict/thesaurus.* " "%s:%s/dict/ &", iptoa(h->m_ip), h->m_dir); log(LOG_INFO, "admin: %s", cmd); system( cmd ); } p.safePrintf( "
" "data distributed" "
"); } } if (r->getLong("reload", 0) != 0) { p.safePrintf("

\n"); if (r->getLong("cast", 0) != 0) { p.safePrintf( "
" "reload command broadcast" "
"); } else if (g_thesaurus.init()) { p.safePrintf( "
" "thesaurus data reloaded" "
"); } else { p.safePrintf( "
" "error reloading thesaurus data" "
"); } } long manualAddLen = 0; char *manualAdd = NULL; SafeBuf manualAddBuf; if ((manualAdd = r->getString("manualadd", &manualAddLen))) { trimWhite(manualAdd); manualAddLen = gbstrlen(manualAdd); File manualFile; manualFile.set(g_hostdb.m_dir, "dict/thesaurus-manual.txt"); if (manualFile.open(O_WRONLY | O_CREAT | O_TRUNC) && (manualFile.write(manualAdd, manualAddLen, 0) == manualAddLen)) { char newl = '\n'; // for write() if (manualAdd[manualAddLen-1] != '\n') manualFile.write(&newl, 1, manualAddLen); p.safePrintf( "
" "updated manual add file sucessfully" "
"); } else { p.safePrintf( "
" "error writing manual add file" "
"); } } else { char ff[PATH_MAX]; snprintf(ff, PATH_MAX, "%sdict/thesaurus-manual.txt", g_hostdb.m_dir); if (manualAddBuf.fillFromFile(ff)) { if (*(manualAddBuf.getBuf()-1) != '\n') manualAddBuf.pushChar('\n'); manualAdd = manualAddBuf.getBufStart(); manualAddLen = manualAddBuf.length(); } } long affinityAddLen = 0; char *affinityAdd = NULL; SafeBuf affinityAddBuf; if ((affinityAdd = r->getString("affinityadd", &affinityAddLen))) { trimWhite(affinityAdd); affinityAddLen = gbstrlen(affinityAdd); File affinityFile; affinityFile.set(g_hostdb.m_dir, "dict/thesaurus-affinity.txt"); if (affinityFile.open(O_WRONLY | O_CREAT | O_TRUNC) && (affinityFile.write(affinityAdd, affinityAddLen, 0) == affinityAddLen)) { char newl = '\n'; // for write() if (affinityAdd[affinityAddLen-1] != '\n') affinityFile.write(&newl, 1, affinityAddLen); p.safePrintf( "
" "updated affinity add file sucessfully" "
"); } else { p.safePrintf( "
" "error writing affinity add file" "
"); } } else { char ff[PATH_MAX]; snprintf(ff, PATH_MAX, "%sdict/thesaurus-affinity.txt", g_hostdb.m_dir); if (affinityAddBuf.fillFromFile(ff)) { if (*(affinityAddBuf.getBuf()-1) != '\n') affinityAddBuf.pushChar('\n'); affinityAdd = affinityAddBuf.getBufStart(); affinityAddLen = affinityAddBuf.length(); } } char *syn = r->getString("synonym"); long len = 0; if (syn) len = gbstrlen(syn); if (len) { SynonymInfo info; bool r = g_thesaurus.getAllInfo(syn, &info, len, SYNBIT_ALL); p.safePrintf("

\n"); p.safePrintf ( "" "" "" "\n", LIGHT_BLUE, DARK_BLUE, info.m_numSyns); if (r) { p.safePrintf("" "" "" "\n", syn, MAX_AFFINITY, MAX_AFFINITY); for (long i = 0; i < info.m_numSyns; i++) { // get the reverse affinity as well long aff = g_thesaurus.getAffinity( info.m_syn[i], syn, info.m_len[i], len); p.safePrintf( "" "" "" "\n"); } p.safePrintf("
" "
Synonym List (%ld)
" "
%s" "1.000/%08lX (1.000/%08lX)" "
" ""); p.safeMemcpy(info.m_syn[i], info.m_len[i]); p.safePrintf("" "" ""); if (info.m_affinity[i] >= 0) { p.safePrintf("%0.3f/%08lX ", (float)info.m_affinity[i] / MAX_AFFINITY, info.m_affinity[i]); } else { p.safePrintf("u "); } if (aff >= 0) { p.safePrintf("(%0.3f/%08lX) ", (float)aff / MAX_AFFINITY, aff); } else { p.safePrintf("(u) "); } p.safePrintf("(%ld) (%ld) (%ld) (%ld) " "(%lld) (%lld)", (long)info.m_type[i], (long)info.m_sort[i], info.m_firstId[i], info.m_lastId[i], info.m_leftSynHash[i], info.m_rightSynHash[i]); for (int j = info.m_firstId[i]; j <= info.m_lastId[i]; j++) { p.safePrintf(" (%lld)", info.m_termId[j]); } p.safePrintf( "" "
"); } else { p.safePrintf("" "" "synonym not found: %s" "" "\n", syn); } } p.safePrintf ( "

\n" ); p.safePrintf ( "" "" "" "\n", LIGHT_BLUE, DARK_BLUE); p.safePrintf ( "" "" "" "\n", getBuf, getBuf); p.safePrintf ( "" "" "" "\n", getBuf); p.safePrintf ( "" "" "" "\n", getBuf); p.safePrintf ( "" "" "" "\n", getBuf); p.safePrintf ( "" "" "" "\n", formBuf); p.safePrintf ( "" "" "\n", DARK_BLUE); p.safePrintf ( "" "" "" "\n", getBuf); p.safePrintf ( "" "" "" "\n", getBuf, getBuf); p.safePrintf ( "" "" "\n", DARK_BLUE); p.safePrintf ( "" "" "\n", formBuf); p.safePrintf ( "" "" "\n", formBuf); p.safePrintf ( "
" "
Thesaurus Controls" "
rebuild all data
" "" "rebuilds synonyms and then begins the rebuild process for " "affinity data; this should only be run on one host, as the " "data is copied when the process is finished; full rebuild " "does not use existing affinity data" "" "
" "
" "rebuild all data (full)
" "
distribute data
" "" "distributes all thesaurus data to all hosts, this is " "normally done automatically but if there was a problem " "with the copy, this lets you do it manually" "" "
" "
" "distribute data
" "
reload data
" "" "reloads the synonyms and affinity table on this host only" "" "
" "
" "" "reload data
" "
reload data (all hosts)
" "" "reloads the synonyms and affinity table on all hosts" "" "
" "
" "" "reload data (all hosts)
" "
list synonyms
" "" "enter a word here to list all synonym entries and their " "affinities" "" "
" "
\">" "" "" "%s" "
" "
Affinity Controls" "
cancel running rebuild
" "" "cancels the rebuild and throws all intermediate data away" "" "
" "
" "cancel running rebuild
" "
rebuild affinity only
" "" "begins the rebuild process for affinity data, has no " "effect if a rebuild is already in progress; full rebuild " "does not reuse existing affinity data" "" "
" "
" "rebuild affinity (full)
" "
" "
Manual File Controls" "
"); p.safePrintf( "manually added pairs
\n" "place word pairs here that should be linked " "as synonyms, one pair per line, seperated by a pipe '|' " "character, optionally followed by another pipe and a type " "designation; any badly formatted lines will be silently " "ignored
\n" "
" "
" "" "" "%s" "
" "affinity value overrides
\n" "place word/phrase pairs here that should have " "there affinity values overridden, format is " "\"word1|word2|value\", where value is a floating point, " "integer (either decimal or hex), or the word \"max\"; " "any badly formatted lines will be silently ignored; note " "that these pairs will only work if the thesaurus otherwise " "has an entry for them, so add them to the manual add file " "above if need be
\n" "
" "
" "" "" "%s" "
\n" ); p.safePrintf ( "

\n" ); p.safePrintf ( "" "" "" "\n", LIGHT_BLUE, DARK_BLUE); long long a, b, c, d, e, f, g, h, i, j, k; StateAffinity *aff = g_thesaurus.m_affinityState; if (!aff) { p.safePrintf ( "\n"); a = b = c = d = e = f = g = h = i = j = k = 0; } else { a = aff->m_oldTable->getNumSlotsUsed(); b = aff->m_oldTable->getNumSlotsUsed() - aff->m_n; c = aff->m_n; d = (gettimeofdayInMilliseconds() - aff->m_time) / 1000; if (!d || !(c / d)) { e = 0; } else { e = b / (c / d); } f = aff->m_sent; g = aff->m_recv; h = aff->m_errors; i = aff->m_old; j = aff->m_cache; k = aff->m_hitsTable.getNumSlotsUsed(); } p.safePrintf ( "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n", a, b, c, d, e, f, g, h, i, j, k); p.safePrintf ( "
" "
Affinity Builder Status" "
" "
Not running
" "
# of total pairs%lli
# of pairs remaining%lli
# of pairs processed%lli
elapsed time in seconds%lli
estimated remaining time in seconds%lli
# of requests sent%lli
# of requests received%lli
# of request errors%lli
# of old values reused%lli
# of cache hits%lli
cache size%lli
\n" ); return g_httpServer.sendDynamicPage ( s, p.getBufStart(), p.length() ); }