-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpZoteroEntries.php
258 lines (225 loc) · 8.17 KB
/
phpZoteroEntries.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
<?php
require_once('phpZoteroEntry.php');
class phpZoteroEntries {
/**
* $entries An array of the zotero entries, hashed by their URI
*/
public $entries = array();
/**
* $arcConf a configuration array for ARC2 (e.g., namespace declarations)
*/
public $arcConf;
public function __construct($feed = false) {
if ($feed) {
$this->addEntriesFromFeed($feed);
}
$this->arcConf = array(
'ns' => array(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'bibo' => 'http://http://purl.org/ontology/bibo/',
'dcterms' => 'http://purl.org/dc/terms/',
'po' => 'http://purl.org/ontology/po/',
'doap' => 'http://usefulinc.com/ns/doap#',
'sioct' => 'http://rdfs.org/sioc/types#',
'sc' => 'http://umbel.org/umbel/sc/',
'address' => 'http://schemas.talis.com/2005/address/schema#',
'marcrel' => 'http://id.loc.gov/vocabulary/relators',
'z' => 'http://www.zotero.org/namespaces/export#'
)
);
}
/**
* addEntriesFromFeed adds all the entries from a zotero api feed to the collection
* @param $feed either a DOMDocument or string xml of the feed
*/
public function addEntriesFromFeed($feed) {
if (is_string($feed)) {
$dom = new DOMDocument();
//cleanup GET param separators in the links in the feed
$feed = str_replace('&', '&', $feed);
$dom->loadXML($feed);
} else if( get_class($feed) == 'DOMDocument' ) {
$dom = $feed;
//$newFeedNode = $dom->importNode($feed, true);
//$dom->appendChild($newFeedNode);
} else {
throw new Exception('Entry must be either an XML string or an ATOM feed DOMNode');
}
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('zxfer', 'http://zotero.org/ns/transfer');
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$entryNodes = $xpath->query('//atom:entry');
for($i=0 ; $i<$entryNodes->length; $i++) {
$newEntry = new phpZoteroEntry($entryNodes->item($i));
$this->entries[$newEntry->itemUri] = $newEntry;
}
}
/**
* addEntry adds a single entry from a zotero api feed to the collection
* @param $entry either a DOMNode or an xml string of the node
*/
public function addEntry($entry) {
$newEntry = new phpZoteroEntry($entry);
$this->entries[$newEntry->itemUri] = $newEntry;
}
/**
* getEntries get all the entries for the collection
* @param $flatten whether to flatten the URI hash to just the values (entries)
* @return array the array of entries, either URI-hashed or flattened
*/
public function getEntries($flatten = false) {
if($flatten) {
return array_values($this->entries);
}
return $this->entries;
}
/**
* getEntriesByType get all the entries of a particular zotero type
* @param $type the type
* @param $flatten whether to flatten the URI hash to just the values (entries)
* @return array the array of entries, either URI-hashed or flattened
*/
public function getEntriesByType($type, $flatten = false) {
$retArray = array();
foreach($this->entries as $entry) {
if($entry->itemType == $type) {
if($flatten) {
$retArray[] = $entry;
} else {
$retArray[$entry->itemUri] = $entry;
}
}
}
return $retArray;
}
/**
* getTypes get all the types of items in the collection
* @return array the zotero types
*/
public function getTypes() {
$retArray = array();
foreach($this->entries as $uri=>$entry) {
$retArray[] = $entry->getItemType();
}
return array_unique($retArray);
}
/**
* getEntriesByFieldValue get the entries with a particular field-value pair
* @param $field the field
* @param $value the value
* @param $flatten whether to flatten the URI hash to just the values (entries)
* @return array the array of entries, either URI-hashed or flattened
*/
public function getEntriesByFieldValue($field, $value, $flatten = false) {
$retArray = array();
foreach($this->entries as $entry) {
if($entry->getFieldValue($field) == $value) {
if($flatten) {
$retArray[] = $entry;
} else {
$retArray[$entry->itemUri] = $entry;
}
}
}
return $retArray;
}
/**
* getEntryByUri get the entry with a particular URI
* @param $uri the uri
* @return phpZoteroEntry the entry
*/
public function getEntryByUri($uri) {
return $this->entries[$uri];
}
/**
* getEntriesGroupsByType return an array with the entries grouped by type
* @param $flatten whether to flatten the URI hash to just the values (entries)
* @return array the array of entries, either URI-hashed or flattened, nested in an array hashed by type
*/
public function getEntriesGroupedByType($flatten = false) {
$retArray = array();
foreach($this->entries as $entry) {
if (! isset($retArray[$entry->itemType])) {
$retArray[$entry->itemType] = array();
}
if($flatten) {
$retArray[$entry->itemType][] = $entry;
} else {
$retArray[$entry->itemType][$entry->itemUri] = $entry;
}
}
return $retArray;
}
/**
* getEntriesAsJson get all the entries as a JSON object
* @param $flatten whether to flatten the URI hash to just the values (entries)
* @return string json encoded string of the entries
*/
public function getEntriesAsJson($flatten = false) {
$retArray = array();
foreach($this->entries as $entry) {
if($flatten) {
$retArray[] = $entry->getEntryAsJson(false);
} else {
$retArray[$entry->itemUri] = $entry->getEntryAsJson(false);
}
}
if($flatten) {
return json_encode($retArray);
}
return json_encode($retArray, true);
}
public function getUris() {
return array_keys($this->entries);
}
/**
* getRdf get the full BIBO RDF serialization of the collection of entries
* @param $format the serialization format
* @throws Exception if PHP_ZOTERO_ENTRIES_ARC_PATH is not defined
*/
public function getRdf($format = 'rdf/xml') {
if( ! defined('PHP_ZOTERO_ENTRIES_ARC_PATH') ) {
throw new Exception('PHP_ZOTERO_ENTRIES_ARC_PATH must be defined and valid to use RDF methods');
}
require_once(PHP_ZOTERO_ENTRIES_ARC_PATH);
switch($format) {
case 'rdf/xml':
$ser = ARC2::getRDFXMLSerializer($this->arcConf);
break;
case 'rdf/json':
$ser = ARC2::getRDFJsonSerializer($this->arcConf);
break;
case 'turtle':
$ser = ARC2::getTurtleSerializer($this->arcConf);
break;
case 'ntriples':
$ser = ARC2::getNTriplesSerializer($this->arcConf);
break;
}
return $ser->getSerializedIndex($this->getArcIndex());
}
/**
* getArcIndex returns the ARC2 Index structure for the collection
* @throws Exception if PHP_ZOTERO_ENTRIES_ARC_PATH is not defined
* @return array ARC2 Index structure
*/
public function getArcIndex() {
if( ! defined('PHP_ZOTERO_ENTRIES_ARC_PATH') ) {
throw new Exception('PHP_ZOTERO_ENTRIES_ARC_PATH must be defined and valid to use RDF methods');
}
require_once(PHP_ZOTERO_ENTRIES_ARC_PATH);
$arcIndex = array();
foreach($this->entries as $entry) {
$arcIndex = array_merge($arcIndex, $entry->getArcIndex() );
}
return $arcIndex;
}
/**
* getNSMapJson get the namespace map for ARC2 structures as JSON
* @@return string json encoded object
*/
public function getNSMapJson() {
return json_encode($this->arcConf['ns'], true);
}
}