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;
19 common_debug("<dl> child nodes is " . $children->length);
23 for ($i = 0; $i < $children->length; $i++) {
25 $child = $children->item($i);
26 if ($child->nodeType != XML_ELEMENT_NODE) {
29 common_log(LOG_INFO, $child->tagName);
30 switch (strtolower($child->tagName)) {
34 $this->importBookmark($user, $dt);
41 $saved = $this->importBookmark($user, $dt, $dd);
44 common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
47 common_log(LOG_WARNING, "Unexpected element $child->tagName found in import.");
49 } catch (Exception $e) {
50 common_log(LOG_ERR, $e->getMessage());
55 function importBookmark($user, $dt, $dd = null)
57 common_debug("DT child nodes length = " . $dt->childNodes->length);
59 for ($i = 0; $i < $dt->childNodes->length; $i++) {
60 $child = $dt->childNodes->item($i);
61 if ($child->nodeType == XML_ELEMENT_NODE) {
62 common_debug('DT has an element child with tag name '. $child->tagName);
66 $as = $dt->getElementsByTagName('a');
68 if ($as->length == 0) {
69 throw new ClientException(_("No <A> tag in a <DT>."));
74 $private = $a->getAttribute('private');
77 throw new ClientException(_('Skipping private bookmark.'));
81 $description = $dd->nodeValue;
86 $title = $a->getAttribute('title');
87 $url = $a->getAttribute('href');
88 $tags = $a->getAttribute('tags');
89 $addDate = $a->getAttribute('add_date');
90 $created = common_sql_date(intval($addDate));
92 $saved = Notice_bookmark::saveNew($user,
97 array('created' => $created));
102 function importHTML($body)
104 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
105 // and notices on unrecognized namespaces.
106 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
107 $dom = new DOMDocument();
108 $ok = $dom->loadHTML($body);
109 error_reporting($old);