-
Notifications
You must be signed in to change notification settings - Fork 0
/
LitDb.php
258 lines (232 loc) · 6.58 KB
/
LitDb.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
<?php
##########################################################
# Testeley - Testing the Mendeley API
#
# Class: LitDb.php
#
# Connection class to interact with a local literature
# database. The database holds record about scientific
# papers of type Paper.php and similarities between
# papers (pairs of uuids). This class provides methods
# to query these tables by uuids (Mendeley unique ids).
#
# Tables:
# doc (uuid, title, authors, journal, year, pmid, mendeley_url)
# sim (uuid1, uuid2)
#
# HS 2011-06-14
##########################################################
require_once('Paper.php');
require_once('Mendeley.php');
require_once('functions.inc.php');
class LitDb {
### Member variables ###
var $dbPath; # path to the sqlite database
var $db; # the database handle
### Constructor ###
# Open a new database connection
function __construct($dbPath){
$this->dbPath = $dbPath;
try {
//create or open the database
$this->db = new SQLiteDatabase($dbPath, 0666, $error);
}
catch(Exception $e) {
die($error);
}
}
### Static functions ###
# creates a new sqlite database with the required tables
# Warning: overwrites existing database
function create() {
$query = 'CREATE TABLE '.TBL_PAPERS.
' (uuid TEXT, title TEXT, authors TEXT, journal TEXT, year INTEGER, pmid INTEGER, mendeley_url TEXT);' .
'CREATE TABLE '.TBL_SIM.
' (uuid1 TEXT, uuid2 TEXT);';
if(!$this->db->queryExec($query, $error)) {
die($error);
} else {
echo 'Database created';
}
}
# delete all records from the database
function delete() {
$query = 'DELETE FROM '.TBL_PAPERS.'; '.
'DELETE FROM '.TBL_SIM.';';
if(!$this->db->queryExec($query, $error)) {
die($error);
} else {
echo 'Database deleted.';
}
}
### Class functions ###
# returns whether this paper is already in the db
function in_db($uuid) {
return $this->from_db($uuid);
}
# stores the given paper record in the db
function to_db($p) {
$query = sprintf(
"INSERT INTO %s (uuid, title, authors, journal, year, pmid, mendeley_url) " .
"VALUES ('%s', '%s', '%s', '%s', %d, %d, '%s'); ",
TBL_PAPERS,
sqlite_escape_string($p->uuid),
sqlite_escape_string($p->title),
json_encode($p->authors),
sqlite_escape_string($p->journal),
sqlite_escape_string($p->year),
sqlite_escape_string($p->pmid),
is_mendeley_url($p->mendeley_url)?$p->mendeley_url:"");
if(!$this->db->queryExec($query, $error)) {
echo $query."<br>";
die($error);
} else {
#echo "<br>".$query;
}
}
# retrieve paper from db by uuid
function from_db($uuid) {
is_uuid($uuid) or die('invalid uuid');
$query = "SELECT * FROM ".TBL_PAPERS." WHERE uuid='".$uuid."';";
if($result = $this->db->query($query, SQLITE_BOTH, $error)) {
if($row = $result->fetch()) {
$p = new Paper($uuid);
$p->title = $row['title'];
$p->authors = json_decode($row['authors'],true);
$p->journal = $row['journal'];
$p->year = $row['year'];
$p->pmid = $row['pmid'];
$p->mendeley_url = $row['mendeley_url'];
return $p;
} else {
return false;
}
}
else {
die($error);
}
}
# retrieve all papers from db
# return an array of papers
function get_all_papers() {
//read data from database
$query = "SELECT uuid FROM ".TBL_PAPERS;
$papers = Array();
if($result = $this->db->query($query, SQLITE_BOTH, $error)) {
while($row = $result->fetch()) {
$uuid = $row['uuid'];
$papers[] = $this->from_db($uuid);
}
return $papers;
}
else {
die($error);
}
}
# retrieve all similarities from db
# return an array of pairs
function get_all_sim() {
$query = "SELECT * FROM ".TBL_SIM;
$pairs = Array();
if($result = $this->db->query($query, SQLITE_BOTH, $error)) {
while($row = $result->fetch()) {
if(isset($row['uuid1']) && isset($row['uuid1'])) {
$uuid1 = $row['uuid1'];
$uuid2 = $row['uuid2'];
$pairs[] = array($uuid1,$uuid2);
}
}
return $pairs;
}
else {
die($error);
}
return $pairs;
}
# retrieve similar documents for the given uuid
# returns an array of uuids
function get_related_from_db($uuid) {
is_uuid($uuid) or die('invalid uuid');
$query = "SELECT uuid2 FROM ".TBL_SIM." WHERE uuid1='.$uuid.'";
$uuids = Array();
if($result = $this->db->query($query, SQLITE_BOTH, $error)) {
while($row = $result->fetch()) {
if(isset($row['uuid']) && is_uuid($row['uuid']))
$uuids[] = $row['uuid'];
}
} else die($error);
return uuids;
}
# returns whether this similarity is already in the db
function sim_in_db($uuid1, $uuid2) {
is_uuid($uuid1) or die('invalid uuid1');
is_uuid($uuid2) or die('invalid uuid2');
$query = sprintf("SELECT uuid1, uuid2 FROM %s WHERE uuid1='%s' AND uuid2='%s';", TBL_SIM, $uuid1, $uuid2);
if($result = $this->db->query($query, SQLITE_BOTH, $error)) {
if($row = $result->fetch()) return true;
else return false;
} else {
die($error);
}
}
# adds a similarity relation between two papers to the db
# does nothing if the relation already exists
function sim_to_db($uuid1, $uuid2) {
is_uuid($uuid1) or die('invalid uuid1');
is_uuid($uuid2) or die('invalid uuid2');
if(!$this->sim_in_db($uuid1, $uuid2)) {
$query = sprintf("INSERT INTO %s (uuid1, uuid2) VALUES ('%s', '%s');",TBL_SIM, $uuid1,$uuid2);
if(!$this->db->queryExec($query, $error)) {
die($error);
}
}
}
/**
* Returns the number of papers in the local database.
* Enter description here ...
*/
function get_num_papers() {
return count($this->get_all_papers());
}
/**
* Returns the number of similarities in the local database.
*/
function get_num_sim() {
return count($this->get_all_sim());
}
/**
* Prints the number of papers and similarities.
*/
function print_db_stats() {
echo 'Documents: '.$this->get_num_papers();
echo '<br>';
echo 'Similarities: '.$this->get_num_sim();
echo '<br>';
}
/**
* Find for all papers in the local database the related papers and add them to the database.
* Warning: This function does a lot of API calls.
*/
function expand() {
$mendeley = new Mendeley(API_KEY);
$docs_in_db = $this->get_all_papers();
foreach($docs_in_db as $doc) {
$uuid1 = $doc->uuid;
$uuids = $mendeley->get_related_uuids($uuid1);
foreach($uuids as $uuid2) {
if(!$this->in_db($uuid2)) {
$paper2 = $mendeley->get_document($uuid2);
$this->to_db($paper2);
}
if(!$this->sim_in_db($uuid1, $uuid2)) {
$this->sim_to_db($uuid1, $uuid2);
//echo '.';
//flush();
}
}
//echo '<br>';
//flush();
}
}
}
?>