enforcing parameter types, avoiding unnecessary metadata in version 2 pastes

This commit is contained in:
El RIDO 2019-05-05 18:22:57 +02:00
parent 6e15903f1e
commit b7a03cfdb9
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
2 changed files with 39 additions and 36 deletions

View File

@ -60,7 +60,7 @@ abstract class AbstractData
* @param array $options * @param array $options
* @return AbstractData * @return AbstractData
*/ */
public static function getInstance($options) public static function getInstance(array $options)
{ {
} }
@ -72,7 +72,7 @@ abstract class AbstractData
* @param array $paste * @param array $paste
* @return bool * @return bool
*/ */
abstract public function create($pasteid, $paste); abstract public function create(string $pasteid, array $paste);
/** /**
* Read a paste. * Read a paste.
@ -81,7 +81,7 @@ abstract class AbstractData
* @param string $pasteid * @param string $pasteid
* @return stdClass|false * @return stdClass|false
*/ */
abstract public function read($pasteid); abstract public function read(string $pasteid);
/** /**
* Delete a paste and its discussion. * Delete a paste and its discussion.
@ -89,7 +89,7 @@ abstract class AbstractData
* @access public * @access public
* @param string $pasteid * @param string $pasteid
*/ */
abstract public function delete($pasteid); abstract public function delete(string $pasteid);
/** /**
* Test if a paste exists. * Test if a paste exists.
@ -98,7 +98,7 @@ abstract class AbstractData
* @param string $pasteid * @param string $pasteid
* @return bool * @return bool
*/ */
abstract public function exists($pasteid); abstract public function exists(string $pasteid);
/** /**
* Create a comment in a paste. * Create a comment in a paste.
@ -110,7 +110,7 @@ abstract class AbstractData
* @param array $comment * @param array $comment
* @return bool * @return bool
*/ */
abstract public function createComment($pasteid, $parentid, $commentid, $comment); abstract public function createComment(string $pasteid, string $parentid, string $commentid, array $comment);
/** /**
* Read all comments of paste. * Read all comments of paste.
@ -119,7 +119,7 @@ abstract class AbstractData
* @param string $pasteid * @param string $pasteid
* @return array * @return array
*/ */
abstract public function readComments($pasteid); abstract public function readComments(string $pasteid);
/** /**
* Test if a comment exists. * Test if a comment exists.
@ -130,7 +130,7 @@ abstract class AbstractData
* @param string $commentid * @param string $commentid
* @return bool * @return bool
*/ */
abstract public function existsComment($pasteid, $parentid, $commentid); abstract public function existsComment(string $pasteid, string $parentid, string $commentid);
/** /**
* Returns up to batch size number of paste ids that have expired * Returns up to batch size number of paste ids that have expired
@ -139,7 +139,7 @@ abstract class AbstractData
* @param int $batchsize * @param int $batchsize
* @return array * @return array
*/ */
abstract protected function _getExpiredPastes($batchsize); abstract protected function _getExpiredPastes(int $batchsize);
/** /**
* Perform a purge of old pastes, at most the given batchsize is deleted. * Perform a purge of old pastes, at most the given batchsize is deleted.
@ -147,7 +147,7 @@ abstract class AbstractData
* @access public * @access public
* @param int $batchsize * @param int $batchsize
*/ */
public function purge($batchsize) public function purge(int $batchsize)
{ {
if ($batchsize < 1) { if ($batchsize < 1) {
return; return;
@ -168,7 +168,7 @@ abstract class AbstractData
* @param int|string $postdate * @param int|string $postdate
* @return int|string * @return int|string
*/ */
protected function getOpenSlot(&$comments, $postdate) protected function getOpenSlot(array &$comments, $postdate)
{ {
if (array_key_exists($postdate, $comments)) { if (array_key_exists($postdate, $comments)) {
$parts = explode('.', $postdate, 2); $parts = explode('.', $postdate, 2);

View File

@ -155,7 +155,7 @@ class Database extends AbstractData
* @param array $paste * @param array $paste
* @return bool * @return bool
*/ */
public function create($pasteid, $paste) public function create(string $pasteid, array $paste)
{ {
if ( if (
array_key_exists($pasteid, self::$_cache) array_key_exists($pasteid, self::$_cache)
@ -180,11 +180,11 @@ class Database extends AbstractData
unset($meta['expire_date']); unset($meta['expire_date']);
} }
if (array_key_exists('opendiscussion', $meta)) { if (array_key_exists('opendiscussion', $meta)) {
$opendiscussion = (bool) $meta['opendiscussion']; $opendiscussion = $meta['opendiscussion'];
unset($meta['opendiscussion']); unset($meta['opendiscussion']);
} }
if (array_key_exists('burnafterreading', $meta)) { if (array_key_exists('burnafterreading', $meta)) {
$burnafterreading = (bool) $meta['burnafterreading']; $burnafterreading = $meta['burnafterreading'];
unset($meta['burnafterreading']); unset($meta['burnafterreading']);
} }
if ($isVersion1) { if ($isVersion1) {
@ -196,6 +196,9 @@ class Database extends AbstractData
$attachmentname = $meta['attachmentname']; $attachmentname = $meta['attachmentname'];
unset($meta['attachmentname']); unset($meta['attachmentname']);
} }
} else {
$opendiscussion = $paste['adata'][2];
$burnafterreading = $paste['adata'][3];
} }
return self::_exec( return self::_exec(
'INSERT INTO ' . self::_sanitizeIdentifier('paste') . 'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
@ -221,7 +224,7 @@ class Database extends AbstractData
* @param string $pasteid * @param string $pasteid
* @return stdClass|false * @return stdClass|false
*/ */
public function read($pasteid) public function read(string $pasteid)
{ {
if ( if (
!array_key_exists($pasteid, self::$_cache) !array_key_exists($pasteid, self::$_cache)
@ -250,6 +253,13 @@ class Database extends AbstractData
$meta = new stdClass; $meta = new stdClass;
} }
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;
}
if (!$isVersion2) { if (!$isVersion2) {
// support pre v1 attachments // support pre v1 attachments
if (property_exists($meta, 'attachment')) { if (property_exists($meta, 'attachment')) {
@ -267,19 +277,12 @@ class Database extends AbstractData
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 = $meta; }
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate']; if ($paste['burnafterreading']) {
$expire_date = (int) $paste['expiredate']; self::$_cache[$pasteid]->meta->burnafterreading = true;
if ($expire_date > 0) { }
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
}
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
}
if ($paste['burnafterreading']) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
} }
} }
} }
@ -293,7 +296,7 @@ class Database extends AbstractData
* @access public * @access public
* @param string $pasteid * @param string $pasteid
*/ */
public function delete($pasteid) public function delete(string $pasteid)
{ {
self::_exec( self::_exec(
'DELETE FROM ' . self::_sanitizeIdentifier('paste') . 'DELETE FROM ' . self::_sanitizeIdentifier('paste') .
@ -317,7 +320,7 @@ class Database extends AbstractData
* @param string $pasteid * @param string $pasteid
* @return bool * @return bool
*/ */
public function exists($pasteid) public function exists(string $pasteid)
{ {
if ( if (
!array_key_exists($pasteid, self::$_cache) !array_key_exists($pasteid, self::$_cache)
@ -337,7 +340,7 @@ class Database extends AbstractData
* @param array $comment * @param array $comment
* @return bool * @return bool
*/ */
public function createComment($pasteid, $parentid, $commentid, $comment) public function createComment(string $pasteid, string $parentid, string $commentid, array $comment)
{ {
if (array_key_exists('data', $comment)) { if (array_key_exists('data', $comment)) {
$version = 1; $version = 1;
@ -376,7 +379,7 @@ class Database extends AbstractData
* @param string $pasteid * @param string $pasteid
* @return array * @return array
*/ */
public function readComments($pasteid) public function readComments(string $pasteid)
{ {
$rows = self::_select( $rows = self::_select(
'SELECT * FROM ' . self::_sanitizeIdentifier('comment') . 'SELECT * FROM ' . self::_sanitizeIdentifier('comment') .
@ -422,7 +425,7 @@ class Database extends AbstractData
* @param string $commentid * @param string $commentid
* @return bool * @return bool
*/ */
public function existsComment($pasteid, $parentid, $commentid) public function existsComment(string $pasteid, string $parentid, string $commentid)
{ {
return (bool) self::_select( return (bool) self::_select(
'SELECT dataid FROM ' . self::_sanitizeIdentifier('comment') . 'SELECT dataid FROM ' . self::_sanitizeIdentifier('comment') .
@ -438,7 +441,7 @@ class Database extends AbstractData
* @param int $batchsize * @param int $batchsize
* @return array * @return array
*/ */
protected function _getExpiredPastes($batchsize) protected function _getExpiredPastes(int $batchsize)
{ {
$pastes = array(); $pastes = array();
$rows = self::_select( $rows = self::_select(
@ -463,7 +466,7 @@ class Database extends AbstractData
* @throws PDOException * @throws PDOException
* @return bool * @return bool
*/ */
private static function _exec($sql, array $params) private static function _exec(string $sql, array $params)
{ {
$statement = self::$_db->prepare($sql); $statement = self::$_db->prepare($sql);
$result = $statement->execute($params); $result = $statement->execute($params);
@ -482,7 +485,7 @@ class Database extends AbstractData
* @throws PDOException * @throws PDOException
* @return array * @return array
*/ */
private static function _select($sql, array $params, $firstOnly = false) private static function _select(string $sql, array $params, bool $firstOnly = false)
{ {
$statement = self::$_db->prepare($sql); $statement = self::$_db->prepare($sql);
$statement->execute($params); $statement->execute($params);