sticking to arrays to reduce conversions, inversion of control to simplify logic

This commit is contained in:
El RIDO 2019-05-05 21:03:58 +02:00
parent b7a03cfdb9
commit 06b90ff48e
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
2 changed files with 77 additions and 78 deletions

View file

@ -16,7 +16,6 @@ use Exception;
use PDO;
use PDOException;
use PrivateBin\Controller;
use stdClass;
/**
* Database
@ -222,69 +221,70 @@ class Database extends AbstractData
*
* @access public
* @param string $pasteid
* @return stdClass|false
* @return array|false
*/
public function read(string $pasteid)
{
if (
!array_key_exists($pasteid, self::$_cache)
) {
if (array_key_exists($pasteid, self::$_cache)) {
return self::$_cache[$pasteid];
}
self::$_cache[$pasteid] = false;
$paste = self::_select(
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
' WHERE dataid = ?', array($pasteid), true
);
if (false !== $paste) {
// create object
$data = json_decode($paste['data']);
$isVersion2 = property_exists($data, 'v') && $data->v >= 2;
if ($paste === false) {
return false;
}
// create array
$data = json_decode($paste['data'], true);
$isVersion2 = array_key_exists('v', $data) && $data['v'] >= 2;
if ($isVersion2) {
self::$_cache[$pasteid] = $data;
list($createdKey) = self::_getVersionedKeys(2);
} else {
self::$_cache[$pasteid] = new stdClass;
self::$_cache[$pasteid]->data = $paste['data'];
self::$_cache[$pasteid] = array('data' => $paste['data']);
list($createdKey) = self::_getVersionedKeys(1);
}
$meta = json_decode($paste['meta']);
if (!is_object($meta)) {
$meta = new stdClass;
$meta = json_decode($paste['meta'], true);
if (!is_array($meta)) {
$meta = array();
}
self::$_cache[$pasteid]->meta = $meta;
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate'];
self::$_cache[$pasteid]['meta'] = $meta;
self::$_cache[$pasteid]['meta'][$createdKey] = (int) $paste['postdate'];
$expire_date = (int) $paste['expiredate'];
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
if (property_exists($meta, 'attachment')) {
self::$_cache[$pasteid]->attachment = $meta->attachment;
unset($meta->attachment);
if (property_exists($meta, 'attachmentname')) {
self::$_cache[$pasteid]->attachmentname = $meta->attachmentname;
unset($meta->attachmentname);
if (array_key_exists('attachment', $meta)) {
self::$_cache[$pasteid]['attachment'] = $meta['attachment'];
unset(self::$_cache[$pasteid]['meta']['attachment']);
if (array_key_exists('attachmentname', $meta)) {
self::$_cache[$pasteid]['attachmentname'] = $meta['attachmentname'];
unset(self::$_cache[$pasteid]['meta']['attachmentname']);
}
}
// support v1 attachments
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'])) {
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
self::$_cache[$pasteid]['attachmentname'] = $paste['attachmentname'];
}
}
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
self::$_cache[$pasteid]['meta']['opendiscussion'] = true;
}
if ($paste['burnafterreading']) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
}
}
}
self::$_cache[$pasteid]['meta']['burnafterreading'] = true;
}
return self::$_cache[$pasteid];
@ -391,23 +391,21 @@ class Database extends AbstractData
if (count($rows)) {
foreach ($rows as $row) {
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
$data = json_decode($row['data']);
if (property_exists($data, 'v') && $data->v >= 2) {
$data = json_decode($row['data'], true);
if (array_key_exists('v', $data) && $data['v'] >= 2) {
$version = 2;
$comments[$i] = $data;
} else {
$version = 1;
$comments[$i] = new stdClass;
$comments[$i]->data = $row['data'];
$comments[$i] = array('data' => $row['data']);
}
list($createdKey, $iconKey) = self::_getVersionedKeys($version);
$comments[$i]->id = $row['dataid'];
$comments[$i]->parentid = $row['parentid'];
$comments[$i]->meta = new stdClass;
$comments[$i]->meta->$createdKey = (int) $row['postdate'];
$comments[$i]['id'] = $row['dataid'];
$comments[$i]['parentid'] = $row['parentid'];
$comments[$i]['meta'] = array($createdKey => (int) $row['postdate']);
foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
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();
$rows = self::_select(
'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)) {
foreach ($rows as $row) {

View file

@ -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->exists(Helper::getPasteId()), 'paste exists after storing it');
$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
$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->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');
$comment1 = json_decode(json_encode(Helper::getComment(1)));
$comment1->id = Helper::getCommentId();
$comment1->parentid = Helper::getPasteId();
$comment2 = json_decode(json_encode(Helper::getComment(2)));
$comment2->id = Helper::getPasteId();
$comment2->parentid = Helper::getPasteId();
$comment1 = Helper::getComment(1);
$comment1['id'] = Helper::getCommentId();
$comment1['parentid'] = Helper::getPasteId();
$comment2 = Helper::getComment(2);
$comment2['id'] = Helper::getPasteId();
$comment2['parentid'] = Helper::getPasteId();
$this->assertEquals(
array(
$comment1->meta->postdate => $comment1,
$comment2->meta->created . '.1' => $comment2,
$comment1['meta']['postdate'] => $comment1,
$comment2['meta']['created'] . '.1' => $comment2,
),
$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->exists(Helper::getPasteId()), 'paste exists after storing it');
$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();
$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);
}