3 class DeliciousBackupImporter
5 function importBookmarks($user, $body)
7 $doc = $this->importHTML($body);
9 $dls = $doc->getElementsByTagName('dl');
11 if ($dls->length != 1) {
12 throw new ClientException(_("Bad import file."));
17 $children = $dl->childNodes;
21 for ($i = 0; $i < $children->length; $i++) {
23 $child = $children->item($i);
24 if ($child->nodeType != XML_ELEMENT_NODE) {
27 common_log(LOG_INFO, $child->tagName);
28 switch (strtolower($child->tagName)) {
32 $this->importBookmark($user, $dt);
39 $saved = $this->importBookmark($user, $dt, $dd);
43 common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
46 common_log(LOG_WARNING, "Unexpected element $child->tagName found in import.");
48 } catch (Exception $e) {
49 common_log(LOG_ERR, $e->getMessage());
55 function importBookmark($user, $dt, $dd = null)
57 // We have to go squirrelling around in the child nodes
58 // on the off chance that we've received another <dt>
61 for ($i = 0; $i < $dt->childNodes->length; $i++) {
62 $child = $dt->childNodes->item($i);
63 if ($child->nodeType == XML_ELEMENT_NODE) {
64 if ($child->tagName == 'dt' && !is_null($dd)) {
65 $this->importBookmark($user, $dt);
66 $this->importBookmark($user, $child, $dd);
72 $as = $dt->getElementsByTagName('a');
74 if ($as->length == 0) {
75 throw new ClientException(_("No <A> tag in a <DT>."));
80 $private = $a->getAttribute('private');
83 throw new ClientException(_('Skipping private bookmark.'));
87 $description = $dd->nodeValue;
92 $title = $a->nodeValue;
93 $url = $a->getAttribute('href');
94 $tags = $a->getAttribute('tags');
95 $addDate = $a->getAttribute('add_date');
96 $created = common_sql_date(intval($addDate));
98 $saved = Notice_bookmark::saveNew($user,
103 array('created' => $created));
108 function importHTML($body)
110 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
111 // and notices on unrecognized namespaces.
112 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
113 $dom = new DOMDocument();
114 $ok = $dom->loadHTML($body);
115 error_reporting($old);