Merge pull request #779 from ArthurHoaro/feature/import-parser-logs

Link imports are now logged in `data/` folder, and can be debug using…
This commit is contained in:
ArthurHoaro 2017-03-11 14:23:05 +01:00 committed by GitHub
commit 196808e14f
6 changed files with 54 additions and 29 deletions

View file

@ -28,6 +28,7 @@ Theming:
- Add OpenSearch to feed templates
- Add `campaign_` to the URL cleanup pattern list
- Add an AUTHORS file and Makefile target to list authors from Git commit data
- Link imports are now logged in `data/` folder, and can be debug using `dev.debug=true` setting.
### Changed
- Docker: enable nginx URL rewriting for the REST API

View file

@ -1,7 +1,13 @@
<?php
use Psr\Log\LogLevel;
use Shaarli\Config\ConfigManager;
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
{
@ -85,14 +91,14 @@ private static function importStatus(
/**
* 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'];
@ -119,10 +125,20 @@ public static function import($post, $files, $linkDb, $pagecache)
$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;
@ -179,7 +195,7 @@ public static function import($post, $files, $linkDb, $pagecache)
$importCount++;
}
$linkDb->save($pagecache);
$linkDb->save($conf->get('resource.page_cache'));
return self::importStatus(
$filename,
$filesize,

View file

@ -11,7 +11,7 @@
"keywords": ["bookmark", "link", "share", "web"],
"require": {
"php": ">=5.5",
"shaarli/netscape-bookmark-parser": "1.*",
"shaarli/netscape-bookmark-parser": "^2.0",
"erusev/parsedown": "1.6",
"slim/slim": "^3.0",
"pubsubhubbub/publisher": "dev-master"

View file

@ -1528,7 +1528,7 @@ function renderPage($conf, $pluginManager, $LINKSDB)
$_POST,
$_FILES,
$LINKSDB,
$conf->get('resource.page_cache')
$conf
);
echo '<script>alert("'.$status.'");document.location=\'?do='
.Router::$PAGE_IMPORT .'\';</script>';

View file

@ -2,6 +2,7 @@
require_once 'application/NetscapeBookmarkUtils.php';
use Shaarli\Config\ConfigManager;
/**
* Utility function to load a file's metadata in a $_FILES-like array
@ -42,6 +43,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
*/
protected $pagecache = 'tests';
/**
* @var ConfigManager instance.
*/
protected $conf;
/**
* @var string Save the current timezone.
*/
@ -65,6 +71,8 @@ protected function setUp()
// 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()
@ -81,7 +89,7 @@ public function testImportEmptyData()
$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));
}
@ -94,7 +102,7 @@ public function testImportNoDoctype()
$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));
}
@ -108,7 +116,7 @@ public function testImportInternetExplorerEncoding()
$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));
@ -137,7 +145,7 @@ public function testImportNested()
$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));
@ -259,7 +267,7 @@ public function testImportDefaultPrivacyNoPost()
$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));
@ -304,7 +312,7 @@ public function testImportKeepPrivacy()
$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));
@ -348,7 +356,7 @@ public function testImportAsPublic()
$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));
@ -372,7 +380,7 @@ public function testImportAsPrivate()
$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));
@ -398,7 +406,7 @@ public function testOverwriteAsPublic()
$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));
@ -418,7 +426,7 @@ public function testOverwriteAsPublic()
$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));
@ -444,7 +452,7 @@ public function testOverwriteAsPrivate()
$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));
@ -465,7 +473,7 @@ public function testOverwriteAsPrivate()
$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));
@ -489,7 +497,7 @@ public function testSkipOverwrite()
$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));
@ -499,7 +507,7 @@ public function testSkipOverwrite()
$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));
@ -518,7 +526,7 @@ public function testSetDefaultTags()
$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));
@ -545,7 +553,7 @@ public function testSanitizeDefaultTags()
$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));
@ -570,7 +578,7 @@ public function testImportSameDate()
$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));

View file

@ -24,7 +24,7 @@
},
"resource": {
"datastore": "tests\/utils\/config\/datastore.php",
"data_dir": "tests\/utils\/config",
"data_dir": "sandbox/",
"raintpl_tpl": "tpl/"
},
"plugins": {