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.
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)) {
104 $this->importBookmark($user, $dt);
112 $saved = $this->importBookmark($user, $dt, $dd);
117 common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
120 common_log(LOG_WARNING,
121 "Unexpected element $child->tagName ".
122 " found in import.");
124 } catch (Exception $e) {
125 common_log(LOG_ERR, $e->getMessage());
134 * Import a single bookmark
136 * Takes a <dt>/<dd> pair. The <dt> has a single
137 * <a> in it with some non-standard attributes.
139 * A <dt><dt><dd> sequence will appear as a <dt> with
140 * anothe <dt> as a child. We handle this case recursively.
142 * @param User $user User to import data as
143 * @param DOMElement $dt <dt> element
144 * @param DOMElement $dd <dd> element
146 * @return Notice imported notice
149 function importBookmark($user, $dt, $dd = null)
151 // We have to go squirrelling around in the child nodes
152 // on the off chance that we've received another <dt>
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);
166 $qm = QueueManager::get();
168 $qm->enqueue(array($user, $dt, $dd), 'dlcsbkmk');
174 * Hides the errors that the dom parser returns
176 * @param string $body Data to import
178 * @return DOMDocument parsed document
181 function importHTML($body)
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);