XML_FOAF
[ class tree: XML_FOAF ] [ index: XML_FOAF ] [ all elements ]

Source for file Parser.php

Documentation is available at Parser.php


1 <?php
2 /* vim: get expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 3.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/3_0.txt |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Davey Shafik <davey@php.net> |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: fsource_XML_FOAF__FOAF_Parser.php.html,v 1.2 2004/01/15 19:06:48 davey Exp $
20
21 /**
22 * FOAF Parser
23 * @package XML_FOAF
24 * @category XML
25 */
26
27 require_once 'XML/FOAF/Common.php';
28
29 define('RDFAPI_INCLUDE_DIR', 'XML/FOAF/RAP/');
30 define('XML_FOAF_NS','http://xmlns.com/foaf/0.1/');
31 define('XML_FOAF_DC_NS','http://purl.org/dc/elements/1.1/');
32 define('XML_FOAF_RDF_NS','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
33 define('XML_FOAF_RDF_SCHEMA_NS', 'http://www.w3.org/2000/01/rdf-schema#');
34 define('XML_FOAF_PERSON', 1);
35 define('XML_FOAF_GROUP', 2);
36 define('XML_FOAF_ORGANIZATION', 3);
37 define('XML_FOAF_AGENT', 4);
38
39 /**
40 * FOAF Parser
41 *
42 * Individual element parsers that start with _fetch will return multiple elements
43 * into the result Array, those that start with _get will return only a single element
44 *
45 * @package XML_FOAF
46 * @author Davey <davey@php.net>
47 * @version 0.1
48 * @copyright Copyright 2003 Davey Shafik and Synaptic Media. All Rights Reserved.
49 * @example docs/examples/example2.php Basic Usage of XML_FOAF_Parser
50 * @todo Implement PEAR_Error handling
51 */
52
53 class XML_FOAF_Parser extends XML_FOAF_Common
54 {
55 /**
56 * @var string Original FOAF file
57 */
58
59 var $foaf_xml = '';
60
61 /**
62 * @var array FOAF data as Array
63 */
64
65 var $foaf_data;
66
67 /**
68 * @var object MemModel of FOAF
69 */
70
71 var $foaf;
72
73 /**
74 * @var object Instance of the RAP RDF_Parser
75 */
76
77 var $rdf_parser;
78
79 /**
80 * @var array Nodes assumed to be primary foaf:Agents
81 */
82
83 var $agent_nodes = array();
84
85 /**
86 * @var array Nodes found in <foaf:knows>
87 */
88
89 var $known_nodes = array();
90
91 /**
92 * XML_FOAF_Parser Constructor
93 */
94
95 function __construct() {
96 require_once RDFAPI_INCLUDE_DIR . 'RdfAPI.php';
97 $this->rdf_parser =& new RdfParser;
98 }
99
100 /**
101 * XML_FOAF_Parser PHP4 Compatible Constructor
102 */
103
104 function XML_FOAF_Parser()
105 {
106 $this->__construct();
107 }
108
109 /**
110 * Parse a FOAF at the specified URI
111 *
112 * @param $uri string URI for a FOAF file
113 * @access public
114 * @return void
115 */
116
117 function parseFromURI($uri)
118 {
119 $this->parseFromFile($uri);
120 }
121
122 /**
123 * Parse a FOAF in the specified File
124 *
125 * @param $file string File Path for a FOAF file
126 * @param $use_include_path Whether to look for the file in the php include_path
127 * @access public
128 * @return void
129 */
130
131 function parseFromFile($file,$use_include_path = false)
132 {
133 $this->foaf = file_get_contents($file,$use_include_path);
134 $this->_parse();
135 }
136
137 /**
138 * Parse a FOAF contained in the specified variable
139 *
140 * @param $mem string Variable holding a FOAF file's XML
141 * @access public
142 * @return void
143 */
144
145 function parseFromMem($mem)
146 {
147 $this->foaf = $mem;
148 $this->_parse();
149 }
150
151 /**#@+
152 * @access private
153 * @return void
154 */
155
156 /**
157 * Calls all the seperate property parsers
158 */
159
160 function _parse()
161 {
162 $this->foaf =& $this->rdf_parser->generateModel($this->foaf);
163 $this->foaf_data = $this->_fetchAgent();
164 $this->_fetchAimChatID();
165 $this->_fetchCurrentProject();
166 $this->_fetchDcDescription();
167 $this->_fetchDcTitle();
168 $this->_fetchDepiction();
169 $this->_fetchFundedBy();
170 $this->_fetchHoldsAccount();
171 $this->_fetchHomepage();
172 $this->_fetchIcqChatID();
173 $this->_fetchImg();
174 $this->_fetchInterest();
175 $this->_fetchJabberID();
176 $this->_fetchLogo();
177 $this->_fetchMade();
178 $this->_fetchMbox();
179 $this->_fetchMboxSha1Sum();
180 $this->_fetchMember();
181 $this->_fetchMsnChatID();
182 $this->_fetchMyersBriggs();
183 $this->_fetchNick();
184 $this->_fetchPage();
185 $this->_fetchPastProject();
186 $this->_fetchPhone();
187 $this->_fetchPublication();
188 $this->_fetchSchoolHomepage();
189 $this->_fetchSeeAlso();
190 $this->_fetchTheme();
191 $this->_fetchWeblog();
192 $this->_fetchWorkInfoHomepage();
193 $this->_fetchWorkplaceHomepage();
194 $this->_fetchYahooChatID();
195 $this->_getBasedNear();
196 $this->_getFamilyName();
197 $this->_getFirstName();
198 $this->_getGeekcode();
199 $this->_getGender();
200 $this->_getGivenName();
201 $this->_getMembershipClass();
202 $this->_getName();
203 $this->_getPlan();
204 $this->_getSurname();
205 $this->_getTitle();
206 }
207
208 /**
209 * Parses our the foaf:Agents
210 *
211 * Looks for all foaf:Agents (foaf:Person,foaf:Group,foaf:Organzation and foaf:Agent)
212 * and decides which are the primary agents (who/what the FOAF is about) and
213 * which are only known by the primary agents
214 *
215 * @access private
216 * @return void
217 */
218
219 function _fetchAgent()
220 {
221 $person_resource = new Resource(XML_FOAF_NS . 'Person');
222 $persons = $this->foaf->find(null,null,$person_resource);
223 $group_resource = new Resource(XML_FOAF_NS . 'Group');
224 $groups = $this->foaf->find(null,null,$group_resource);
225 $organization_resource = new Resource(XML_FOAF_NS . 'Organization');
226 $organizations = $this->foaf->find(null,null,$organization_resource);
227 $agent_resource = new Resource(XML_FOAF_NS . 'Agent');
228 $agents = $this->foaf->find(null,null,$agent_resource);
229 $agents->addModel($persons);
230 $agents->addModel($groups);
231 $agents->addModel($organizations);
232 $knows_resource = new Resource(XML_FOAF_NS . 'knows');
233 $knows = $this->foaf->find(null,$knows_resource,null);
234 $i = 0;
235 $agent_nodes = array();
236 $known_nodes = array();
237 foreach ($agents->triples as $agent) {
238 $agent_nodes[$agent->subj->uri] = $agent->obj->uri;
239 $i += 1;
240 foreach ($knows->triples as $know) {
241 if ($agent->subj->uri == $know->obj->uri) {
242 $agent_type = pathinfo($agent->obj->uri);
243 $agent_type = $agent_type['basename'];
244 $known = array('node' => $know->obj->uri, 'type' => $agent_type);
245 $known_nodes["{$know->subj->uri}"][] = $known;
246 $this->known_nodes[] = $know->obj->uri;
247 unset($agent_nodes[$agent->subj->uri]);
248 }
249 }
250 }
251
252 $agents = array();
253 $i = 0;
254 foreach ($agent_nodes as $node => $agent_type) {
255 $agent_type = pathinfo($agent_type);
256 $agent_type = $agent_type['basename'];
257 $agents[$i] = array ('node' => $node, 'type' => $agent_type);
258 $this->agent_nodes[] = $node;
259 if (isset($known_nodes[$node])) {
260 foreach ($known_nodes[$node] as $knows) {
261 $agents[$i]['knows'][] = $knows;
262 }
263 }
264 $i += 1;
265 }
266 if (!is_array($this->known_nodes)) {
267 $this->known_nodes = array();
268 }
269 return $agents;
270 }
271
272 /**
273 * Finds the foaf:name's and inserts them into the result array
274 *
275 * If more than one foaf:name is found for a single foaf:Agent, the
276 * last found will be the one shown in the result
277 */
278
279 function _getName()
280 {
281 $this->_getProperty(XML_FOAF_NS, 'name', 'label');
282 }
283
284 /**
285 * Finds all foaf:depiction's and inserts them into the result Array
286 */
287
288 function _fetchDepiction()
289 {
290 $this->_fetchProperty(XML_FOAF_NS, 'depiction', 'uri');
291 }
292
293 /**
294 * Finds all foaf:fundedBy's and inserts them into the result Array
295 */
296
297 function _fetchFundedBy()
298 {
299 $this->_fetchProperty(XML_FOAF_NS, 'fundedBy', 'uri');
300 }
301
302 /**
303 * Finds all foaf:logo's and inserts them into the result Array
304 */
305
306 function _fetchLogo()
307 {
308 $this->_fetchProperty(XML_FOAF_NS, 'logo', 'uri');
309 }
310
311 /**
312 * Finds all foaf:page's and inserts them into the result Array
313 */
314
315 function _fetchPage()
316 {
317 $this->_fetchProperty(XML_FOAF_NS, 'page', 'uri');
318 }
319
320 /**
321 * Finds all foaf:theme's and inserts them into the result Array
322 */
323
324 function _fetchTheme()
325 {
326 $this->_fetchProperty(XML_FOAF_NS, 'theme', 'uri');
327 }
328
329 /**
330 * Finds all the foaf:title and inserts them into the result Array
331 *
332 * If more than one foaf:title is found for one foaf:Agent the
333 * last one found is insert into the result
334 */
335
336 function _getTitle()
337 {
338 $this->_getProperty(XML_FOAF_NS, 'title', 'label');
339 }
340
341 /**
342 * Finds all foaf:nick's and inserts them into the result Array
343 */
344
345 function _fetchNick()
346 {
347 $nick_resource = new Resource(XML_FOAF_NS . 'nick');
348 $nicks = $this->foaf->find(null,$nick_resource,null);
349 foreach ($nicks->triples as $nick) {
350 if (in_array($nick->subj->uri,$this->agent_nodes)) {
351 foreach ($this->agent_nodes as $key => $value) {
352 if ($value == $nick->subj->uri) {
353 $this->foaf_data[$key]['nick'][] = $nick->obj->label;
354 }
355 break;
356 }
357 } elseif (in_array($nick->subj->uri,$this->known_nodes)) {
358 foreach ($this->foaf_data as $agent_key => $agent) {
359 if (isset($agent['knows']) && is_array($agent['knows'])) {
360 foreach ($agent['knows'] as $nick_key => $nick_array) {
361 if (isset($nick_array['node']) && ($nick_array['node'] == $nick->subj->uri)) {
362 $this->foaf_data[$agent_key]['knows'][$nick_key]['nick'][] = $nick->obj->label;
363 break 2;
364 }
365 }
366 }
367 }
368 }
369 }
370 }
371
372 /**
373 * Finds all foaf:givenName's and inserts them into the result Array
374 *
375 * If more than one foaf:givenName is found for a single foaf:Agent, the
376 * last one found is inserted into the result array
377 */
378
379 function _getGivenName()
380 {
381 $this->_getProperty(XML_FOAF_NS, 'givenName', 'label');
382 }
383
384 /**
385 * Finds all foaf:phone's and inserts them into the result Array
386 */
387
388 function _fetchPhone()
389 {
390 $this->_fetchProperty(XML_FOAF_NS, 'phone', 'uri');
391 }
392
393 /**
394 * Finds all foaf:mbox's and inserts them into the result Array
395 */
396
397 function _fetchMbox()
398 {
399 $this->_fetchProperty(XML_FOAF_NS, 'mbox', 'uri');
400 }
401
402 /**
403 * Finds all foaf:mbox_sha1sum's and inserts them into the result Array
404 */
405
406 function _fetchMboxSha1Sum()
407 {
408 $this->_fetchProperty(XML_FOAF_NS, 'mbox_sha1sum', 'label');
409 }
410
411 /**
412 * Finds all foaf:gender's and inserts them into the result Array
413 *
414 * If more than one foaf:gender is found for a single foaf:Agent, the
415 * last found is inserted into the result Array.
416 */
417
418 function _getGender()
419 {
420 $this->_getProperty(XML_FOAF_NS, 'gender', 'label');
421 }
422
423 /**
424 * Finds all foaf:jabberID's and inserts them into the result Array
425 */
426
427 function _fetchJabberID()
428 {
429 $this->_fetchProperty(XML_FOAF_NS, 'jabberID', 'label');
430 }
431
432 /**
433 * Finds all foaf:aimChatID's and inserts them into the result Array
434 */
435
436 function _fetchAimChatID()
437 {
438 $this->_fetchProperty(XML_FOAF_NS, 'aimChatID', 'label');
439 }
440
441 /**
442 * Finds all foaf:icqChatID's and inserts them into the result Array
443 */
444
445 function _fetchIcqChatID()
446 {
447 $this->_fetchProperty(XML_FOAF_NS, 'icqChatID', 'label');
448 }
449
450 /**
451 * Finds all foaf:yahooChatID's and inserts them into the result Array
452 */
453
454 function _fetchYahooChatID()
455 {
456 $this->_fetchProperty(XML_FOAF_NS, 'yahooChatID', 'label');
457 }
458
459 /**
460 * Finds all foaf:msnChatID's and inserts them into the result Array
461 */
462
463 function _fetchMsnChatID()
464 {
465 $this->_fetchProperty(XML_FOAF_NS, 'msnChatID', 'label');
466 }
467
468 /**
469 * Finds all foaf:onlineAccount's and inserts them into the result Array
470 */
471
472 function _fetchHoldsAccount()
473 {
474 $holds_account_resource = new Resource(XML_FOAF_NS . 'holdsAccount');
475 $holds_accounts = $this->foaf->find(null,$holds_account_resource,null);
476 $account_name_resource = new Resource(XML_FOAF_NS . 'accountName');
477 $account_names = $this->foaf->find(null,$account_name_resource,null);
478 $account_service_homepage_resource = new Resource(XML_FOAF_NS . 'accountServiceHomepage');
479 $account_service_homepages = $this->foaf->find(null,$account_service_homepage_resource,null);
480 $rdf_type_resource = new Resource(XML_FOAF_RDF_NS . 'type');
481 $rdf_types = $this->foaf->find(null,$rdf_type_resource,null);
482 foreach ($holds_accounts->triples as $holds_account) {
483 foreach ($account_names->triples as $account_name) {
484 if ($account_name->subj->uri == $holds_account->obj->uri) {
485 $accounts[$account_name->subj->uri]['accountname'] = $account_name->obj->label;
486 }
487 }
488 foreach ($account_service_homepages->triples as $account_service_homepage) {
489 if ($account_service_homepage->subj->uri == $holds_account->obj->uri) {
490 $accounts[$account_service_homepage->subj->uri]['accountservicehompage'] = $account_service_homepage->obj->uri;
491 }
492 }
493 foreach ($rdf_types->triples as $rdf_type) {
494 if ($rdf_type->subj->uri == $holds_account->obj->uri) {
495 $account_type = pathinfo($rdf_type->obj->uri);
496 $accounts[$rdf_type->subj->uri]['type'] = $account_type['basename'];
497 }
498 }
499 }
500
501 $online_account_resource = new Resource(XML_FOAF_NS . 'OnlineAccount');
502 $online_accounts = $this->foaf->find(null,null,$online_account_resource);
503 $online_chat_account_resource = new Resource(XML_FOAF_NS .'OnlineChatAccount');
504 $online_chat_accounts = $this->foaf->find(null,null,$online_chat_account_resource);
505 $online_gaming_account_resource = new Resource(XML_FOAF_NS . 'OnlineGamingAccount');
506 $online_gaming_accounts = $this->foaf->find(null,null,$online_gaming_account_resource);
507 $online_ecommerce_account_resource = new Resource(XML_FOAF_NS . 'OnlineEcommerceAccount');
508 $online_ecommerce_accounts = $this->foaf->find(null,null,$online_ecommerce_account_resource);
509
510 foreach ($online_accounts->triples as $account_type) {
511 if (!isset($accounts[$account_type->subj->uri]['type'])) {
512 $accounts[$account_type->subj->uri]['type'] = 'OnlineAccount';
513 }
514 }
515
516 foreach ($online_chat_accounts->triples as $account_type) {
517 if (!isset($accounts[$account_type->subj->uri]['type'])) {
518 $accounts[$account_type->subj->uri]['type'] = 'OnlineChatAccount';
519 }
520 }
521
522 foreach ($online_gaming_accounts->triples as $account_type) {
523 if (!isset($accounts[$account_type->subj->uri]['type'])) {
524 $accounts[$account_type->subj->uri]['type'] = 'OnlineGamingAccount';
525 }
526 }
527
528 foreach ($online_ecommerce_accounts->triples as $account_type) {
529 if (!isset($accounts[$account_type->subj->uri]['type'])) {
530 $accounts[$account_type->subj->uri]['type'] = 'OnlineEcommerceAccount';
531 }
532 }
533
534 foreach ($holds_accounts->triples as $holds_account) {
535 $agent_accounts[$holds_account->subj->uri][] = $accounts[$holds_account->obj->uri];
536 }
537
538 if (isset($agent_accounts)) {
539 foreach ($agent_accounts as $node => $accounts) {
540 if (in_array($node,$this->agent_nodes)) {
541 foreach ($this->foaf_data as $key => $value) {
542 if ($value['node'] == $node) {
543 $this->foaf_data[$key]['holdsaccount'] = $agent_accounts[$node];
544 }
545 break;
546 }
547 } elseif (in_array($node,$this->known_nodes)) {
548 foreach ($this->foaf_data as $agent_key => $agent) {
549 foreach ($agent['knows'] as $holds_account_key => $holds_account_array) {
550 if (isset($holds_account_array['node']) && ($holds_account_array['node'] == $node)) {
551 $this->foaf_data[$agent_key]['knows'][$holds_account_key]['holdsaccount'] = $agent_accounts[$node];
552 break 2;
553 }
554 }
555 }
556 }
557 }
558 }
559 }
560
561 /**
562 * Finds all foaf:homepage's and inserts them into the result Array
563 */
564
565 function _fetchHomepage()
566 {
567 $this->_fetchProperty(XML_FOAF_NS, 'homepage', 'uri');
568 }
569
570 /**
571 * Finds all foaf:weblog's and inserts them into the result Array
572 */
573
574 function _fetchWeblog()
575 {
576 $this->_fetchProperty(XML_FOAF_NS, 'weblog', 'uri');
577 }
578
579 /**
580 * Finds all foaf:made's and inserts them into the result Array
581 */
582
583 function _fetchMade()
584 {
585 $this->_fetchProperty(XML_FOAF_NS, 'made', 'uri');
586 }
587
588 /* foaf:Person */
589
590 /**
591 * Finds all foaf:geekcode's and inserts them into the result Array
592 *
593 * If more than one foaf:geekcode is found for a single foaf:Agent, the
594 * last found will be inserted into the result Array
595 */
596
597 function _getGeekcode()
598 {
599 $this->_getProperty(XML_FOAF_NS, 'geekcode', 'label');
600 }
601
602 /**
603 * Finds all foaf:firstName's and inserts them into the result Array
604 *
605 * If more than one foaf:firstName is found for a single foaf:Agent, the
606 * last found will be inserted into the result Array
607 */
608
609 function _getFirstName()
610 {
611 $this->_getProperty(XML_FOAF_NS, 'firstName', 'label');
612 }
613
614 /**
615 * Finds all foaf:surname's and inserts them into the result Array
616 *
617 * If more than one foaf:surname is found for a single foaf:Agent, the
618 * last found will be inserted into the result Array
619 */
620
621 function _getSurname()
622 {
623 $this->_getProperty(XML_FOAF_NS, 'surname', 'label');
624 }
625
626 /**
627 * Finds all foaf:familyName's and inserts them into the result Array
628 *
629 * If more than one foaf:familyName is found for a single foaf:Agent, the
630 * last found will be inserted into the result Array
631 */
632
633 function _getFamilyName()
634 {
635 $this->_getProperty(XML_FOAF_NS, 'familyName', 'label');
636 }
637
638 /**
639 * Finds all foaf:plan's and inserts them into the result Array
640 *
641 * If more than one foaf:plan is found for a single foaf:Agent, the
642 * last found will be inserted into the result Array
643 */
644
645 function _getPlan()
646 {
647 $this->_getProperty(XML_FOAF_NS, 'plan', 'label');
648 }
649
650 /**
651 * Finds all foaf:img's and inserts them into the result Array
652 */
653
654 function _fetchImg()
655 {
656 $this->_fetchProperty(XML_FOAF_NS, 'img', 'uri');
657 }
658
659 /**
660 * Finds all foaf:myersBriggs's and inserts them into the result Array
661 */
662
663 function _fetchMyersBriggs()
664 {
665 $this->_fetchProperty(XML_FOAF_NS, 'myersBriggs', 'label');
666 }
667
668 /**
669 * Finds all foaf:workplaceHompage's and inserts them into the result Array
670 */
671
672 function _fetchWorkplaceHomepage()
673 {
674 $this->_fetchProperty(XML_FOAF_NS, 'workplaceHomepage', 'uri');
675 }
676
677 /**
678 * Finds all foaf:workInfoHomepage's and inserts them into the result Array
679 */
680
681 function _fetchWorkInfoHomepage()
682 {
683 $this->_fetchProperty(XML_FOAF_NS, 'workInfoHomepage', 'uri');
684 }
685
686 /**
687 * Finds all foaf:schoolHomepage's and inserts them into the result Array
688 */
689
690 function _fetchSchoolHomepage()
691 {
692 $this->_fetchProperty(XML_FOAF_NS, 'schoolHomepage', 'uri');
693 }
694
695 /**
696 * Finds all foaf:publication's and inserts them into the result Array
697 */
698
699 function _fetchPublication()
700 {
701 $this->_fetchProperty(XML_FOAF_NS, 'publication', 'uri');
702 }
703
704 /**
705 * Finds all foaf:currentProject's and inserts them into the result Array
706 */
707
708 function _fetchCurrentProject()
709 {
710 $this->_fetchProperty(XML_FOAF_NS, 'currentProject', 'uri');
711 }
712
713 /**
714 * Finds all foaf:pastProject's and inserts them into the result Array
715 */
716
717 function _fetchPastProject()
718 {
719 $this->_fetchProperty(XML_FOAF_NS, 'pastProject', 'uri');
720 }
721
722 /**
723 * Finds all foaf:basedNear's and inserts them into the result Array
724 */
725
726 function _getBasedNear()
727 {
728
729 }
730
731 /* foaf:Person && foaf:Group */
732
733 /**
734 * Finds all foaf:interest's and inserts them into the result Array
735 */
736
737 function _fetchInterest()
738 {
739 $this->_fetchProperty(XML_FOAF_NS, 'interest', 'uri');
740 }
741
742 /* foaf:Group */
743
744 /**
745 * Finds all foaf:member's and inserts them into the result Array
746 *
747 * @todo Need to figure out how to point to an agent in the foaf_data :)
748 */
749
750 function _fetchMember()
751 {
752
753 }
754
755 /**
756 * Finds all foaf:membershipClass's and inserts them into the result Array
757 *
758 * If more than one foaf:plan is found for a single foaf:Agent, the
759 * last found will be inserted into the result Array
760 *
761 * @todo Use http://xmlns.com/foaf/0.1/#term_Group for reference (second example)
762 * @todo figure out how to point to an agent in the foaf_data
763 */
764
765 function _getMembershipClass()
766 {
767
768 }
769
770 /* end of Agent only methods */
771
772 /**
773 * Finds all rdf:seeAlso's and inserts them into the result Array
774 */
775
776 function _fetchSeeAlso()
777 {
778 $this->_fetchProperty(XML_FOAF_RDF_SCHEMA_NS, 'seeAlso', 'uri');
779 }
780
781 /**
782 * Finds all dc:title's and inserts them into the result Array
783 *
784 * These are inserted at $result['dc']['title'][$uri] where $uri is the URI
785 * they are for. You will need to check this for titles and descriptions upon output
786 * for any element you want them for.
787 */
788
789 function _fetchDcTitle()
790 {
791 $dc_title_resource = new Resource(XML_FOAF_DC_NS . 'title');
792 $dc_titles = $this->foaf->find(null,$dc_title_resource,null);
793 foreach ($dc_titles->triples as $title) {
794 $this->foaf_data['dc']['title'][$title->subj->uri] = $title->obj->label;
795 }
796 }
797
798
799 /**
800 * Finds all dc:description's and inserts them into the result Array
801 *
802 * These are inserted at $result['dc']['description'][$uri] where $uri is the URI
803 * they are for. You will need to check this for titles and descriptions upon output
804 * for any element you want them for.
805 */
806
807 function _fetchDcDescription()
808 {
809 $dc_description_resource = new Resource(XML_FOAF_DC_NS . 'description');
810 $dc_descriptions = $this->foaf->find(null,$dc_description_resource,null);
811 foreach ($dc_descriptions->triples as $description) {
812 $this->foaf_data['dc']['description'][$description->subj->uri] = $description->obj->label;
813 }
814 }
815
816 /**#@-*/
817
818 /**
819 * Fetch a FOAF Property with multiple values
820 *
821 * @param $xmlns string XML Namespace URI
822 * @param $property string XML Element name
823 * @param $obj_value string Triple's "Object" value (label or uri typically)
824 * @access private
825 * @return void
826 */
827
828 function _fetchProperty($xmlns,$property,$obj_value)
829 {
830 $obj_value = strtolower($obj_value);
831 $property_resource = new Resource($xmlns . $property);
832 $properties = $this->foaf->find(null,$property_resource,null);
833 foreach ($properties->triples as $triple) {
834 if (in_array($triple->subj->uri,$this->agent_nodes)) {
835 foreach ($this->agent_nodes as $node_uri => $node_data) {
836 if ($node_data == $triple->subj->uri) {
837 $property = strtolower(str_replace('_','',$property));
838 $this->foaf_data[$node_uri][$property][] = $triple->obj->{$obj_value};
839 }
840 break;
841 }
842 } elseif (in_array($triple->subj->uri,$this->known_nodes)) {
843 foreach ($this->foaf_data as $agent_key => $agent) {
844 if (isset($agent['knows']) && is_array($agent['knows'])) {
845 foreach ($agent['knows'] as $node_uri => $node_data) {
846 if (isset($node_data['node']) && ($node_data['node'] == $triple->subj->uri)) {
847 $property = strtolower(str_replace('_','',$property));
848 $this->foaf_data[$agent_key]['knows'][$node_uri][$property][] = $triple->obj->{$obj_value};
849 break 2;
850 }
851 }
852 }
853 }
854 }
855 }
856 }
857
858 /**
859 * Fetch a FOAF Property with a single value
860 *
861 * @param $xmlns string XML Namespace URI
862 * @param $property string XML Element name
863 * @param $obj_value string Triple's "Object" value (label or uri typically)
864 * @access private
865 * @return void
866 */
867
868 function _getProperty($xmlns,$property,$obj_value)
869 {
870 $obj_value = strtolower($obj_value);
871 $property_resource = new Resource($xmlns . $property);
872 $properties = $this->foaf->find(null,$property_resource,null);
873 foreach ($properties->triples as $triple) {
874 if (in_array($triple->subj->uri,$this->agent_nodes)) {
875 foreach ($this->agent_nodes as $node_uri => $node_data) {
876 if ($node_data == $triple->subj->uri) {
877 $property = strtolower(str_replace('_','',$property));
878 $this->foaf_data[$node_uri][$property] = $triple->obj->{$obj_value};
879 }
880 break;
881 }
882 } elseif (in_array($triple->subj->uri,$this->known_nodes)) {
883 foreach ($this->foaf_data as $agent_key => $agent) {
884 if (isset($agent['knows']) && is_array($agent['knows'])) {
885 foreach ($agent['knows'] as $node_uri => $node_data) {
886 if (isset($node_data['node']) && ($node_data['node'] == $triple->subj->uri)) {
887 $property = strtolower(str_replace('_','',$property));
888 $this->foaf_data[$agent_key]['knows'][$node_uri][$property] = $triple->obj->{$obj_value};
889 break 2;
890 }
891 }
892 }
893 }
894 }
895 }
896 }
897
898 /**
899 * Return parsed FOAF data as an Object
900 *
901 * @todo Make it work!
902 * @access public
903 * @return object
904 */
905
906 function toObject()
907 {
908 $foaf_object = new stdClass();
909 foreach ($this->foaf_data as $key=>$value) {
910 $foaf_object->$key = $value;
911 }
912 return $foaf_object;
913 }
914
915 /**
916 * Return parsed FOAF data as an Array
917 *
918 * @access public
919 * @return array
920 */
921
922 function toArray()
923 {
924 return $this->foaf_data;
925 }
926
927 /**
928 * Return parsed FOAF data pretty HTML
929 *
930 * @todo Write code to return an HTML table
931 * @access public
932 * @return string
933 */
934
935 function toHTML(&$foaf_data)
936 {
937 require_once 'Validate.php';
938 $table = '<table>';
939 if (!is_array ($foaf_data)) {
940 $foaf_data = array();
941 }
942 foreach ($foaf_data as $key => $agent) {
943 if (isset ($agent['type'])) {
944 $table .= '<tr><th colspan="2" class="xml_foaf_' .strtolower($agent['type']). '><h1 class="xml_foaf">';
945 if (isset($agent['name'])) {
946 $table .= $agent['name'];
947 } else {
948 $name = NULL;
949 if (isset($agent['firstname'])) {
950 $name .= $agent['firstname'];
951 } elseif (isset($agent['givenname'])) {
952 $name .= $agent['givenname'];
953 }
954 if (isset($agent['surname'])) {
955 $name .= ' ' .$agent['surname'];
956 } elseif (isset($agent['familyname'])) {
957 $name .= ' ' .$agent['familyname'];
958 }
959 if (is_null($name) && isset($agent['nick'])) {
960 $name = $agent['nick'][0];
961 }
962 if (is_null($name)) {
963 $name = $agent['node'];
964 }
965 $table .= $name;
966 }
967 $table .= '</h1></th></tr>';
968 unset($agent['node']);
969 foreach ($agent as $key => $property) {
970 $table .= '<tr><th>' .$key. '</th><td>';
971 if (!is_array($property)) {
972 if (Validate::uri($property,array('allowed_schemes' => array('http','ftp')))) {
973 $table .= '<a href="' .$property. '">' .$property. '</a></td></tr>';
974 } else {
975 $table .= $property. '</td></tr>';
976 }
977 } else {
978 if ($key == 'knows') {
979 $table .= $this->toHTML($property);
980 } elseif ($key != 'holdsaccount') {
981 $table .= '<ul>';
982 foreach ($property as $child) {
983 if (Validate::uri($child,array('allowed_schemes' => array('http','ftp')))) {
984 $table .= '<li>';
985 if (isset($this->foaf_data['dc']['title'][$child])) {
986 $table .= '<h2 class="xml_foaf"><a href="' .$child. '">';
987 $table .= $this->foaf_data['dc']['title'][$child];
988 $table .= '</a></h2>';
989 } else {
990 $table .= '<a href="' .$child. '">' .$child. '</a>';
991 }
992 if (isset($this->foaf_data['dc']['description'][$child])) {
993 $table .= '<p class="xml_foaf">' .$this->foaf_data['dc']['description'][$child]. '</p>';
994 }
995 $table .= '</li>';
996 } else {
997 $table .= '<li>' .$child. '</li>';
998 }
999 }
1000 $table .= '</ul>';
1001 } else {
1002 foreach ($property as $account) {
1003 $table .= '<table>';
1004 foreach ($account as $key => $data) {
1005 $table .= '<tr><th>' .$key. '</th><td>' .$data. '</td></tr>';
1006 }
1007 $table .= '</table>';
1008 }
1009 }
1010 }
1011 }
1012 }
1013 }
1014 $table .= '</table>';
1015 return $table;
1016 }
1017 }
1018
1019 ?>

Documentation generated on Thu, 15 Jan 2004 19:03:16 +0000 by phpDocumentor 1.2.3