[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/include/ -> ArchiveDay.class.php (source)

   1  <?php
   2  /* 
   3   * phpMyVisites : website statistics and audience measurements
   4   * Copyright (C) 2002 - 2006
   5   * http://www.phpmyvisites.net/ 
   6   * phpMyVisites is free software (license GNU/GPL)
   7   * Authors : phpMyVisites team
   8  */
   9  
  10  // $Id: ArchiveDay.class.php 236 2007-11-04 15:04:08Z matthieu_ $
  11  
  12  require_once  INCLUDE_PATH."/core/include/ArchiveCategory.class.php";
  13  require_once  INCLUDE_PATH."/core/include/ArchiveTable.class.php";
  14  
  15  /**
  16   * Class that manages Day archives
  17   * Used by Archive and used for a simple day archiving
  18   */
  19  class ArchiveDay extends Archive
  20  {
  21      /**
  22       * @var array objectName => objectValue
  23       */
  24      var $objects;
  25  
  26      /**
  27       * constructor
  28       * 
  29       * @param object $site
  30       * @param string $s_date
  31       */
  32  	function ArchiveDay($site, $s_date = '')
  33      {
  34          parent::Archive($site);
  35          
  36          /*print($s_date);
  37          print("<pre>");
  38          debug_print_backtrace();
  39          print("</pre>");
  40          */
  41          $this->setPeriodType(DB_ARCHIVES_PERIOD_DAY);
  42          
  43          if(!empty($s_date))
  44          {
  45              $this->setDate($s_date);
  46          }
  47          
  48          $this->objects['provider'] = new ArchiveTable('provider');
  49          
  50          $this->objects['treeCategory'] = new ArchiveCategory($this->site->getId());
  51          
  52          $a_archiveTable = array(
  53          'provider', 'config', 'resolution', 'vars_name', 'vars_value', 'page', 'file', 'category',
  54          'search_engine', 'keyword', 'site', 'newsletter', 'partner_name', 'partner_url'
  55          );
  56          foreach($a_archiveTable as $name)
  57          {
  58              $this->objects[$name] = new ArchiveTable($name);
  59          }
  60          
  61      }
  62      
  63      /**
  64       * set dates to the object, after reseting the datas to the previous date. 
  65       * It also sets the date2 value to current time + 10 min.
  66       * Used during the check of archives state, to know if we need to compute again.
  67       * 
  68       * @param string $s_date
  69       * 
  70       * @return void
  71       */
  72  	function setDate($s_date)
  73      {
  74          /*
  75           * reset info of the current object
  76           */
  77          $this->reset();
  78          
  79          /*
  80           * set the date
  81           */
  82          $this->date = $this->offsetDate(new Date($s_date));
  83          
  84          if($this->date->isPeriodFinished($this->periodType))
  85          {
  86              $this->state = DB_ARCHIVES_DONE;
  87          }
  88          else
  89          {
  90              $this->state = DB_ARCHIVES_TEMP;
  91          }
  92          
  93          /*
  94           * set a date2 only for compatibility with parent::initDb and updateDb
  95           */
  96          // add 15s to keep the same archive for graphics
  97          $this->date2 = new Str(time() + TIME_BEFORE_NEW_DAY_ARCHIVE + SECURE_TIME_BEFORE_NEW_ARCHIVE); 
  98          
  99          
 100          $this->setLiteralDate();
 101      }
 102      
 103      
 104      /**
 105       * resets current date info
 106       * It doesn't reset the objects values because by changing the date, 
 107       * we do not want to compute objects values again! 
 108       * 
 109       * @return void
 110       */
 111  	function reset()
 112      {        
 113          $this->date = null;
 114          $this->toRecord = null;
 115          $this->idArchives = null; 
 116          $this->state = null;
 117          $this->archiveOk = null;
 118      }    
 119      
 120      /**
 121       * verify if the current day is archived, and if the archive is not too old
 122       * 
 123       * @return bool
 124       */
 125  	function isArchived()
 126      {
 127          // case days, be careful to parrallel archiving
 128          if($this->periodType == DB_ARCHIVES_PERIOD_DAY)
 129          {            
 130              $r = query("SELECT idarchives, date2
 131                      FROM ".T_ARCHIVES ."
 132                      WHERE idsite = ".$this->site->getId() ."
 133                          AND date1 = '".$this->date->get()."'
 134                          AND done = ". DB_ARCHIVES_FAIL ."
 135                          AND period = ".$this->periodType."
 136                      ORDER BY idarchives DESC
 137                      LIMIT 1"
 138                      );
 139              if(mysql_num_rows($r) > 0)
 140              {
 141                  $l = mysql_fetch_assoc($r);
 142  
 143                  $dateToWait =  $l['date2'] + TIME_TO_WAIT_FOR_PARALLEL_ARCHIVE;
 144                  $delta = time() + TIME_BEFORE_NEW_DAY_ARCHIVE + SECURE_TIME_BEFORE_NEW_ARCHIVE - $dateToWait;
 145                  if($delta < 0 )
 146                  {
 147                      print("There is currently an archiving which is being processed (idarchives = ".$l['idarchives']."). Please wait a few minutes (". -$delta ." seconds), and try again (we have to wait for the archive to be totally computed!");
 148                      print("<hr /><a href='http://www.phpmyvisites.us/documentation/Concurrent_archiving'>Help in english</a><br /><a href='http://www.phpmyvisites.net/documentation/Archivages_concurrents'>Help in french (aide en français)</a>");
 149                      exit;
 150                  }
 151              }
 152          }
 153          //print(" OK pas de temps idsite = ".$this->site->getId() ." AND date1 = '".$this->date->get()."'<br>");
 154          $r = query("SELECT idarchives, date2" .
 155                  " FROM ".T_ARCHIVES .
 156                  " WHERE idsite = ".$this->site->getId() .
 157                  " AND date1 = '".$this->date->get()."'" .
 158                  " AND done = ". $this->state .
 159                  " AND period = ".$this->periodType.
 160                  " ORDER BY idarchives DESC
 161                    LIMIT 1"
 162                  );
 163          if(mysql_num_rows($r) > 0)
 164          {
 165              $l = mysql_fetch_assoc($r);
 166              
 167              // check if we again archive if temp archived yet
 168              if($this->state === DB_ARCHIVES_TEMP && $l['date2'] < time())
 169              {
 170                  
 171                  if(!isset($GLOBALS['header_message_tpl_out']))
 172                  {
 173                      $GLOBALS['header_message_tpl'] .= "Temporary Archive is now out of date, archive again. <br />";
 174                  }
 175                  $GLOBALS['header_message_tpl_out'] = true;
 176                  
 177                  return false;
 178              }
 179              else if($this->state === DB_ARCHIVES_TEMP)
 180              {
 181                  if(!isset($GLOBALS['header_message_tpl_temp']))
 182                  {
 183                      $GLOBALS['header_message_tpl'] .= "Temporary archive is less than ".round((TIME_BEFORE_NEW_DAY_ARCHIVE + SECURE_TIME_BEFORE_NEW_ARCHIVE) / 60)."min, it's ok !";
 184                  }
 185                  $GLOBALS['header_message_tpl_temp'] = true;
 186              }
 187              else
 188              {
 189                  //printDebug("<u>Day ".$this->date->get()." archived definitely, it's ok !</u><br>");
 190              }
 191              $this->idArchives = $l['idarchives'];
 192              return true;
 193          }
 194          else
 195          {
 196              $GLOBALS['header_message_tpl'] .= "Day not archived yet...<br>";
 197              return false;
 198          }
 199      }
 200      
 201      /**
 202       * compute all current day information, from the logs
 203       * Complex processus, and beatiful arithmetic
 204       */
 205  	function compute()
 206      {
 207          // simpler archive for current period because else it's too long to compute, 
 208          // and too heavy for server
 209          if(CURRENT_DAY_SIMPLE_ARCHIVE 
 210                  && $this->state == DB_ARCHIVES_TEMP)
 211          {
 212              
 213              $this->toRecord['simple'] = 1;
 214          }
 215          else
 216          {
 217              $this->toRecord['simple'] = 0;
 218          }
 219  
 220          if(!isset($GLOBALS['header_message_tpl_archiving']))
 221          {
 222              $GLOBALS['header_message_tpl'] .= "<strong>Archiving ".$this->date->get()."...</strong><br />";
 223          }
 224          $GLOBALS['header_message_tpl_archiving'] = true;
 225          
 226          $siteUrls = $this->site->getUrls();
 227          $siteInfo = $this->site->getInfo();
 228          
 229          /*
 230           * init
 231           */
 232           
 233          $this->initDb();
 234          
 235          /*
 236           * global info
 237           */                    
 238          $r = query("SELECT count(distinct idcookie) as nb_uniq_vis, count(*) as nb_vis, 
 239                               sum(total_pages) as nb_pag, max(total_pages) as nb_max_pag, 
 240                               sum(total_time) as sum_vis_lth,
 241                               sum(case total_pages when 1 then 1 else 0 end) as nb_vis_1pag 
 242                      FROM ".T_VISIT."
 243                      WHERE server_date = '".$this->date->get()."'
 244                          AND idsite = ".$this->site->getId()."
 245                      GROUP BY server_date
 246                   ");
 247          if(mysql_num_rows($r) == 0)
 248          {
 249              $this->toRecord['nb_vis'] = 0;
 250              $this->toRecord['nb_uniq_vis'] = 0;
 251              
 252              $this->saveDb();
 253              return true;
 254          }
 255              
 256          $r = mysql_fetch_assoc($r);
 257          //printDebug($r);    
 258          
 259          foreach($r as $key => $value)
 260          {
 261              $this->toRecord[$key] = $value;
 262          }
 263          
 264          $r = query("SELECT count(distinct l.idpage) as nb_uniq_pag " .
 265                  " FROM ".T_VISIT." as v 
 266                      LEFT JOIN ".T_LINK_VP." as l USING (idvisit)" .
 267                  " WHERE v.server_date = '".$this->date->get()."'" .
 268                  " AND v.idsite = ".$this->site->getId()." " .
 269                  " LIMIT 1");
 270          $r = mysql_fetch_assoc($r);
 271          //printDebug($r);    
 272          $this->toRecord['nb_uniq_pag'] = $r['nb_uniq_pag'];
 273  
 274          /*
 275           * returning visitors
 276           */
 277           $r = query("SELECT count(distinct idcookie) as nb_uniq_vis_returning ".$GLOBALS['test']."
 278                       FROM ".T_VISIT."
 279                       WHERE server_date = '".$this->date->get()."'
 280                       AND idsite = ".$this->site->getId()."
 281                       AND returning = 1
 282                       GROUP BY returning"
 283                      );
 284          $l = mysql_fetch_assoc($r);
 285          
 286          $this->toRecord['nb_vis_returning'] = $l['s'];
 287          $this->toRecord['nb_uniq_vis_returning'] = $l['nb_uniq_vis_returning'];
 288          $this->toRecord['nb_pag_returning'] = $l['sumpage'];
 289          $this->toRecord['nb_vis_1pag_returning'] = $l['onepage'];
 290          $this->toRecord['sum_vis_lth_returning'] = $l['sumtime'];
 291  
 292          /*
 293          printDebug("<br>returning visitor ". $this->toRecord['nb_vis_returning']);        
 294          printDebug("<br>pag  returning ". $this->toRecord['nb_pag_returning']);        
 295          printDebug("<br>vis a 1 page returning ". $this->toRecord['nb_vis_1pag_returning']);        
 296          printDebug("<br>sum vis returnoing ". $this->toRecord['sum_vis_lth_returning']);
 297          //exit;
 298          */
 299          /**
 300           * number of visits per visitor
 301           */
 302          $r = query("SELECT count(*) as s
 303                          FROM ".T_VISIT."
 304                          WHERE idsite = ".$this->site->getId()."
 305                          AND server_date = '".$this->date->get()."'
 306                          GROUP BY idcookie");
 307          $res = array();
 308          while($fx = mysql_fetch_assoc($r))
 309          {
 310              @$this->toRecord['vis_nb_vis'][(int)$fx['s']]++;
 311          }
 312          ksort($this->toRecord['vis_nb_vis']);
 313          
 314          /*
 315           * server time
 316           */
 317          $r = query("SELECT HOUR(server_time) as h " .$GLOBALS['test'].
 318                  " FROM ".T_VISIT.
 319                  " WHERE server_date = '".$this->date->get()."'" .
 320                  " AND idsite = ".$this->site->getId()." " .
 321                  " GROUP BY h");
 322          
 323          while($l = mysql_fetch_assoc($r))
 324          {
 325              //printDebug("<hr>server ".$l['h']."h<br>");
 326              $this->toRecord['vis_st'][$l['h']] = $l['s'];    
 327              $this->toRecord['pag_st'][$l['h']] = $l['sumpage'];    
 328              $this->toRecord['int_st'][$l['h']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']); 
 329          }
 330          ksort($this->toRecord['vis_st']);
 331          ksort($this->toRecord['pag_st']);        
 332          
 333          /*
 334           * local time
 335           */
 336          $r = query("SELECT HOUR(local_time) as h, sum(returning) as returning " .$GLOBALS['test'].
 337                  " FROM ".T_VISIT.
 338                  " WHERE server_date = '".$this->date->get()."'" .
 339                  " AND idsite = ".$this->site->getId()." " .
 340                  " GROUP BY h");
 341          
 342          while($l = mysql_fetch_assoc($r))
 343          {
 344              $this->toRecord['vis_lt'][$l['h']] = (int)$l['s'];    
 345              $this->toRecord['pag_lt'][$l['h']] = (int)$l['sumpage'];    
 346              $this->toRecord['int_lt'][$l['h']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 347              
 348              // returning
 349              $this->toRecord['vis_period'][$l['h']] = array(
 350                          ARRAY_INDEX_PAGES_COUNT  => $l['sumpage'],
 351                          ARRAY_INDEX_RETURNING_COUNT => $l['returning'],
 352                          ARRAY_INDEX_NEW_COUNT => $l['s'] - $l['returning']
 353                          );
 354          }
 355          for($i = 0; $i < 24; $i++)
 356          {
 357              if(!isset($this->toRecord['vis_period'][$i]))
 358              {
 359                  $this->toRecord['vis_period'][$i] = array(
 360                          ARRAY_INDEX_PAGES_COUNT  => 0,
 361                          ARRAY_INDEX_RETURNING_COUNT => 0,
 362                          ARRAY_INDEX_NEW_COUNT => 0
 363                          );
 364              }
 365          }
 366          ksort($this->toRecord['vis_lt']);        
 367          ksort($this->toRecord['pag_lt']);
 368  
 369          
 370          /*
 371           * visit length
 372           */
 373  
 374          foreach($GLOBALS['timeGap'] as $value)
 375          {
 376              $min = $value[0] * 60;
 377              $max = $value[1] * 60;
 378              $r = query("SELECT count(*) as s " .
 379                      " FROM ".T_VISIT. 
 380                      " WHERE total_time >= ".$min." " .
 381                      " AND total_time <= ".$max."" .
 382                      " AND server_date = '".$this->date->get()."'" .
 383                      " AND idsite = ".$this->site->getId()
 384                      );
 385              $l = mysql_fetch_assoc($r);
 386              $this->toRecord['vis_lth'][] = $l['s'];
 387          }
 388          
 389          /*
 390           * visit per page number
 391           */
 392  
 393          foreach($GLOBALS['pagesGap'] as $value)
 394          {
 395              $min = $value[0];
 396              $max = isset($value[1])?$value[1]:'10000';
 397              
 398              $r = query("SELECT count(*) as s " .
 399                      " FROM ".T_VISIT. 
 400                      " WHERE total_pages >= ".$min." " .
 401                      " AND total_pages <= ".$max."" .
 402                      " AND server_date = '".$this->date->get()."'" .
 403                      " AND idsite = ".$this->site->getId()." "
 404                      );
 405              $l = mysql_fetch_assoc($r);
 406              $this->toRecord['vis_nb_pag'][] = $l['s'];
 407          }
 408  
 409          if($this->toRecord['simple'] == 1)
 410          {    
 411              $this->saveDb();
 412              return true;
 413          }
 414          /*
 415           * country
 416           */
 417          $r = query("SELECT country as c " .$GLOBALS['test'].
 418                  " FROM ".T_VISIT. 
 419                  " WHERE server_date = '".$this->date->get()."'" .
 420                  " AND idsite = ".$this->site->getId()." " .
 421                  " GROUP BY c" 
 422                  );
 423          
 424  
 425          while($l = mysql_fetch_assoc($r))
 426          {
 427              $this->toRecord['vis_country'][$l['c']] = $l['s'];
 428              $this->toRecord['int_country'][$l['c']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 429          }
 430          
 431  
 432          /*
 433           * continent
 434           */
 435          $r = query("SELECT continent as c " .$GLOBALS['test'].
 436                  " FROM ".T_VISIT. 
 437                  " WHERE server_date = '".$this->date->get()."'" .
 438                  " AND idsite = ".$this->site->getId()." " .
 439                  " GROUP BY c" 
 440                  );
 441          
 442          while($l = mysql_fetch_assoc($r))
 443          {
 444              $this->toRecord['vis_continent'][$l['c']] = $l['s'];
 445              $this->toRecord['int_continent'][$l['c']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 446          }
 447          
 448          
 449          /*
 450           * provider
 451           */
 452          $r = query("SELECT hostname_ext as h " .$GLOBALS['test']. // else
 453                  " FROM ".T_VISIT. 
 454                  " WHERE server_date = '".$this->date->get()."'" .
 455                  " AND idsite = ".$this->site->getId()." " .
 456                  " GROUP BY h" );
 457                  
 458          while($l = mysql_fetch_assoc($r))
 459          {
 460              $key = $this->objects['provider']->getId($l['h']);
 461              $this->toRecord['vis_provider'][$key] = $l['s'];
 462          }
 463          
 464          
 465          /*
 466           * config (os+browser+resolution)
 467           */
 468          $r = query("SELECT CONCAT(os, ';', browser_name, ';', resolution) as c " .$GLOBALS['test'].
 469                  " FROM ".T_VISIT. 
 470                  " WHERE  server_date = '".$this->date->get()."'" .
 471                  " AND idsite = ".$this->site->getId()." " .
 472                  " GROUP BY c");
 473  
 474          while($l = mysql_fetch_assoc($r))
 475          {
 476              $key = $this->objects['config']->getId($l['c']);
 477              $this->toRecord['vis_config'][$key] = $l['s'];    
 478          }
 479          
 480          
 481          /*
 482           * os
 483           */
 484          $r = query("SELECT os as o " .$GLOBALS['test'].
 485                  " FROM ".T_VISIT. 
 486                  " WHERE server_date = '".$this->date->get()."'" .
 487                  " AND idsite = ".$this->site->getId()." " .
 488                  " GROUP BY o");
 489          
 490          while($l = mysql_fetch_assoc($r))
 491          {
 492              $this->toRecord['vis_os'][$l['o']] = $l['s'];    
 493              $this->toRecord['int_os'][$l['o']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 494          }
 495          
 496          /*
 497           * browser
 498           */
 499          $r = query("SELECT CONCAT(browser_name, ';', browser_version) as b " .$GLOBALS['test'].
 500                  " FROM ".T_VISIT. 
 501                  " WHERE server_date = '".$this->date->get()."'" .
 502                  " AND idsite = ".$this->site->getId()." " .
 503                  " GROUP BY b");
 504          while($l = mysql_fetch_assoc($r))
 505          {
 506              $this->toRecord['vis_browser'][$l['b']] = $l['s'];    
 507              $this->toRecord['int_browser'][$l['b']] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 508          }
 509          
 510          /*
 511           * browser families
 512           */
 513          $this->toRecord['vis_browser_type'] = array(
 514                      'ie'=>0,
 515                      'gecko'=>0,
 516                      'khtml'=>0,
 517                      'opera'=>0
 518                      );
 519          
 520          foreach($this->toRecord['vis_browser'] as $key => $value)
 521          {
 522              foreach($GLOBALS['browserFamilies'] as $familyName => $a_browsers)
 523              {
 524                  if(in_array(substr($key, 0, 2), $a_browsers))
 525                  {
 526                      $this->toRecord['vis_browser_type'][$familyName] += $value;                
 527                  }
 528              }
 529          }
 530      
 531          /*
 532           * resolution
 533           */
 534          $r = query("SELECT resolution as r " .$GLOBALS['test'].
 535                  " FROM ".T_VISIT. 
 536                  " WHERE server_date = '".$this->date->get()."'" .
 537                  " AND idsite = ".$this->site->getId()." " .
 538                  " GROUP BY r" 
 539                  );
 540  
 541          while($l = mysql_fetch_assoc($r))
 542          {
 543              if(strlen($l['r']) > 5)
 544              {
 545                  $key = $this->objects['resolution']->getId($l['r']);
 546                  $this->toRecord['vis_resolution'][$key] = $l['s'];    
 547                  $this->toRecord['int_resolution'][$key] = array($l['s'],  $l['sumpage'], $l['onepage'], $l['sumtime']);
 548              }
 549          }
 550          
 551          /*
 552           * plugins
 553           */
 554          $r = query("SELECT  sum(case pdf when 1 then 1 else 0 end) as pdf, 
 555                              sum(case flash when 1 then 1 else 0 end) as flash, 
 556                               sum(case java when 1 then 1 else 0 end) as java, 
 557                              sum(case javascript when 1 then 1 else 0 end) as javascript, 
 558                              sum(case director when 1 then 1 else 0 end) as director,
 559                               sum(case quicktime when 1 then 1 else 0 end) as quicktime, 
 560                              sum(case realplayer when 1 then 1 else 0 end) as realplayer,
 561                              sum(case windowsmedia when 1 then 1 else 0 end) as windowsmedia,
 562                              sum(case cookie when 1 then 1 else 0 end) as cookie
 563                   FROM ".T_VISIT."
 564                   WHERE server_date = '".$this->date->get()."'
 565                   AND idsite = ".$this->site->getId());
 566          
 567          $this->toRecord['vis_plugin'] = mysql_fetch_assoc($r);
 568          
 569          /*
 570           * vis_pag_grp
 571           */     
 572          $this->computeGroupPageInfo();
 573          
 574          /*
 575           * referer
 576           */
 577          $this->computeReferer();
 578  
 579          /*
 580           * Sort and limit huge arrays
 581           */
 582          $this->sortAndLimitToRecord();
 583          
 584          /*
 585           * final save, close your eyes and prey 
 586           */
 587           $this->saveDb();
 588           
 589           /*
 590            * delete link_vp link_vpv and path records for this day
 591            */
 592           
 593           $this->deleteOldRecords();
 594           
 595           return true;
 596      }
 597      
 598  	function deleteOldRecords()
 599      {
 600          parent::deleteOldRecords( );
 601          
 602          if($this->date->get() != getDateFromTimestamp( time() ))
 603          {
 604              if(version_compare(getMysqlVersion(), '4.0') != -1)
 605              {
 606                  $r = query("DELETE 
 607                              FROM ".T_LINK_VP.", ".T_LINK_VPV."
 608                              USING ".T_VISIT."
 609                                  LEFT JOIN ".T_LINK_VP."
 610                                  USING ( idvisit )
 611                                  LEFT JOIN ".T_LINK_VPV."
 612                                  USING ( idlink_vp )
 613                              WHERE ".T_VISIT.".server_date = '".$this->date->get()."'
 614                                   AND ".T_VISIT.".idsite = ".$this->site->getId()."
 615                           ");
 616              }
 617          
 618              // mysql < 4.0
 619              else
 620              {                
 621                  // delete link_vp records
 622                  print("Your mysql version is less than 4.0, so this process will be very long 
 623                  (a big feature is not implemented in your 3.23 version). 
 624                  <br>You should use mysql 4.0 or mysql 4.1!<br>
 625                  <br>Vous devriez utiliser mysql 4.0 ou supérieure pour plus de performances 
 626                  (une fonctionnalité très importante est manquante à votre version 3.23<br>");
 627                  
 628                  // select all link_vp id
 629                  $r = query("SELECT idlink_vp
 630                              FROM ".T_VISIT."
 631                                  LEFT JOIN ".T_LINK_VP."
 632                                  USING ( idvisit )
 633                              WHERE ".T_VISIT.".server_date = '".$this->date->get()."'
 634                                   AND ".T_VISIT.".idsite = ".$this->site->getId()."
 635                                   ");
 636                  
 637                  while($l = mysql_fetch_row($r))
 638                  {
 639                      if(!empty($l[0]))
 640                          $r2 = query("DELETE FROM ".T_LINK_VP."
 641                                  WHERE idlink_vp = ".$l[0]);
 642                  }
 643                  
 644              }
 645              
 646              $r3 = query("OPTIMIZE TABLE ".T_LINK_VP.", ".T_LINK_VPV);
 647          }                    
 648      }
 649      
 650      
 651      /**
 652       * Computes all info relative to pages, groups, and files
 653       * Arrays computed contain sum by group (and subgroups), for pages and files
 654       */
 655  	function computeGroupPageInfo()
 656      {
 657          /*
 658           * pages and hierarchy
 659           */
 660          // removed in 2.2
 661           $r = query("SELECT sum(total_time_page_ref) as s, idpage_ref" .
 662                  " FROM ".T_VISIT." as v LEFT JOIN ".T_LINK_VP." as l USING (idvisit) " .
 663                  " WHERE v.server_date = '".$this->date->get()."'" .
 664                  " AND v.idsite = ".$this->site->getId()."
 665                  GROUP BY idpage_ref"
 666          );
 667          while($l = mysql_fetch_assoc($r))
 668          {
 669              if($l['idpage_ref'] != 0)
 670              {
 671                  $vis_pag_grp[$l['idpage_ref']] = array(
 672                          ARRAY_INDEX_TIME_TOTAL => $l['s']
 673                      );
 674              }    
 675          }
 676          
 677          $r = query("SELECT count(*) as s, p.name as name, p.idpage as idpage,  " .
 678                  " COALESCE(c.idcategory,0) as idcategory, COALESCE(c.level, 1) as level, " .
 679                  " COALESCE(c.idparent,0) as idparent, COALESCE(c.name,0) as namecategory " .
 680                  " FROM (((".T_VISIT." as v LEFT JOIN ".T_LINK_VP." as l USING (idvisit)) " .
 681                  "    LEFT JOIN ".T_PAGE." as p USING (idpage))" .
 682                  "        LEFT JOIN ".T_CATEGORY." as c USING (idcategory))" .
 683                  " WHERE v.server_date = '".$this->date->get()."'" .
 684                  " AND v.idsite = ".$this->site->getId()." " .
 685                  " GROUP BY p.idpage");
 686  
 687          while($l = mysql_fetch_assoc($r))
 688          {
 689              // pages names beginning by PREFIX_FILES are files, else there are normal pages
 690  //            if(substr($l['name'], 0, 5) === PREFIX_FILES)
 691              if(isPrefixTag ($l['name']))
 692              {
 693                  $type = ARRAY_TYPE_FILE;
 694  //                $l['name'] = substr($l['name'], 5);
 695  //                $l['name'] = deletePrefixTag ($l['name']);
 696              }
 697              else
 698              {
 699                  $type = ARRAY_TYPE_PAGE;
 700              }
 701              
 702              if(!isset($vis_pag_grp[$l['idpage']]))
 703              {
 704                  $vis_pag_grp[$l['idpage']] = array(
 705                          ARRAY_INDEX_TIME_TOTAL => DEFAULT_TIME_PAGE
 706                      );
 707  
 708              }
 709              $vis_pag_grp[$l['idpage']] = 
 710                      array(
 711                          ARRAY_INDEX_COUNT => $l['s'],
 712                          ARRAY_INDEX_PAGE_NAME => databaseEscape($l['name']),
 713                          ARRAY_INDEX_IDPAGE => $l['idpage'],
 714                          ARRAY_INDEX_IDCATEGORY => $l['idcategory'],
 715                          ARRAY_INDEX_LEVEL => $l['level'],
 716                          ARRAY_INDEX_IDPARENT => $l['idparent'],
 717                          ARRAY_INDEX_TYPE => $type,
 718                          )
 719                      + $vis_pag_grp[$l['idpage']]
 720                  ;
 721          
 722          }
 723          
 724          //exit;
 725          /*
 726           * variables and their values
 727           */
 728          $r = query("SELECT count(*) as s, l.idpage as idpage, vv.name as vars_name, 
 729                              IF( varchar_value IS NULL, int_value,varchar_value) as vars_value
 730                   FROM (((".T_VISIT." as v INNER JOIN ".T_LINK_VP." as l USING (idvisit)) 
 731                      INNER JOIN ".T_LINK_VPV." as ll USING (idlink_vp))
 732                          INNER JOIN ".T_VARS." as vv USING (idvars))
 733                   WHERE v.server_date = '".$this->date->get()."'
 734                   AND v.idsite = ".$this->site->getId()." 
 735                   GROUP BY l.idpage, ll.idvars"
 736                   );
 737  
 738          while($l = mysql_fetch_assoc($r))
 739          {
 740              $vis_pag_grp[$l['idpage']][ARRAY_INDEX_VARS][$l['vars_name']][] = array( 
 741                                      ARRAY_INDEX_VAR_COUNT => $l['s'],
 742                                      ARRAY_INDEX_VAR_VALUE => $l['vars_value']
 743                                      );
 744          }
 745          
 746          /*
 747           * entry pages
 748           */
 749          $r = query("SELECT count(*) as s, entry_idpage as c, sum(case total_pages when 1 then 1 else 0 end) as t
 750                  FROM ".T_VISIT."
 751                  WHERE server_date = '".$this->date->get()."'
 752                  AND idsite = ".$this->site->getId()." 
 753                  GROUP BY c");
 754  
 755          while($l = mysql_fetch_assoc($r))
 756          {
 757              
 758              // pages that are only but always "one visit page"
 759              if($l['t'] > 0)
 760              {
 761                  $vis_pag_grp[$l['c']][ARRAY_INDEX_PAGES_VISIT_ONEPAGE] = $l['t'];
 762              }    
 763              
 764              $vis_pag_grp[$l['c']][ARRAY_INDEX_ENTRYPAGE] = $l['s'];    
 765          }
 766          
 767          
 768          /*
 769           * exit pages
 770           */
 771          $r = query("SELECT count(*) as s, exit_idpage as c
 772                  FROM ".T_VISIT."
 773                  WHERE server_date = '".$this->date->get()."'
 774                  AND idsite = ".$this->site->getId()." 
 775                  GROUP BY c");
 776  
 777          while($l = mysql_fetch_assoc($r))
 778          {
 779              $vis_pag_grp[$l['c']][ARRAY_INDEX_EXITPAGE] = $l['s'];    
 780          }
 781               
 782           
 783          $a_AllpageInfo = $vis_pag_grp;
 784          
 785          // sort array ording idLebel ASC
 786          uasort($a_AllpageInfo, "sortingAllPageInfo");
 787          
 788          
 789          $res=array();
 790          $res2=array();
 791          
 792          foreach($a_AllpageInfo as $key => $a_pageInfo)
 793          {
 794              if(!isset($a_pageInfo[ARRAY_INDEX_TYPE]))
 795              {
 796                  printDebug('<b>There was a problem during archiving...</b>');
 797                  //var_dump($a_pageInfo);
 798                  //var_dump($key);
 799                  //printDebug($a_pageInfo);
 800                  continue;
 801              }
 802              // pages
 803              if(!isset($a_pageInfo[ARRAY_INDEX_TYPE]) 
 804                  || $a_pageInfo[ARRAY_INDEX_TYPE] === ARRAY_TYPE_PAGE)
 805              {
 806                  $key = 'p'.$key;
 807              }
 808              // files
 809              else if($a_pageInfo[ARRAY_INDEX_TYPE] === ARRAY_TYPE_FILE)
 810              {
 811                  $key = 'f'.$key;
 812              }
 813              else
 814              {
 815                  echo "prob";exit;
 816              }
 817              
 818              $a = array($key => $a_pageInfo);
 819              
 820              if(isset($a_pageInfo[ARRAY_INDEX_IDCATEGORY]) && $a_pageInfo[ARRAY_INDEX_IDCATEGORY] != 0)
 821              {
 822                  $res2 = $this->objects['treeCategory']->getTreeCompleted($a_pageInfo[ARRAY_INDEX_IDCATEGORY], $a);
 823              }
 824              else
 825              {
 826                  $res2 = $a;
 827              }
 828              
 829              // merge recursively this page tree with global tree
 830              $res = array_merge_recursive($res, $res2);
 831          }    
 832          if(DEBUG)
 833          {
 834              $t = strlen(serialize($res))/1000;
 835          }
 836          
 837          /*
 838           * convert page & category id from visit base to archive base
 839           * sum for groups, subgroups, etc. of values of
 840           * - hits
 841           * - entry point
 842           * - exit point
 843           * - length
 844           * - hits for each group varname-varvalue
 845           * - [...]
 846           */ 
 847          $this->walkAndSumArray($res);
 848          if(DEBUG)
 849          {
 850              $t = strlen(serialize($res))/1000;
 851          }
 852          $this->toRecord['vis_pag_grp'] = $res;
 853      }
 854      
 855      /**
 856       * Recursively go through the array of pages/groups/files and sum all values, arrays
 857       * 
 858       * @param array $array passed by reference
 859       * 
 860       * @return array array('file' => array_values, 'page' => array_values)
 861       */
 862  	function walkAndSumArray(&$array)
 863      {
 864          $t = sizeof($array);
 865          for($i = 0; $i <$t; $i++)
 866          {
 867              $a_value =& $array[key($array)];
 868              
 869              if(is_array($a_value) && sizeof($a_value) > 0)
 870              {
 871                  
 872                  // if array is a category recursively calls the function that sums all values
 873                  if(substr(key($array), 0, 1) === 'c')
 874                  {
 875                      $to_sum[] = $this->walkAndSumArray($a_value);
 876                      
 877                      // idcategory base logs To idcategory base archives
 878                      $a_value[ARRAY_INDEX_IDCATEGORY] = 
 879                          $this->objects['category']->getId(
 880                              $this->objects['treeCategory']->getName(substr(key($array), 1))
 881                                  );
 882                      
 883                  }
 884                  // else it's a page or a file and we have to sum all these values and return them
 885                  else
 886                  {
 887                      if(!isset($a_value[ARRAY_INDEX_TYPE]) 
 888                          || $a_value[ARRAY_INDEX_TYPE] === ARRAY_TYPE_PAGE)
 889                      {
 890                          $type = 'page';
 891                      }
 892                      else if($a_value[ARRAY_INDEX_TYPE] === ARRAY_TYPE_FILE)
 893                      {
 894                          $type = 'file';
 895                      }
 896                      // this should not happen
 897                      else
 898                      {
 899                          print("NO TYPE ! Not normal; report it to phpmyvisites forums = ");
 900                          var_dump($a_value);
 901                          //printDebug($a_value);
 902                          $type = 'page';
 903                          //exit();
 904                      }
 905                      
 906                      
 907                      // why this could not be set?
 908                      // for files dude!
 909                      if(isset($a_value[ARRAY_INDEX_PAGE_NAME]))
 910                      {
 911                          // idpage base 1 (logs) to idpage base 2 (archive)
 912                          $a_value[ARRAY_INDEX_IDPAGE] = 
 913                                  $this->objects[$type]->getId(@$a_value[ARRAY_INDEX_PAGE_NAME]);
 914                          unset($a_value[ARRAY_INDEX_PAGE_NAME]);
 915                      }
 916                      
 917                      //var_dump($a_value);
 918                      
 919                      $a_value_to_save = array();
 920                      
 921                      //modular variables (see array in javascript code for use)
 922                      if(isset($a_value[ARRAY_INDEX_VARS]) && is_array($a_value[ARRAY_INDEX_VARS]))
 923                      {
 924                          foreach($a_value[ARRAY_INDEX_VARS] as $var_name => $a_vars_value)
 925                          {
 926                              $a_values_to_save = array();
 927                              
 928                              // foreach variable, for each different value
 929                              foreach($a_vars_value as $a_values)
 930                              {
 931                                  // base1 (logs) to base2 (archives) for variables values
 932                                  $idValue = $this->objects['vars_value']->getId($a_values[ARRAY_INDEX_VAR_VALUE]);
 933                                  $a_values_to_save[$idValue] = array(
 934                                      ARRAY_INDEX_VAR_COUNT => $a_values[ARRAY_INDEX_VAR_COUNT],
 935                                      ARRAY_INDEX_VAR_VALUE => $idValue
 936                                      );
 937                              }
 938                              // vars_name base 1 (logs) to id vars_name base 2 (archive)
 939                              $a_value_to_save[$this->objects['vars_name']->getId($var_name)] = 
 940                                      $a_values_to_save;
 941                          }
 942                      }
 943                      $a_value[ARRAY_INDEX_VARS] = $a_value_to_save;
 944      
 945                      // add to the array to sum for the 'pmv_sum'
 946                      $to_sum[][$type] = $a_value;
 947                  }
 948              }
 949              next($array);
 950          }
 951  
 952          // init returned array
 953          $res['page'] = $res['file'] = array(
 954                  ARRAY_INDEX_COUNT => 0,
 955                  ARRAY_INDEX_TIME_TOTAL => 0,
 956                  ARRAY_INDEX_ENTRYPAGE => 0,
 957                  ARRAY_INDEX_EXITPAGE => 0,
 958                  ARRAY_INDEX_PAGES_VISIT_ONEPAGE => 0,
 959                  ARRAY_INDEX_VARS => array()
 960                  );
 961          
 962          //printDebug("dernier noeud, on somme<br>");
 963          
 964          // sums all values in $to_sum
 965          foreach($to_sum as $a_info)
 966          {
 967              foreach($a_info as $typePage => $a_page_info)
 968              {
 969                  $res[$typePage] = $this->sumArray($res[$typePage], $a_page_info);
 970              }
 971          }
 972   
 973          // pages sum
 974          $array['p_pmv_sum'] = $res['page'];
 975          
 976          // files sum
 977          $array['f_pmv_sum'] = $res['file'];
 978   
 979          $toReturn = array(
 980                      'page' => $res['page'],
 981                      'file' => $res['file'], 
 982                      );
 983                  
 984          //var_dump($toReturn);    
 985          return $toReturn;
 986      }
 987      
 988      /**
 989       * Computes the arrays sum, considering the very special of arrays to sum
 990       * 
 991       *  @param array $return array1 containing the previous sum
 992       *  @param array $a array2 to sum to array1
 993       *  
 994       *  @return array two arrays sumed
 995       */
 996  	function sumArray($return, $a)
 997      {
 998          $a_vars_res = array();
 999          
1000          // sum variables values, in the case when it's not a file (no variables for a file)
1001          if(isset($a[ARRAY_INDEX_VARS]) && is_array($a[ARRAY_INDEX_VARS]))
1002          {
1003              // compute vars_sum
1004              foreach($a[ARRAY_INDEX_VARS] as $var_name => $a_vars_value)
1005              {
1006                  //printDebug("VAR2HERE : ");
1007                  //printDebug($a_vars_value);
1008                  //printDebug(" + ");
1009                  //printDebug($return[ARRAY_INDEX_VARS]);
1010                  //printDebug("<br>");
1011                  
1012                  foreach($a_vars_value as $key => $a_vars_values)
1013                  {
1014                      //print($a[ARRAY_INDEX_IDCATEGORY]. ", ".$key. ":: ");
1015                      @$return[ARRAY_INDEX_VARS][$var_name][$key][ARRAY_INDEX_VAR_VALUE] = 
1016                                  $a_vars_values[ARRAY_INDEX_VAR_VALUE];
1017  
1018                      @$return[ARRAY_INDEX_VARS][$var_name][$key][ARRAY_INDEX_VAR_COUNT] = 
1019                                  $a_vars_values[ARRAY_INDEX_VAR_COUNT] 
1020                                  + $return[ARRAY_INDEX_VARS][$var_name][$key][ARRAY_INDEX_VAR_COUNT];
1021                  }
1022              }
1023              //printDebug("AFTERSUM");
1024              //printDebug($return);
1025          }
1026  
1027          return array( 
1028                  ARRAY_INDEX_COUNT => (int)($return[ARRAY_INDEX_COUNT] +  $a[ARRAY_INDEX_COUNT]),
1029                  
1030                  ARRAY_INDEX_IDCATEGORY => @$a[ARRAY_INDEX_IDCATEGORY],
1031                  
1032                  // ARRAY_INDEX_LEVEL => $a[ARRAY_INDEX_LEVEL],
1033                  // ARRAY_INDEX_IDPARENT => $a[ARRAY_INDEX_IDPARENT],
1034                  
1035                  ARRAY_INDEX_TIME_TOTAL => 
1036                              @$return[ARRAY_INDEX_TIME_TOTAL] + @$a[ARRAY_INDEX_TIME_TOTAL],
1037                  
1038                  ARRAY_INDEX_PAGES_VISIT_ONEPAGE => 
1039                              $return[ARRAY_INDEX_PAGES_VISIT_ONEPAGE] + @$a[ARRAY_INDEX_PAGES_VISIT_ONEPAGE],
1040                  
1041                  ARRAY_INDEX_ENTRYPAGE => 
1042                              $return[ARRAY_INDEX_ENTRYPAGE] + @$a[ARRAY_INDEX_ENTRYPAGE],
1043                  
1044                  ARRAY_INDEX_EXITPAGE => 
1045                              $return[ARRAY_INDEX_EXITPAGE] +  @$a[ARRAY_INDEX_EXITPAGE],
1046                  
1047                  ARRAY_INDEX_VARS => $return[ARRAY_INDEX_VARS]
1048              );                    
1049      }
1050      
1051  
1052      /**
1053       * computes sum of the array a_values and the new values to add
1054       * 
1055       * @param array $a_values Contains current values
1056       * @param int $s Hits
1057       * @param int $sumpage Number of pages views
1058       * @param int $onepage Number of one page visit
1059       * @param int $sumtime Total time spent on all the $s visits
1060       * 
1061       * @return array $a_values array $a_values with new values sumed
1062       */
1063  	function getInterestValuesSum($a_values, $s, $sumpage, $onepage, $sumtime)
1064      {
1065          // init
1066          if(!isset($a_values[0])) $a_values[0] = 0;
1067          if(!isset($a_values[1])) $a_values[1] = 0;
1068          if(!isset($a_values[2])) $a_values[2] = 0;
1069          if(!isset($a_values[3])) $a_values[3] = 0;
1070          
1071          $a_values[0] += $s;
1072          $a_values[1] += $sumpage;
1073          $a_values[2] += $onepage;
1074          $a_values[3] += $sumtime;
1075          
1076          return $a_values;
1077      }
1078      
1079      /**
1080       * Computes all info relative to referer
1081       * Detects for each different referer URL (or each entry page, when no referer URLs), the name (and keyword),
1082       * the domain, the precise page, the partner, the newsletter
1083       * 
1084       * Saves in var toRecord all precomputed values. Isn't that great ?
1085       */
1086  	function computeReferer()
1087      {
1088          // Init
1089          $hits = 0;
1090          
1091          $vis_listing = array(
1092              'vis_search_engine',
1093              'vis_keyword',
1094              'vis_newsletter',
1095              'vis_partner',
1096              'vis_site'
1097              );
1098          foreach($vis_listing as $value) $this->toRecord[$value] = array();
1099          
1100          $resDirect =0;
1101          
1102          $int_sum = array();
1103          $int_sum['search_engine'] = array();
1104          $int_sum['keyword'] = array();
1105          $int_sum['site'] = array();
1106          $int_sum['partner_name'] = array();
1107          $int_sum['newsletter'] = array();
1108          $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = array();
1109          $int_sum['type'][REF_TYPE_NEWSLETTER] = array();
1110          $int_sum['type'][REF_TYPE_PARTNER] = array();
1111          $int_sum['type'][REF_TYPE_SEARCH_ENGINE] = array();
1112          $int_sum['type'][REF_TYPE_SITE] = array();
1113          
1114          
1115          // query that selects all distinct referer URLs
1116          $r = query("SELECT count(*) as s, referer as r" .$GLOBALS['test'].
1117                  " FROM ".T_VISIT. 
1118                  " WHERE server_date= '".$this->date->get()."' " .
1119                  " AND idsite = ".$this->site->getId()." " .
1120                  " AND referer IS NOT NULL" .
1121                  " GROUP BY r
1122                      ");
1123                      
1124          while($l = mysql_fetch_assoc($r))
1125          {
1126              // different types :
1127              // - partner identified as it is
1128              // - search engines that give keywords
1129              // - direct access, url belongs to the site
1130              // - other misc websites 
1131              
1132              $hits = $l['s'];
1133              
1134              $refererUrl = $l['r'];
1135              
1136              // now... lets go in identifying !
1137              // and don't go back it's so important !
1138              $refererUrlParse = parse_url($refererUrl);
1139              
1140              // if referer exists (and not 'blockedReferer' or other)
1141              if(isset($refererUrlParse['host']))
1142              {
1143                  $refererHost = $refererUrlParse['host'];
1144                  $refererSH = $refererUrlParse['scheme'].'://'.$refererUrlParse['host'];
1145                  //printDebug("<b>".$refererHost." </b> ");
1146                  
1147                  /*
1148                   * search engine
1149                   */
1150                  if(array_key_exists($refererHost, $GLOBALS['searchEngines']))
1151                  {
1152                      
1153                      // which search engine ?
1154                      $sname = $GLOBALS['searchEngines'][$refererHost][0];
1155                      $vname = $GLOBALS['searchEngines'][$refererHost][1];
1156                      
1157                      // id base logs to id base archives
1158                      $ids = $this->objects['search_engine']->getId($sname);
1159                      
1160                      // init
1161                      if(!isset($this->toRecord['vis_search_engine'][$ids]))
1162                      {
1163                          $this->toRecord['vis_search_engine'][$ids]['pmv_sum'] = 0; 
1164                          $int_sum['search_engine'][$ids] = array();
1165                      }
1166                      
1167                      $this->toRecord['vis_search_engine'][$ids]['pmv_sum'] += $hits;
1168                      
1169                      // interest for current engine
1170                      $int_sum['search_engine'][$ids] = $this->getInterestValuesSum($int_sum['search_engine'][$ids], 
1171                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1172                                                              
1173                      // interest for the type search_engine
1174                      $int_sum['type'][REF_TYPE_SEARCH_ENGINE] = 
1175                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_SEARCH_ENGINE], 
1176                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1177                                                              
1178                      // if there is a query, there may be a keyword...
1179                      if(isset($refererUrlParse['query']))
1180                      {
1181                          /*
1182                           *  keyword ?
1183                           */
1184                          
1185                          $query = $refererUrlParse['query'];
1186                          
1187                          // for google image take the prev variable 
1188                          // and take the q var in this variable once urldecoded
1189                          
1190                          // but sometimes we have images.google.fr/search which is exactly like www.google.fr/search....
1191                          if(ereg("^images.google.",$refererHost))
1192                          {
1193                              //print("befo = ".$query);
1194                              $query = getUrlParamValue($query, "prev");
1195                              
1196                              // case images.google.com/search we replace in the host images by www
1197                              if($query === false )
1198                              {
1199                                  $refererHost = str_replace( "images","www",$refererHost);
1200                                  $vname = $GLOBALS['searchEngines'][$refererHost][1];
1201                              }
1202                              else
1203                              {
1204                                  $query = urldecode($query);
1205                                  // we want the query from "/images?q=+logo+designs&start=360&ndsp=20&svnum=10&hl=en&lr=&sa=N"
1206                                  $query = substr($query, strpos( $query, "?") + 1);
1207                                  // getUrlParam need htmlentitied
1208                                  $query = htmlentities($query);
1209                                  //print("<br> after = ".$query);
1210                              }
1211                          }
1212                          
1213                          // search for keywords now &vname=keyword
1214                          $key = strtolower(getUrlParamValue($query, $vname));
1215                          
1216                          // for search engines that don't use utf-8
1217                          if((function_exists('iconv')) && (isset($GLOBALS['searchEngines'][$refererHost][2])))
1218                          {
1219                              $charset = trim($GLOBALS['searchEngines'][$refererHost][2]);
1220                              if(!empty($charset)) {
1221                                  $key = urlencode(@iconv($charset, 'utf-8//TRANSLIT', urldecode($key)));
1222                              }
1223                          }
1224                          
1225                          //print($refererUrlParse['query'].",". $vname." : ".$key."<br>");
1226                          
1227                          // base logs => base archives
1228                          $idk = $this->objects['keyword']->getId($key);
1229                          
1230                          /*
1231                           * init
1232                           */
1233                          if(!isset($this->toRecord['vis_search_engine'][$ids][$idk]))
1234                          {
1235                              $this->toRecord['vis_search_engine'][$ids][$idk] = 0;
1236                          }
1237                          if(!isset($this->toRecord['vis_keyword'][$idk][$ids]))
1238                          {
1239                              $this->toRecord['vis_keyword'][$idk][$ids] = 0;
1240                              $int_sum['keyword'][$idk] = array();
1241                          }
1242                          if(!isset($this->toRecord['vis_keyword'][$idk]['pmv_sum']))
1243                          {
1244                              $this->toRecord['vis_keyword'][$idk]['pmv_sum'] = 0;
1245                          }
1246                          
1247                          
1248                          $this->toRecord['vis_search_engine'][$ids][$idk] += $hits;
1249                          
1250                          $this->toRecord['vis_keyword'][$idk][$ids] += $hits;
1251                          $this->toRecord['vis_keyword'][$idk]['pmv_sum'] += $hits;
1252                          
1253                          $int_sum['keyword'][$idk] = $this->getInterestValuesSum($int_sum['keyword'][$idk], 
1254                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1255                      }
1256                  }
1257                  /*
1258                   * direct entry
1259                   */
1260                  else if($this->site->isUrlIn($refererUrl))
1261                  {
1262                      //printDebug("<br>directentry<br>");
1263                      $resDirect += $hits;
1264                      // interest for the type 'direct_entry'
1265                      $int_sum['type'][REF_TYPE_DIRECT_ENTRY] =  
1266                                      $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], 
1267                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1268                  }
1269                  /*
1270                   * site partner
1271                   */
1272                  else if($this->site->isPartner($refererUrl))
1273                  {
1274                      //printDebug("<br>partner<br>");
1275                      
1276                      // comments are not necessary here, code is so clear :-D
1277                      $url = array_search( $refererHost,  $this->site->getPartnerUrlsFlat());
1278                      $pname = $this->site->getPartnerName();
1279                      
1280                      $idp = $this->objects['partner_name']->getId($pname);
1281                      $idpu = $this->objects['partner_url']->getId($refererUrl);
1282                      
1283                      // init
1284                      if(!isset($this->toRecord['vis_partner'][$idp]['pmv_sum']))
1285                      {
1286                          $this->toRecord['vis_partner'][$idp]['pmv_sum'] = 0;
1287                          $int_sum['partner_name'][$idp] = array();
1288                      }
1289                      
1290                      $this->toRecord['vis_partner'][$idp][$idpu] = $hits;
1291                      $this->toRecord['vis_partner'][$idp]['pmv_sum']  += $hits;
1292                      
1293                      //printDebug($this->toRecord['vis_partner']);
1294                      
1295                      $int_sum['partner_name'][$idp] = $this->getInterestValuesSum($int_sum['partner_name'][$idp], 
1296                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1297                      $int_sum['type'][REF_TYPE_PARTNER] =
1298                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_PARTNER], 
1299                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1300                  }
1301                  /*
1302                   * other site
1303                   */
1304                  else
1305                  {
1306                      //printDebug(" misc site | ");
1307                      
1308                      $idmsh = $this->objects['site']->getId($refererHost);
1309                      $idms = $this->objects['site']->getId($refererUrl);
1310                      
1311                      // init
1312                      if(!isset($this->toRecord['vis_site'][$idmsh]['pmv_sum']))
1313                      {
1314                          $this->toRecord['vis_site'][$idmsh]['pmv_sum'] = 0;
1315                          $int_sum['site'][$idmsh] = array();
1316                      }
1317                      
1318                      $this->toRecord['vis_site'][$idmsh]['pmv_sum'] += $hits;
1319                      $this->toRecord['vis_site'][$idmsh][$idms] = $hits;
1320                      
1321                      $int_sum['site'][$idmsh] = $this->getInterestValuesSum($int_sum['site'][$idmsh], 
1322                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1323                      $int_sum['type'][REF_TYPE_SITE] =
1324                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_SITE], 
1325                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1326                  }
1327              }
1328              else
1329              {
1330                  // direct entry
1331                  $resDirect += $hits;
1332                  
1333                  //printDebug("<br>directentry<br>");
1334                  
1335                  $int_sum['type'][REF_TYPE_DIRECT_ENTRY] =  
1336                              $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], 
1337                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1338              }
1339          }        
1340          
1341          // query that selects entry pages, when there are no referer URls
1342          // it allows detection of partners, newsletters and direct entries
1343          $r = query("SELECT count(distinct idvisit) as sp, pu.idpage_url as p, 
1344                              pu.url as u" .$GLOBALS['test'].
1345                  " FROM ".T_VISIT. " as v, ".T_PAGE_URL." as pu 
1346                    WHERE v.entry_idpageurl = pu.idpage_url 
1347                    AND v.server_date= '".$this->date->get()."' 
1348                   AND v.idsite = ".$this->site->getId()." 
1349                   AND v.referer IS NULL
1350                   GROUP by entry_idpageurl
1351                      ");
1352  
1353          while($l = mysql_fetch_assoc($r))
1354          {
1355              // different types :
1356              // - partner identified by ID in URL
1357              // - newsletter identified by ID in URL 
1358              // - direct entries
1359              $hits = $l['sp'];
1360              $idPageEntry = $l['p']; // useless
1361              
1362              // url parsing can fail when for example its value is "/translate... etc."
1363              // we count it as a direct entry
1364              $urlParse = @parse_url($l['u']);
1365              
1366              // if referer exists (and not blockedReferer or other)
1367              if(isset($urlParse['query']))
1368              {
1369                  //printDebug($l['p']." ");
1370                  $urlQuery = $urlParse['query'];    
1371                  
1372  //                print($urlQuery); exit;
1373                  /*
1374                   * partner
1375                   */
1376                  if($pid = getUrlParamValue($urlQuery, PARAM_URL_PARTNER))
1377                  {
1378                      
1379                      //printDebug("<br><b>PID $pid detected !</b>");
1380                      //printDebug("<br>partner</br>");
1381                      
1382                      $pname = $this->site->getPartnerNameFromId($pid);
1383                      
1384                      $idp = $this->objects['partner_name']->getId($pname);
1385                                          
1386                      // init
1387                      if(!isset($this->toRecord['vis_partner'][$idp]['pmv_sum']))
1388                      {
1389                          $this->toRecord['vis_partner'][$idp]['pmv_sum'] = 0;
1390                          $int_sum['partner_name'][$idp] = array();
1391                      }
1392                      
1393                      $this->toRecord['vis_partner'][$idp]['pmv_sum']  += $hits;
1394                      
1395                      //printDebug($this->toRecord['vis_partner']);
1396                      
1397                      $int_sum['partner_name'][$idp] = $this->getInterestValuesSum($int_sum['partner_name'][$idp], 
1398                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1399                      $int_sum['type'][REF_TYPE_PARTNER] = 
1400                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_PARTNER], 
1401                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1402                  }
1403                  
1404                  /*
1405                   * newsletter
1406                   */
1407                  else if($nid = getUrlParamValue($urlQuery, PARAM_URL_NEWSLETTER))
1408                  {
1409                      //print("NID to searc = $nid");
1410                      if ((defined("NEWSLETTER_ID_MAX_AUTO")) && (defined("NEWSLETTER_FORCE_CREATE"))
1411                      && ((NEWSLETTER_ID_MAX_AUTO == -1) || ($nid <= NEWSLETTER_ID_MAX_AUTO))) {
1412                          $nname = $this->site->getNewsletterName($nid, NEWSLETTER_FORCE_CREATE);
1413                      }
1414                      else {
1415                          $nname = $this->site->getNewsletterName($nid);
1416                      }
1417                      
1418                      // if newsletter exists
1419                      if(!$nname)
1420                      {
1421                          print("Newsletter name not found! Maybe it's an empty name in phpmv_newsletter ? (for ".PARAM_URL_NEWSLETTER." = $nid)");
1422                          //exit;
1423                      }
1424                      else 
1425                      {
1426              //            print("<br><b>NID $nid detected !</b>");
1427              //            print("<br>newsletter</br>");
1428                      
1429                          $idn = $this->objects['newsletter']->getId($nname);
1430                          
1431                          // init
1432                          if(!isset($this->toRecord['vis_newsletter'][$idn]))
1433                          {
1434                              $this->toRecord['vis_newsletter'][$idn] = 0;
1435                              $int_sum['newsletter'][$idn] = array();
1436                          }
1437                          
1438                          $this->toRecord['vis_newsletter'][$idn] += $hits;
1439                          
1440                          //printDebug($this->toRecord['vis_newsletter']);
1441                          
1442                          $int_sum['newsletter'][$idn] = $this->getInterestValuesSum($int_sum['newsletter'][$idn], 
1443                                                                  $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1444                          $int_sum['type'][REF_TYPE_NEWSLETTER] =  
1445                                      $this->getInterestValuesSum($int_sum['type'][REF_TYPE_NEWSLETTER], 
1446                                                                  $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1447                      }
1448                  }
1449                  /*
1450                   * direct access
1451                   */
1452                  else
1453                  {
1454                      $resDirect += $hits;
1455  
1456                      $int_sum['type'][REF_TYPE_DIRECT_ENTRY] =  
1457                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], 
1458                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1459                  }
1460              }
1461              else
1462              {
1463                  $resDirect += $hits;
1464  
1465                  $int_sum['type'][REF_TYPE_DIRECT_ENTRY] =  
1466                                  $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], 
1467                                                              $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
1468              }
1469              
1470          }
1471          
1472  
1473          /*
1474           * developed interest average
1475           */
1476           
1477          // database-interest-field-name => interestsum-array-keyname
1478          
1479          $int_sum_to_avg = array(
1480          'int_search_engine' => 'search_engine',
1481          'int_keyword' => 'keyword',
1482          'int_site' => 'site',
1483          'int_partner' => 'partner_name',
1484          'int_newsletter' => 'newsletter',
1485          'int_referer_type' => 'type'
1486          );
1487          
1488          foreach($int_sum_to_avg as $int_name => $value)
1489          {
1490              uasort($int_sum[$value], "sortingInterest");
1491              $this->toRecord[$int_name] = $int_sum[$value];
1492          }
1493  
1494          /*
1495           * sum by referer type
1496           */
1497          
1498          $nbSearchEngine = 0;
1499          foreach($this->toRecord['vis_search_engine'] as $key => $value)
1500          {
1501              $nbSearchEngine += $value['pmv_sum'];
1502          }
1503  
1504          $nbSite = 0;
1505          foreach($this->toRecord['vis_site'] as $key => $value)
1506          {
1507              $nbSite += $value['pmv_sum'];
1508          }
1509          $nbUniqSite = sizeof($this->toRecord['vis_site']);
1510          $nbNewsletter = array_sum($this->toRecord['vis_newsletter']);
1511          
1512          $nbPartner = 0;
1513          foreach($this->toRecord['vis_partner'] as $key => $value)
1514          {
1515              $nbPartner += $value['pmv_sum'];
1516          }
1517          
1518          $this->toRecord['nb_direct'] = $resDirect;
1519          $this->toRecord['nb_search_engine'] = $nbSearchEngine;
1520          $this->toRecord['nb_site'] = $nbSite;
1521          $this->toRecord['nb_newsletter'] = $nbNewsletter;
1522          $this->toRecord['nb_partner'] = $nbPartner;
1523              
1524          /*printDebug("<br><br><b>Access types </b><br> Direct : $nbDirect <br>Search engines : $nbSearchEngine " .
1525                  "<br> By site : $nbSite <br> Unique sites : $nbUniqSite <br>" .
1526                  "Partner : $nbPartner <br> Newsletter : $nbNewsletter <br>");
1527          */
1528  
1529  
1530          foreach($vis_listing as $value)
1531          {
1532              if($value != 'vis_newsletter')
1533              {
1534                  
1535                  if(is_array($this->toRecord[$value]))
1536                  {
1537                      foreach($this->toRecord[$value] as $key2 => $value2)
1538                      {
1539                          if(is_array($this->toRecord[$value][$key2]))
1540                          {
1541                              arsort($this->toRecord[$value][$key2]);
1542                          }
1543                          $this->toRecord[$value][$key2] = getArrayOffsetLimit(
1544                                                      $this->toRecord[$value][$key2], 
1545                                                      0, 
1546                                                      MAX_DISTINCT_DETAILS_ELEMENTS
1547                                                  );
1548                      }
1549                  }
1550                  else
1551                  {
1552                      arsort($this->toRecord[$value]);
1553                  }
1554              }
1555          }
1556      }
1557  
1558      /**
1559       * returns "select *" of the day var date, after being computed if necessary
1560       */
1561  	function getArchived()
1562      {
1563          //printDebug("<h1>".$this->date->get()."</h1>");
1564          
1565          if(empty($this->archiveOk))
1566          {
1567              if(empty($this->idArchives) && !$this->isArchived())
1568              {
1569  
1570                  $this->compute();
1571              }
1572          }
1573          
1574          $this->archiveOk = true;
1575          //print("here once ".$this->date->get());
1576          
1577          $r = query("SELECT * " .
1578                  " FROM ".T_ARCHIVES.
1579                  " WHERE idarchives = ". $this->idArchives .
1580                  " LIMIT 1"
1581                  );
1582          
1583          if(mysql_num_rows($r) == 0)
1584          {
1585              trigger_error("Error during the archiving process for the date ".$this->date->get().
1586                              " (period = ".$this->periodType .") for the site ". $this->site->getName() ." (id = ".$this->site->getId() .")",
1587                              E_USER_ERROR);
1588          
1589          }
1590          $allValues = mysql_fetch_assoc($r);
1591          
1592          $end = sizeof($allValues);
1593          reset($allValues);
1594          for($i = 0; $i < $end ; $i ++)
1595          {
1596              $data =& $allValues[key($allValues)];
1597              
1598              if(is_null($data))
1599              {
1600                  $data = compress(serialize( array() ), $allValues['compressed']);
1601              }
1602              
1603              next($allValues);
1604          }
1605          
1606          return $allValues;
1607      }
1608  }
1609  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics