Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
funnelback_log_iterator.inc
1 <?php
27 {
28 
29 
30  protected $offset;
31  protected $log;
32  protected $rotation_index;
33  protected $total_size;
34  protected $blank_query;
35 
36 
44  function __construct($log, $rotation_index=NULL)
45  {
46  $this->offset = 0;
47  $this->log = $log;
48  $this->rotation_index = $rotation_index;
49 
50  $this->total_size = $this->getLogSize($this->log, $this->rotation_index);
51 
52  if (empty($this->total_size)) {
53  $this->total_size = 0;
54  }
55 
56  $fm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager');
57  $this->blank_query = $fm->getNullQueryKeyword();
58 
59  }//end constructor
60 
61 
71  public function getLogSize($log, $rotation_index)
72  {
73  $suffix = ((!is_null($rotation_index) && is_numeric($rotation_index)) ? '.'.$rotation_index : '');
74  $file = $log.$suffix;
75 
76  $size = FALSE;
77  if (file_exists($file)) {
78  $size = sprintf('%u', filesize($file));
79  }//end if
80 
81  return $size;
82 
83  }//end getLogSize()
84 
85 
94  public function getNextEntry()
95  {
96  if ($this->offset < $this->total_size) {
97  $suffix = ((!is_null($this->rotation_index) && is_numeric($this->rotation_index)) ? '.'.$this->rotation_index : '');
98  $logfile = $this->log.$suffix;
99  $line = $this->_getLogLine($logfile, $this->offset);
100  $line_offset = array_get_index($line, 'offset', FALSE);
101 
102  if ($line_offset != FALSE && $line_offset >= 0) {
103  $this->offset = $line_offset;
104  }//end if
105 
106  $line['line'] = $this->_decodeLine(array_get_index($line, 'line', FALSE));
107 
108  return $line;
109 
110  } else {
111  return FALSE;
112  }//end if
113 
114  }//end getNextEntry()
115 
116 
123  public function getCurrentProgress()
124  {
125  $result = Array(
126  'current' => $this->offset,
127  'total' => $this->total_size,
128  );
129 
130  return $result;
131 
132  }//end getCurrentProgress()
133 
134 
144  protected function _getLogLine($file, $offset)
145  {
146  if (empty($file)) return FALSE;
147 
148  $file_contents = Array();
149 
150  if (file_exists($file) && $handle = fopen($file, 'r')) {
151  if (fseek($handle, $offset) >= 0 && !feof($handle)) {
152  $line = fgets($handle);
153  // new offset
154  $offset = ftell($handle);
155  $result = Array(
156  'line' => trim($line),
157  'offset' => $offset,
158  );
159  } else {
160  $result = FALSE;
161  }
162  fclose($handle);
163  } else {
164  trigger_localised_error('CORE0018', E_USER_ERROR, $file);
165  $result = FALSE;
166  }
167 
168  return $result;
169 
170  }//end _getLogLine()
171 
172 
184  protected function _decodeLine($line)
185  {
186  $decoded = Array();
187 
188  if ($line === FALSE || !is_string($line) || strpos($line, ',') === FALSE) {
189  // This doesn't need encoding, let's leave
190  return $decoded;
191  }//end if
192 
193  $line_data = explode(',', $line);
194 
195  $decoded['date'] = strtotime($line_data[0]);
196  $decoded['ip'] = $line_data[1];
197  $decoded['query'] = str_replace($this->blank_query, '', $line_data[2]);
198  $decoded['include'] = $line_data[3];
199  $decoded['exclude'] = $line_data[4];
200  $decoded['start'] = $line_data[5];
201  $decoded['ranks'] = $line_data[6];
202  $decoded['codes'] = $line_data[7];
203  $decoded['full_matches'] = $line_data[8];
204  $decoded['partial_matches'] = $line_data[9];
205  $decoded['elapsed_time'] = $line_data[10];
206  $decoded['profile'] = $line_data[11];
207 
208  return $decoded;
209 
210  }//end _decodeLine()
211 
212 
213 }//end class
214 
215 
216 ?>