コード例 #1
0
/**
 * Sums values in the pages/groups/files two arrays of N dimensions that can be very different
 * 
 * @param array $a1
 * @param array $a2
 * 
 * @return array
 */
function getPagGrpArrayMultiDimSum($a1, $a2)
{
    if (!empty($a2) && is_array($a2)) {
        foreach ($a2 as $key => $value) {
            if (!isset($a1[$key])) {
                $a1[$key] = $value;
            } else {
                if (substr($key, 0, 1) === 'c') {
                    $a1[$key] = getPagGrpArrayMultiDimSum($value, $a1[$key]);
                } else {
                    $a1[$key] = sumArray($value, $a1[$key], true);
                }
            }
        }
    }
    return $a1;
}
コード例 #2
0
 /**
  * Computes datas for all subPeriodValues values 
  * and saves it in a new row in the database table archives
  * 
  * @return void
  */
 function compute()
 {
     if (CURRENT_PERIOD_SIMPLE_ARCHIVE && $this->state === DB_ARCHIVES_TEMP) {
         $this->toRecord['simple'] = 1;
     } else {
         $this->toRecord['simple'] = 0;
     }
     // date in the IN(...) SQL field for unique visitors
     $inIdArchives = '';
     $this->toRecord['vis_pag_grp'] = array();
     // init
     $toInitInt = $this->intValuesToSum;
     $toInitArray = array_merge($this->arrayOneDimToSum, $this->arrayPmvSumToSum, $this->arrayIntToSum);
     foreach ($toInitInt as $value) {
         $this->toRecord[$value] = 0;
     }
     foreach ($toInitArray as $value) {
         $this->toRecord[$value] = array();
     }
     // for each subPeriod, sum all values
     foreach ($this->subPeriodValues as $idArchives => $subPeriodValues) {
         foreach ($this->intValuesToSum as $name) {
             $this->toRecord[$name] += $subPeriodValues[$name];
         }
         // case we don't want a SIMPLE archiving
         if ($this->toRecord['simple'] == 0) {
             //printDebug("Sum2 : <br>");
             foreach ($this->arrayOneDimToSum as $name) {
                 if (!isset($subPeriodValues[$name])) {
                     $subPeriodValues[$name] = compress(serialize(array()), $subPeriodValues['compressed']);
                 }
                 $this->toRecord[$name] = getArrayOneDimVeryVeryVerySpecialSum($this->toRecord[$name], unserialize(uncompress($subPeriodValues[$name], $subPeriodValues['compressed'])), "sum");
             }
             //printDebug("Sum3 : <br>");
             foreach ($this->arrayPmvSumToSum as $name) {
                 if (!isset($subPeriodValues[$name])) {
                     $subPeriodValues[$name] = compress(serialize(array()), $subPeriodValues['compressed']);
                 }
                 $this->toRecord[$name] = getArrayOneDimVeryVeryVerySpecialSum($this->toRecord[$name], unserialize(uncompress($subPeriodValues[$name], $subPeriodValues['compressed'])), "sumArray");
             }
             //printDebug("Sum4 : <br>");
             foreach ($this->arrayIntToSum as $name) {
                 if (!isset($subPeriodValues[$name])) {
                     $subPeriodValues[$name] = compress(serialize(array()), $subPeriodValues['compressed']);
                 }
                 $this->toRecord[$name] = getArrayOneDimVeryVeryVerySpecialSum($this->toRecord[$name], unserialize(uncompress($subPeriodValues[$name], $subPeriodValues['compressed'])), "sumArray");
             }
             //printDebug("<h1>Sum5  $idArchives: </h1><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>");
             //printDebug("table to add");
             //printDebug(unserialize($subPeriodValues['vis_pag_grp']));
             if (!is_string($subPeriodValues['vis_pag_grp'])) {
                 $subPeriodValues['vis_pag_grp'] = compress(serialize(array()), $subPeriodValues['compressed']);
             }
             $this->toRecord['vis_pag_grp'] = getPagGrpArrayMultiDimSum($this->toRecord['vis_pag_grp'], unserialize(uncompress($subPeriodValues['vis_pag_grp'], $subPeriodValues['compressed'])));
         }
         $inIdArchives .= "'" . $idArchives . "', ";
     }
     //printDebug($this->toRecord['vis_pag_grp']);
     //exit;
     /*
      * Sorting arrays
      */
     $this->sortAndLimitToRecord();
     $inIdArchives = substr($inIdArchives, 0, strlen($inIdArchives) - 2);
     // if year, look for info averaging month archives
     if ($this->periodType === DB_ARCHIVES_PERIOD_YEAR) {
         $r = query("SELECT sum(nb_uniq_vis) as nb_uniq_vis, max(nb_max_pag) as nb_max_pag\n\t\t\t\t\tFROM " . T_ARCHIVES . "\n\t\t\t\t\tWHERE idarchives IN (" . $inIdArchives . ")");
         // unique visitors returning sum on the period
         $nb_uniq_vis_returning = query("SELECT sum(nb_uniq_vis_returning) as nb_uniq_vis_returning\n\t\t\t\t\t\t\t\t\t\t\tFROM " . T_ARCHIVES . "\n\t\t\t\t\t\t\t\t\t\t\tWHERE idarchives IN (" . $inIdArchives . ")");
         // returning visitors
         $returning = query("SELECT nb_vis_returning as s, idarchives as pseudodate, \n\t\t\t\t\t\t\t\t\tnb_pag_returning as sp, date1 as pseudodate2\n\t\t\t\t\t\tFROM " . T_ARCHIVES . "\n\t\t\t\t\t\tWHERE idsite = " . $this->site->getId() . "\n\t\t\t\t\t\tAND idarchives IN (" . $inIdArchives . ")\n\t\t\t\t\t\tGROUP BY pseudodate\n\t\t\t\t\t\t");
     } else {
         $r = query("SELECT count(distinct idcookie) as nb_uniq_vis, max(total_pages) as nb_max_pag\n\t\t\t\tFROM " . T_VISIT . "\n\t\t\t\tWHERE idsite = " . $this->site->getId() . "\n\t\t\t\tAND server_date IN (" . $inIdArchives . ")");
         // unique visitors returning sum on the period
         $nb_uniq_vis_returning = query("SELECT count(distinct idcookie) as nb_uniq_vis_returning\t\n\t\t\t\t\t\t\t\t\t\t\tFROM " . T_VISIT . "\n\t\t\t\t\t\t\t\t\t\t\tWHERE idsite = " . $this->site->getId() . "\n\t\t\t\t\t\t\t\t\t\t\tAND server_date IN (" . $inIdArchives . ")\n\t\t\t\t\t\t\t\t\t\t\tAND returning = 1\n\t\t\t\t \t\t\t\t\t\t\tGROUP BY returning");
         // returning visitors
         $returning = query("SELECT count(*) as s, server_date as pseudodate, \n\t\t\t\t\t\t\t\t\t\tsum(total_pages) as sp\n\t\t\t\t\t\tFROM " . T_VISIT . "\n\t\t\t\t\t\tWHERE idsite = " . $this->site->getId() . "\n\t\t\t\t\t\tAND server_date IN (" . $inIdArchives . ")\n\t\t\t\t\t\tAND returning = 1\n\t\t\t\t\t\tGROUP BY pseudodate");
         // for each returning visit, how many time did they return?
         $f1 = query("SELECT count(*) as s\n\t\t\t\t\t\tFROM " . T_VISIT . "\n\t\t\t\t\t\tWHERE idsite = " . $this->site->getId() . "\n\t\t\t\t\t\tAND server_date IN (" . $inIdArchives . ")\n\t\t\t\t\t\tGROUP BY idcookie");
         $res = array();
         while ($fx = mysql_fetch_assoc($f1)) {
             @$res[(int) $fx['s']]++;
         }
         ksort($res);
         $this->toRecord['vis_nb_vis'] = $res;
     }
     $nb_uniq_vis_returning = mysql_fetch_assoc($nb_uniq_vis_returning);
     $this->toRecord['nb_uniq_vis_returning'] = $nb_uniq_vis_returning['nb_uniq_vis_returning'];
     $l = mysql_fetch_assoc($r);
     $this->toRecord['nb_uniq_vis'] = $l['nb_uniq_vis'];
     $this->toRecord['nb_max_pag'] = $l['nb_max_pag'];
     // visits per period, new vs returning, and pages hits
     $returningTotal = 0;
     $returningPag = 0;
     $this->toRecord['vis_period'] = array();
     while ($l2 = mysql_fetch_assoc($returning)) {
         $visitsTotal = $this->subPeriodValues[$l2['pseudodate']]['nb_vis'];
         $pagesTotal = $this->subPeriodValues[$l2['pseudodate']]['nb_pag'];
         $returningVisits = $l2['s'];
         $returningPag += $l2['sp'];
         $returningTotal += $returningVisits;
         $this->toRecord['vis_period'][isset($l2['pseudodate2']) ? $l2['pseudodate2'] : $l2['pseudodate']] = array(ARRAY_INDEX_RETURNING_COUNT => (int) $returningVisits, ARRAY_INDEX_NEW_COUNT => $visitsTotal - $returningVisits, ARRAY_INDEX_PAGES_COUNT => (int) $pagesTotal);
     }
     ksort($this->toRecord['vis_period']);
     // number of returning visits
     $this->toRecord['nb_vis_returning'] = $returningTotal;
     $this->toRecord['nb_pag_returning'] = $returningPag;
     //printDebug("TO RECORD<br>");
     //printDebug($this->toRecord);
     /*
      * init
      */
     $this->initDb();
     /*
      * final save, close your eyes and prey 
      */
     $this->saveDb();
     /**
      * delete all visit/month records
      */
     $this->deleteOldRecords();
 }