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 PDO;
use PDOException; use PDOException;
use PrivateBin\Controller; use PrivateBin\Controller;
use stdClass;
/** /**
* Database * Database
@ -222,70 +221,71 @@ 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;
$paste = self::_select(
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
' WHERE dataid = ?', array($pasteid), true
);
if (false !== $paste) { self::$_cache[$pasteid] = false;
// create object $paste = self::_select(
$data = json_decode($paste['data']); 'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
$isVersion2 = property_exists($data, 'v') && $data->v >= 2; ' WHERE dataid = ?', array($pasteid), true
if ($isVersion2) { );
self::$_cache[$pasteid] = $data;
list($createdKey) = self::_getVersionedKeys(2);
} else {
self::$_cache[$pasteid] = new stdClass;
self::$_cache[$pasteid]->data = $paste['data'];
list($createdKey) = self::_getVersionedKeys(1);
}
$meta = json_decode($paste['meta']); if ($paste === false) {
if (!is_object($meta)) { return false;
$meta = new stdClass; }
} // 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] = array('data' => $paste['data']);
list($createdKey) = self::_getVersionedKeys(1);
}
self::$_cache[$pasteid]->meta = $meta; $meta = json_decode($paste['meta'], true);
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate']; if (!is_array($meta)) {
$expire_date = (int) $paste['expiredate']; $meta = array();
if ($expire_date > 0) { }
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
}
if (!$isVersion2) { self::$_cache[$pasteid]['meta'] = $meta;
// support pre v1 attachments self::$_cache[$pasteid]['meta'][$createdKey] = (int) $paste['postdate'];
if (property_exists($meta, 'attachment')) { $expire_date = (int) $paste['expiredate'];
self::$_cache[$pasteid]->attachment = $meta->attachment; if ($expire_date > 0) {
unset($meta->attachment); self::$_cache[$pasteid]['meta']['expire_date'] = $expire_date;
if (property_exists($meta, 'attachmentname')) { }
self::$_cache[$pasteid]->attachmentname = $meta->attachmentname; if ($isVersion2) {
unset($meta->attachmentname); return self::$_cache[$pasteid];
} }
}
// support v1 attachments // support pre v1 attachments
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) { if (array_key_exists('attachment', $meta)) {
self::$_cache[$pasteid]->attachment = $paste['attachment']; self::$_cache[$pasteid]['attachment'] = $meta['attachment'];
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) { unset(self::$_cache[$pasteid]['meta']['attachment']);
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname']; if (array_key_exists('attachmentname', $meta)) {
} self::$_cache[$pasteid]['attachmentname'] = $meta['attachmentname'];
} unset(self::$_cache[$pasteid]['meta']['attachmentname']);
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
}
if ($paste['burnafterreading']) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
}
}
} }
} }
// support v1 attachments
elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
self::$_cache[$pasteid]['attachment'] = $paste['attachment'];
if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
self::$_cache[$pasteid]['attachmentname'] = $paste['attachmentname'];
}
}
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]['meta']['opendiscussion'] = true;
}
if ($paste['burnafterreading']) {
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) {

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->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);
} }