stats/split-stats.php
changeset 15 5e2d1514ccd0
equal deleted inserted replaced
14:506a7e0fb106 15:5e2d1514ccd0
       
     1 <?php
       
     2 
       
     3 @set_time_limit(0);
       
     4 @ini_set('memory_limit', '256M');
       
     5 
       
     6 if ( $dir = @opendir('.') )
       
     7 {
       
     8   while ( $dh = @readdir($dir) )
       
     9   {
       
    10     if ( !preg_match('/^stats-data(-[0-9]+)\.php$/', $dh) )
       
    11       continue;
       
    12     
       
    13     split_stats_file($dh);
       
    14   }
       
    15   closedir($dir);
       
    16 }
       
    17 
       
    18 function split_stats_file($file)
       
    19 {
       
    20   echo "loading $file";
       
    21   
       
    22   require($file);
       
    23   if ( !is_array($stats_data) )
       
    24   {
       
    25     return false;
       
    26   }
       
    27   
       
    28   unlink($file);
       
    29   
       
    30   echo "\rprocessing $file\n";
       
    31   
       
    32   $newdata = array();
       
    33   foreach ( $stats_data['messages'] as $channel => &$chandata )
       
    34   {
       
    35     echo "  processing channel $channel\n";
       
    36     foreach ( $chandata as $i => $message )
       
    37     {
       
    38       $message_day = gmdate('Ymd', $message['time']);
       
    39       if ( !isset($newdata[$message_day]) )
       
    40       {
       
    41         echo "\r    processing " . gmdate('Y-m-d', $message['time']);
       
    42         $newdata[$message_day] = array(
       
    43           'messages' => array()
       
    44         );
       
    45         if ( isset($stats_data['counts']) )
       
    46         {
       
    47           $newdata[$message_day]['counts'] = $stats_data['counts'];
       
    48         }
       
    49         if ( isset($stats_data['anonymous']) )
       
    50         {
       
    51           $newdata[$message_day]['anonymous'] = $stats_data['anonymous'];
       
    52         }
       
    53       }
       
    54       if ( !isset($newdata[$message_day]['messages'][$channel]) )
       
    55       {
       
    56         $newdata[$message_day]['messages'][$channel] = array();
       
    57       }
       
    58       $newdata[$message_day]['messages'][$channel][] = $message;
       
    59       unset($chandata[$i]);
       
    60     }
       
    61     echo "\n";
       
    62   }
       
    63   foreach ( $newdata as $date => &$data )
       
    64   {
       
    65     echo "\r  writing output for $date";
       
    66     write_stats_file("stats-data-$date.php", $data);
       
    67   }
       
    68   echo "\n";
       
    69 }
       
    70 
       
    71 function write_stats_file($file, $data)
       
    72 {
       
    73   $fp = @fopen($file, 'w');
       
    74   if ( !$fp )
       
    75     return false;
       
    76   
       
    77   ob_start();
       
    78   var_export($data);
       
    79   $data = ob_get_contents();
       
    80   ob_end_clean();
       
    81   
       
    82   fwrite($fp, "<?php\n\$stats_data = $data;\n");
       
    83   fclose($fp);
       
    84   unset($data);
       
    85 }