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/
47 class DeliciousBackupImporter extends QueueHandler
50 * Transport of the importer
52 * @return string transport string
60 * Import an in-memory bookmark list to a user's account
62 * Take a delicious.com backup file (same as Netscape bookmarks.html)
63 * and import to StatusNet as Bookmark activities.
65 * The document format is terrible. It consists of a <dl> with
66 * a bunch of <dt>'s, occasionally with <dd>'s adding descriptions.
67 * There are sometimes <p>'s lost inside.
69 * @param array $data pair of user, text
71 * @return boolean success value
73 function handle($data)
75 list($user, $body) = $data;
77 $doc = $this->importHTML($body);
79 // If we can't parse it, it's no good
85 $dls = $doc->getElementsByTagName('dl');
87 if ($dls->length != 1) {
88 // TRANS: Client exception thrown when a file upload is incorrect.
89 throw new ClientException(_m('Bad import file.'));
94 $children = $dl->childNodes;
98 for ($i = 0; $i < $children->length; $i++) {
100 $child = $children->item($i);
101 if ($child->nodeType != XML_ELEMENT_NODE) {
104 switch (strtolower($child->tagName)) {
106 // <dt> nodes contain primary information about a bookmark.
107 // We can't import the current one just yet though, since
108 // it may be followed by a <dd>.
111 $this->importBookmark($user, $dt);
120 // This <dd> contains a description for the bookmark in
121 // the preceding <dt> node.
122 $saved = $this->importBookmark($user, $dt, $dd);
129 common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
132 common_log(LOG_WARNING,
133 "Unexpected element $child->tagName ".
134 " found in import.");
136 } catch (Exception $e) {
137 common_log(LOG_ERR, $e->getMessage());
142 // There was a final bookmark without a description.
144 $this->importBookmark($user, $dt);
145 } catch (Exception $e) {
146 common_log(LOG_ERR, $e->getMessage());
154 * Import a single bookmark
156 * Takes a <dt>/<dd> pair. The <dt> has a single
157 * <a> in it with some non-standard attributes.
159 * A <dt><dt><dd> sequence will appear as a <dt> with
160 * anothe <dt> as a child. We handle this case recursively.
162 * @param User $user User to import data as
163 * @param DOMElement $dt <dt> element
164 * @param DOMElement $dd <dd> element
166 * @return Notice imported notice
168 function importBookmark($user, $dt, $dd = null)
170 $as = $dt->getElementsByTagName('a');
172 if ($as->length == 0) {
173 // TRANS: Client exception thrown when a bookmark in an import file is incorrectly formatted.
174 throw new ClientException(_m("No <A> tag in a <DT>."));
179 $private = $a->getAttribute('private');
182 // TRANS: Client exception thrown when a bookmark in an import file is private.
183 throw new ClientException(_m('Skipping private bookmark.'));
187 $description = $dd->nodeValue;
191 $addDate = $a->getAttribute('add_date');
194 'profile_id' => $user->id,
195 'title' => $a->nodeValue,
196 'description' => $description,
197 'url' => $a->getAttribute('href'),
198 'tags' => $a->getAttribute('tags'),
199 'created' => common_sql_date(intval($addDate))
202 $qm = QueueManager::get();
203 $qm->enqueue($data, 'dlcsbkmk');
209 * Hides the errors that the dom parser returns
211 * @param string $body Data to import
213 * @return DOMDocument parsed document
216 function importHTML($body)
218 // DOMDocument::loadHTML may throw warnings on unrecognized elements,
219 // and notices on unrecognized namespaces.
220 $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
221 $dom = new DOMDocument();
222 $ok = $dom->loadHTML($body);
223 error_reporting($old);
226 foreach ($dom->getElementsByTagName('body') as $node) {
227 $this->fixListsIn($node);
236 function fixListsIn(DOMNode $body) {
239 foreach ($body->childNodes as $node) {
240 if ($node->nodeType == XML_ELEMENT_NODE) {
241 $el = strtolower($node->nodeName);
248 foreach ($toFix as $node) {
249 $this->fixList($node);
253 function fixList(DOMNode $list) {
256 foreach ($list->childNodes as $node) {
257 if ($node->nodeType == XML_ELEMENT_NODE) {
258 $el = strtolower($node->nodeName);
259 if ($el == 'dt' || $el == 'dd') {
264 // Technically, these can only appear inside a <dd>...
265 $this->fixList($node);
270 foreach ($toFix as $node) {
271 $this->fixListItem($node);
275 function fixListItem(DOMNode $item) {
276 // The HTML parser in libxml2 doesn't seem to properly handle
277 // many cases of implied close tags, apparently because it doesn't
278 // understand the nesting rules specified in the HTML DTD.
280 // This leads to sequences of adjacent <dt>s or <dd>s being incorrectly
281 // interpreted as parent->child trees instead of siblings:
283 // When parsing this input: "<dt>aaa <dt>bbb"
284 // should be equivalent to: "<dt>aaa </dt><dt>bbb</dt>"
285 // but we're seeing instead: "<dt>aaa <dt>bbb</dt></dt>"
287 // It does at least know that going from dt to dd, or dd to dt,
288 // should make a break.
292 foreach ($item->childNodes as $node) {
293 if ($node->nodeType == XML_ELEMENT_NODE) {
294 $el = strtolower($node->nodeName);
295 if ($el == 'dt' || $el == 'dd') {
296 // dt & dd cannot contain each other;
297 // This node was incorrectly placed; move it up a level!
302 // Technically, these can only appear inside a <dd>.
303 $this->fixList($node);
308 $parent = $item->parentNode;
309 $next = $item->nextSibling;
310 foreach ($toMove as $node) {
311 $item->removeChild($node);
312 $parent->insertBefore($node, $next);
313 $this->fixListItem($node);