PHP Classes

File: gRank.php

Recommend this page to a friend!
  Classes of Cesar D. Rodas   guaranix Rank   gRank.php   Download  
File: gRank.php
Role: Class source
Content type: text/plain
Description: This is the class that will calcualte the PR
Class: guaranix Rank
Implementation of the Google page rank algorithm
Author: By
Last change: One error were fix.

On my test I defined DUMPING, but in the current version, it was changed to $this->dump;

Now it works Well.
Date: 17 years ago
Size: 4,663 bytes
 

Contents

Class file image Download
<?
/****************************************************************************************
* guaranix Rank *
* *
* This is a class that implements the Google Rank algorithm called PageRank. *
* The algorithm is too much effective for get what Page (or what are you trying *
* to rank) is more important than the others. *
* *
* The whole algorithm with explanation could be found at: *
* http://dbpubs.stanford.edu:8090/pub/1999-66 *
* *
* Becouse the algorithm is not mine (I didn't do the research about this ranking) *
* this source is free *
* *
* *
****************************************************************************************/

class gRank
{
    var
$mysql;
    var
$table;
   
    var
$dump;
    var
$sql_limit; #SQL limit
   
   
function gRank()
    {
       
$this->dump = 0.85;
       
$this->sql_limit = 1000;
    }
    function
calculate()
    {
        print
"Preparing... ";
       
$this->Prepare();
        print
"Prepared\n";
       
        print
"Calculating the Pagerank with out Danglins\n";
       
$this->CalculatePR_WitoutDanglings();
       
        print
"Calculating the Pagerank\n";
       
$this->CalculatePR();
       
$this->FinishPR();
        print
"Finish, all the result are in the table PR_FINISHED\n";
       
       
    }
   
    function
FinishPR()
    {
        global
$sql_finish;
       
        foreach(
$sql_finish as $Sql)
           
mysql_query($Sql,$this->mysql) or die(mysql_error($this->mysql));
    }
   
    function
Prepare()
    {
        global
$sql;
       
        foreach(
$sql as $Sql)
           
mysql_query($Sql,$this->mysql) or die(mysql_error($this->mysql));
       
       
#Now we will calculate formulates it of each page for its PageRank
       
for($i=0; ;$i++)
        {
           
$main = mysql_query("select id from tmp_pr where formula = '' limit ".$this->sql_limit,$this->mysql) or die(mysql_error($this->mysql));
            if (
mysql_num_rows($main) == 0) break;
           
            while (
$row = mysql_fetch_array($main))
            {
               
$Sql = "select pagerank.master, tmp_pr.nroout from pagerank inner
                    join tmp_pr on (tmp_pr.id = pagerank.master) where pagerank.slave = "
.$row['id'];
               
               
$xSql = mysql_query($Sql, $this->mysql) or die(mysql_error( $this->mysql));
               
$formula = "";
               
                while (
$row1 = mysql_fetch_array($xSql))
                {
                   
$formula .= "PR(".$row1['master'].")/".$row1['nroout']." + ";
                }
               
$formula = substr($formula,0,strlen($formula) - 2);
               
$formula = mysql_escape_string($formula);
               
mysql_free_result($xSql);
               
$Sql = "update tmp_pr set formula = '".$formula."' where id = ".$row['id'];
               
               
mysql_query($Sql,$this->mysql) or die(mysql_error($this->mysql));
            }
        }
    }
   
    function
CalculatePR_WitoutDanglings()
    {
        for (
$i=0; $i < 52; $i++)
        {
            print
"\tItineracion $i\n";
            for (
$e = 0; ; $e++)
            {
                if (
$this->InternalPRcalculator($e,false) == false)
                    break;
            }
        }
    }
   
    function
CalculatePR()
    {
        for (
$e = 0; ; $e++)
        {
            if (
$this->InternalPRcalculator($e) == false)
                break;
        }
    }
   
    function
InternalPRcalculator($Start, $dangling = true)
    {
        global
$buff;
       
$start = $Start * $this->sql_limit;
       
$where = "";
        if (!
$dangling)
        {
           
$where = "where nroout != 0";
        }
       
       
$sql = mysql_query("select id, formula from tmp_pr $where limit $start, ".$this->sql_limit,$this->mysql) or die(mysql_error($this->mysql));

        if (
mysql_num_rows($sql) == 0)
            return
false;
        while (
$row = mysql_fetch_array($sql))
        {
            eval(
'$nro = (float)'.$row['formula'].';');
           
$nro = (float)(1 - $this->dump) + $this->dump * ($nro);
           
mysql_query("update tmp_pr set pr = '$nro' where id = ".$row['id']);
           
$buff[$row['id']] = $nro;
        }
        return
true;
    }
}

/*
* This function will calcula the PR of a given page.
*/
function PR($i)
{
    global
$buff; #This is the memory buffer
       
   
if (!isset($buff[$i]))
    {
       
$sql1 = mysql_query("select pr from tmp_pr where id = $i");
       
$row = mysql_fetch_array($sql1);
       
mysql_free_result($sql1);
       
$buff[$i] = $row[0];
    }
    return
$buff[$i];
}


?>