]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Bookmark/deliciousbackupimporter.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / Bookmark / deliciousbackupimporter.php
index 6ab87b5212a4931f6eba0055a9ab0d46d2f51d66..0ceba61d89a1619b009cf771bf4e20b82b335a1a 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2010, StatusNet, Inc.
  *
  * Importer class for Delicious.com backups
- * 
+ *
  * PHP version 5
  *
  * This program is free software: you can redistribute it and/or modify
@@ -34,8 +34,6 @@ if (!defined('STATUSNET')) {
     exit(1);
 }
 
-require_once INSTALLDIR . '/lib/apiauth.php';
-
 /**
  * Importer class for Delicious bookmarks
  *
@@ -46,9 +44,18 @@ require_once INSTALLDIR . '/lib/apiauth.php';
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
-
-class DeliciousBackupImporter
+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
      *
@@ -56,23 +63,37 @@ class DeliciousBackupImporter
      * 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.
+     * a bunch of <dt>'s, occasionally with <dd>'s adding descriptions.
      * There are sometimes <p>'s lost inside.
      *
-     * @param User   $user User whose feed we're going to fill
-     * @param string $body Body of the file
+     * @param array $data pair of user, text
      *
-     * @return void
+     * @return boolean success value
      */
-
-    function importBookmarks($user, $body)
+    function handle($data)
     {
-        $doc = $this->importHTML($body);
+        list($user, $body) = $data;
+
+        try {
+            $doc = $this->importHTML($body);
+        } catch (ClientException $cex) {
+            // XXX: message to the user
+            common_log(LOG_WARNING, $cex->getMessage());
+            return true;
+        }
+
+        // If we can't parse it, it's no good
+
+        if (empty($doc)) {
+            return true;
+        }
 
         $dls = $doc->getElementsByTagName('dl');
 
         if ($dls->length != 1) {
-            throw new ClientException(_("Bad import file."));
+            // XXX: message to the user
+            common_log(LOG_WARNING, 'Bad input file');
+            return true;
         }
 
         $dl = $dls->item(0);
@@ -87,9 +108,11 @@ class DeliciousBackupImporter
                 if ($child->nodeType != XML_ELEMENT_NODE) {
                     continue;
                 }
-                common_log(LOG_INFO, $child->tagName);
                 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);
@@ -100,15 +123,20 @@ class DeliciousBackupImporter
                 case 'dd':
                     $dd = $child;
 
-                    $saved = $this->importBookmark($user, $dt, $dd);
+                    if (!empty($dt)) {
+                        // 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, 
+                    common_log(LOG_WARNING,
                                "Unexpected element $child->tagName ".
                                " found in import.");
                 }
@@ -117,16 +145,26 @@ class DeliciousBackupImporter
                 $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. 
+     * anothe <dt> as a child. We handle this case recursively.
      *
      * @param User       $user User to import data as
      * @param DOMElement $dt   <dt> element
@@ -134,36 +172,22 @@ class DeliciousBackupImporter
      *
      * @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 <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>."));
+            // TRANS: Client exception thrown when a bookmark in an import file is incorrectly formatted.
+            throw new ClientException(_m("No <A> tag in a <DT>."));
         }
 
         $a = $as->item(0);
-                    
+
         $private = $a->getAttribute('private');
 
         if ($private != 0) {
-            throw new ClientException(_('Skipping private bookmark.'));
+            // TRANS: Client exception thrown when a bookmark in an import file is private.
+            throw new ClientException(_m('Skipping private bookmark.'));
         }
 
         if (!empty($dd)) {
@@ -171,21 +195,19 @@ class DeliciousBackupImporter
         } 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));
+        $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))
+        );
 
-        return $saved;
+        $qm = QueueManager::get();
+        $qm->enqueue($data, 'dlcsbkmk');
     }
 
     /**
@@ -208,9 +230,94 @@ class DeliciousBackupImporter
         error_reporting($old);
 
         if ($ok) {
+            foreach ($dom->getElementsByTagName('body') as $node) {
+                $this->fixListsIn($node);
+            }
             return $dom;
         } else {
             return null;
         }
     }
+
+
+    function fixListsIn(DOMNode $body) {
+        $toFix = array();
+
+        foreach ($body->childNodes as $node) {
+            if ($node->nodeType == XML_ELEMENT_NODE) {
+                $el = strtolower($node->nodeName);
+                if ($el == 'dl') {
+                    $toFix[] = $node;
+                }
+            }
+        }
+
+        foreach ($toFix as $node) {
+            $this->fixList($node);
+        }
+    }
+
+    function fixList(DOMNode $list) {
+        $toFix = array();
+
+        foreach ($list->childNodes as $node) {
+            if ($node->nodeType == XML_ELEMENT_NODE) {
+                $el = strtolower($node->nodeName);
+                if ($el == 'dt' || $el == 'dd') {
+                    $toFix[] = $node;
+                }
+                if ($el == 'dl') {
+                    // Sublist.
+                    // Technically, these can only appear inside a <dd>...
+                    $this->fixList($node);
+                }
+            }
+        }
+
+        foreach ($toFix as $node) {
+            $this->fixListItem($node);
+        }
+    }
+
+    function fixListItem(DOMNode $item) {
+        // The HTML parser in libxml2 doesn't seem to properly handle
+        // many cases of implied close tags, apparently because it doesn't
+        // understand the nesting rules specified in the HTML DTD.
+        //
+        // This leads to sequences of adjacent <dt>s or <dd>s being incorrectly
+        // interpreted as parent->child trees instead of siblings:
+        //
+        // When parsing this input: "<dt>aaa <dt>bbb"
+        // should be equivalent to: "<dt>aaa </dt><dt>bbb</dt>"
+        // but we're seeing instead: "<dt>aaa <dt>bbb</dt></dt>"
+        //
+        // It does at least know that going from dt to dd, or dd to dt,
+        // should make a break.
+
+        $toMove = array();
+
+        foreach ($item->childNodes as $node) {
+            if ($node->nodeType == XML_ELEMENT_NODE) {
+                $el = strtolower($node->nodeName);
+                if ($el == 'dt' || $el == 'dd') {
+                    // dt & dd cannot contain each other;
+                    // This node was incorrectly placed; move it up a level!
+                    $toMove[] = $node;
+                }
+                if ($el == 'dl') {
+                    // Sublist.
+                    // Technically, these can only appear inside a <dd>.
+                    $this->fixList($node);
+                }
+            }
+        }
+
+        $parent = $item->parentNode;
+        $next = $item->nextSibling;
+        foreach ($toMove as $node) {
+            $item->removeChild($node);
+            $parent->insertBefore($node, $next);
+            $this->fixListItem($node);
+        }
+    }
 }