]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/deliciousbackupimporter.php
bc5a91be8072c11c13ab4b74970d61c836884c98
[quix0rs-gnu-social.git] / plugins / Bookmark / deliciousbackupimporter.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Importer class for Delicious.com backups
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Bookmark
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Importer class for Delicious bookmarks
39  *
40  * @category  Bookmark
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class DeliciousBackupImporter extends QueueHandler
49 {
50     /**
51      * Transport of the importer
52      *
53      * @return string transport string
54      */
55
56     function transport()
57     {
58         return 'dlcsback';
59     }
60
61     /**
62      * Import an in-memory bookmark list to a user's account
63      *
64      * Take a delicious.com backup file (same as Netscape bookmarks.html)
65      * and import to StatusNet as Bookmark activities.
66      *
67      * The document format is terrible. It consists of a <dl> with
68      * a bunch of <dt>'s, occasionally with <dd>'s adding descriptions.
69      * There are sometimes <p>'s lost inside.
70      *
71      * @param array $data pair of user, text
72      *
73      * @return boolean success value
74      */
75
76     function handle($data)
77     {
78         list($user, $body) = $data;
79
80         $doc = $this->importHTML($body);
81
82         $dls = $doc->getElementsByTagName('dl');
83
84         if ($dls->length != 1) {
85             throw new ClientException(_("Bad import file."));
86         }
87
88         $dl = $dls->item(0);
89
90         $children = $dl->childNodes;
91
92         $dt = null;
93
94         for ($i = 0; $i < $children->length; $i++) {
95             try {
96                 $child = $children->item($i);
97                 if ($child->nodeType != XML_ELEMENT_NODE) {
98                     continue;
99                 }
100                 switch (strtolower($child->tagName)) {
101                 case 'dt':
102                     // <dt> nodes contain primary information about a bookmark.
103                     // We can't import the current one just yet though, since
104                     // it may be followed by a <dd>.
105                     if (!empty($dt)) {
106                         // No DD provided
107                         $this->importBookmark($user, $dt);
108                         $dt = null;
109                     }
110                     $dt = $child;
111                     break;
112                 case 'dd':
113                     $dd = $child;
114
115                     // This <dd> contains a description for the bookmark in
116                     // the preceding <dt> node.
117                     $saved = $this->importBookmark($user, $dt, $dd);
118
119                     $dt = null;
120                     $dd = null;
121                     break;
122                 case 'p':
123                     common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
124                     break;
125                 default:
126                     common_log(LOG_WARNING, 
127                                "Unexpected element $child->tagName ".
128                                " found in import.");
129                 }
130             } catch (Exception $e) {
131                 common_log(LOG_ERR, $e->getMessage());
132                 $dt = $dd = null;
133             }
134         }
135         if (!empty($dt)) {
136             // There was a final bookmark without a description.
137             try {
138                 $this->importBookmark($user, $dt);
139             } catch (Exception $e) {
140                 common_log(LOG_ERR, $e->getMessage());
141             }
142         }
143
144         return true;
145     }
146
147     /**
148      * Import a single bookmark
149      * 
150      * Takes a <dt>/<dd> pair. The <dt> has a single
151      * <a> in it with some non-standard attributes.
152      * 
153      * A <dt><dt><dd> sequence will appear as a <dt> with
154      * anothe <dt> as a child. We handle this case recursively. 
155      *
156      * @param User       $user User to import data as
157      * @param DOMElement $dt   <dt> element
158      * @param DOMElement $dd   <dd> element
159      *
160      * @return Notice imported notice
161      */
162
163     function importBookmark($user, $dt, $dd = null)
164     {
165         $qm = QueueManager::get();
166         
167         $qm->enqueue(array($user, $dt, $dd), 'dlcsbkmk');
168     }
169
170     /**
171      * Parse some HTML
172      *
173      * Hides the errors that the dom parser returns
174      *
175      * @param string $body Data to import
176      *
177      * @return DOMDocument parsed document
178      */
179
180     function importHTML($body)
181     {
182         // DOMDocument::loadHTML may throw warnings on unrecognized elements,
183         // and notices on unrecognized namespaces.
184         $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
185         $dom = new DOMDocument();
186         $ok  = $dom->loadHTML($body);
187         error_reporting($old);
188
189         if ($ok) {
190             foreach ($dom->getElementsByTagName('body') as $node) {
191                 $this->fixListsIn($node);
192             }
193             return $dom;
194         } else {
195             return null;
196         }
197     }
198
199
200     function fixListsIn(DOMNode $body) {
201         $toFix = array();
202
203         foreach ($body->childNodes as $node) {
204             if ($node->nodeType == XML_ELEMENT_NODE) {
205                 $el = strtolower($node->nodeName);
206                 if ($el == 'dl') {
207                     $toFix[] = $node;
208                 }
209             }
210         }
211
212         foreach ($toFix as $node) {
213             $this->fixList($node);
214         }
215     }
216
217     function fixList(DOMNode $list) {
218         $toFix = array();
219
220         foreach ($list->childNodes as $node) {
221             if ($node->nodeType == XML_ELEMENT_NODE) {
222                 $el = strtolower($node->nodeName);
223                 if ($el == 'dt' || $el == 'dd') {
224                     $toFix[] = $node;
225                 }
226                 if ($el == 'dl') {
227                     // Sublist.
228                     // Technically, these can only appear inside a <dd>...
229                     $this->fixList($node);
230                 }
231             }
232         }
233
234         foreach ($toFix as $node) {
235             $this->fixListItem($node);
236         }
237     }
238
239     function fixListItem(DOMNode $item) {
240         // The HTML parser in libxml2 doesn't seem to properly handle
241         // many cases of implied close tags, apparently because it doesn't
242         // understand the nesting rules specified in the HTML DTD.
243         //
244         // This leads to sequences of adjacent <dt>s or <dd>s being incorrectly
245         // interpreted as parent->child trees instead of siblings:
246         //
247         // When parsing this input: "<dt>aaa <dt>bbb"
248         // should be equivalent to: "<dt>aaa </dt><dt>bbb</dt>"
249         // but we're seeing instead: "<dt>aaa <dt>bbb</dt></dt>"
250         //
251         // It does at least know that going from dt to dd, or dd to dt,
252         // should make a break.
253
254         $toMove = array();
255
256         foreach ($item->childNodes as $node) {
257             if ($node->nodeType == XML_ELEMENT_NODE) {
258                 $el = strtolower($node->nodeName);
259                 if ($el == 'dt' || $el == 'dd') {
260                     // dt & dd cannot contain each other;
261                     // This node was incorrectly placed; move it up a level!
262                     $toMove[] = $node;
263                 }
264                 if ($el == 'dl') {
265                     // Sublist.
266                     // Technically, these can only appear inside a <dd>.
267                     $this->fixList($node);
268                 }
269             }
270         }
271
272         $parent = $item->parentNode;
273         $next = $item->nextSibling;
274         foreach ($toMove as $node) {
275             $item->removeChild($node);
276             $parent->insertBefore($node, $next);
277             $this->fixListItem($node);
278         }
279     }
280
281 }