*/
class LinkDB implements Iterator, Countable, ArrayAccess
{
+ // Links are stored as a PHP serialized string
+ private $datastore;
+
+ // Datastore PHP prefix
+ protected static $phpPrefix = '<?php /* ';
+
+ // Datastore PHP suffix
+ protected static $phpSuffix = ' */ ?>';
+
// List of links (associative array)
// - key: link date (e.g. "20110823_124546"),
// - value: associative array (keys: title, description...)
*
* @param $isLoggedIn is the user logged in?
*/
- function __construct($isLoggedIn, $hidePublicLinks)
+ function __construct($datastore, $isLoggedIn, $hidePublicLinks)
{
- // FIXME: do not access $GLOBALS, pass the datastore instead
+ $this->datastore = $datastore;
$this->loggedIn = $isLoggedIn;
$this->hidePublicLinks = $hidePublicLinks;
$this->checkDB();
*/
private function checkDB()
{
- if (file_exists($GLOBALS['config']['DATASTORE'])) {
+ if (file_exists($this->datastore)) {
return;
}
// Create a dummy database for example
$this->links = array();
$link = array(
- 'title'=>'Shaarli - sebsauvage.net',
- 'url'=>'http://sebsauvage.net/wiki/doku.php?id=php:shaarli',
- 'description'=>'Welcome to Shaarli! This is a bookmark. To edit or delete me, you must first login.',
+ 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
+ 'url'=>'https://github.com/shaarli/Shaarli/wiki',
+ 'description'=>'Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.
+
+ To learn how to use Shaarli, consult the link "Help/documentation" at the bottom of this page.
+
+ You use the community supported version of the original Shaarli project, by Sebastien Sauvage.',
'private'=>0,
- 'linkdate'=>'20110914_190000',
+ 'linkdate'=> date('Ymd_His'),
'tags'=>'opensource software'
);
$this->links[$link['linkdate']] = $link;
$link = array(
'title'=>'My secret stuff... - Pastebin.com',
'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
- 'description'=>'SShhhh!! I\'m a private link only YOU can see. You can delete me too.',
+ 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.',
'private'=>1,
- 'linkdate'=>'20110914_074522',
+ 'linkdate'=> date('Ymd_His', strtotime('-1 minute')),
'tags'=>'secretstuff'
);
$this->links[$link['linkdate']] = $link;
// Write database to disk
// TODO: raise an exception if the file is not write-able
file_put_contents(
- // FIXME: do not use $GLOBALS
- $GLOBALS['config']['DATASTORE'],
- PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
+ $this->datastore,
+ self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
);
}
// Read data
// Note that gzinflate is faster than gzuncompress.
// See: http://www.php.net/manual/en/function.gzdeflate.php#96439
- // FIXME: do not use $GLOBALS
$this->links = array();
- if (file_exists($GLOBALS['config']['DATASTORE'])) {
+ if (file_exists($this->datastore)) {
$this->links = unserialize(gzinflate(base64_decode(
- substr(file_get_contents($GLOBALS['config']['DATASTORE']),
- strlen(PHPPREFIX), -strlen(PHPSUFFIX)))));
+ substr(file_get_contents($this->datastore),
+ strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
}
// If user is not logged in, filter private links.
die('You are not authorized to change the database.');
}
file_put_contents(
- $GLOBALS['config']['DATASTORE'],
- PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
+ $this->datastore,
+ self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
);
invalidateCaches();
}
// FIXME: accept double-quotes to search for a string "as is"?
$filtered = array();
$search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
- $keys = ['title', 'description', 'url', 'tags'];
+ $keys = array('title', 'description', 'url', 'tags');
foreach ($this->links as $link) {
$found = false;
require_once 'application/Utils.php';
require_once 'tests/utils/ReferenceLinkDB.php';
-define('PHPPREFIX', '<?php /* ');
-define('PHPSUFFIX', ' */ ?>');
-
/**
* Unitary tests for LinkDB
{
// datastore to test write operations
protected static $testDatastore = 'tests/datastore.php';
- protected static $dummyDatastoreSHA1 = 'e3edea8ea7bb50be4bcb404df53fbb4546a7156e';
+ protected static $dummyDatastoreFilesize = 759;
protected static $refDB = null;
protected static $publicLinkDB = null;
protected static $privateLinkDB = null;
public static function setUpBeforeClass()
{
self::$refDB = new ReferenceLinkDB();
- self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX);
+ self::$refDB->write(self::$testDatastore);
- $GLOBALS['config']['DATASTORE'] = self::$testDatastore;
- self::$publicLinkDB = new LinkDB(false, false);
- self::$privateLinkDB = new LinkDB(true, false);
+ self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
+ self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
}
/**
*/
protected function setUp()
{
- $GLOBALS['config']['DATASTORE'] = self::$testDatastore;
if (file_exists(self::$testDatastore)) {
unlink(self::$testDatastore);
}
*/
public function testConstructLoggedIn()
{
- new LinkDB(true, false);
+ new LinkDB(self::$testDatastore, true, false);
$this->assertFileExists(self::$testDatastore);
}
*/
public function testConstructLoggedOut()
{
- new LinkDB(false, false);
+ new LinkDB(self::$testDatastore, false, false);
$this->assertFileExists(self::$testDatastore);
}
*/
public function testConstructDatastoreNotWriteable()
{
- $GLOBALS['config']['DATASTORE'] = 'null/store.db';
- new LinkDB(false, false);
+ new LinkDB('null/store.db', false, false);
}
/**
*/
public function testCheckDBNew()
{
- $linkDB = new LinkDB(false, false);
+ $linkDB = new LinkDB(self::$testDatastore, false, false);
unlink(self::$testDatastore);
$this->assertFileNotExists(self::$testDatastore);
// ensure the correct data has been written
$this->assertEquals(
- self::$dummyDatastoreSHA1,
- sha1_file(self::$testDatastore)
+ self::$dummyDatastoreFilesize,
+ filesize(self::$testDatastore)
);
}
*/
public function testCheckDBLoad()
{
- $linkDB = new LinkDB(false, false);
+ $linkDB = new LinkDB(self::$testDatastore, false, false);
$this->assertEquals(
- self::$dummyDatastoreSHA1,
- sha1_file(self::$testDatastore)
+ self::$dummyDatastoreFilesize,
+ filesize(self::$testDatastore)
);
$checkDB = self::getMethod('checkDB');
// ensure the datastore is left unmodified
$this->assertEquals(
- self::$dummyDatastoreSHA1,
- sha1_file(self::$testDatastore)
+ self::$dummyDatastoreFilesize,
+ filesize(self::$testDatastore)
);
}
*/
public function testReadEmptyDB()
{
- file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX);
- $emptyDB = new LinkDB(false, false);
+ file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
+ $emptyDB = new LinkDB(self::$testDatastore, false, false);
$this->assertEquals(0, sizeof($emptyDB));
$this->assertEquals(0, count($emptyDB));
}
*/
public function testSaveDB()
{
- $testDB = new LinkDB(true, false);
+ $testDB = new LinkDB(self::$testDatastore, true, false);
$dbSize = sizeof($testDB);
$link = array(
$testDB->savedb();
- $testDB = new LinkDB(true, false);
+ $testDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEquals($dbSize + 1, sizeof($testDB));
}
*/
public function testCountHiddenPublic()
{
- $linkDB = new LinkDB(false, true);
+ $linkDB = new LinkDB(self::$testDatastore, false, true);
$this->assertEquals(
0,