- function importBookmarks($user, $body)
- {
- $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;
- }
- common_log(LOG_INFO, $child->tagName);
- 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 <p> in the <dl>.');
- 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;
- }
- }
- }
-
- 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 <dt>
- // 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;
- }
- }
- }
-
- $as = $dt->getElementsByTagName('a');
-
- if ($as->length == 0) {
- throw new ClientException(_("No <A> tag in a <DT>."));
- }
-
- $a = $as->item(0);
-
- $private = $a->getAttribute('private');
-
- if ($private != 0) {
- throw new ClientException(_('Skipping private bookmark.'));
- }
-
- if (!empty($dd)) {
- $description = $dd->nodeValue;
- } else {
- $description = null;
- }
-
- $title = $a->nodeValue;
- $url = $a->getAttribute('href');
- $tags = $a->getAttribute('tags');
- $addDate = $a->getAttribute('add_date');
- $created = common_sql_date(intval($addDate));
-
- $saved = Notice_bookmark::saveNew($user,
- $title,
- $url,
- $tags,
- $description,
- array('created' => $created));
-
- return $saved;
- }
-
- function importHTML($body)
- {
+ /**
+ * 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 <dl> with
+ * a bunch of <dt>'s, occasionally with <dd>'s adding descriptions.
+ * There are sometimes <p>'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':
+ // <dt> nodes contain primary information about a bookmark.
+ // We can't import the current one just yet though, since
+ // it may be followed by a <dd>.
+ if (!empty($dt)) {
+ // No DD provided
+ $this->importBookmark($user, $dt);
+ $dt = null;
+ }
+ $dt = $child;
+ break;
+ case 'dd':
+ $dd = $child;
+
+ // This <dd> contains a description for the bookmark in
+ // the preceding <dt> node.
+ $saved = $this->importBookmark($user, $dt, $dd);
+
+ $dt = null;
+ $dd = null;
+ break;
+ case 'p':
+ common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
+ 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;
+ }
+ }
+ if (!empty($dt)) {
+ // There was a final bookmark without a description.
+ try {
+ $this->importBookmark($user, $dt);
+ } catch (Exception $e) {
+ common_log(LOG_ERR, $e->getMessage());
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Import a single bookmark
+ *
+ * Takes a <dt>/<dd> pair. The <dt> has a single
+ * <a> in it with some non-standard attributes.
+ *
+ * A <dt><dt><dd> sequence will appear as a <dt> with
+ * anothe <dt> as a child. We handle this case recursively.
+ *
+ * @param User $user User to import data as
+ * @param DOMElement $dt <dt> element
+ * @param DOMElement $dd <dd> element
+ *
+ * @return Notice imported notice
+ */
+
+ function importBookmark($user, $dt, $dd = null)
+ {
+ $as = $dt->getElementsByTagName('a');
+
+ if ($as->length == 0) {
+ throw new ClientException(_("No <A> tag in a <DT>."));
+ }
+
+ $a = $as->item(0);
+
+ $private = $a->getAttribute('private');
+
+ if ($private != 0) {
+ throw new ClientException(_('Skipping private bookmark.'));
+ }
+
+ if (!empty($dd)) {
+ $description = $dd->nodeValue;
+ } else {
+ $description = null;
+ }
+ $addDate = $a->getAttribute('add_date');
+
+ $data = array(
+ 'profile_id' => $user->id,
+ 'title' => $a->nodeValue,
+ 'description' => $description,
+ 'url' => $a->getAttribute('href'),
+ 'tags' => $a->getAttribute('tags'),
+ 'created' => common_sql_date(intval($addDate))
+ );
+
+ $qm = QueueManager::get();
+ $qm->enqueue($data, '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)
+ {