/
get_semantic_data.class.php
772 lines (731 loc) · 23.5 KB
/
get_semantic_data.class.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
<?php
/**
* Ninuxoo 2.0
*
* PHP Version 5.3
*
* @copyright 2013-2014 Alessandro Gubitosi / Gubi (http://iod.io)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3
* @link https://github.com/gubi/Ninuxoo-2.0
*/
/**
* A class for search and get semantic data from Wikipedia and dbpedia
*
* This class search in Wikipedia API for a given term and returns the relative page name with its own common data taken with semantic SPARQL query
*
* @package Ninuxoo 2.0
* @author Alessandro Gubitosi <gubi.ale@iod.io>
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3
* @access public
* @link https://github.com/gubi/Ninuxoo-2.0/blob/master/common/include/classes/get_semantic_data.class.php
* @uses wikipedia.class.php Wikipedia class
* @uses EasyRdf.php EasyRdf
*/
class semantic_data{
/**
* Construct
*
* Initialize the class
*
* @global string $this->class_dir Current class directory
* @global string $this->lib_dir Lib directory, based on $this->class_dir
* @global string $this->startime The time in which the script starts
* @global string $this->wikipedia Wikipedia initialized class
* @global string $this->easyrdf EasyRdf initialized class
* @global string $this->regUrl Regex to parse URL
* @see semantic_data::start_time() Start time
* @see semantic_data::set_prefix() Set prefix
* @see wikipedia-::srlimit() Sr limit
* @see wikipedia-::srprop() Sr prop
* @return void
*/
function __construct() {
$this->class_dir = __DIR__;
$this->lib_dir = str_replace("classes", "lib", $this->class_dir);
require_once($this->class_dir . "/wikipedia.class.php");
require_once($this->lib_dir . "/easyrdf/lib/EasyRdf.php");
$this->startime = $this->start_time();
$this->wikipedia = new wikipedia();
$this->wikipedia->srlimit(1);
$this->wikipedia->srprop("");
$this->set_prefix();
$this->easyrdf = new EasyRdf_Sparql_Client("http://it.dbpedia.org/sparql");
$this->regUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
}
/**
* Indents a flat JSON string to make it more human-readable
*
* Taken from Dave Perrett blog
*
* @url http://www.daveperrett.com/articles/2008/03/11/format-json-with-php/
* @param string $json The original JSON string to process
* @return string Indented version of the original JSON string
* @access public
*/
private function indent($json) {
$result = "";
$pos = 0;
$strLen = strlen($json);
$indentStr = "\t";
$newLine = "\n";
$prevChar = "";
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
$result .= str_repeat($indentStr, $pos);
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}
/**
* Active debug status
*
* @param bool $status Is debug status (usually "true" when calling)
* @global $this->debug Debug status
* @access public
*/
public function debug($status) {
$this->debug = $status;
}
/**
* Set debug output
*
* @param string $out `array`, `object` or `all` for print each one (default `all`).<br>You can combine with format() using `html` for html output inside formatted values
* @global $this->output Debug output
* @access public
*/
public function output($out = "all") {
$this->output = $out;
}
/**
* Similar to output() but for API export data
*
* @param string $format `array`, `json`, `jsonp` or `html` (default is `json`)
* @global $this->format Display format
* @access public
*/
public function format($format = "json") {
$this->format = $format;
}
/**
* Start calculating execution time
*
* @return int $mtime The start of execution time
* @access public
*/
public function start_time() {
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
/**
* End calculating execution time
*
* @param int $startime The time start of execution
* @return int The end of execution time
* @access public
*/
public function end_time($startime) {
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
return round($mtime - $startime, 5);
}
/**
* Default semantic prefixes for Ninuxoo
*
* @param string $type The type of result to return: `semantic` or `array`
* @return string|array The Ninuxoo default semantic prefixes
* @access private
*/
private function get_default_prefixes($type = "array") {
$default_prefixes = array(
"dbo" => "http://dbpedia.org/ontology/",
"dbp" => "http://it.dbpedia.org/property/",
"dbpp" => "http://dbpedia.org/property/",
"dbr" => "http://dbpedia.org/resource/",
"dc" => "http://purl.org/dc/elements/1.1/",
"dcterms" => "http://purl.org/dc/terms/",
"event" => "http://purl.org/NET/c4dm/event.owl#",
"foaf" => "http://xmlns.com/foaf/0.1/",
"mo" => "http://purl.org/ontology/mo/",
"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
"tl" => "http://purl.org/NET/c4dm/timeline.owl#",
"xs" => "http://www.w3.org/2001/XMLSchema#",
"xsd" => "http://www.w3.org/2001/XMLSchema#"
);
switch($type) {
case "semantic":
foreach($default_prefixes as $owl => $url) {
$prefix .= "PREFIX " . $owl . ": <" . $url . ">\n";
}
return $prefix . "\n";
break;
case "array":
return $default_prefixes;
break;
}
}
/**
* Set EasyRdf prefixes
*
* @param bool $return_prefix_array Return or not the array of Ninuxoo semantic prefixes
* @see semantic_data::get_default_prefixes() Get default prefixes
* @return array|void The Ninuxoo default semantic prefixes
* @access private
*/
private function set_prefix($return_prefix_array = false) {
if($return_prefix_array) {
return $this->get_default_prefixes();
} else {
foreach($this->get_default_prefixes() as $owl => $url) {
EasyRdf_Namespace::set($owl, $url);
}
}
}
/**
* Check if file exists remotely
*
* @param string $url The url of file
* @return bool File exists
* @access private
*/
private function is_remote_file($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch) !==FALSE) {
return true;
} else {
return false;
}
}
/**
* Check if given string is a link
*
* @param string $url String to be examined
* @return bool
* @access private
*/
private function is_url($url) {
return (preg_match($this->regUrl, $url) ? true : false);
}
/**
* Clean text for Wikipedia search
*
* @param string $text The url of file
* @return bool File exists
* @access private
*/
private function clean_text($text) {
$text = trim(preg_replace("/(disc)(\s+|)(\d+)|(cd)(\s+|)(\d+)/i", "", preg_replace("/(\[.*?\])|(\(.*?\))/i", "", str_replace("_", " ", $text))));
if(strpos($text, " ") === false) {
return trim($text);
} else {
foreach(explode(" ", $text) as $search_item) {
if(strlen($search_item) >= 2) {
$s[] = $search_item;
}
}
return implode(" ", $s);
}
}
/**
* Construct the semantic query OPTIONALS
*
* @param array $optional Array of optional semantic keys
* @return string Semantic query OPTIONALS part
* @access private
*/
private function construct_optional($optional) {
$nt = ($this->debug) ? "\n " : "";
if(is_array($optional) && count($optional) > 0) {
foreach($optional as $sk => $sv) {
$opt .= "OPTIONAL { ?item " . $sk . " ?" . $sv . " } . " . $nt;
}
} else {
$opt = "";
}
return $opt;
}
/**
* Create the semantic query
*
* @param string $owl Owl to query
* @param array $optional Array of optional semantic keys
* @param string $filter Results results by given label
* @param int $limit Limit the results to show
* @see semantic_data::debug() Debug
* @see semantic_data::construct_optional() Construct optionals
* @return string Semantic query
* @access private
*/
private function create_sematic_query($owl, $optional = array(), $filter = "", $limit = 1) {
$n = ($this->debug) ? "\n" : "";
$nt = ($this->debug) ? "\n " : "";
$query = "SELECT * WHERE {" . (trim($owl) ? $nt . "?item a dbo:" . $owl . " . " : "") . $nt . "?item rdfs:label ?label . " . $nt . "?item dbo:abstract ?abstract . " . $nt . $nt . $this->construct_optional($optional) . $nt . (!trim($filter) ? "" : 'FILTER (str(?label) = "' . $filter . '")') . $n . '} LIMIT ' . $limit . $n;
return $query;
}
/**
* Convert links to html or array
*
* @param string $item Item to be examined
* @param bool $html The output must be html link or array?
* @param string $wiki_url The link is a Wikipedia link or not?
* @return string|array Parsed link
* @access private
*/
private function get_link($item, $html, $wiki_url) {
if(preg_match($this->regUrl, $item, $url)) {
$f = (pathinfo($url[0]));
if($html) {
return preg_replace($this->regUrl, '<a target="_blank" href="' . (($wiki_url) ? str_replace("dbpedia.org/resource", "wikipedia.org/wiki", $url[0]) : $url[0]) . '">' . str_replace(array("_", "Categoria:"), array(" ", ""), $f["basename"]) . '</a>', $item);
} else {
$arr["text"] = str_replace(array("_", "Categoria:"), array(" ", ""), $f["basename"]);
$arr["link"] = (($wiki_url) ? str_replace("dbpedia.org/resource", "wikipedia.org/wiki", $url[0]) : $url[0]);
return $arr;
}
} else {
return $item;
}
}
/**
* Start query for audio data
*
* @param string $album Album label to search
* @see semantic_data::clean_text() Clean text
* @see semantic_data::create_sematic_query() Create sematic query
* @see wikipedia-::search() Wikipedia > Search
* @see easyrdf::query() EasyRdf > Query
* @return string|void Search result parsed
* @access private
*/
public function audio($album) {
$wiki = $this->wikipedia->search($this->clean_text($album));
if(count($wiki[0]) > 0) {
$title = str_replace("_", " ", $wiki[0]["title"]);
$optional = array(
"dbp:titolo" => "titolo",
"dbp:artista" => "artista",
"rdfs:comment" => "commento",
"dbp:registrato" => "registrazione",
"dbo:totalDiscs" => "dischi",
"dbo:totalTracks" => "tracce",
"dbp:durata" => "durata",
"foaf:depiction" => "immagine",
"dbo:thumbnail" => "thumbnail",
"dbp:anno" => "anno",
"dbp:genere" => "genere",
"dbp:precedente" => "disco_precedente",
"dbp:successivo" => "disco_successivo",
"dbp:tipoAlbum" => "tipo_album. ",
"foaf:isPrimaryTopicOf" => "pagina_Wikipedia"
);
$query = $this->create_sematic_query("Album", $optional, $title);
$result = $this->easyrdf->query($query);
if(count($result) > 0) {
$this->ret[] = $this->process($result, $title, $query);
} else {
print "no results";
exit();
}
} else {
print "no results";
exit();
}
}
/**
* Start query for book data
*
* @param string $libro Book title to search
* @see semantic_data::clean_text() Clean text
* @see semantic_data::create_sematic_query() Create sematic query
* @see wikipedia-::search() Wikipedia > Search
* @see easyrdf::query() EasyRdf > Query
* @return string|void Search result parsed
* @access private
*/
public function book($libro) {
$wiki = $this->wikipedia->search($this->clean_text($libro));
if(count($wiki[0]) > 0) {
$title = str_replace("_", " ", $wiki[0]["title"]);
$optional = array(
"dbp:titolo" => "titolo",
"dbp:autore" => "autore",
"rdfs:comment" => "commento",
"dbp:lingua" => "lingua",
"dbp:annoorig" => "anno",
"dbo:genere" => "genere",
"dbp:immagine" => "immagine",
"dbo:thumbnail" => "thumbnail",
"dbp:sottogenere" => "sottogenere",
"dbp:protagonista" => "protagonista",
"dbp:tipo" => "tipo",
"foaf:isPrimaryTopicOf" => "pagina_Wikipedia"
);
$query = $this->create_sematic_query("Book", $optional, $title);
$result = $this->easyrdf->query($query);
if(count($result) > 0) {
$this->ret[] = $this->process($result, $title, $query);
} else {
print "no results";
exit();
}
} else {
print "no results";
exit();
}
}
/**
* Start query for film data
*
* @param string $film Film title to search
* @see semantic_data::clean_text() Clean text
* @see semantic_data::create_sematic_query() Create sematic query
* @see wikipedia-::search() Wikipedia > Search
* @see easyrdf::query() EasyRdf > Query
* @return string|void Search result parsed
* @access private
*/
public function film($film) {
$wiki = $this->wikipedia->search($this->clean_text($film));
if(count($wiki[0]) > 0) {
$title = str_replace("_", " ", $wiki[0]["title"]);
$optional = array(
"dbp:titoloitaliano" => "titolo_in_italiano",
"dbp:titolooriginale" => "titolo_originale",
"dbp:attori" => "attori",
"dbp:annouscita" => "anno",
"rdfs:comment" => "commento",
"dbp:casaproduzione" => "casa_di_produzione",
"foaf:depiction" => "immagine",
"dbo:thumbnail" => "thumbnail",
"dbp:didascalia" => "didascalia",
"dbp:distribuzioneitalia" => "distribuzione_in_Italia",
"dbp:doppiatoriitaliani" => "doppiatori",
"dbp:durata" => "durata",
"dbp:fotografo" => "fotografia",
"dbp:montatore" => "montaggio",
"dbp:produttore" => "produzione",
"dbp:sceneggiatore" => "sceneggiatura",
"dbp:scenografo" => "scenografia",
"dbp:soggetto" => "soggetto",
"dbp:regista" => "regia",
"dbp:tipoaudio" => "audio",
"dbp:tipocolore" => "colore",
"dbp:genere" => "genere ",
"foaf:isPrimaryTopicOf" => "pagina_Wikipedia"
);
$query = $this->create_sematic_query("Film", $optional, $title);
$result = $this->easyrdf->query($query);
if(count($result) > 0) {
$this->ret[] = $this->process($result, $title, $query);
} else {
print "no results";
exit();
}
} else {
print "no results";
exit();
}
}
/**
* Start query for artist data
*
* @param string $person Artist name to search
* @see semantic_data::clean_text() Clean text
* @see semantic_data::create_sematic_query() Create sematic query
* @see wikipedia-::search() Wikipedia > Search
* @see easyrdf::query() EasyRdf > Query
* @return string|void Search result parsed
* @access private
*/
public function person($person) {
$wiki = $this->wikipedia->search($this->clean_text($person));
if(count($wiki[0]) > 0) {
$title = str_replace("_", " ", $wiki[0]["title"]);
$optional = array(
"dbp:nome" => "nome",
"dbp:cognome" => "cognome",
"dbo:formerName" => "formerName",
"rdfs:comment" => "commento",
"rdfs:contenuto" => "contenuto",
"dbp:nazione" => "nazione",
"dbp:nazionalità" => "nazionalita",
"dbp:postnazionalità" => "postnazionalita",
"dbp:profession" => "professione",
"dbp:tipoArtista" => "tipo_di_artista",
"dbp:numeroAlbumLive" => "album_dal_vivo",
"dbp:numeroTotaleAlbumPubblicati" => "totale_album",
"dbp:attività" => "attivita",
"dbp:attivitàaltre" => "altre_attivita",
"dbp:immagine" => "immagine",
"foaf:depiction" => "depiction",
"dbo:thumbnail" => "thumbnail",
"dbp:genere" => "genere",
"dbp:annonascita" => "anno_di_nascita",
"dbp:annomorte" => "anno_di_morte",
"dbo:birthPlace" => "luogo_nascita",
"dbo:deathPlace" => "luogo_di_morte",
"dbp:annoInizioAttività" => "inizio_attivita",
"dbp:annoFineAttività" => "fine_attivita",
"dbp:tombeFamose" => "luogo_di_sepoltura",
"foaf:isPrimaryTopicOf" => "pagina_Wikipedia"
);
$query = $this->create_sematic_query("Person", $optional, $title);
$result = $this->easyrdf->query($query);
if(count($result) > 0) {
$this->ret[] = $this->process($result, $title, $query);
} else {
print "no results";
exit();
}
} else {
print "no results";
exit();
}
}
/**
* Start query for thing
*
* @param string $thing Something to search
* @see semantic_data::clean_text() Clean text
* @see semantic_data::create_sematic_query() Create sematic query
* @see wikipedia-::search() Wikipedia > Search
* @see easyrdf::query() EasyRdf > Query
* @return string|void Search result parsed
* @access private
*/
public function thing($thing) {
$wiki = $this->wikipedia->search($this->clean_text($thing));
if(count($wiki[0]) > 0) {
$title = str_replace("_", " ", $wiki[0]["title"]);
$optional = array(
"dcterms:subject" => "soggetto",
"dbp:tipoDiOggetto" => "oggetto",
"rdfs:comment" => "commento",
"rdfs:contenuto" => "contenuto",
"dbp:lingua" => "lingua",
"dbp:nazione" => "nazione",
"dbp:nazionalità" => "nazionalita",
"dbp:attivitàaltre" => "altre_attivita",
"dbp:immagine" => "immagine",
"foaf:depiction" => "depiction",
"dbo:thumbnail" => "thumbnail",
"dbp:annoorig" => "anno",
"dbp:inizioProduzione" => "anno",
"dbp:genere" => "genere",
"dbp:ingredienti" => "ingredienti",
"dbp:paese" => "paese",
"dbp:regione" => "regione",
"dbp:tombeFamose" => "luogo_di_sepoltura",
"foaf:isPrimaryTopicOf" => "pagina_Wikipedia"
);
$query = $this->create_sematic_query("", $optional, $title);
$result = $this->easyrdf->query($query);
if(count($result) > 0) {
$this->ret[] = $this->process($result, $title, $query);
} else {
print "no results";
exit();
}
} else {
print "no results";
exit();
}
}
/**
* Parse the output
*
* @param array $result Array of semantic query result
* @param string $wiki Wikipedia result
* @param string $query Created semantic query
* @see semantic_data::is_remote_file() Is remote file
* @see semantic_data::debug() Debug
* @see semantic_data::get_default_prefixes() Get default prefixes
* @see semantic_data::output() Output
* @see semantic_data::format() Format
* @see semantic_data::startime() Start time
* @see semantic_data::end_time() End time
* @return string|void Search result parsed
* @access private
*/
private function process($result, $wiki, $query) {
foreach($result[0] as $k => $row) {
if($this->is_url($row) && $k !== "immagine" && $k !== "thumbnail" && $k !== "item") {
$res[$k] = $this->get_link(trim($row), (($this->output == "html") ? true : false), true);
} else {
if($k == "item") {
$res["risorsa_dbpedia"] = $this->get_link(trim($row), (($this->output == "html") ? true : false), false);
}
if($this->is_remote_file(trim($result[0]->immagine))) {
$res["immagine"] = trim($result[0]->immagine);
$res["depiction"] = trim($result[0]->depiction);
} else {
if($this->is_remote_file(trim($result[0]->thumbnail))) {
$res["thumbnail"] = trim($result[0]->thumbnail);
$res["depiction"] = trim($result[0]->depiction);
} else {
$pi = pathinfo(trim($result[0]->thumbnail));
$res["thumbnail"] = str_replace("commons", "it", trim($result[0]->thumbnail));
$res["immagine"] = str_replace("commons/thumb", "it", trim($pi["dirname"]));
$res["depiction"] = trim($result[0]->depiction);
}
}
if(!trim($res["thumbnail"])) {
unset($res["thumbnail"]);
}
if(!trim($res["immagine"])) {
unset($res["immagine"]);
}
if(!trim($res["depiction"])) {
unset($res["depiction"]);
}
if(isset($res["durata"]) && strlen(trim($res["durata"])) > 0) {
$res["durata_totale"] = gmdate("H:i:s", round($res["durata"]));
}
if(isset($res["audio"]) && strpos(trim($res["audio"]), "sonor") !== false) {
$res["audio"] = "si";
}
if(isset($res["colore"]) && strpos(trim($res["colore"]), "color") !== false) {
$res["colore"] = "si";
}
if($k !== "item" && $k !== "thumbnail") {
if($this->output == "html") {
$res[$k] = str_replace(array("> ", " </"), array(">", "</"), preg_replace("/\*(\ |)(.*?)\:(.*?)(\n|$)/", '<dt><small>$2</small></dt><dd><small>$3</small></dd>', trim($row)));
if(strpos($res[$k], "<dt>") !== false) {
$res[$k] = '<dl class="dl-horizontal collapse" id="' . $k . '_link">' . $res[$k] . "</dl>";
}
} else {
$obj = trim($row);
if(preg_match_all("/\*(|\s+)(.*?)\n/ms", $obj, $m)) {
if(is_array($m[2])) {
foreach($m[2] as $mmk => $mmv) {
if(strpos($mmv, ":") !== false) {
list($a, $r) = explode(":", $mmv);
$res[$k][trim($a)] = trim($r);
}
}
} else {
$res[$k] = $m[2];
}
} else {
$res[$k] = $obj;
}
}
}
}
}
if($this->debug) {
header("Content-type: text/plain; charset=utf-8");
print "Search result in Wikipedia: " . $wiki . "\n\n";
print "dbpedia page: " . $res["risorsa_dbpedia"]. "\n\n";
print "SPARQL ENDPOINT:\nhttp://it.dbpedia.org/sparql\n\n";
print "QUERY IN ENDPOINT:\nhttp://it.dbpedia.org/sparql?query=" . urlencode($this->get_default_prefixes("semantic") . $query) . "\n\n";
print "SEMANTIC QUERY:\n" . str_repeat("*", 150) . "\n\n";
print $this->get_default_prefixes("semantic");
print $query;
print "\n\n" . str_repeat("*", 150) . "\n\n";
print "QUERY RESULT:\n" . str_repeat("-", 150) . "\n\n";
$this->output = ($this->output == "") ? "all" : $this->output;
switch($this->output) {
case "all":
print_r($result);
print_r($res);
break;
case "array":
print_r($res);
break;
case "object":
print_r($res);
break;
}
$end_time = $this->end_time($this->startime);
print "\n" . str_repeat("-", 150) . "\nNinuxoo 2.0 Semantic query engine\nQuery generata automaticamente il " . date("d/m/Y \a\l\l\e h:i:s") . "\nTempo di esecuzione: " . $end_time . " secondi.\n\n";
} else {
$end_time = $this->end_time($this->startime);
$res["time"] = $end_time;
$this->format = ($this->format == "") ? "json" : $this->format;
$this->export[] = $res;
}
}
/**
* Export the output
*
* @param string $print `true` print output, `false` return as variable
* @see semantic_data::indent() Indent
* @see semantic_data::format() Format
* @return string|void
* @access public
*/
public function export($print = true) {
switch($this->format) {
case "array":
if($print) {
header("Content-type: text/plain; charset=utf-8");
print_r($this->export);
} else {
return $this->export;
}
break;
case "json":
if($print) {
header("Content-type: text/plain; charset=utf-8");
print json_encode($this->export);
} else {
return json_encode($this->export);
}
break;
case "jsonp":
if($print) {
header("Content-type: text/plain; charset=utf-8");
print $this->indent(json_encode($this->export));
} else {
return $this->indent(json_encode($this->export));
}
break;
case "html":
foreach($this->export as $ek => $ev) {
foreach($ev as $rk => $rv) {
$th .= "<th>" . $rk . "</th>";
$td .= "<td>" . $rv . "</td>";
}
if($print) {
header("Content-type: text/html");
print '<table border="1"><tr>' . $th . "</tr><tr>" . mb_convert_encoding($td, "HTML-ENTITIES", "UTF-8") . "</tr></table>";
} else {
return '<table border="1"><tr>' . $th . "</tr><tr>" . mb_convert_encoding($td, "HTML-ENTITIES", "UTF-8") . "</tr></table>";
}
}
break;
}
}
}
?>