]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Starting point for adding bookmarks
authorEvan Prodromou <evan@status.net>
Mon, 20 Dec 2010 17:04:02 +0000 (12:04 -0500)
committerEvan Prodromou <evan@status.net>
Mon, 20 Dec 2010 17:04:02 +0000 (12:04 -0500)
plugins/Bookmark/BookmarkPlugin.php
plugins/Bookmark/deliciousbackupimporter.php [new file with mode: 0644]
plugins/Bookmark/importbookmarks.php [new file with mode: 0644]

index 3f367703911277f4261ce7ed9b796fdf69c368bd..14b1f1d2a5f918a717cf1e16ba7008e041dee018 100644 (file)
@@ -115,6 +115,7 @@ class BookmarkPlugin extends Plugin
                        include_once $dir.'/'.$cls.'.php';
                        return false;
                case 'BookmarkForm':
+               case 'DeliciousBackupImporter':
                        include_once $dir.'/'.strtolower($cls).'.php';
                        return false;
                default:
diff --git a/plugins/Bookmark/deliciousbackupimporter.php b/plugins/Bookmark/deliciousbackupimporter.php
new file mode 100644 (file)
index 0000000..af34f60
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+
+class DeliciousBackupImporter
+{
+       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;
+
+               common_debug("<dl> child nodes is " . $children->length);
+
+               $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;
+                               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());
+                       }
+               }
+       }
+
+       function importBookmark($user, $dt, $dd = null)
+       {
+               common_debug("DT child nodes length = " . $dt->childNodes->length);
+
+               for ($i = 0; $i < $dt->childNodes->length; $i++) {
+                       $child = $dt->childNodes->item($i);
+                       if ($child->nodeType == XML_ELEMENT_NODE) {
+                               common_debug('DT has an element child with tag name '. $child->tagName);
+                       }
+               }
+
+               $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->getAttribute('title');
+               $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)
+       {
+        // 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;
+               }
+       }
+}
diff --git a/plugins/Bookmark/importbookmarks.php b/plugins/Bookmark/importbookmarks.php
new file mode 100644 (file)
index 0000000..3074be3
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010 StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
+
+$shortoptions = 'i:n:f:';
+$longoptions = array('id=', 'nickname=', 'file=');
+
+$helptext = <<<END_OF_IMPORTBOOKMARKS_HELP
+importbookmarks.php [options]
+Restore a backed-up Delicious.com bookmark file
+
+  -i --id       ID of user to import bookmarks for
+  -n --nickname nickname of the user to import for
+  -f --file     file to read from (STDIN by default)
+
+END_OF_IMPORTBOOKMARKS_HELP;
+
+require_once INSTALLDIR.'/scripts/commandline.inc';
+
+function getBookmarksFile()
+{
+    $filename = get_option_value('f', 'file');
+
+    if (empty($filename)) {
+        show_help();
+        exit(1);
+    }
+
+    if (!file_exists($filename)) {
+        throw new Exception("No such file '$filename'.");
+    }
+
+    if (!is_file($filename)) {
+        throw new Exception("Not a regular file: '$filename'.");
+    }
+
+    if (!is_readable($filename)) {
+        throw new Exception("File '$filename' not readable.");
+    }
+
+    // TRANS: Commandline script output. %s is the filename that contains a backup for a user.
+    printfv(_("Getting backup from file '%s'.")."\n",$filename);
+
+    $html = file_get_contents($filename);
+
+       return $html;
+}
+
+
+try {
+       $dbi = new DeliciousBackupImporter();
+
+       $user = getUser();
+
+    $html = getBookmarksFile();
+
+       $dbi->importBookmarks($user, $html);
+
+} catch (Exception $e) {
+    print $e->getMessage()."\n";
+    exit(1);
+}