[ Index ] |
|
Code source de phpMyVisites 2.3 |
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: Archive.class.php 102 2006-10-01 16:28:30Z matthieu_ $ 11 12 13 $GLOBALS['test'] = ", sum(case total_pages when 1 then 1 else 0 end) as onepage," . 14 " sum(total_pages) as sumpage, count(*) as s, sum(total_time) as sumtime"; 15 16 require_once INCLUDE_PATH."/core/include/Date.class.php"; 17 require_once INCLUDE_PATH."/core/include/Str.class.php"; 18 require_once INCLUDE_PATH."/core/include/Archive.functions.php"; 19 require_once INCLUDE_PATH."/core/include/functions.php"; 20 21 /** 22 * Base class used by other ArchiveClass to assign some standard attributes 23 * 24 */ 25 class Archive 26 { 27 /** 28 * @var object Site 29 */ 30 var $site; 31 32 /** 33 * @var array fieldName => fieldValue to save in database 34 */ 35 var $toRecord; // array 'field_name' => value to record 36 37 /** 38 * @var int Current idarchives of the archive process 39 */ 40 var $idArchives; 41 42 /** 43 * @var int State of the current archive process (Must be DB_ARCHIVES_*) 44 */ 45 var $state; // DB_x 46 47 /** 48 * @var object Date 49 */ 50 var $date; 51 var $date2; 52 53 /** 54 * @var int Type of the current archive period (Must be DB_ARCHIVES_PERIOD_*) 55 */ 56 var $periodType; 57 58 59 // int value 60 /** 61 * @var array integer arrays to sum conserving keys array( 3, 4, 6 ); 62 */ 63 var $intValuesToSum; 64 65 // key => int value 66 var $arrayOneDimToSum; 67 68 // key => array('pmv_sum' => X, 'iddetail1' => Y, 'iddetail2' => Z); 69 var $arrayPmvSumToSum; 70 71 var $arrayIntToSum; 72 var $archiveOk; // flag to known if finished, used when using current archive twice 73 74 /** 75 * @var string literal date corresponding to $this->date 76 */ 77 var $literalDate; 78 79 /** 80 * @param object $site 81 */ 82 function Archive($site) 83 { 84 $this->site = $site; 85 $this->state = DB_ARCHIVES_DONE; 86 $this->toRecord = array(); 87 88 $this->archiveOk = false; 89 90 $this->intValuesToSum = array( 91 'nb_vis', 'nb_pag', 'nb_uniq_pag', 'nb_vis_1pag', 'sum_vis_lth', 92 'nb_direct', 'nb_search_engine', 'nb_site', 'nb_newsletter', 'nb_partner', 93 'nb_vis_returning', 'nb_pag_returning', 'nb_vis_1pag_returning', 'sum_vis_lth_returning' 94 ); 95 96 // key => int value 97 $this->arrayOneDimToSum = array( 98 'vis_st', 'vis_lt', 'pag_st', 'pag_lt', 'vis_lth', 'vis_nb_pag', 99 'vis_country', 'vis_continent', 'vis_provider', 'vis_config', 'vis_os', 'vis_browser', 'vis_browser_type', 100 'vis_resolution', 'vis_plugin', 101 'vis_newsletter', 102 'vis_nb_vis' 103 ); 104 105 // key => array('pmv_sum' => X, 'iddetail1' => Y, 'iddetail2' => Z); 106 $this->arrayPmvSumToSum = array( 107 'vis_search_engine', 'vis_keyword', 'vis_site', 'vis_partner' 108 ); 109 110 $this->arrayIntToSum = array( 111 'int_lt', 'int_st', 'int_referer_type', 'int_search_engine', 'int_keyword', 'int_site', 'int_partner', 112 'int_newsletter', 'int_country', 'int_continent', 'int_os', 'int_browser', 'int_resolution' 113 ); 114 115 } 116 117 /** 118 * Update database, set var state 119 */ 120 function updateDbState() 121 { 122 updateLine( 123 T_ARCHIVES, 124 array( 'done' => $this->state, 'idarchives' => $this->idArchives), 125 'idarchives'); 126 } 127 128 /** 129 * saves var toRecord values in the database, serializing arrays 130 */ 131 function saveDb() 132 { 133 if( 134 // day + simple day archive, means array empty, do NOT compress 135 // ($this->periodType === DB_ARCHIVES_PERIOD_DAY && !CURRENT_DAY_SIMPLE_ARCHIVE) 136 // || 137 // ($this->periodType !== DB_ARCHIVES_PERIOD_DAY && !CURRENT_PERIOD_SIMPLE_ARCHIVE) 138 COMPRESS_DB_DATA 139 && function_exists('gzcompress')) 140 { 141 $this->toRecord['compressed'] = 1; 142 } 143 else 144 { 145 $this->toRecord['compressed'] = 0; 146 } 147 /* 148 print("<pre>"); 149 var_dump($this->toRecord); 150 print("<br><br><br>"); 151 */ 152 153 foreach($this->toRecord as $fieldName => $a_value) 154 { 155 if(is_array($a_value)) 156 { 157 $a_value = databaseEscape(compress(serialize($a_value), $this->toRecord['compressed'])); 158 } 159 //print("<br>update $fieldName data = ".strlen(serialize($a_value))); 160 161 updateLine( 162 T_ARCHIVES, 163 array( $fieldName => $a_value, 'idarchives' => $this->idArchives), 164 'idarchives'); 165 } 166 167 $this->updateDbState(); 168 } 169 170 /** 171 * set periodType 172 * 173 * @param int $type 174 */ 175 function setPeriodType($type) 176 { 177 $this->periodType = $type; 178 179 } 180 181 function getLiteralDate() 182 { 183 return $this->literalDate; 184 } 185 function setLiteralDate() 186 { 187 switch($this->periodType) 188 { 189 190 case DB_ARCHIVES_PERIOD_DAY: 191 $typeDateDisplay = 1; 192 break; 193 194 case DB_ARCHIVES_PERIOD_WEEK: 195 $typeDateDisplay = 6; 196 break; 197 198 case DB_ARCHIVES_PERIOD_MONTH: 199 $typeDateDisplay = 4; 200 201 break; 202 203 case DB_ARCHIVES_PERIOD_YEAR: 204 $typeDateDisplay = 11; 205 break; 206 } 207 208 $this->literalDate = getDateDisplay($typeDateDisplay, $this->date); 209 } 210 /** 211 * init the database, inserting a new row for the current archive process 212 * assigns var idArchives 213 * 214 * @return true 215 */ 216 function initDb() 217 { 218 $r2 = query("INSERT INTO ".T_ARCHIVES." (idsite, done, period, date1, date2)" . 219 " VALUES (".$this->site->getId().", " . 220 " ". DB_ARCHIVES_FAIL."," . 221 $this->periodType .", ". 222 " '".$this->date->get()."'," . 223 " '".$this->date2->get()."')" 224 ); 225 226 $this->idArchives = mysql_insert_id(); 227 return true; 228 } 229 230 function deleteOldRecords() 231 { 232 // delete failed archives 233 // delete temp archives older than today 234 // date2 <> yesterday because of monthly and weekly archive for months/week not finished yet 235 // once on 10 calls 236 if( time() % 10 == 0 ) 237 { 238 $r = query("SELECT max(idarchives) 239 FROM ".T_ARCHIVES 240 ); 241 $r = mysql_fetch_row($r); 242 243 // max id - 50 is the max id we want to delete, else it would be dangerous 244 $maxId = (int)$r[0] - 100; 245 if($maxId > 1) 246 { 247 // we delete only old tries for archived 248 // because if someone asked for an archive 10min ago, we don't delete it! 249 $query = "DELETE FROM ".T_ARCHIVES." 250 WHERE (done = ".DB_ARCHIVES_FAIL." 251 OR done = ".DB_ARCHIVES_TEMP.") 252 AND date1 <> '".date("Y-m-d")."' 253 AND date2 <> '".date("Y-m-d")."' 254 AND date2 <> '".date("Y-m-d", time()-86400)."' 255 AND idarchives < $maxId 256 "; 257 $r = query($query); 258 //print("deleted records= ".mysql_affected_rows() ."<br> $query <br>"); 259 } 260 } 261 if( time() % 30 == 0) 262 { 263 $r = query("OPTIMIZE TABLE ".T_ARCHIVES); 264 $r = query("OPTIMIZE TABLE ".T_VISIT); 265 } 266 267 // delete doubled archives 268 269 // delete for days 270 // double archive is when concat(idsite, date1) is the same 271 // for period = DB_DAYS and done = DONE 272 //$this->deleteDoubleArchives(true); 273 274 // delete for other period than days (week/month/year) 275 // double archive is when concat(idsite, date1, date2) is the same 276 // for period <> DB_DAYS and done = DONE 277 $this->deleteDoubleArchives(false); 278 } 279 280 function deleteDoubleArchives( $dayPeriod ) 281 { 282 if($dayPeriod) 283 { 284 $concatSQL = 'CONCAT( idsite , period, date1)'; 285 $periodTest = '='; 286 } 287 else 288 { 289 $concatSQL = 'CONCAT( idsite , period, date1, date2)'; 290 $periodTest = '<>'; 291 292 } 293 $r = query("SELECT count(*) as c, idsite, date1, date2, $concatSQL as ac 294 FROM ".T_ARCHIVES." 295 WHERE period ".$periodTest." ".DB_ARCHIVES_PERIOD_DAY." 296 AND done = ".DB_ARCHIVES_DONE." 297 GROUP BY ac 298 HAVING c > 1" 299 ); 300 while($l = mysql_fetch_assoc($r)) 301 { 302 //print($l['c'] . ": ". $l['idsite']." for date " . $l['date1']."<>".$l['date2']." ( ".$l['ac'].")<br>"); 303 304 $concatToLookFor = $l['ac']; 305 // for the date select the archives to keep 306 $r2 = query("SELECT idarchives as id 307 FROM ".T_ARCHIVES." 308 WHERE $concatSQL = '".$concatToLookFor."' 309 AND done = ".DB_ARCHIVES_DONE." 310 ORDER BY idarchives DESC 311 LIMIT 1"); 312 $l2 = mysql_fetch_assoc($r2); 313 $idToKeep = $l2['id']; 314 315 $query = "DELETE 316 FROM ".T_ARCHIVES." 317 WHERE $concatSQL = '".$concatToLookFor."' 318 AND idarchives <> $idToKeep 319 AND done = ".DB_ARCHIVES_DONE." 320 "; 321 $r3 = query($query); 322 } 323 } 324 /** 325 * offset the date, considering the date of the first log record for the current site 326 * and considering today date. It gives a date which is between these 2 dates. 327 * 328 * @param object $o_date 329 */ 330 function offsetDate($o_date, $minDay = null) 331 { 332 $o_dateR = new Date($o_date->get()); 333 if(is_null($minDay)) 334 $minDay = $this->site->getMinDay(); 335 336 // Manage with future 337 if($o_date->getTimestamp() > time()) 338 { 339 $o_dateR = new Date(getDateFromTimestamp(time())); 340 } 341 342 343 // manage with past 344 if($o_date->getTimestamp() < $minDay->getTimestamp()) 345 { 346 $o_dateR = $minDay; 347 } 348 return $o_dateR; 349 } 350 351 /** 352 * Returns limit of the period periodType containing $s_date 353 * Works with weeks, month, years, and misc periods (in this case, minDate is the first day where there are logs) 354 * 355 * @param string $s_date 356 */ 357 function getPeriodDatesLimit($s_date) 358 { 359 // objects 360 $date = new Date($s_date); 361 $minDate = $this->site->getMinDay(); 362 363 $date = $this->offsetDate($date); 364 365 //print("MinDate is ".$minDate->get()."<br>Date asked is $s_date<br>Date after Offset is ".$date->get()."<br>"); 366 367 switch ($this->periodType) 368 { 369 370 case DB_ARCHIVES_PERIOD_MONTH: 371 // detect min day for the month 372 // if the minimum date month is different, we surely take the whole month 373 if($minDate->getMonth() == $date->getMonth() 374 && $minDate->getYear() == $date->getYear()) 375 { 376 $minDateForSure = $minDate->get(); 377 } 378 else 379 { 380 $minDateForSure = $date->getYear()."-".$date->getMonth()."-01"; 381 } 382 383 // detect max date for the month 384 // if month asked is the current month then max day is today, else its the last day of the month 385 if($date->getMonth() == date("m") && $date->getYear() == date("Y")) // >= because if month in future, take today 386 { 387 388 // if today is the first day of the month, don't say 389 // "yesterday is the last day of the month" 390 // but "today is the only day of the month" 391 if( date("j",time() ) == 1) 392 { 393 $maxDateForSure = getDateFromTimestamp(time()); 394 } 395 else 396 { 397 $maxDateForSure = getDateFromTimestamp(time()-86400); 398 } 399 } 400 else 401 { 402 $maxDateForSure = $date->getYear()."-".$date->getMonth()."-".date("t", $date->getTimestamp()); 403 } 404 break; 405 case DB_ARCHIVES_PERIOD_WEEK: 406 407 $time = $date->getTimestamp(); 408 409 // detect beginning of the week 410 while(date("W", $time-86400) == $date->getWeek() 411 && $time-86400 >= $minDate->getTimestamp() 412 ) 413 { 414 $time -= 86400; 415 } 416 417 $minDateForSure = getDateFromTimestamp($time); 418 419 // end of week 420 while(date("W", $time+86400) == $date->getWeek() 421 && $time+86400 <= time() 422 ) 423 { 424 $time += 86400; 425 } 426 $maxDateForSure = getDateFromTimestamp($time); 427 428 break; 429 case DB_ARCHIVES_PERIOD_MISC_PERIOD: 430 $minDateForSure = $minDate->get(); 431 $maxDateForSure = $date->get(); 432 break; 433 434 case DB_ARCHIVES_PERIOD_YEAR: 435 436 // min is minDate if year common 437 if($date->getYear() == date("Y", $minDate->getYear())) 438 { 439 $minDateForSure = $minDate->get(); 440 } 441 else 442 { 443 $minDateForSure = $date->getYear()."-01-01"; 444 } 445 446 // max date is today if year is current year 447 if($date->getYear() == date("Y")) 448 { 449 $maxDateForSure = getDateFromTimestamp(time()); 450 451 } 452 else 453 { 454 $maxDateForSure = $date->getYear()."-12-31"; 455 } 456 break; 457 458 default: 459 break; 460 } 461 462 $this->date = new Date($minDateForSure); 463 $this->date2 = new Date($maxDateForSure); 464 465 466 //print("max=$maxDateForSure <br> ".$this->date->get().",". $this->date2->get()."<br>"); 467 $this->date = $this->offsetDate($this->date); 468 $this->date2 = $this->offsetDate($this->date2); 469 } 470 471 472 function sortAndLimitToRecord() 473 { 474 $doNotSort = array('vis_st', 'vis_lt', 'pag_st', 'pag_lt', 'vis_lth', 'vis_nb_pag', 'vis_nb_vis'); 475 476 foreach ($this->arrayOneDimToSum as $name) 477 { 478 if( !in_array($name, $doNotSort)) 479 { 480 arsort($this->toRecord[$name]); 481 } 482 $this->toRecord[$name] = getArrayOffsetLimit($this->toRecord[$name], 0, MAX_DISTINCT_ELEMENTS); 483 } 484 485 foreach($this->arrayPmvSumToSum as $name) 486 { 487 uasort($this->toRecord[$name], "sortingPmv"); 488 $this->toRecord[$name] = getArrayOffsetLimit($this->toRecord[$name], 0, MAX_DISTINCT_ELEMENTS); 489 } 490 491 foreach($this->arrayIntToSum as $name) 492 { 493 uasort($this->toRecord[$name], "sortingInterest"); 494 $this->toRecord[$name] = getArrayOffsetLimit($this->toRecord[$name], 0, MAX_DISTINCT_INTEREST_ELEMENTS); 495 } 496 497 } 498 } 499 500 501 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 14:10:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |