/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.8
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


int IndexedSearch(MYSQL* mysql,char* Query)
{
char sqlQuery[MAXQUERYSIZE];
int NumOfResults=0;
MYSQL_RES result;
MYSQL_ROW row;
DWORD timems;
MYSQL_RES* tRes;

	timems=GetTickCount();

	snprintf_mysql_escaped_sql_statement(mysql, sqlQuery, MAXQUERYSIZE, "select hostname,page,title,match(title,text,hostname,page) against(\'%s\') as relevancy,match(title,text,hostname,page) against(\'%s\' in boolean mode) as wrdcount from pagelist where match(title,text,hostname,page) against(\'%s\' in boolean mode) order by wrdcount DESC,relevancy DESC;",Query,Query,Query);

	if(!mysql_query(mysql, sqlQuery))
	{
		tRes=mysql_store_result(mysql);
		if (tRes)
		{
			memcpy(&result,tRes,sizeof(MYSQL_RES));
		}
	}
	else
	{
		tRes=NULL;
		ERROR_LOG(mysql_error(mysql))
		ERROR_LOG(sqlQuery)
		printf("Error while executing query\n\n");
		return -1;
	}

	printf("\nSearch in %i ms - %i results found\n\n",(int)(GetTickCount()-timems),(int)result.row_count);

	if((row=mysql_fetch_row(&result))==NULL)
	{
		printf("Nothing Found\n");

	return 0;
	}
	else
	{
		printf("+ %s\n",row[2]);
		printf(" - Url: http://%s%s\n",row[0],row[1]);
		printf(" - Relevancy: %s\n",row[3]);
		printf(" - Word found: %s\n\n",row[4]);

		while ((row = mysql_fetch_row(&result))!=NULL)
		{
			printf("+ %s\n",row[2]);
			printf(" - Url: http://%s%s\n",row[0],row[1]);
			printf(" - Relevancy: %s\n",row[3]);
			printf(" - Word found: %s\n\n",row[4]);
			NumOfResults++;
		}
		
	}
return 1;
}


int IndexedSearchXML2Sock(MYSQL* mysql,char* Query,SOCKET sock)
{
char sqlQuery[MAXQUERYSIZE];
char *tmpStr;
int NumOfResults=0;
MYSQL_RES result;
MYSQL_ROW row;
DWORD timems;
MYSQL_RES** tRes;

	timems=GetTickCount();

	strtrim(Query, Query);

    tRes=(MYSQL_RES **)malloc(sizeof(MYSQL_RES *));
	
	if(tRes==NULL)
		MemoryCorruptedHandler("IndexedSearchXML2Sock");

	snprintf_mysql_escaped_sql_statement(mysql, sqlQuery, MAXQUERYSIZE, "select id, hostname,page,title,match(title,text,hostname,page) against(\'%s\') as relevancy,match(title,text,hostname,page) against(\'%s\' in boolean mode) as wrdcount from pagelist where match(title,text,hostname,page) against(\'%s\' in boolean mode) order by wrdcount DESC,relevancy DESC;",Query,Query,Query);

    if(my_mysql_query_and_store_results(mysql, sqlQuery,tRes,&result,BLOCKINDEX))
    {
        FREE(tRes);
		ERROR_LOG(mysql_error(mysql))
		ERROR_LOG(sqlQuery)
		printf("Error while executing query\n\n");
		return -1;
    }

	SEND(sock,"<xml>\r\n\r\n");

	SEND(sock," <stats>\r\n");

	tmpStr = malloc(100);

	snprintf(tmpStr,99,"  <results>%i</results>\r\n  <execution_time>%i</execution_time>\r\n", (int)result.row_count, (int)(GetTickCount()-timems));

	tmpStr[99] = 0;

	SEND(sock,tmpStr);

	FREE(tmpStr);

	SEND(sock," </stats>\r\n\r\n");

	while ((row = mysql_fetch_row(&result))!=NULL)
	{
		SEND(sock," <result>\r\n");

		tmpStr = malloc(10000);

		snprintf(tmpStr,10000,"  <id>%s</id>\r\n  <number>%i</number>\r\n  <url>http://%s%s</url>\r\n  <title>%s</title>\r\n  <relevancy>%s</relevancy>\r\n  <words>%s</words>\r\n",row[0],NumOfResults+1,row[1],row[2],row[3],row[4],row[5]);

		tmpStr[10000-1] = 0;

		SEND(sock,tmpStr);

		FREE(tmpStr);

		NumOfResults++;
		SEND(sock," </result>\r\n\r\n");
	}
		
	SEND(sock,"</xml>\r\n");
    FREE(tRes);

return 1;
}

