-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
285 lines (241 loc) · 9.55 KB
/
index.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
require_once 'vendor/autoload.php';
ini_set('memory_limit', -1);
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
// OPTIONAL: Set version truncation to none, so full versions will be returned
// By default only minor versions will be returned (e.g. X.Y)
// for other options see VERSION_TRUNCATION_* constants in DeviceParserAbstract class
// DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);
// for mac opened files
ini_set('auto_detect_line_endings',TRUE);
class processCSV {
public $bots = 0,
$rows = 0,
$repeatUsers = 0,
$users = [],
$stats = [
'link_title' => [],
'link_layout' => [],
'link_location' => [],
'content_type' => [],
'link_count' => [],
'clicked' => 0
],
$filename,
$filePath,
$handle,
$newCSV,
$colMap,
$device,
$browser;
public function __construct($directory) {
$startTime = microtime(true);
$this->directory = $directory;
$this->timeStarted = microtime(true);
$this->serverRequestTime = $_SERVER['REQUEST_TIME'];
$files = array_diff(scandir($directory), array('.', '..'));
// foreach file in directory
foreach($files as $file) {
// check if it's a CSV
if(strpos($file, '.csv') === false ) {
continue;
}
$fileStart = microtime(true);
$filePath = $this->directory.$file;
$this->handle = fopen($filePath, 'r');
$this->colMap = false;
if($this->handle !== false) {
$newFile = $this->serverRequestTime.'-processed-'.$file;
$this->newCSV = fopen('/Users/jj/Dropbox/mamp/sites/user-agent-csv/csv/processed/'.$newFile, 'w');
echo "Processing $newFile";
$this->buildCSV();
// close our files
fclose($this->newCSV);
fclose($this->handle);
$elapsedTime = microtime(true) - $fileStart;
echo "\n$file processed into $newFile in ".(string) $elapsedTime."s\n";
}
}
$this->outputData();
$elapsedTime = microtime(true) - $this->timeStarted;
echo "\nAll files processed in ".(string) $elapsedTime."s\n";
}
protected function buildCSV() {
// what column is the user agent data in???
while (($data = fgetcsv($this->handle, 20000, ",")) !== FALSE) {
// PROCESS FIRST ROW AND SET HEADER ROW
if($this->colMap === false) {
// first row will be the column headers, so we need to locate the user agent column
foreach($data as $index => $column) {
$this->colMap[$column] = $index;
}
// add in our new columns
$newCols = ['os', 'device', 'device_brand', 'device_model', 'browser_type', 'browser_name', 'browser_version'];
foreach($newCols as $colName) {
$this->colMap[$colName] = count($this->colMap);
}
// write the first line
$this->writeHeaderRow();
continue;
}
// check if we already have this user
/*
if(!empty($data[$this->colMap['user_id']]) && in_array($data[$this->colMap['user_id']], $this->users, true)) {
// skip it because we only want the first entry from each user
$this->repeatUsers++;
continue;
}
$this->users[] = $data[$this->colMap['user_id']];
*/
$dd = new DeviceDetector($data[$this->colMap['browser']]);
// If called, getBot() will only return true if a bot was detected (speeds up detection a bit)
$dd->discardBotInformation();
$dd->parse();
if ($dd->isBot()) {
// increse bot count
$this->bots++;
// discard row
continue;
}
// get device and browser info
$client = $dd->getClient(); // holds information about browser, feed reader, media player, ...
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
$brand = $dd->getBrandName();
$model = $dd->getModel();
// Map to new order
$csvRow = [
'link_title' => $data[$this->colMap['link_title']],
'link_layout' => $data[$this->colMap['link_layout']],
'link_location' => $data[$this->colMap['link_location']],
'content_type' => $data[$this->colMap['content_type']],
'link_count' => $data[$this->colMap['link_count']],
'link_clicked' => $data[$this->colMap['link_clicked']],
'clicked' => $data[$this->colMap['clicked']],
'os' => (isset($osInfo['name']) ? $osInfo['name'] : '') . (isset($osInfo['version']) ? ' '.$osInfo['version']: ''),
'device' => ucfirst($device),
'device_brand' => $brand,
'device_model' => $model,
'browser_type' => $client['type'],
'browser_name' => $client['name'],
'browser_version' => $client['version'],
'site' => $data[$this->colMap['site']],
'page_url' => $data[$this->colMap['page_url']],
'referrer' => $data[$this->colMap['referrer']],
'paragraphs' => $data[$this->colMap['paragraphs']],
'displayed_url_1' => $data[$this->colMap['displayed_url_1']],
'displayed_url_2' => $data[$this->colMap['displayed_url_2']],
'displayed_url_3' => $data[$this->colMap['displayed_url_3']],
'displayed_url_4' => $data[$this->colMap['displayed_url_4']],
'displayed_url_5' => $data[$this->colMap['displayed_url_5']],
'time_logged' => $data[$this->colMap['time_logged']],
'time_updated' => $data[$this->colMap['time_updated']],
'user_id' => $data[$this->colMap['user_id']],
'browser' => $data[$this->colMap['browser']],
'row' => $data[$this->colMap['row']]
];
// increase total row count
$this->rows++;
// output to console to keep track of our process
if($this->rows % 1000 === 0) {
echo ".";
}
// increase other stats
$this->addStats($csvRow);
// write to the CSV
$this->writeRow($csvRow);
}
}
/**
* Writes a row to the CSV
* @param $data ARRAY of data to write.
*/
public function writeRow($data) {
$headers = $this->getCSVHeaderRow();
$rowData = [];
// maps the data to match the row header order
foreach($headers as $colName) {
$rowData[] = $data[$colName];
}
// write the CSV
fputcsv($this->newCSV, $rowData);
}
/**
* Writes the header row to the CSV
*/
public function writeHeaderRow() {
$headers = $this->getCSVHeaderRow();
// write to the CSV
fputcsv($this->newCSV, $headers);
}
// Outputs our header row in the order it will eventually be mapped to
public function getCSVHeaderRow() {
return [
'link_title',
'link_layout',
'link_location',
'content_type',
'link_count',
'link_clicked',
'clicked',
'os',
'device',
'device_brand',
'device_model',
'browser_type',
'browser_name',
'browser_version',
'site',
'page_url',
'referrer',
'paragraphs',
'displayed_url_1',
'displayed_url_2',
'displayed_url_3',
'displayed_url_4',
'displayed_url_5',
'time_logged',
'time_updated',
'user_id',
'browser',
'row'
];
}
public function addStats($row) {
$this->increaseStats('link_title', $row);
$this->increaseStats('link_layout', $row);
$this->increaseStats('link_location', $row);
$this->increaseStats('content_type', $row);
$this->increaseStats('link_count', $row);
if($row['clicked'] == 1) $this->stats['clicked']++;
}
public function increaseStats($name, $row) {
if(empty($name) || !isset($this->stats[$name][$row[$name]]) && empty($row[$name])) {
return;
}
if(!isset($this->stats[$name][$row[$name]])) {
$this->stats[$name][$row[$name]] = 1;
} elseif(!empty($row[$name])) {
$this->stats[$name][$row[$name]]++;
}
}
public function outputData() {
echo "\n\nBot Rows Deleted: $this->bots\n";
echo "Duplicate User Rows: $this->repeatUsers\n";
echo "Total Rows: $this->rows\n";
$this->outputStats($this->stats);
}
public function outputStats($stats) {
foreach($stats as $key => $val) {
if(is_array($val)) {
echo "\n$key\n";
$this->outputStats($val);
} else {
echo "$key: $val\n";
}
}
}
}
// run the class
new processCSV('/Users/jj/Dropbox/mamp/sites/user-agent-csv/csv/cme/');