]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/deliciousbackupimporter.php
remove debugging outputter from delicious backup importer
[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.
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                     if (!empty($dt)) {
103                         // No DD provided
104                         $this->importBookmark($user, $dt);
105                         $dt = null;
106                     }
107                     $dt = $child;
108                     break;
109                 case 'dd':
110                     $dd = $child;
111
112                     $saved = $this->importBookmark($user, $dt, $dd);
113
114                     $dt = null;
115                     $dd = null;
116                 case 'p':
117                     common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
118                     break;
119                 default:
120                     common_log(LOG_WARNING, 
121                                "Unexpected element $child->tagName ".
122                                " found in import.");
123                 }
124             } catch (Exception $e) {
125                 common_log(LOG_ERR, $e->getMessage());
126                 $dt = $dd = null;
127             }
128         }
129
130         return true;
131     }
132
133     /**
134      * Import a single bookmark
135      * 
136      * Takes a <dt>/<dd> pair. The <dt> has a single
137      * <a> in it with some non-standard attributes.
138      * 
139      * A <dt><dt><dd> sequence will appear as a <dt> with
140      * anothe <dt> as a child. We handle this case recursively. 
141      *
142      * @param User       $user User to import data as
143      * @param DOMElement $dt   <dt> element
144      * @param DOMElement $dd   <dd> element
145      *
146      * @return Notice imported notice
147      */
148
149     function importBookmark($user, $dt, $dd = null)
150     {
151         // We have to go squirrelling around in the child nodes
152         // on the off chance that we've received another <dt>
153         // as a child.
154
155         for ($i = 0; $i < $dt->childNodes->length; $i++) {
156             $child = $dt->childNodes->item($i);
157             if ($child->nodeType == XML_ELEMENT_NODE) {
158                 if ($child->tagName == 'dt' && !is_null($dd)) {
159                     $this->importBookmark($user, $dt);
160                     $this->importBookmark($user, $child, $dd);
161                     return;
162                 }
163             }
164         }
165
166         $qm = QueueManager::get();
167         
168         $qm->enqueue(array($user, $dt, $dd), 'dlcsbkmk');
169     }
170
171     /**
172      * Parse some HTML
173      *
174      * Hides the errors that the dom parser returns
175      *
176      * @param string $body Data to import
177      *
178      * @return DOMDocument parsed document
179      */
180
181     function importHTML($body)
182     {
183         // DOMDocument::loadHTML may throw warnings on unrecognized elements,
184         // and notices on unrecognized namespaces.
185         $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
186         $dom = new DOMDocument();
187         $ok  = $dom->loadHTML($body);
188         error_reporting($old);
189
190         if ($ok) {
191             return $dom;
192         } else {
193             return null;
194         }
195     }
196 }