forked from zotero/dataserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelations.inc.php
315 lines (266 loc) · 8.56 KB
/
Relations.inc.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2010 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
class Zotero_Relations extends Zotero_DataObjects {
protected static $ZDO_object = 'relation';
protected static $primaryFields = array(
'id' => 'relationID',
'libraryID' => '',
'key' => '',
'subject' => '',
'predicate' => '',
'object' => ''
);
public static $relatedItemPredicate = 'dc:relation';
public static $linkedObjectPredicate = 'owl:sameAs';
public static $deletedItemPredicate = 'dc:replaces';
private static $namespaces = array(
"dc" => 'http://purl.org/dc/elements/1.1/',
"owl" => 'http://www.w3.org/2002/07/owl#'
);
public static function get($libraryID, $relationID, $existsCheck=false) {
$relation = new Zotero_Relation;
$relation->libraryID = $libraryID;
$relation->id = $relationID;
return $relation;
}
/**
* @return Zotero_Relation[]
*/
public static function getByURIs($libraryID, $subject=false, $predicate=false, $object=false) {
if ($predicate) {
$predicate = implode(':', self::_getPrefixAndValue($predicate));
}
if (!is_int($libraryID)) {
throw new Exception('$libraryID must be an integer');
}
if (!$subject && !$predicate && !$object) {
throw new Exception("No values provided");
}
$sql = "SELECT relationID FROM relations WHERE libraryID=?";
$params = array($libraryID);
if ($subject) {
$sql .= " AND subject=?";
$params[] = $subject;
}
if ($predicate) {
$sql .= " AND predicate=?";
$params[] = $predicate;
}
if ($object) {
$sql .= " AND object=?";
$params[] = $object;
}
$rows = Zotero_DB::columnQuery(
$sql, $params, Zotero_Shards::getByLibraryID($libraryID)
);
if (!$rows) {
return array();
}
$toReturn = array();
foreach ($rows as $id) {
$relation = new Zotero_Relation;
$relation->libraryID = $libraryID;
$relation->id = $id;
$toReturn[] = $relation;
}
return $toReturn;
}
public static function getSubject($libraryID, $subject=false, $predicate=false, $object=false) {
$subjects = array();
$relations = self::getByURIs($libraryID, $subject, $predicate, $object);
foreach ($relations as $relation) {
$subjects[] = $relation->subject;
}
return $subjects;
}
public static function getObject($libraryID, $subject=false, $predicate=false, $object=false) {
$objects = array();
$relations = self::getByURIs($libraryID, $subject, $predicate, $object);
foreach ($relations as $relation) {
$objects[] = $relation->object;
}
return $objects;
}
public static function makeKey($subject, $predicate, $object) {
return md5($subject . " " . $predicate . " " . $object);
}
public static function add($libraryID, $subject, $predicate, $object) {
$predicate = implode(':', self::_getPrefixAndValue($predicate));
$relation = new Zotero_Relation;
$relation->libraryID = $libraryID;
$relation->subject = $subject;
$relation->predicate = $predicate;
$relation->object = $object;
$relation->save();
}
public static function eraseByURIPrefix($libraryID, $prefix, $ignorePredicates=false) {
Zotero_DB.beginTransaction();
$prefix = $prefix . '%';
$sql = "SELECT relationID FROM relations WHERE libraryID=? AND subject LIKE ?";
$params = [$libraryID, $prefix];
if ($ignorePredicates) {
foreach ($ignorePredicates as $ignorePredicate) {
$sql .= " AND predicate != ?";
$params[] = $ignorePredicate;
}
}
$sql .= " UNION SELECT relationID FROM relations WHERE libraryID=? AND object LIKE ?";
$params = array_merge($params, [$libraryID, $prefix]);
if ($ignorePredicates) {
foreach ($ignorePredicates as $ignorePredicate) {
$sql .= " AND predicate != ?";
$params[] = $ignorePredicate;
}
}
$ids = Zotero_DB::columnQuery(
$sql, $params, Zotero_Shards::getByLibraryID($libraryID)
);
foreach ($ids as $id) {
$relation = self::get($libraryID, $id);
Zotero_Relations::delete($libraryID, $relation->key);
}
Zotero_DB::commit();
}
/**
* Delete any relations that have the URI as either the subject
* or the object
*/
public static function eraseByURI($libraryID, $uri, $ignorePredicates=false) {
Zotero_DB::beginTransaction();
$sql = "SELECT relationID FROM relations WHERE libraryID=? AND subject=?";
$params = [$libraryID, $uri];
if ($ignorePredicates) {
foreach ($ignorePredicates as $ignorePredicate) {
$sql .= " AND predicate != ?";
$params[] = $ignorePredicate;
}
}
$sql .= " UNION SELECT relationID FROM relations WHERE libraryID=? AND object=?";
$params = array_merge($params, [$libraryID, $uri]);
if ($ignorePredicates) {
foreach ($ignorePredicates as $ignorePredicate) {
$sql .= " AND predicate != ?";
$params[] = $ignorePredicate;
}
}
$ids = Zotero_DB::columnQuery(
$sql, $params, Zotero_Shards::getByLibraryID($libraryID)
);
if ($ids) {
foreach ($ids as $id) {
$relation = self::get($libraryID, $id);
Zotero_Relations::delete($libraryID, $relation->key);
}
}
Zotero_DB::commit();
}
public static function purge($libraryID) {
$sql = "SELECT subject FROM relations "
. "WHERE libraryID=? AND predicate!=? "
. "UNION "
. "SELECT object FROM relations "
. "WHERE libraryID=? AND predicate!=?";
$uris = Zotero.DB.columnQuery(
$sql,
array(
$libraryID,
self::$deletedItemPredicate,
$libraryID,
self::$deletedItemPredicate
),
Zotero_Shards::getByLibraryID($libraryID)
);
if ($uris) {
$prefix = Zotero_URI::getBaseURI();
Zotero_DB::beginTransaction();
foreach ($uris as $uri) {
// Skip URIs that don't begin with the default prefix,
// since they don't correspond to local items
if (strpos($uri, $prefix) === false) {
continue;
}
if (preg_match('/\/items\//', $uri) && !Zotero_URI::getURIItem($uri)) {
self::eraseByURI($uri);
}
if (preg_match('/\/collections\//', $uri) && !Zotero_URI::getURICollection($uri)) {
self::eraseByURI($uri);
}
}
Zotero_DB::commit();
}
}
/**
* Converts a DOMElement item to a Zotero_Relation object
*
* @param DOMElement $xml Relation data as DOM element
* @param Integer $libraryID
* @return Zotero_Relation Zotero relation object
*/
public static function convertXMLToRelation(DOMElement $xml, $userLibraryID) {
$relation = new Zotero_Relation;
$libraryID = $xml->getAttribute('libraryID');
if ($libraryID) {
$relation->libraryID = $libraryID;
}
else {
$relation->libraryID = $userLibraryID;
}
$subject = $xml->getElementsByTagName('subject')->item(0)->nodeValue;
$predicate = $xml->getElementsByTagName('predicate')->item(0)->nodeValue;
$object = $xml->getElementsByTagName('object')->item(0)->nodeValue;
if ($predicate == 'dc:isReplacedBy') {
$relation->subject = $object;
$relation->predicate = 'dc:replaces';
$relation->object = $subject;
}
else {
$relation->subject = $subject;
$relation->predicate = $predicate;
$relation->object = $object;
}
return $relation;
}
/**
* Converts a Zotero_Relation object to a SimpleXMLElement item
*
* @param object $item Zotero_Relation object
* @return SimpleXMLElement Relation data as SimpleXML element
*/
public static function convertRelationToXML(Zotero_Relation $relation) {
return $relation->toXML();
}
private static function _getPrefixAndValue($uri) {
$parts = explode(':', $uri);
if (isset($parts[1])) {
if (!self::$namespaces[$parts[0]]) {
throw ("Invalid prefix '{$parts[0]}'");
}
return $parts;
}
foreach (self::$namespaces as $prefix => $val) {
if (strpos($uri, $val) === 0) {
$value = substr($uri, strlen($val) - 1);
return array($prefix, $value);
}
}
throw new Exception("Invalid namespace in URI '$uri'");
}
}
?>