/
rdfto.arc2.php
810 lines (681 loc) · 29.4 KB
/
rdfto.arc2.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
<?php
include_once(dirname(__FILE__).'/rdfto.interface.php');
/**
* ARC2 Template Object
*
* implements the RDF Template Object and Helper methods, using ARC2 library
* to parse RDF files. ARC2 lib must be included before using this class.
*
* @see http://arc.semsol.org/
*
* @version 0.1
* @author Michael Haschke, http://eye48.com/
* @license GNU General Public License 2, http://www.gnu.org/licenses/gpl-2.0.html
**/
class ARC2File_Template_Object implements RDF_Template_Object, RDF_Template_Helper
{
public $resource = null; // arc2 resource object
public $uri; // contains the resource URI which is wrapped by the object */
public $ns_prefix = 'foaf'; // contains namespace prefix of model which defines the type of the currently wrapped object */
public $level = 0; // linked data level (how many steps of objects which has been requested for other objects)
public $levelMax = 1; // maximum level for linked data
public $requests = 0; // count linked data requests
public $requestsMax = 50; // maximum linked data requests
public $requestsUris = array(); // contains all requested URIs, must be additionally saved because we cannot check the array for unsuccesful requests and sameAs data
public $requestsTimeout = 3;
public $user_agent_string = 'User-Agent: RDFTO/ARC Reader (+http://arc.semsol.org/)';
public $ignoreUris = array();
public $cache_space_prefix = 'RDFTO'; // e.g. 'MyApplication'
public $templateImage = 'SET TEMPLATE FOR IMAGE'; // e.g. '<img src="##URL##" alt="##DESC##"/>'
/* Constructor for Template Object
*
* TODO
* @since 0.1
*/
public function __construct(Array $environment)
{
$this->levelMax = $environment['levelMax'];
$this->requestsMax = $environment['requestsMax'];
return;
}
/* TODO
* @since 0.1
*/
public function initResource($arc2_resource)
{
$this->resource = $arc2_resource;
$this->uri = $arc2_resource->uri;
$this->updateNamespacePrefix();
$this->addLogMessage('init: count '.count($this->resource->index).' -- memory '.intval(memory_get_usage(true)/1024).'kb');
$this->includeSameAs($this->uri);
return;
}
/* Magic method __GET
*
* Magically get vars from the used RDF parser library or transform
* requests for predicates of the wrapped resource. Returns arrays of the
* objects.
*
* @since 0.1
*
* @parameter $name name of ARC2 resource variable or predicate
*
* @return Array result array with simple types or ARC2File_Template_Object
*/
public function __get($name)
{
// test for native arc2 resource vars
if (isset($this->resource->$name))
return $this->resource->$name;
// or form to predicates
$getLinkedData = true;
// check for prefix '_nld_' which implies not to request linked data
if (substr($name, 0, 5) == '_nld_')
{
$getLinkedData = false;
$name = substr($name, 5);
}
$predicate = explode('_', $name);
if (count($predicate) == 1)
{
$predicate = $this->ns_prefix.':'.$name;
}
elseif (count($predicate) == 2)
{
$predicate = implode(':', $predicate);
}
else
{
$predicate = $predicate[0].':'.implode('_', array_slice($predicate, 1));
}
$objects = $this->resource->getProps($predicate, $this->uri);
// return empty object array
if (count($objects)==0) return array();
foreach ($objects as $index => $data)
{
if ($data['type']=='uri' || $data['type']=='bnode')
{
// return wrapped arc2 resource for type=uri|bnode
if (isset($this->resource->index[$data['value']]))
{
$objects[$index] = clone $this;
$objects[$index]->uri = $data['value']; // object
}
// except for resources without further indexed data
else
{
$objects[$index] = $data['value']; // must be an uri
if ($getLinkedData)
$objects[$index] = $this->getLinkedData($data['value']);
}
}
else
{
// return the value straight (without type)
// for literals and other data types
$objects[$index] = $data['value']; // data|literal
// add another value for language
if (isset($data['lang']))
$objects[$data['lang']][] = $data['value']; // data|literal
}
}
return $objects;
}
/* Magic method __CALL
*
* Magically call methods from the used RDF parser library or transform
* requests for predicates filtered by object types of the wrapped resource.
* Returns arrays of the objects.
*
* @since 0.1
*
* @param string $name name of Arc2 method or predicate
* @param Array $types arguments for Arc2 method or rdf:type filter (use a
* minus '-' as prefix to exclude instances of rdf:type,
* e.g. '-vcard:Home'), last item of the array could be
* a boolean TRUE if the results should be interesected
*
* @return Array result array with simple types or ARC2File_Template_Object
*/
public function __call($name, $types)
{
// check for existing methods in Arc2 resource object
// TODO
// emulate predicate requests filtered by rdf:type
// no filters attached
if (count($types) == 0) return $this->$name;
// get results without filtering
$references = $this->$name;
// check intersect argument
if ($types[(count($types)-1)] === true)
{
$types = array_slice($types, 0, -1);
$intersect = true;
}
else
{
$intersect = false;
}
// part add/sub filters
$filters_add = array();
$filters_sub = array();
foreach($types as $type)
{
if (substr($type, 0, 1) == '-')
{
$type = substr($type, 1);
$filters_sub[] = ($s = strpos($type, ':')) ? $this->resource->ns[(substr($type, 0, $s))].substr($type, $s+1) : $type;
}
else
{
$filters_add[] = ($s = strpos($type, ':')) ? $this->resource->ns[(substr($type, 0, $s))].substr($type, $s+1) : $type;
}
}
// only use results with informations about rdf:type
$references_types = array();
foreach ($references as $id => $ref)
{
if (!is_object($ref) ||
!isset($this->resource->index[$ref->uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']))
{
unset($references[$id]);
}
else
{
foreach ($this->resource->index[$ref->uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] as $reftype)
{
$references_types[$ref->uri][] = $reftype['value'];
}
}
}
reset($references);
// use substractive filters to delete items from results
# $this->resource->index[$this->uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][0]['value']
foreach ($references as $id => $ref)
{
foreach ($filters_sub as $type)
{
if (array_search($type, $references_types[$ref->uri]) !== false)
{
unset($references[$id]);
break;
}
}
}
reset($references);
// use additive filters to keep result items
foreach ($references as $id => $ref)
{
$count_types = 0;
foreach ($filters_add as $type)
{
if (array_search($type, $references_types[$ref->uri]) !== false)
{
$count_types++;
}
}
if ($count_types == 0 || ($intersect && $count_types != count($filters_add)))
{
unset($references[$id]);
}
}
return $references;
}
/* TODO
* @since 0.1
*/
public function __toString()
{
$index = array();
if (isset($this->resource->index[$this->uri]))
$index[$this->uri] = $this->resource->index[$this->uri];
return serialize($index);
}
/* TODO
* @since 0.1
*/
public function getLinkedData($uri, $alias = null)
{
// $testuri = ''; // may defined by sub class for debug checks
$this->addLogMessage(' --> request data: '.$uri.' ('.$alias.')');
$indexedData = null;
// must be an uri b/c blanknode without data makes no sense
// but better to check
if (substr($uri, 0 , 1) != '_'
&& array_search($uri, $this->ignoreUris) === false
&& array_search($uri, $this->requestsUris) === false)
{
$this->requestsUris[] = $uri;
// test cache for indexed data of uri
if (false === ($indexedData = $this->getCache(array('name'=>$uri, 'space'=>$this->cache_space_prefix.'Data'))))
{
// no data cached - request data from uri
// use uri without local anchors (they should be included)
$uriAbs = $uri;
if (strpos($uri, '#') !== false)
$uriAbs = substr($uri, 0, strpos($uri, '#'));
// try to get linked data, only if max LinkedData level
// and requests not reached
$linkedData = null;
// check cache for data
if (false === ($linkedData = $this->getCache(array('name'=>$uriAbs, 'space'=>$this->cache_space_prefix.'DataAbs'))))
{
// if (is_array($linkedData) && count($linkedData) == 0) die($uriAbs);
if ($this->level < $this->levelMax &&
$this->requests < $this->requestsMax)
{
// parse feed
$dataParser = ARC2::getRDFParser(array(
'reader_timeout'=>$this->requestsTimeout,
'http_user_agent_header' => $this->user_agent_string,
'keep_time_limit'=>true
)
);
/*
if (!isset($dataParser->reader)) {
ARC2::inc('Reader');
$dataParser->reader = & new ARC2_Reader($dataParser->a, $dataParser);
}
$dataParser->reader->timeout = $this->requestsTimeout;
//*/
//$dataParser->parse($uriAbs, null, 0, $this->requestsTimeout);
$dataParser->parse($uriAbs, null);
//print_r(array($uriAbs=>$dataParser->reader->getResponseHeaders()));
$this->requests++;
if (is_object($dataParser))
{
$linkedData = $dataParser->getSimpleIndex(0);
// save cache
$this->addLogMessage(' --> save to cache: '.$uriAbs);
$this->saveCache(array('data'=>$linkedData, 'name'=>$uriAbs, 'space'=>$this->cache_space_prefix.'DataAbs', 'time'=>true));
}
unset($dataParser);
}
$this->addLogMessage(' --> read: '.$uriAbs.' ('.count($linkedData).' elements)');
}
else
{
$this->addLogMessage(' --> read from cache: '.$uriAbs.' ('.count($linkedData).' elements)');
}
// check outdated cache as fallback
// better old data than no data :)
if (!$linkedData)
{
if ($linkedData = $this->getCache(array('name'=>$uriAbs, 'space'=>$this->cache_space_prefix.'DataAbs', 'time'=>-1)))
{
$this->addLogMessage(' --> read from OUTDATED cache: '.$uriAbs.' ('.count($linkedData).' elements)');
}
}
if (isset($testuri) && $uriAbs == $testuri) die('<pre>'.print_r($linkedData, true).'</pre>');
if ($linkedData)
{
// check linkedData, only keep uri and blanknodes which are referenced by resource
if (isset($linkedData[$uri]))
{
//$indexedData[$uri] = $linkedData[$uri];
$indexedData = $linkedData;
if (isset($testuri) && $uri == $testuri) die('<pre>'.print_r($indexedData, true).'</pre>');
/* TODO: reduceMemoryUsage()
$indexedData[$uri] = $linkedData[$uri];
if ($this->environment['FP_config']['LinkedData']['useBnodes'] === true)
{
// keep bnodes in results
$bnodes = $this->listBnodes($indexedData[$uri]);
foreach ($bnodes as $key)
{
$indexedData[$key] = $linkedData[$key];
}
}
else
{
// let bnodes out of result set
$indexedData[$uri] = $this->deleteBnodes($indexedData[$uri]);
}
// TODO: get all other relevant resources (referenced in any way over x steps)
unset($bnodes);
*/
$this->addLogMessage(' --> use: '.$uri.' ('.count($indexedData[$uri]).' elements)');
if (isset($testuri) && $uri == $testuri) die('<pre>'.print_r($indexedData, true).'</pre>');
unset($linkedData);
// save cache for indexed data of uri
$this->saveCache(array('data'=>$indexedData, 'name'=>$uri, 'space'=>$this->cache_space_prefix.'Data', 'time'=>true));
}
}
}
else
{
$this->addLogMessage(' --> read from cache: '.$uri.' ('.count($indexedData[$uri]).' elements)');
}
} // end of if (substr($uri, 0 , 1) != '_')
if (isset($testuri) && $uri == $testuri) die('<pre>'.print_r($indexedData, true).'</pre>');
if ($indexedData)
{
// return Foafpress_resource
$ARC2TO = clone $this;
// check alias (e.g. for owl:sameAs)
if ($alias)
{
$indexedData[$alias] = $indexedData[$uri];
unset($indexedData[$uri]);
}
if (isset($testuri) && $uri == $testuri) die('<pre>'.print_r($indexedData, true).'</pre>');
// merge data
$this->resource->index = ARC2::getMergedIndex($this->resource->index, $indexedData);
if (isset($testuri) && $uri == $testuri) die('<pre>'.print_r($this->resource->index, true).'</pre>');
unset($indexedData);
$ARC2TO->uri = $uri;
$ARC2TO->updateNamespacePrefix();
if ($alias)
{
//TODO: $ARC2TO->includeSameAs($alias);
$ARC2TO->uri = $alias;
}
else
{
$ARC2TO->level++;
}
$this->addLogMessage('now: count '.count($this->resource->index).' -- memory '.intval(memory_get_usage(true)/1024).'kb');
return $ARC2TO;
}
else
{
$this->addLogMessage('now: count '.count($this->resource->index).' -- memory '.intval(memory_get_usage(true)/1024).'kb');
// no data or error, only return requested uri
return $uri;
}
}
/* TODO
* @since 0.1
*/
public function includeSameAs($alias = null)
{
$sameAs = $this->_nld_owl_sameAs;
if (is_array($sameAs) && count($sameAs)>0)
{
foreach ($sameAs as $sameRes)
{
if (!is_object($sameRes))
$this->getLinkedData($sameRes, ($alias?$alias:$this->uri));
}
}
return;
}
/**
* Update Namespace Prefix
*
* tries to get the prefix from the namespace of the resource type, and
* using it as default prefix for the Foafpress object
*
* @return string $concept type without namespace
*/
public function updateNamespacePrefix()
{
$concept = false;
if (isset($this->resource->index[$this->uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']))
{
// get resource type
$type = $this->resource->index[$this->uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][0]['value'];
// get namespace and concept from type
$concept = str_replace($this->resource->ns, '', $type);
$model = substr($type, 0, -1 * strlen($concept));
$model_ns = array_search($model, $this->resource->ns);
if ($model_ns !== false) $this->ns_prefix = $model_ns;
}
return $concept;
}
/* Get data from cache
*
* you may implement it to improve performance, use the cache API from your
* environmental system
*/
public function getCache(Array $vars)
{
return false;
}
/* Write data to cache
*
* you may implement it to improve performance, use the cache API from your
* environmental system
*/
public function saveCache(Array $vars)
{
return false;
}
/* TODO
* @since 0.1
*
* @param boolean $strict If true, then only use literals in prefered languages, otherwise use first literal as fallback
*/
public function getLiteral(Array $predicates = array(), Array $preferedLanguages = array(), $strict = false)
{
$languages = $preferedLanguages;
//if (!is_array($predicates)) $predicates = array(strval($predicates));
if (count($predicates) == 0) return null;
//if (!is_array($languages)) $languages = array(strval($languages));
foreach ($predicates as $p)
{
$o = $this->$p;
if (is_array($o) && count($o) > 0)
{
foreach ($languages as $l)
{
if (isset($o[$l]))
{
return (is_object($o[$l][0]))?$o[$l][0]->uri:$o[$l][0];
break;
}
}
if ($strict === false)
{
return (is_object($o[0]))?$o[0]->uri:$o[0];
break;
}
}
}
return null;
}
/* TODO
* @since 0.1
*/
public function getImage(Array $predicates = array(), $useThumbnail = null)
{
if (count($predicates) == 0) return null;
$imgSrc = null;
$imgAlt = null;
foreach ($predicates as $p)
{
$p = '_nld_'.$p;
$pics = $this->$p;
if (is_array($pics) && count($pics) > 0)
{
foreach ($pics as $pic)
{
if (is_object($pic))
{
$imgSrc = $pic->uri;
if ($useThumbnail === true)
{
$thumbs = $pic->_nld_foaf_thumbnail;
if (is_array($thumbs) && count($thumbs) > 0)
{
$imgSrc = htmlspecialchars($thumbs[0]->uri, ENT_COMPAT, 'UTF-8');
}
}
$imgAlt = htmlspecialchars($pic->getLiteral(array('rdfs_label', 'dc_title', 'foaf_name', 'dc_description')), ENT_COMPAT, 'UTF-8');
break;
}
else
{
$imgSrc = htmlspecialchars($pic, ENT_COMPAT, 'UTF-8');
break;
}
}
break;
}
}
// $this->templateImage = '<img src="##URL##" alt="##DESC##"/>'
return $imgSrc?str_replace(array('##URL##', '##DESC##'), array($imgSrc, $imgAlt), $this->templateImage):null;
}
/* Reduce Memory Usage
*
* you may implement it to improve performance, use the cache API from your
* environmental system
*/
public function reduceMemoryUsage($resource)
{
return $resource;
}
/**
* listActivity
*
* tries to get feeds by checking various ways to relate feeds with foaf
* profiles.
*
* @see http://wiki.foaf-project.org/w/PersonWeblogRssDocumentationIssue
*
* @since 0.1
*
* @param array $check array with strings of predicates which should be checked
* @param int $items number of returned activity items
*
* @return array with activity items
*/
public function listActivity(Array $check = array(), $numberItems = null)
{
// $ARC2TO = $this;
if (!isset($cacheTimeActivity)) $cacheTimeActivity = $this->cacheTimeActivity;
if (!is_array($check) || count($check) == 0)
$check = array('seeAlso', 'made', 'weblog', 'account', 'homepage');
if ($numberItems == null) $numberItems = 50;
$numberItems = intval($numberItems);
// get all rdfs:seeAlso
$rdfs_seeAlso = array();
if (array_search('seeAlso', $check) !== false)
$rdfs_seeAlso = $this->_nld_rdfs_seeAlso;
// get all foaf:made
$foaf_made = array();
if (array_search('made', $check) !== false)
$foaf_made = $this->_nld_foaf_made;
// get all foaf:weblog in foaf:Document
$weblog_seeAlso = array();
if (array_search('weblog', $check) !== false)
{
$foaf_weblog = $this->_nld_foaf_weblog;
foreach($foaf_weblog as $weblog)
{
if (is_object($weblog) && $weblog->_nld_rdfs_seeAlso)
$weblog_seeAlso = array_merge($weblog_seeAlso, $weblog->_nld_rdfs_seeAlso);
unset($weblog);
}
unset($foaf_weblog);
}
// get all foaf:homepage in foaf:Document
$homepage_seeAlso = array();
if (array_search('homepage', $check) !== false)
{
$foaf_homepage = $this->_nld_foaf_homepage;
foreach($foaf_homepage as $homepage)
{
if (is_object($homepage) && $homepage->_nld_rdfs_seeAlso)
$homepage_seeAlso = array_merge($homepage_seeAlso, $homepage->_nld_rdfs_seeAlso);
unset($homepage);
}
unset($foaf_homepage);
}
// get all foaf:holdsAccount in foaf:OnlineAccount
$account_seeAlso = array();
if (array_search('account', $check) !== false)
{
$foaf_holdsAccount = $this->_nld_foaf_holdsAccount;
foreach($foaf_holdsAccount as $holdsAccount)
{
if (is_object($holdsAccount) && $holdsAccount->_nld_rdfs_seeAlso)
$account_seeAlso = array_merge($account_seeAlso, $holdsAccount->_nld_rdfs_seeAlso);
unset($holdsAccount);
}
unset($foaf_holdsAccount);
}
// check for type, must be rss:channel
$possibleFeeds = array_unique(array_merge($rdfs_seeAlso, $foaf_made, $weblog_seeAlso, $account_seeAlso, $homepage_seeAlso));
unset($rdfs_seeAlso, $foaf_made, $weblog_seeAlso, $account_seeAlso, $homepage_seeAlso);
$confirmedFeeds = array();
foreach ($possibleFeeds as $feed)
{
if (is_object($feed) &&
is_array($feed->_nld_rdf_type) &&
isset($feed->_nld_rdf_type[0]) &&
$feed->_nld_rdf_type[0] == 'http://purl.org/rss/1.0/channel')
{
$confirmedFeeds[$feed->uri] = $feed->getLiteral(array('rdfs_label', 'dc_title'));
}
}
unset($possibleFeeds);
$activity = array('feeds'=>$confirmedFeeds, 'stream'=>array());
// load feeds and read items
$i = 0;
$sortByDate = array();
$uniqueUris = array();
foreach ($confirmedFeeds as $feed => $feedTitle)
{
$feedIndex = null;
if (false === ($feedIndex = $this->getCache(array('name'=>$feed, 'space'=>$this->cache_space_prefix.'Activity', 'time'=>$cacheTimeActivity))))
{
// parse feed
$feedParser = ARC2::getRDFParser(array(
'reader_timeout'=>$this->requestsTimeout,
'http_user_agent_header' => $this->user_agent_string,
'keep_time_limit'=>true
)
);
$feedParser->parse($feed, null, 0, $this->requestsTimeout);
if (is_object($feedParser))
{
$feedIndex = $feedParser->getSimpleIndex(0);
// save cache
$this->saveCache(array('data'=>$feedIndex, 'name'=>$feed, 'space'=>$this->cache_space_prefix.'Activity', 'time'=>true));
}
}
if (is_array($feedIndex) && count($feedIndex) > 0)
{
// get feed title from feed when resource did not defined any title
$indexKeys = array_keys($feedIndex);
if (isset($feedIndex[$indexKeys[0]]['http://purl.org/rss/1.0/title']) && $activity['feeds'][$feed] === null)
$activity['feeds'][$feed] = $feedIndex[$indexKeys[0]]['http://purl.org/rss/1.0/title'][0]['value'];
// get feed items
foreach ($feedIndex as $uri => $content)
{
if (isset($content['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']) &&
$content['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][0]['value'] == 'http://purl.org/rss/1.0/item' &&
array_search($content['http://purl.org/rss/1.0/link'][0]['value'], $uniqueUris) === false)
{
// content
$activity['stream'][$i]['source'] = $feed;
$activity['stream'][$i]['date'] = (isset($content['http://purl.org/dc/elements/1.1/date']))?strtotime($content['http://purl.org/dc/elements/1.1/date'][0]['value']):0;
$activity['stream'][$i]['link'] = htmlspecialchars($content['http://purl.org/rss/1.0/link'][0]['value'], ENT_COMPAT, 'UTF-8');
$activity['stream'][$i]['title'] = $content['http://purl.org/rss/1.0/title'][0]['value'];
$activity['stream'][$i]['content'] = (isset($content['http://purl.org/rss/1.0/modules/content/encoded']))?$content['http://purl.org/rss/1.0/modules/content/encoded'][0]['value']:null;
$activity['stream'][$i]['output'] = '<a href="'.$activity['stream'][$i]['link'].'">'.htmlspecialchars(strip_tags($activity['stream'][$i]['title']), ENT_COMPAT, 'UTF-8').'</a>'; //TODO do not transform html-entities a second time
$activity['stream'][$i]['contentarray_origin'] = $content;
$sortByDate[$i] = $activity['stream'][$i]['date'];
// orgacheck
$uniqueUris[] = $activity['stream'][$i]['link'];
$i++;
}
}
}
}
// sort by date desc
array_multisort($sortByDate, SORT_DESC, $activity['stream']);
// only give back maximum number of items
$activity['stream'] = array_slice($activity['stream'], 0, $numberItems);
return $activity;
}
/* Write message to log
*
* you may implement it for debugging reasons
*/
protected function addLogMessage($msg)
{
return true;
}
}