2 // Open CouchDb connection to Ajatus
3 require('CouchDb/Couch.php');
4 $couch = new Couch(array('host' => 'localhost', 'port' => 5984));
5 if (!$couch->running())
7 die("Cannot connect to CouchDB in {$couch->host()}:{$coudh->port()}\n");
10 $couchdb = $couch->database('ajatus_db_content', $couch);
11 if(!$couchdb->exists())
13 die("CouchDB database {$couchdb->name()} does not exist\n");
17 if (substr($url, 0, 4) != 'http')
19 die("Usage:\n php sync.php http://username:password@www.example.net/openpsa\n");
22 if (substr($url, -1) != '/')
28 $fp = @fopen
("{$url}contacts/buddylist/xml", 'r');
31 die("Failed retrieving Buddy List\n");
36 $xml .= fread($fp, 8192);
42 die("Failed to read buddy data\n");
45 $simplexml = simplexml_load_string($xml);
46 if (isset($simplexml->buddy
))
48 foreach ($simplexml->buddy
as $buddy)
52 foreach ($buddy->attributes() as $key => $value)
61 foreach ($buddy as $field => $value)
63 $buddydata[$field] = (string) $value;
66 // Get either existing object from Couch or initialize empty object
67 $document = $couchdb->get($couch_id);
68 $document->value
= array();
69 $document->value
['_type'] = 'contact';
70 $document->value
['_source'] = 'Midgard';
71 $document->value
['metadata'] = array();
72 $document->value
['metadata']['archived'] = array('val' => '');
73 $document->value
['metadata']['deleted'] = array('val' => 0);
74 foreach ($buddydata as $field => $value)
76 // Map OpenPsa field names to Ajatus field names
80 $document->value
['xmpp'] = array('val' => $value);
82 case 'person_homepage':
83 $document->value
['homepage'] = array('val' => $value);
86 $document->value
['description'] = array('val' => $value);
89 $document->value
[$field] = array('val' => $value);
92 if (!$document->save())
94 echo "Failed saving contact {$guid}, {$document->lastStatusCode}\n";
98 echo "Contact {$guid} saved\n";