. * * @category Bookmark * @package StatusNet * @author Evan Prodromou * @copyright 2010 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @link http://status.net/ */ if (!defined('STATUSNET')) { // This check helps protect against security problems; // your code file can't be executed directly from the web. exit(1); } /** * Importer class for Delicious bookmarks * * @category Bookmark * @package StatusNet * @author Evan Prodromou * @copyright 2010 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @link http://status.net/ */ class DeliciousBackupImporter extends QueueHandler { /** * Transport of the importer * * @return string transport string */ function transport() { return 'dlcsback'; } /** * Import an in-memory bookmark list to a user's account * * Take a delicious.com backup file (same as Netscape bookmarks.html) * and import to StatusNet as Bookmark activities. * * The document format is terrible. It consists of a
with * a bunch of
's, occasionally with
's. * There are sometimes

's lost inside. * * @param array $data pair of user, text * * @return boolean success value */ function handle($data) { list($user, $body) = $data; $doc = $this->importHTML($body); $dls = $doc->getElementsByTagName('dl'); if ($dls->length != 1) { throw new ClientException(_("Bad import file.")); } $dl = $dls->item(0); $children = $dl->childNodes; $dt = null; for ($i = 0; $i < $children->length; $i++) { try { $child = $children->item($i); if ($child->nodeType != XML_ELEMENT_NODE) { continue; } switch (strtolower($child->tagName)) { case 'dt': if (!empty($dt)) { // No DD provided $this->importBookmark($user, $dt); $dt = null; } $dt = $child; break; case 'dd': $dd = $child; $saved = $this->importBookmark($user, $dt, $dd); $dt = null; $dd = null; case 'p': common_log(LOG_INFO, 'Skipping the

in the

.'); break; default: common_log(LOG_WARNING, "Unexpected element $child->tagName ". " found in import."); } } catch (Exception $e) { common_log(LOG_ERR, $e->getMessage()); $dt = $dd = null; } } return true; } /** * Import a single bookmark * * Takes a
/
pair. The
has a single * in it with some non-standard attributes. * * A
sequence will appear as a
with * anothe
as a child. We handle this case recursively. * * @param User $user User to import data as * @param DOMElement $dt
element * @param DOMElement $dd
element * * @return Notice imported notice */ function importBookmark($user, $dt, $dd = null) { // We have to go squirrelling around in the child nodes // on the off chance that we've received another
// as a child. for ($i = 0; $i < $dt->childNodes->length; $i++) { $child = $dt->childNodes->item($i); if ($child->nodeType == XML_ELEMENT_NODE) { if ($child->tagName == 'dt' && !is_null($dd)) { $this->importBookmark($user, $dt); $this->importBookmark($user, $child, $dd); return; } } } $qm = QueueManager::get(); $qm->enqueue(array($user, $dt, $dd), 'dlcsbkmk'); } /** * Parse some HTML * * Hides the errors that the dom parser returns * * @param string $body Data to import * * @return DOMDocument parsed document */ function importHTML($body) { // DOMDocument::loadHTML may throw warnings on unrecognized elements, // and notices on unrecognized namespaces. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE)); $dom = new DOMDocument(); $ok = $dom->loadHTML($body); error_reporting($old); if ($ok) { return $dom; } else { return null; } } }