forked from mialondon/Share-what-you-see
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharewhatyousee_functions.php
315 lines (266 loc) · 10.4 KB
/
sharewhatyousee_functions.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
<?php
/*
* Functions for the Share What You See plugin
*
* File information:
* Contains functions to query the APIs for the terms provided
*
*/
/*
* Notes on import adaptations:
*
* Notes for Europeana:
* Some of the fields are less clean than one would like, but apparently that's
* down to the data providers
*
* Import written for: WordPress 3 for generic WordPress installs
*
*/
/*
* Needs adapting for SWYS ###
* ### also not to use the Europeana enrichment fields cos they weren't reliable
* Was: Specialised search box for Tag Duel - asks for period (time) and place
* Values may be free-text or come from the OpenSearch term list [Ian]
* Might be nice to add optional free-text search if it's likely to return lots of results
* TO DO: should give a venue list as typing one in would give mismatches
* TO DO: should have the ability to search by city (which means knowing which venues are in which city)
*/
function SWYSPrintSearchForm() {
?>
<h3>Search for something you saw...</h3><form method="post" action="">
<label class="" for="search_venue">What was the venue</label>
<input name="search_venue" size="30" tabindex="1" value="" id="search_venue" autocomplete="on" type="text"><br />
<!-- <label class="" for="search_accession">Do you know the accession number?</label>
<input name="search_accession" size="30" tabindex="2" value="" id="search_accession" autocomplete="on" type="text"><br />-->
<label class="" for="search_title">Or maybe the object/artwork title?</label>
<input name="search_title" size="30" tabindex="2" value="" id="search_title" autocomplete="on" type="text"><br />
<label class="" for="search_term">Or just try a general search</label>
<input name="search_term" size="30" tabindex="3" value="" id="search_term" autocomplete="on" type="text"><br />
<input type="submit" name="search" value="Go!" />
</form>
<?php
}
/*
* Inputs: $terms is the search terms given in the box, $mode is display or import
*/
function SWYSGetEuropeanaSearchResults($search_terms,$search_place,$search_time,$mode,$search_sources) {
$api_provider;
$type;
// get site options ## but IIRC this don't work right now
$mmg_import_Europeana_API_key = $mmg_import_options_arr["mmg_import_Europeana_API_key"];
$mmg_import_Europeana_API_URL = $mmg_import_options_arr["mmg_import_Europeana_API_URL"];
// ### to do re-write later to get rid of the case statements
switch ($search_sources) {
case 'Europeana':
$type = 'xml';
$api_provider = 'Europeana'; // ### will presumably need to be the source provider? Check the field
$url = 'http://api.europeana.eu/api/opensearch.rss' . '?searchTerms=';
// the search seems to deal with extra '&' so am doing that rather than conditional crap
if (!empty($search_terms)) {
$url .= $search_terms . '&';
}
if (!empty($search_)) {
$url .= 'enrichment_period_term:' . $search_time .'&';
}
if (!empty($search_place)) {
$url .= 'enrichment_place_label:' . $search_place;
}
$url .= '&wskey=' . 'XADNJCFGME';
// $url = $mmg_import_Europeana_API_URL . '?searchTerms=' . $search_terms . '&enrichment_period_term=' . $search_time . '&enrichment_place_label=' . $search_place . '&wskey=' . $mmg_import_Europeana_API_key; // ### not getting the settings?
echo $url;
// could also look for completeness (<europeana:completeness>) values 1 - 10
break;
}
echo "<br />Loading list file for your search... <br />";
//echo $url;
$ch = curl_init(); // relies on curl
curl_setopt($ch, CURLOPT_USERAGENT, 'API client');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
if (!empty($output)) {
switch ($type) {
case 'xml':
if ($search_sources == 'Europeana') {
$rss = simplexml_load_string($output);
$data = $rss->channel;
//$atom = $rss->channel;
//$data = $atom->children('atom');
}
break;
}
}
if (!empty($data)) {
if ($mode == 'display') {
echo '<ul>';
foreach($data->entry as $e){
echo '<li><a href="' . $e->link[0]['href'] .
'">'.$e->title.'</a></li>';
}
echo '</ul>';
//echo '<pre>';
//print_r($data);
//echo '</pre>';
} else {
// this will be custom field etc stuff in WordPress
// process the file
// ### this needs to be updated for SWYS as not importing but displaying back to screen
if( sizeof($data) > 0 ){
$i;
echo 'Loading records into database...';
switch ($search_sources) {
case 'Europeana':
$terms = 'Keyword='.$search_terms.'&Place='.$search_place.'&Time='.$search_time; // for reference later
//echo '<pre>'.print_r($data).'</pre>';
$i = mmgDoEuropeanaImportXML($data,$terms); // ### change this
break;
}
//echo "Building SQL string... ";
echo '<p>'.$i.' objects loaded.</p>';
} else {
echo 'Return size apparently < 0'; // April 2011 ###
}
}
} else {
echo '<br />No results found for that search term. Try again!';
}
}
function mmgDoEuropeanaImportXML($data,$terms) {
$i;
//echo 'hello';
// Iterate over each <item> in the channel element.
echo '<pre>'.print_r($data).'</pre>';
echo 'size: '.sizeof($data->item) ;
foreach($data->item as $item) {
echo 'in loop<br />';
// foreach($data->channel->item as $doc) {
// Now we need to fetch the URL identified in the <link> element
$ch = curl_init(); // relies on curl
curl_setopt($ch, CURLOPT_USERAGENT, 'API client');
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_URL, $doc->link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resultdoc = curl_exec($ch);
curl_close($ch);
// Check that the link element really returned us something
if ( !empty($resultdoc) ) {
// Parse what was returned
$xmldoc = simplexml_load_string($resultdoc);
// process rss for title, maker/artist (if we can get it), date, description, image/thumbnail, provider
}
}
}
function mmgInsertObject($object_name,$accession_number,$api_provider, $data_source_url, $source_display_url, $description, $date_earliest, $date_latest, $interpretative_date, $interpretative_place, $image_url, $terms) {
global $wpdb;
if (!empty($image_url)) {
// debug echo '<h2>in insert statement</h2>';
$sqlresult = $wpdb->query( $wpdb->prepare( "
INSERT IGNORE INTO ". db_objecttable . "
(".db_objecttablefields.")
VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )" ,
array( $object_name,$accession_number,$api_provider, $data_source_url, $source_display_url, $description, $date_earliest, $date_latest, $interpretative_place, $interpretative_place, $image_url, $terms ) ) );
if ( $sqlresult == 0 ) {
echo 'OK <br/>';
}
else {
// echo 'Error : ' . mysql_errno() . ' ' . mysql_error() . $object_name . '<br/>';
// echo $object_name . '-' . $accession_number . '-' . $api_provider . '-' . $data_source_url. '-' . $source_display_url . '-' . $description . '-de:' . $date_earliest . '-dl:' . $date_latest. '-id:' . $interpretative_date. '-ip:' . $interpretative_place . '-' . $image_url . '-' . $terms . '.<br/>';
}
}
else {
echo 'Item has no image....';
}
}
function createSWYSPost($europeana_url) {
$xml = file_get_contents($europeana_url);
// Create DOMDocument and load the xml to parse
$doc = new DOMDocument();
$doc->loadXML($xml);
// Create DOMXPath object so we can use XPath queries
$xpath = new DOMXPath($doc);
$xpath->registerNameSpace('srw', 'http://www.loc.gov/zing/srw/');
$xpath->registerNameSpace('europeana', 'http://www.europeana.eu');
$xpath->registerNameSpace('dc', 'http://purl.org/dc/elements/1.1/');
$xpath->registerNameSpace('dcterms', 'http://purl.org/dc/terms/');
$records = $doc->getElementsByTagName("dc");
$xpath_description = "./dc:description/text()";
$xpath_title = "./dc:title/text()";
$xpath_provider = "./dcterms:isPartOf/text()";
$xpath_image = "./europeana:object/text()";
foreach($records as $record) {
$nodeList_title = $xpath->evaluate($xpath_title,$record);
if ($nodeList_title->length > 0) {
$content_title = $nodeList_title->item(0)->nodeValue;
$title = $content_title;
} else {
$title = "UNKNOWN TITLE";
}
$nodeList_description = $xpath->evaluate($xpath_description,$record);
if ($nodeList_description->length > 0) {
$content_description = $nodeList_description->item(0)->nodeValue;
$description = $content_description;
} else {
$description = "NO DESCRIPTION";
}
$nodeList_provider = $xpath->evaluate($xpath_provider,$record);
if ($nodeList_provider->length > 0) {
$content_provider = $nodeList_provider->item(0)->nodeValue;
$provider = $content_provider;
} else {
$provider = "UNKNOWN PROVIDER";
}
$nodeList_image = $xpath->evaluate($xpath_image,$record);
if ($nodeList_image->length > 0) {
$content_image = $nodeList_image->item(0)->nodeValue;
$image = $content_image;
} else {
$image = "";
}
}
$content = "";
if (!empty($image)) {
$content .= "<a href=\"".$image."\" alt=\"".$title."\" />";
}
if (!empty($title)) {
$content .= "<strong>Title:</strong> ".$title."<br />";
}
if (!empty($description)) {
$content .= "<strong>Description:</strong> ".$description."<br />";
}
$new_post = array(
'post_title' => $title,
'post_content' => convert_chars($content)
//Default field values will do for the rest - so we don't need to worry about these - see http://codex.wordpress.org/Function_Reference/wp_insert_post
);
$post_id = wp_insert_post($new_post);
if (is_object($post_id)) {
//error - what to do?
return false;
}
elseif ($post_id == 0) {
//error - what to do?
return false;
}
else {
add_post_meta($post_id, 'object_title', $title);
// add_post_meta($post_id, 'object_maker', $maker);
// add_post_meta($post_id, 'object_date', $date);
add_post_meta($post_id, 'object_provider', $provider);
//other custom fields here if required
}
return $post_id;
}
function getSingleValue($document,$xpath) {
$result;
$nodelist = $document->xpath($xpath);
echo "nl: $nodelist <br/>";
if ( count($nodelist) > 0 ) {
$result = $nodelist[0];
}
else {
$result = "";
}
return $result;
}
?>