Make the tag result list scrollable so long tags can be read
[ajatus.git] / plugins / openpsa / sync.php
blob67cd64443ac64d873a4b1a48122300b08579e52b
1 <?php
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");
16 $url = $argv[1];
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) != '/')
24 $url .= '/';
27 // Get buddies
28 $fp = @fopen("{$url}contacts/buddylist/xml", 'r');
29 if (!$fp)
31 die("Failed retrieving Buddy List\n");
33 $xml = '';
34 while (!feof($fp))
36 $xml .= fread($fp, 8192);
38 fclose($fp);
40 if (empty($xml))
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)
50 $guid = '';
51 $buddydata = array();
52 foreach ($buddy->attributes() as $key => $value)
54 if ($key == 'guid')
56 $guid = $value;
59 $couch_id = $guid;
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
77 switch ($field)
79 case 'jid':
80 $document->value['xmpp'] = array('val' => $value);
81 break;
82 case 'person_homepage':
83 $document->value['homepage'] = array('val' => $value);
84 break;
85 case 'notes':
86 $document->value['description'] = array('val' => $value);
87 break;
88 default:
89 $document->value[$field] = array('val' => $value);
92 if (!$document->save())
94 echo "Failed saving contact {$guid}, {$document->lastStatusCode}\n";
96 else
98 echo "Contact {$guid} saved\n";