3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Importer class for Delicious.com backups
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Importer class for Delicious bookmarks
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/
48 class DeliciousBackupImporter extends QueueHandler
51 * Transport of the importer
53 * @return string transport string
62 * Import an in-memory bookmark list to a user's account
64 * Take a delicious.com backup file (same as Netscape bookmarks.html)
65 * and import to StatusNet as Bookmark activities.
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.
71 * @param array $data pair of user, text
73 * @return boolean success value
76 function handle($data)
78 list($user, $body) = $data;
80 $doc = $this->importHTML($body);
82 $dls = $doc->getElementsByTagName('dl');
84 if ($dls->length != 1) {
85 throw new ClientException(_("Bad import file."));
90 $children = $dl->childNodes;
94 for ($i = 0; $i < $children->length; $i++) {
96 $child = $children->item($i);
97 if ($child->nodeType != XML_ELEMENT_NODE) {
100 switch (strtolower($child->tagName)) {
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>.
107 $this->importBookmark($user, $dt);
115 // This <dd> contains a description for the bookmark in
116 // the preceding <dt> node.
117 $saved = $this->importBookmark($user, $dt, $dd);
123 common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
126 common_log(LOG_WARNING,
127 "Unexpected element $child->tagName ".
128 " found in import.");
130 } catch (Exception $e) {
131 common_log(LOG_ERR, $e->getMessage());
136 // There was a final bookmark without a description.
138 $this->importBookmark($user, $dt);
139 } catch (Exception $e) {
140 common_log(LOG_ERR, $e->getMessage());
148 * Import a single bookmark
150 * Takes a <dt>/<dd> pair. The <dt> has a single
151 * <a> in it with some non-standard attributes.
153 * A <dt><dt><dd> sequence will appear as a <dt> with
154 * anothe <dt> as a child. We handle this case recursively.
156 * @param User $user User to import data as
157 * @param DOMElement $dt <dt> element
158 * @param DOMElement $dd <dd> element
160 * @return Notice imported notice
163 function importBookmark($user, $dt, $dd = null)
165 $as = $dt->getElementsByTagName('a');
167 if ($as->length == 0) {
168 throw new ClientException(_("No <A> tag in a <DT>."));
173 $private = $a->getAttribute('private');
176 throw new ClientException(_('Skipping private bookmark.'));
180 $description = $dd->nodeValue;
184 $addDate = $a->getAttribute('add_date');
187 'profile_id' => $user->id,
188 'title' => $a->nodeValue,
189 'description' => $description,
190 'url' => $a->getAttribute('href'),
191 'tags' => $a->getAttribute('tags'),
192 'created' => common_sql_date(intval($addDate))
195 $qm = QueueManager::get();
196 $qm->enqueue($data, 'dlcsbkmk');
202 * Hides the errors that the dom parser returns
204 * @param string $body Data to import
206 * @return DOMDocument parsed document
209 function importHTML($body)
211 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
212 // and notices on unrecognized namespaces.
213 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
214 $dom = new DOMDocument();
215 $ok = $dom->loadHTML($body);
216 error_reporting($old);
219 foreach ($dom->getElementsByTagName('body') as $node) {
220 $this->fixListsIn($node);
229 function fixListsIn(DOMNode $body) {
232 foreach ($body->childNodes as $node) {
233 if ($node->nodeType == XML_ELEMENT_NODE) {
234 $el = strtolower($node->nodeName);
241 foreach ($toFix as $node) {
242 $this->fixList($node);
246 function fixList(DOMNode $list) {
249 foreach ($list->childNodes as $node) {
250 if ($node->nodeType == XML_ELEMENT_NODE) {
251 $el = strtolower($node->nodeName);
252 if ($el == 'dt' || $el == 'dd') {
257 // Technically, these can only appear inside a <dd>...
258 $this->fixList($node);
263 foreach ($toFix as $node) {
264 $this->fixListItem($node);
268 function fixListItem(DOMNode $item) {
269 // The HTML parser in libxml2 doesn't seem to properly handle
270 // many cases of implied close tags, apparently because it doesn't
271 // understand the nesting rules specified in the HTML DTD.
273 // This leads to sequences of adjacent <dt>s or <dd>s being incorrectly
274 // interpreted as parent->child trees instead of siblings:
276 // When parsing this input: "<dt>aaa <dt>bbb"
277 // should be equivalent to: "<dt>aaa </dt><dt>bbb</dt>"
278 // but we're seeing instead: "<dt>aaa <dt>bbb</dt></dt>"
280 // It does at least know that going from dt to dd, or dd to dt,
281 // should make a break.
285 foreach ($item->childNodes as $node) {
286 if ($node->nodeType == XML_ELEMENT_NODE) {
287 $el = strtolower($node->nodeName);
288 if ($el == 'dt' || $el == 'dd') {
289 // dt & dd cannot contain each other;
290 // This node was incorrectly placed; move it up a level!
295 // Technically, these can only appear inside a <dd>.
296 $this->fixList($node);
301 $parent = $item->parentNode;
302 $next = $item->nextSibling;
303 foreach ($toMove as $node) {
304 $item->removeChild($node);
305 $parent->insertBefore($node, $next);
306 $this->fixListItem($node);