Skip to content

Commit

Permalink
Support import of Netscape-style HTML bookmark files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Oct 29, 2015
1 parent 313a31f commit 3af33db
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
17 changes: 14 additions & 3 deletions trean/data.php
Expand Up @@ -13,7 +13,9 @@
$app_ob = Horde_Registry::appInit('trean');

/* Importable file types. */
$file_types = array('json' => _("Firefox JSON"));
$file_types = array(
'json' => _("Firefox JSON"),
'html' => _("Netscape-style HTML file"));

/* Templates for the different import steps. */
$templates = array(
Expand All @@ -31,9 +33,18 @@

if ($import_format) {
$data = null;

switch ($import_format) {
case 'html':
$class = 'Trean_Data_Html';
break;
case 'json':
default:
$class = 'Trean_Data_Json';
break;
}
try {
// @TODO: So far, only Firefox JSON
$data = new Trean_Data_Json(
$data = new $class(
$injector->getInstance('Horde_Core_Data_Storage'),
array(
'browser' => $injector->getInstance('Horde_Browser'),
Expand Down
86 changes: 86 additions & 0 deletions trean/lib/Data/Html.php
@@ -0,0 +1,86 @@
<?php
/**
* Horde_Data implementation for Mozilla's HTML format.
*
* Copyright 2013-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package Trean
*/
class Trean_Data_Html extends Horde_Data_Base
{
protected $_extension = 'html';
protected $_contentType = 'text/html';
protected $_folders = array();

public function importData($contents, $header = false)
{
$data = array();
$lines = file($contents);
$rows = array();
foreach ($lines as $line) {
if (strpos($line, '<DT><H3') !== false) {
// Start of a folder.
$this->_folders[] = trim(strip_tags($line));
} elseif (strpos($line, '</DL>') !== false) {
array_pop($this->_folders);
// End of folder.
} elseif (preg_match("/<DT><A HREF=\"*(.*?)\".*>(.*)<\/A>/", $line, $temp)) {
// Bookmark.
$rows[] = array(
'bookmark_url' => trim($temp[1]),
'bookmark_title' => trim($temp[2]),
'bookmark_description' => '',
'bookmark_tags' => $this->_folders,
'bookmark_dt' => false, // @todo
);
} elseif (strpos($line, '<DD>') !== false) {
// Should be description of previous bookmark.
$rows[count($rows) - 1]['bookmark_description'] = trim(strip_tags($line));
}
}

return $rows;
}

/**
* Takes all necessary actions for the given import step, parameters and
* form values and returns the next necessary step.
*
* @param integer $action The current step. One of the IMPORT_* constants.
* @param array $param An associative array containing needed
* parameters for the current step.
*
* @return mixed Either the next step as an integer constant or imported
* data set after the final step.
* @throws Horde_Data_Exception
*/
public function nextStep($action, $param = array())
{
switch ($action) {
case Horde_Data::IMPORT_FILE:
parent::nextStep($action, $param);
return $this->importData($_FILES['import_file']['tmp_name']);
}
}

/**
* Stub to return exported data.
*/
public function exportData($data, $method = 'REQUEST')
{
// TODO
}

/**
* Stub to export data to a file.
*/
public function exportFile($filename, $data)
{
// TODO
}

}
4 changes: 3 additions & 1 deletion trean/package.xml
Expand Up @@ -22,7 +22,7 @@
<email>jan@horde.org</email>
<active>yes</active>
</lead>
<date>2014-12-10</date>
<date>2015-10-29</date>
<version>
<release>1.1.0RC2</release>
<api>1.1.0</api>
Expand Down Expand Up @@ -90,6 +90,7 @@
<file name="Tagsearch.php" role="horde" />
</dir> <!-- /lib/Block -->
<dir name="Data">
<file name="Html.php" role="horde" />
<file name="Json.php" role="horde" />
</dir> <!-- /lib/Data -->
<dir name="Factory">
Expand Down Expand Up @@ -488,6 +489,7 @@
<install as="trean/lib/Block/Bookmarks.php" name="lib/Block/Bookmarks.php" />
<install as="trean/lib/Block/Mostclicked.php" name="lib/Block/Mostclicked.php" />
<install as="trean/lib/Block/Tagsearch.php" name="lib/Block/Tagsearch.php" />
<install as="trean/lib/Data/Html.php" name="lib/Data/Html.php" />
<install as="trean/lib/Data/Json.php" name="lib/Data/Json.php" />
<install as="trean/lib/Factory/TagBrowser.php" name="lib/Factory/TagBrowser.php" />
<install as="trean/lib/Queue/Task/Crawl.php" name="lib/Queue/Task/Crawl.php" />
Expand Down

0 comments on commit 3af33db

Please sign in to comment.