-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.php
150 lines (110 loc) · 3.75 KB
/
install.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
<?php
include("config.php");
// Create connection with database
$connection = mysql_connect($sql_address, $sql_username, $sql_password);
if(!$connection){
die('Check your config.php file!');
}
// Attempt to create database
if(mysql_query("CREATE DATABASE $sql_database")) {
echo "MySQL database created\n";
}else{
echo "MySQL databse already exists\n";
}
// Select working database
mysql_select_db($sql_database);
// Drop all tables - in case they had already been created
mysql_query("DROP TABLE IF EXISTS spx_daily");
mysql_query("DROP TABLE IF EXISTS recommendations");
mysql_query("DROP TABLE IF EXISTS websites");
//mysql_query("DROP TABLE IF EXISTS APPLshortterm_com");
//mysql_query("DROP TABLE IF EXISTS *");
/* query all tables */
$found_tables = array();
$sql = "SHOW TABLES FROM $sql_database";
if($result = mysql_query($sql)){
/* add table name to array */
while($row = mysql_fetch_row($result)){
$found_tables[]=$row[0];
}
}
else{
die("Error, could not list tables. MySQL Error: " . mysql_error());
}
/* loop through and drop each table */
foreach($found_tables as $table_name){
$sql = "DROP TABLE $sql_database.$table_name";
if($result = mysql_query($sql)){
echo "Success - table $table_name deleted.";
} else{
echo "Error deleting $table_name. MySQL Error: " . mysql_error() . "";
}
}
// Create tables
$res = mysql_query("CREATE TABLE spx_daily (
date DATE,
price DOUBLE
)");
if(!$res) die(mysql_error()."\n");
$res = mysql_query("CREATE TABLE recommendations (
symbol VARCHAR(8),
date DATE,
direction TINYINT,
url TEXT
)");
if(!$res) die(mysql_error()."\n");
$res = mysql_query("CREATE TABLE websites (
url TEXT
)");
if(!$res) die(mysql_error()."\n");
mysql_query('CREATE INDEX spx_date_index ON spx_daily (date)');
mysql_query('CREATE INDEX symbol_index ON recommendations (symbol)');
mysql_query('CREATE INDEX symbol_date_index ON recommendations (symbol,date)');
mysql_query('CREATE INDEX date_index ON recommendations (date)');
// Fill spx_daily table
echo "Loading spx_daily...\n";
$spxFile = fopen("data/spx.csv", "r");
while(feof($spxFile)==false){
$line = explode(",",fgets($spxFile));
if(count($line)==2)
mysql_query("INSERT INTO spx_daily VALUES ('$line[0]', '$line[1]')");
}
fclose($spxFile);
$spxCount = mysql_fetch_array(mysql_query('SELECT count(*) FROM spx_daily'));
echo "Added $spxCount[0] rows...\n";
// Fill recommendations table
echo "Loading recommendations... (be patient)\n";
$recommendations = fopen("data/full_recommendations.csv", "r");
// Skip 1 line
fgets($recommendations);
while(feof($recommendations)==false){
$line = fgets($recommendations);
$line = str_replace("\"", "", $line);
$line = explode(",", $line);
if(count($line)==4) {
$line[2] = ($line[2][0]=='p')?1:-1;
mysql_query("INSERT INTO recommendations VALUES ('$line[0]', '$line[1]', '$line[2]', '$line[3]')");
}
}
fclose($recommendations);
$recCount = mysql_fetch_array(mysql_query('SELECT count(*) FROM recommendations'));
echo "Added $recCount[0] rows...\n";
//fill the websites table
echo "Loading websites...\n";
$query = mysql_query("SELECT DISTINCT url FROM recommendations");
$urlList = array();
reset($urlList);
while($result = mysql_fetch_array($query)) {
$linkArray = parse_url($result['url']);
$url = $linkArray['host'];
$replacement = "";
$search = "www.";
$url = str_replace($search, $replacement, $url);
if (isset($urlList[$url]) == FALSE){
$urlList[$url] = $url;
mysql_query("INSERT INTO websites VALUES ('$url')");
}
}
$urlCount = mysql_fetch_array(mysql_query('SELECT count(*) FROM websites'));
echo "Added $urlCount[0] rows...\n";
?>