<?php
+use Psr\Log\LogLevel;
+use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser;
+use Katzgrau\KLogger\Logger;
+
/**
* Utilities to import and export bookmarks using the Netscape format
+ * TODO: Not static, use a container.
*/
class NetscapeBookmarkUtils
{
/**
* Imports Web bookmarks from an uploaded Netscape bookmark dump
*
- * @param array $post Server $_POST parameters
- * @param array $files Server $_FILES parameters
- * @param LinkDB $linkDb Loaded LinkDB instance
- * @param string $pagecache Page cache
+ * @param array $post Server $_POST parameters
+ * @param array $files Server $_FILES parameters
+ * @param LinkDB $linkDb Loaded LinkDB instance
+ * @param ConfigManager $conf instance
*
* @return string Summary of the bookmark import status
*/
- public static function import($post, $files, $linkDb, $pagecache)
+ public static function import($post, $files, $linkDb, $conf)
{
$filename = $files['filetoupload']['name'];
$filesize = $files['filetoupload']['size'];
$defaultPrivacy = 0;
$parser = new NetscapeBookmarkParser(
- true, // nested tag support
- $defaultTags, // additional user-specified tags
- strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
+ true, // nested tag support
+ $defaultTags, // additional user-specified tags
+ strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy
+ $conf->get('resource.data_dir') // log path, will be overridden
+ );
+ $logger = new Logger(
+ $conf->get('resource.data_dir'),
+ ! $conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG,
+ [
+ 'prefix' => 'import.',
+ 'extension' => 'log',
+ ]
);
+ $parser->setLogger($logger);
$bookmarks = $parser->parseString($data);
$importCount = 0;
$importCount++;
}
- $linkDb->save($pagecache);
+ $linkDb->save($conf->get('resource.page_cache'));
return self::importStatus(
$filename,
$filesize,
*/
protected $pagecache = 'tests';
+ /**
+ * @var ConfigManager instance.
+ */
+ protected $conf;
+
/**
* @var string Save the current timezone.
*/
// start with an empty datastore
file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
$this->linkDb = new LinkDB(self::$testDatastore, true, false);
+ $this->conf = new ConfigManager('tests/utils/config/configJson');
+ $this->conf->set('resource.page_cache', $this->pagecache);
}
public static function tearDownAfterClass()
$this->assertEquals(
'File empty.htm (0 bytes) has an unknown file format.'
.' Nothing was imported.',
- NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
+ NetscapeBookmarkUtils::import(NULL, $files, NULL, $this->conf)
);
$this->assertEquals(0, count($this->linkDb));
}
$files = file2array('no_doctype.htm');
$this->assertEquals(
'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
- NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
+ NetscapeBookmarkUtils::import(NULL, $files, NULL, $this->conf)
);
$this->assertEquals(0, count($this->linkDb));
}
$this->assertEquals(
'File internet_explorer_encoding.htm (356 bytes) was successfully processed:'
.' 1 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
);
$this->assertEquals(1, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_nested.htm (1337 bytes) was successfully processed:'
.' 8 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
);
$this->assertEquals(8, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(1, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 2 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 2 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 0 links imported, 0 links overwritten, 2 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
$this->assertEquals(
'File same_date.htm (453 bytes) was successfully processed:'
.' 3 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
+ NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
);
$this->assertEquals(3, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));