-
Notifications
You must be signed in to change notification settings - Fork 40
/
export.php
executable file
·88 lines (75 loc) · 1.95 KB
/
export.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
<?php
/* Exports database as CSV
*
* NB: If you are crawling a large website (> 65,000 URLs) Excel cannot open the file, however Access can
*
* @package Crawler
*/
/**
* Include necessary files
*/
include('config.php');
include('includes/functions.php');
include('includes/mysql_functions.php');
/**
* Set the content headers
*/
header('Content-type: application/CSV');
header("Content-Disposition: attachment; filename=export.csv");
/**
* SQL query to generate our results
*/
$pages = mysql_query("SELECT url, title, clicks, http_code, crawl_tag, size, type, modified, (SELECT count(*) FROM links WHERE `to` = urls.ID) as incoming, (SELECT count(*) FROM links WHERE `to` = urls.ID) as outgoing from urls");
/**
* Count the number of pages in our dataset
*/
$count = mysql_num_rows($pages);
/**
* Get array of fields by parsing keys of first row array
*
* NOTE TO SELF: There is a better way to do this
*/
$fields = array_keys(mysql_fetch_assoc($pages));
/**
* Print the header row and a new line charecter
*/
foreach ($fields as $field) {
echo "$field\t";
}
echo "\n";
/**
* When we looped through to grab the field names, we moevd the internal pointer.
* Reset internal pointer so our loop includes the first row
*/
mysql_data_seek($pages,0);
/**
* Loop through the rows (pages)
*/
for ($i=0; $i < $count; $i++) {
/**
* Fetch the row as an associative array
*/
$page = mysql_fetch_assoc($pages);
/**
* Loop through each field within the row
*/
foreach ($page as $key=>$field) {
/**
* If it the 'size', or 'modified' field, make it human readible, otherwise just output
*/
switch($key) {
case 'size':
echo file_size($field);
break;
case 'modified':
if (!is_null($field)) echo date('Y-m-d H:i:s',$field);
break;
default:
echo $field;
break;
} //End switch
echo "\t";
} //End Field
echo "\n";
} //End Row
?>