
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.7
 *  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
 *
 */

#ifndef __RANK
#define __RANK

int GetHostRank(int host_id)
{
MYSQL_RES **tmpRes;
char sqlQuery[MAXQUERYSIZE];
MYSQL_RES gRes;
MYSQL_ROW row;
int rank;

    if(host_id == 0)
        return -1;

	tmpRes=(MYSQL_RES **)malloc(sizeof(MYSQL_RES *));
	
	if(tmpRes==NULL)
		MemoryCorruptedHandler("GetHostRank");
	
	sprintf(sqlQuery,"select count(distinct host_id,linkedhost_id) from rels where linkedhost_id = %d", host_id);
	if(!my_mysql_query_and_store_results(&gMysqlDB1, sqlQuery,tmpRes,&gRes,NO_BLOCK))
	{
		if((row = mysql_fetch_row(&gRes))==NULL)
		{
			if(*tmpRes)
			{
				mysql_free_result(*tmpRes);
			}

			FREE(tmpRes);

			return -1;
		}
		rank=atoi(row[0]);
		
		if(*tmpRes)
		{
			mysql_free_result(*tmpRes);
		}

		FREE(tmpRes);

		return rank;
	}
	else
	{
		FREE(tmpRes);

		return -1;
	}
}

int CalcPageRank(struct sHost host)
{
char sqlQuery[MAXQUERYSIZE];
int hostrank;
    
	hostrank=GetHostRank( host.host_id );

	if(hostrank==-1)
	{
		printf("\r\nError while calculating HostRank\r\n");
		return -1;
	}

	printf("\r\n  + HostRank: %i",hostrank);	
	printf("\r\n   - Calculating Page Rank...");
	
	/* PR(px)=HostRank+(MaxPRLev/Level) */
	sprintf(sqlQuery,"UPDATE pagelist SET rank=%i+abs(%i/level) WHERE host_id = %d",hostrank+1,MAXPRLEV,host.host_id);
	my_mysql_query(&gMysqlDB2, sqlQuery,NO_BLOCK);

	printf("OK\r\n");

return 1;
}

#endif

/*EOF*/

