]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/deliciousbackupimporter.php
some corrections for double-posting of bookmarks
[quix0rs-gnu-social.git] / plugins / Bookmark / deliciousbackupimporter.php
1 <?php
2
3 class DeliciousBackupImporter
4 {
5         function importBookmarks($user, $body)
6         {
7                 $doc = $this->importHTML($body);
8
9                 $dls = $doc->getElementsByTagName('dl');
10
11                 if ($dls->length != 1) {
12                         throw new ClientException(_("Bad import file."));
13                 }
14
15                 $dl = $dls->item(0);
16
17                 $children = $dl->childNodes;
18
19                 $dt = null;
20
21                 for ($i = 0; $i < $children->length; $i++) {
22                         try {
23                                 $child = $children->item($i);
24                                 if ($child->nodeType != XML_ELEMENT_NODE) {
25                                         continue;
26                                 }
27                                 common_log(LOG_INFO, $child->tagName);
28                                 switch (strtolower($child->tagName)) {
29                                 case 'dt':
30                                         if (!empty($dt)) {
31                                                 // No DD provided
32                                                 $this->importBookmark($user, $dt);
33                                                 $dt = null;
34                                         }
35                                         $dt = $child;
36                                         break;
37                                 case 'dd':
38                                         $dd = $child;
39                                         $saved = $this->importBookmark($user, $dt, $dd);
40                                         $dt = null;
41                                         $dd = null;
42                                 case 'p':
43                                         common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
44                                         break;
45                                 default:
46                                         common_log(LOG_WARNING, "Unexpected element $child->tagName found in import.");
47                                 }
48                         } catch (Exception $e) {
49                                 common_log(LOG_ERR, $e->getMessage());
50                                 $dt = $dd = null;
51                         }
52                 }
53         }
54
55         function importBookmark($user, $dt, $dd = null)
56         {
57                 // We have to go squirrelling around in the child nodes
58                 // on the off chance that we've received another <dt>
59                 // as a child.
60
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);
67                                         return;
68                                 }
69                         }
70                 }
71
72                 $as = $dt->getElementsByTagName('a');
73
74                 if ($as->length == 0) {
75                         throw new ClientException(_("No <A> tag in a <DT>."));
76                 }
77
78                 $a = $as->item(0);
79                                         
80                 $private = $a->getAttribute('private');
81
82                 if ($private != 0) {
83                         throw new ClientException(_('Skipping private bookmark.'));
84                 }
85
86                 if (!empty($dd)) {
87                         $description = $dd->nodeValue;
88                 } else {
89                         $description = null;
90                 }
91
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));
97
98                 $saved = Notice_bookmark::saveNew($user,
99                                                                                   $title,
100                                                                                   $url,
101                                                                                   $tags,
102                                                                                   $description,
103                                                                                   array('created' => $created));
104
105                 return $saved;
106         }
107
108         function importHTML($body)
109         {
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);
116
117                 if ($ok) {
118                         return $dom;
119                 } else {
120                         return null;
121                 }
122         }
123 }