sticking to arrays to reduce conversions, inversion of control to simplify logic
This commit is contained in:
parent
b7a03cfdb9
commit
06b90ff48e
|
@ -16,7 +16,6 @@ use Exception;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use PrivateBin\Controller;
|
use PrivateBin\Controller;
|
||||||
use stdClass;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database
|
* Database
|
||||||
|
@ -222,69 +221,70 @@ class Database extends AbstractData
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $pasteid
|
* @param string $pasteid
|
||||||
* @return stdClass|false
|
* @return array|false
|
||||||
*/
|
*/
|
||||||
public function read(string $pasteid)
|
public function read(string $pasteid)
|
||||||
{
|
{
|
||||||
if (
|
if (array_key_exists($pasteid, self::$_cache)) {
|
||||||
!array_key_exists($pasteid, self::$_cache)
|
return self::$_cache[$pasteid];
|
||||||
) {
|
}
|
||||||
|
|
||||||
self::$_cache[$pasteid] = false;
|
self::$_cache[$pasteid] = false;
|
||||||
$paste = self::_select(
|
$paste = self::_select(
|
||||||
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
|
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
|
||||||
' WHERE dataid = ?', array($pasteid), true
|
' WHERE dataid = ?', array($pasteid), true
|
||||||
);
|
);
|
||||||
|
|
||||||
if (false !== $paste) {
|
if ($paste === false) {
|
||||||
// create object
|
return false;
|
||||||
$data = json_decode($paste['data']);
|
}
|
||||||
$isVersion2 = property_exists($data, 'v') && $data->v >= 2;
|
// create array
|
||||||
|
$data = json_decode($paste['data'], true);
|
||||||
|
$isVersion2 = array_key_exists('v', $data) && $data['v'] >= 2;
|
||||||
if ($isVersion2) {
|
if ($isVersion2) {
|
||||||
self::$_cache[$pasteid] = $data;
|
self::$_cache[$pasteid] = $data;
|
||||||
list($createdKey) = self::_getVersionedKeys(2);
|
list($createdKey) = self::_getVersionedKeys(2);
|
||||||
} else {
|
} else {
|
||||||
self::$_cache[$pasteid] = new stdClass;
|
self::$_cache[$pasteid] = array('data' => $paste['data']);
|
||||||
self::$_cache[$pasteid]->data = $paste['data'];
|
|
||||||
list($createdKey) = self::_getVersionedKeys(1);
|
list($createdKey) = self::_getVersionedKeys(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$meta = json_decode($paste['meta']);
|
$meta = json_decode($paste['meta'], true);
|
||||||
if (!is_object($meta)) {
|
if (!is_array($meta)) {
|
||||||
$meta = new stdClass;
|
$meta = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$_cache[$pasteid]->meta = $meta;
|
self::$_cache[$pasteid]['meta'] = $meta;
|
||||||
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate'];
|
self::$_cache[$pasteid]['meta'][$createdKey] = (int) $paste['postdate'];
|
||||||
$expire_date = (int) $paste['expiredate'];
|
$expire_date = (int) $paste['expiredate'];
|
||||||
if ($expire_date > 0) {
|
if ($expire_date > 0) {
|
||||||
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
|
self::$_cache[$pasteid]['meta']['expire_date'] = $expire_date;
|
||||||
|
}
|
||||||
|
if ($isVersion2) {
|
||||||
|
return self::$_cache[$pasteid];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$isVersion2) {
|
|
||||||
// support pre v1 attachments
|
// support pre v1 attachments
|
||||||
if (property_exists($meta, 'attachment')) {
|
if (array_key_exists('attachment', $meta)) {
|
||||||
self::$_cache[$pasteid]->attachment = $meta->attachment;
|
self::$_cache[$pasteid]['attachment'] = $meta['attachment'];
|
||||||
unset($meta->attachment);
|
unset(self::$_cache[$pasteid]['meta']['attachment']);
|
||||||
if (property_exists($meta, 'attachmentname')) {
|
if (array_key_exists('attachmentname', $meta)) {
|
||||||
self::$_cache[$pasteid]->attachmentname = $meta->attachmentname;
|
self::$_cache[$pasteid]['attachmentname'] = $meta['attachmentname'];
|
||||||
unset($meta->attachmentname);
|
unset(self::$_cache[$pasteid]['meta']['attachmentname']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// support v1 attachments
|
// support v1 attachments
|
||||||
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
|
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
|
||||||
self::$_cache[$pasteid]->attachment = $paste['attachment'];
|
self::$_cache[$pasteid]['attachment'] = $paste['attachment'];
|
||||||
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
|
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
|
||||||
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
|
self::$_cache[$pasteid]['attachmentname'] = $paste['attachmentname'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($paste['opendiscussion']) {
|
if ($paste['opendiscussion']) {
|
||||||
self::$_cache[$pasteid]->meta->opendiscussion = true;
|
self::$_cache[$pasteid]['meta']['opendiscussion'] = true;
|
||||||
}
|
}
|
||||||
if ($paste['burnafterreading']) {
|
if ($paste['burnafterreading']) {
|
||||||
self::$_cache[$pasteid]->meta->burnafterreading = true;
|
self::$_cache[$pasteid]['meta']['burnafterreading'] = true;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$_cache[$pasteid];
|
return self::$_cache[$pasteid];
|
||||||
|
@ -391,23 +391,21 @@ class Database extends AbstractData
|
||||||
if (count($rows)) {
|
if (count($rows)) {
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
|
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
|
||||||
$data = json_decode($row['data']);
|
$data = json_decode($row['data'], true);
|
||||||
if (property_exists($data, 'v') && $data->v >= 2) {
|
if (array_key_exists('v', $data) && $data['v'] >= 2) {
|
||||||
$version = 2;
|
$version = 2;
|
||||||
$comments[$i] = $data;
|
$comments[$i] = $data;
|
||||||
} else {
|
} else {
|
||||||
$version = 1;
|
$version = 1;
|
||||||
$comments[$i] = new stdClass;
|
$comments[$i] = array('data' => $row['data']);
|
||||||
$comments[$i]->data = $row['data'];
|
|
||||||
}
|
}
|
||||||
list($createdKey, $iconKey) = self::_getVersionedKeys($version);
|
list($createdKey, $iconKey) = self::_getVersionedKeys($version);
|
||||||
$comments[$i]->id = $row['dataid'];
|
$comments[$i]['id'] = $row['dataid'];
|
||||||
$comments[$i]->parentid = $row['parentid'];
|
$comments[$i]['parentid'] = $row['parentid'];
|
||||||
$comments[$i]->meta = new stdClass;
|
$comments[$i]['meta'] = array($createdKey => (int) $row['postdate']);
|
||||||
$comments[$i]->meta->$createdKey = (int) $row['postdate'];
|
|
||||||
foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
|
foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
|
||||||
if (array_key_exists($rowKey, $row) && !empty($row[$rowKey])) {
|
if (array_key_exists($rowKey, $row) && !empty($row[$rowKey])) {
|
||||||
$comments[$i]->meta->$commentKey = $row[$rowKey];
|
$comments[$i]['meta'][$commentKey] = $row[$rowKey];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -446,7 +444,8 @@ class Database extends AbstractData
|
||||||
$pastes = array();
|
$pastes = array();
|
||||||
$rows = self::_select(
|
$rows = self::_select(
|
||||||
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
|
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
|
||||||
' WHERE expiredate < ? AND expiredate != ? LIMIT ?', array(time(), 0, $batchsize)
|
' WHERE expiredate < ? AND expiredate != ? LIMIT ?',
|
||||||
|
array(time(), 0, $batchsize)
|
||||||
);
|
);
|
||||||
if (count($rows)) {
|
if (count($rows)) {
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase
|
||||||
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
||||||
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
||||||
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
||||||
$this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(Helper::getPasteId()));
|
$this->assertEquals($paste, $this->_model->read(Helper::getPasteId()));
|
||||||
|
|
||||||
// storing comments
|
// storing comments
|
||||||
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'v1 comment does not yet exist');
|
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'v1 comment does not yet exist');
|
||||||
|
@ -50,16 +50,16 @@ class DatabaseTest extends PHPUnit_Framework_TestCase
|
||||||
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment does not yet exist');
|
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment does not yet exist');
|
||||||
$this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId(), Helper::getComment(2)) !== false, 'store v2 comment');
|
$this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId(), Helper::getComment(2)) !== false, 'store v2 comment');
|
||||||
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment exists after storing it');
|
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment exists after storing it');
|
||||||
$comment1 = json_decode(json_encode(Helper::getComment(1)));
|
$comment1 = Helper::getComment(1);
|
||||||
$comment1->id = Helper::getCommentId();
|
$comment1['id'] = Helper::getCommentId();
|
||||||
$comment1->parentid = Helper::getPasteId();
|
$comment1['parentid'] = Helper::getPasteId();
|
||||||
$comment2 = json_decode(json_encode(Helper::getComment(2)));
|
$comment2 = Helper::getComment(2);
|
||||||
$comment2->id = Helper::getPasteId();
|
$comment2['id'] = Helper::getPasteId();
|
||||||
$comment2->parentid = Helper::getPasteId();
|
$comment2['parentid'] = Helper::getPasteId();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
array(
|
||||||
$comment1->meta->postdate => $comment1,
|
$comment1['meta']['postdate'] => $comment1,
|
||||||
$comment2->meta->created . '.1' => $comment2,
|
$comment2['meta']['created'] . '.1' => $comment2,
|
||||||
),
|
),
|
||||||
$this->_model->readComments(Helper::getPasteId())
|
$this->_model->readComments(Helper::getPasteId())
|
||||||
);
|
);
|
||||||
|
@ -84,7 +84,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase
|
||||||
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
||||||
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
||||||
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
||||||
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(Helper::getPasteId()));
|
$this->assertEquals($original, $this->_model->read(Helper::getPasteId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -282,7 +282,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase
|
||||||
$statement->closeCursor();
|
$statement->closeCursor();
|
||||||
|
|
||||||
$this->assertTrue($model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
$this->assertTrue($model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
||||||
$this->assertEquals(json_decode(json_encode($original)), $model->read(Helper::getPasteId()));
|
$this->assertEquals($original, $model->read(Helper::getPasteId()));
|
||||||
|
|
||||||
Helper::rmDir($this->_path);
|
Helper::rmDir($this->_path);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue