]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/deliciousbackupimporter.php
Starting point for adding 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                 common_debug("<dl> child nodes is " . $children->length);
20
21                 $dt = null;
22
23                 for ($i = 0; $i < $children->length; $i++) {
24                         try {
25                                 $child = $children->item($i);
26                                 if ($child->nodeType != XML_ELEMENT_NODE) {
27                                         continue;
28                                 }
29                                 common_log(LOG_INFO, $child->tagName);
30                                 switch (strtolower($child->tagName)) {
31                                 case 'dt':
32                                         if (!empty($dt)) {
33                                                 // No DD provided
34                                                 $this->importBookmark($user, $dt);
35                                                 $dt = null;
36                                         }
37                                         $dt = $child;
38                                         break;
39                                 case 'dd':
40                                         $dd = $child;
41                                         $saved = $this->importBookmark($user, $dt, $dd);
42                                         $dt = null;
43                                 case 'p':
44                                         common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
45                                         break;
46                                 default:
47                                         common_log(LOG_WARNING, "Unexpected element $child->tagName found in import.");
48                                 }
49                         } catch (Exception $e) {
50                                 common_log(LOG_ERR, $e->getMessage());
51                         }
52                 }
53         }
54
55         function importBookmark($user, $dt, $dd = null)
56         {
57                 common_debug("DT child nodes length = " . $dt->childNodes->length);
58
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);
63                         }
64                 }
65
66                 $as = $dt->getElementsByTagName('a');
67
68                 if ($as->length == 0) {
69                         throw new ClientException(_("No <A> tag in a <DT>."));
70                 }
71
72                 $a = $as->item(0);
73                                         
74                 $private = $a->getAttribute('private');
75
76                 if ($private != 0) {
77                         throw new ClientException(_('Skipping private bookmark.'));
78                 }
79
80                 if (!empty($dd)) {
81                         $description = $dd->nodeValue;
82                 } else {
83                         $description = null;
84                 }
85
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));
91
92                 $saved = Notice_bookmark::saveNew($user,
93                                                                                   $title,
94                                                                                   $url,
95                                                                                   $tags,
96                                                                                   $description,
97                                                                                   array('created' => $created));
98
99                 return $saved;
100         }
101
102         function importHTML($body)
103         {
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);
110
111                 if ($ok) {
112                         return $dom;
113                 } else {
114                         return null;
115                 }
116         }
117 }