-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
92 lines (72 loc) · 2.52 KB
/
import.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
<?php
//try to establish a database connection
try {
//create new database object
$DbConn = new PDO(
'mysql:http://danu6.it.nuigalway.ie//;port=3306;dbname=mydb2463;',
'mydb2463ca',
'mi3tax',
array(PDO::ATTR_PERSISTENT => false)
);
//make the database layer throw exceptions on error
$DbConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$DbConn->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true );
$DbConn->setAttribute( PDO::ATTR_EMULATE_PREPARES, true );
$DbConn->query('SET NAMES "utf8"');
}
catch (Exception $e) {}
$dir ="/home/danu_ca9/files/";
$a = scandir($dir);
foreach ($a as $key => $value) {
$pos = strpos($value, '.jsonld');
if ($pos === false) {
//nothing
}
else
{
//echo $value;
$ext = pathinfo($value, PATHINFO_EXTENSION);
if($ext=='jsonld')
{
$file_contents = file_get_contents($dir.$value);
$json= json_decode($file_contents, true);
$keywords = $json[0]['keywords'];
$csv = $json[0]['distribution'][1]['contentUrl'];
$js = $json[0]['distribution'][0]['contentUrl'];
$keywordsStr = "";
foreach ($keywords as $keywordsK => $keywordsV) {
$keywordsStr .= $keywordsV . " ";
}
$keywordsStr = strtolower( $keywordsStr );
$keywordsStr = preg_replace('/[^a-z0-9]+/i', ' ', $keywordsStr);
$keywordsStr = preg_replace('/\s+/', ' ',$keywordsStr);
//insert
try {
$STMT = $DbConn->prepare(
"INSERT INTO DatasetInfo (
keyword,
content,
csvLink,
jsonLink
) VALUES (
:keyword,
:content,
:csvLink,
:jsonLink
)"
);
$STMT->bindParam( 'keyword', $keywordsStr , PDO::PARAM_STR );
$STMT->bindParam( 'content', $file_contents, PDO::PARAM_STR );
$STMT->bindParam('csvLink', $csv, PDO::PARAM_STR);
$STMT->bindParam('jsonLink', $js, PDO::PARAM_STR);
$STMT->execute();
}
catch (Exception $e) {
print ( print_r( $e,true));exit();
}
print $dir.$value."\n";
//print ( print_r( $keywordsStr,true));exit();
}
}
}
?>