class-logreader.php
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Log reading section of the plugin
*
* @link
*
* @package Wt_Import_Export_For_Woo
*/
if (!defined('ABSPATH')) {
exit;
}
if(!class_exists('Wt_Import_Export_For_Woo_Basic_Logreader')){
class Wt_Import_Export_For_Woo_Basic_Logreader
{
private $file_path='';
private $file_pointer=null;
private $mode='';
public function __construct()
{
}
public function init($file_path)
{
$mode = 'r';
$this->file_path=$file_path;
$this->mode=$mode;
$this->file_pointer=@fopen($file_path, $mode);
}
public function close_file_pointer()
{
if($this->file_pointer!=null)
{
fclose($this->file_pointer);
}
}
public function get_full_data($file_path)
{
$out=array(
'response'=>false,
'data_str'=>'',
);
$this->init($file_path);
if(!is_resource($this->file_pointer))
{
return $out;
}
$data=fread($this->file_pointer, filesize($file_path));
$this->close_file_pointer();
$out=array(
'response'=>false,
'data_str'=>$data,
);
return $out;
}
/**
* Read log file as batch
* @param string path of file to read
* @param int offset in bytes. default 0
* @param int total row in a batch. default 50
* @return array response, next offset, data array, finished or not flag
*/
public function get_data_as_batch($file_path, $offset=0, $batch_count=50)
{
$out=array(
'response'=>false,
'offset'=>$offset,
'data_arr'=>array(),
'finished'=>false, //end of file reached or not
);
$this->init($file_path);
if(!is_resource($this->file_pointer))
{
return $out;
}
fseek($this->file_pointer, $offset);
$row_count=0;
$next_offset=$offset;
$finished=false;
$data_arr=array();
while(($data=fgets($this->file_pointer))!==false)
{
$data=maybe_unserialize($data);
if(is_array($data))
{
$data_arr[]=$data;
$row_count++;
$next_offset=ftell($this->file_pointer);
}
if($row_count==$batch_count)
{
break;
}
}
if($next_offset==filesize($file_path))
{
$finished=true;
}
$this->close_file_pointer();
$out=array(
'response'=>true,
'offset'=>$next_offset,
'data_arr'=>$data_arr,
'finished'=>$finished,
);
return $out;
}
}
}