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