diff --git a/data/config.ini b/data/config.ini index 480c22ef..ddaa77c1 100644 --- a/data/config.ini +++ b/data/config.ini @@ -12,6 +12,8 @@ activationBodyPath = mail/activation.txt [database] dsn = sqlite:db.sqlite +user = +password = maxPostSize = 10485760 ;10mb maxCustomThumbnailSize = 1048576 ;1mb diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index 94c7e6f8..c8fe3395 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -133,6 +133,8 @@ abstract class AbstractDao implements ICrudDao protected function findBy($columnName, $value) { + if (is_array($value) and empty($value)) + return []; $query = $this->fpdo->from($this->tableName)->where($columnName, $value); $arrayEntities = iterator_to_array($query); return $this->arrayToEntities($arrayEntities); diff --git a/src/Dao/EntityConverters/AbstractEntityConverter.php b/src/Dao/EntityConverters/AbstractEntityConverter.php index 69591fae..fee0dab4 100644 --- a/src/Dao/EntityConverters/AbstractEntityConverter.php +++ b/src/Dao/EntityConverters/AbstractEntityConverter.php @@ -19,4 +19,16 @@ abstract class AbstractEntityConverter implements IEntityConverter } protected abstract function toBasicEntity(array $array); + + protected function dbTimeToEntityTime($time) + { + if ($time === null) + return null; + return date('c', strtotime($time)); + } + + protected function entityTimeToDbTime($time) + { + return $time; + } } diff --git a/src/Dao/EntityConverters/FavoriteEntityConverter.php b/src/Dao/EntityConverters/FavoriteEntityConverter.php index eaf0b7d9..3db49b67 100644 --- a/src/Dao/EntityConverters/FavoriteEntityConverter.php +++ b/src/Dao/EntityConverters/FavoriteEntityConverter.php @@ -10,7 +10,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity 'id' => $entity->getId(), 'userId' => $entity->getUserId(), 'postId' => $entity->getPostId(), - 'time' => $entity->getTime(), + 'time' => $this->entityTimeToDbTime($entity->getTime()), ]; } @@ -19,7 +19,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity $entity = new \Szurubooru\Entities\Favorite($array['id']); $entity->setUserId($array['userId']); $entity->setPostId($array['postId']); - $entity->setTime($array['time']); + $entity->setTime($this->dbTimeToEntityTime($array['time'])); return $entity; } } diff --git a/src/Dao/EntityConverters/GlobalParamEntityConverter.php b/src/Dao/EntityConverters/GlobalParamEntityConverter.php index 3d567a3d..5ff69a97 100644 --- a/src/Dao/EntityConverters/GlobalParamEntityConverter.php +++ b/src/Dao/EntityConverters/GlobalParamEntityConverter.php @@ -8,16 +8,16 @@ class GlobalParamEntityConverter extends AbstractEntityConverter implements IEnt return [ 'id' => $entity->getId(), - 'key' => $entity->getKey(), - 'value' => $entity->getValue(), + 'dataKey' => $entity->getKey(), + 'dataValue' => $entity->getValue(), ]; } public function toBasicEntity(array $array) { $entity = new \Szurubooru\Entities\GlobalParam($array['id']); - $entity->setKey($array['key']); - $entity->setValue($array['value']); + $entity->setKey($array['dataKey']); + $entity->setValue($array['dataValue']); return $entity; } } diff --git a/src/Dao/EntityConverters/PostEntityConverter.php b/src/Dao/EntityConverters/PostEntityConverter.php index c7899d08..cfe44f96 100644 --- a/src/Dao/EntityConverters/PostEntityConverter.php +++ b/src/Dao/EntityConverters/PostEntityConverter.php @@ -10,8 +10,8 @@ class PostEntityConverter extends AbstractEntityConverter implements IEntityConv 'id' => $entity->getId(), 'name' => $entity->getName(), 'userId' => $entity->getUserId(), - 'uploadTime' => $entity->getUploadTime(), - 'lastEditTime' => $entity->getLastEditTime(), + 'uploadTime' => $this->entityTimeToDbTime($entity->getUploadTime()), + 'lastEditTime' => $this->entityTimeToDbTime($entity->getLastEditTime()), 'safety' => $entity->getSafety(), 'contentType' => $entity->getContentType(), 'contentChecksum' => $entity->getContentChecksum(), @@ -22,7 +22,7 @@ class PostEntityConverter extends AbstractEntityConverter implements IEntityConv 'originalFileSize' => $entity->getOriginalFileSize(), 'originalFileName' => $entity->getOriginalFileName(), 'featureCount' => $entity->getFeatureCount(), - 'lastFeatureTime' => $entity->getLastFeatureTime(), + 'lastFeatureTime' => $this->entityTimeToDbTime($entity->getLastFeatureTime()), ]; } @@ -31,8 +31,8 @@ class PostEntityConverter extends AbstractEntityConverter implements IEntityConv $entity = new \Szurubooru\Entities\Post(intval($array['id'])); $entity->setName($array['name']); $entity->setUserId($array['userId']); - $entity->setUploadTime($array['uploadTime']); - $entity->setLastEditTime($array['lastEditTime']); + $entity->setUploadTime($this->dbTimeToEntityTime($array['uploadTime'])); + $entity->setLastEditTime($this->dbTimeToEntityTime($array['lastEditTime'])); $entity->setSafety(intval($array['safety'])); $entity->setContentType(intval($array['contentType'])); $entity->setContentChecksum($array['contentChecksum']); @@ -43,7 +43,7 @@ class PostEntityConverter extends AbstractEntityConverter implements IEntityConv $entity->setOriginalFileSize($array['originalFileSize']); $entity->setOriginalFileName($array['originalFileName']); $entity->setFeatureCount(intval($array['featureCount'])); - $entity->setLastFeatureTime($array['lastFeatureTime']); + $entity->setLastFeatureTime($this->dbTimeToEntityTime($array['lastFeatureTime'])); $entity->setMeta(\Szurubooru\Entities\Post::META_TAG_COUNT, intval($array['tagCount'])); $entity->setMeta(\Szurubooru\Entities\Post::META_FAV_COUNT, intval($array['favCount'])); $entity->setMeta(\Szurubooru\Entities\Post::META_SCORE, intval($array['score'])); diff --git a/src/Dao/EntityConverters/PostScoreEntityConverter.php b/src/Dao/EntityConverters/PostScoreEntityConverter.php index 28126448..59cb3885 100644 --- a/src/Dao/EntityConverters/PostScoreEntityConverter.php +++ b/src/Dao/EntityConverters/PostScoreEntityConverter.php @@ -10,7 +10,7 @@ class PostScoreEntityConverter extends AbstractEntityConverter implements IEntit 'id' => $entity->getId(), 'userId' => $entity->getUserId(), 'postId' => $entity->getPostId(), - 'time' => $entity->getTime(), + 'time' => $this->entityTimeToDbTime($entity->getTime()), 'score' => $entity->getScore(), ]; } @@ -20,7 +20,7 @@ class PostScoreEntityConverter extends AbstractEntityConverter implements IEntit $entity = new \Szurubooru\Entities\PostScore($array['id']); $entity->setUserId($array['userId']); $entity->setPostId($array['postId']); - $entity->setTime($array['time']); + $entity->setTime($this->dbTimeToEntityTime($array['time'])); $entity->setScore(intval($array['score'])); return $entity; } diff --git a/src/Dao/EntityConverters/SnapshotEntityConverter.php b/src/Dao/EntityConverters/SnapshotEntityConverter.php index 4b1343be..3a869385 100644 --- a/src/Dao/EntityConverters/SnapshotEntityConverter.php +++ b/src/Dao/EntityConverters/SnapshotEntityConverter.php @@ -8,7 +8,7 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity return [ 'id' => $entity->getId(), - 'time' => $entity->getTime(), + 'time' => $this->entityTimeToDbTime($entity->getTime()), 'type' => $entity->getType(), 'primaryKey' => $entity->getPrimaryKey(), 'userId' => $entity->getUserId(), @@ -21,7 +21,7 @@ class SnapshotEntityConverter extends AbstractEntityConverter implements IEntity public function toBasicEntity(array $array) { $entity = new \Szurubooru\Entities\Snapshot(intval($array['id'])); - $entity->setTime($array['time']); + $entity->setTime($this->dbTimeToEntityTime($array['time'])); $entity->setType(intval($array['type'])); $entity->setPrimaryKey($array['primaryKey']); $entity->setUserId($array['userId']); diff --git a/src/Dao/EntityConverters/UserEntityConverter.php b/src/Dao/EntityConverters/UserEntityConverter.php index 4534816d..d2057a9e 100644 --- a/src/Dao/EntityConverters/UserEntityConverter.php +++ b/src/Dao/EntityConverters/UserEntityConverter.php @@ -13,8 +13,8 @@ class UserEntityConverter extends AbstractEntityConverter implements IEntityConv 'emailUnconfirmed' => $entity->getEmailUnconfirmed(), 'passwordHash' => $entity->getPasswordHash(), 'accessRank' => $entity->getAccessRank(), - 'registrationTime' => $entity->getRegistrationTime(), - 'lastLoginTime' => $entity->getLastLoginTime(), + 'registrationTime' => $this->entityTimeToDbTime($entity->getRegistrationTime()), + 'lastLoginTime' => $this->entityTimeToDbTime($entity->getLastLoginTime()), 'avatarStyle' => $entity->getAvatarStyle(), 'browsingSettings' => $entity->getBrowsingSettings(), 'accountConfirmed' => $entity->isAccountConfirmed(), @@ -29,8 +29,8 @@ class UserEntityConverter extends AbstractEntityConverter implements IEntityConv $entity->setEmailUnconfirmed($array['emailUnconfirmed']); $entity->setPasswordHash($array['passwordHash']); $entity->setAccessRank(intval($array['accessRank'])); - $entity->setRegistrationTime($array['registrationTime']); - $entity->setLastLoginTime($array['lastLoginTime']); + $entity->setRegistrationTime($this->dbTimeToEntityTime($array['registrationTime'])); + $entity->setLastLoginTime($this->dbTimeToEntityTime($array['lastLoginTime'])); $entity->setAvatarStyle(intval($array['avatarStyle'])); $entity->setBrowsingSettings($array['browsingSettings']); $entity->setAccountConfirmed($array['accountConfirmed']); diff --git a/src/Dao/GlobalParamDao.php b/src/Dao/GlobalParamDao.php index 3974ef37..d00c1c02 100644 --- a/src/Dao/GlobalParamDao.php +++ b/src/Dao/GlobalParamDao.php @@ -24,11 +24,11 @@ class GlobalParamDao extends AbstractDao implements ICrudDao public function findByKey($key) { - return $this->findOneBy('key', $key); + return $this->findOneBy('dataKey', $key); } public function deleteByKey($key) { - return $this->deleteBy('key', $key); + return $this->deleteBy('dataKey', $key); } } diff --git a/src/DatabaseConnection.php b/src/DatabaseConnection.php index 0a75570e..6d67cb25 100644 --- a/src/DatabaseConnection.php +++ b/src/DatabaseConnection.php @@ -30,7 +30,8 @@ class DatabaseConnection $cwd = getcwd(); if ($this->config->getDataDirectory()) chdir($this->config->getDataDirectory()); - $this->pdo = new \PDO($this->config->database->dsn); + $this->pdo = new \PDO($this->config->database->dsn, $this->config->database->user, + $this->config->database->password); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); chdir($cwd); } diff --git a/src/FormData/UserEditFormData.php b/src/FormData/UserEditFormData.php index 21935108..573d283a 100644 --- a/src/FormData/UserEditFormData.php +++ b/src/FormData/UserEditFormData.php @@ -52,8 +52,8 @@ class UserEditFormData implements \Szurubooru\IValidatable { if (!is_string($this->browsingSettings)) throw new \InvalidArgumentException('Browsing settings must be stringified JSON.'); - else if (strlen($this->browsingSettings) > 2000) - throw new \InvalidArgumentException('Stringified browsing settings can have at most 2000 characters.'); + else if (strlen($this->browsingSettings) > 300) + throw new \InvalidArgumentException('Stringified browsing settings can have at most 300 characters.'); } } } diff --git a/src/Upgrades/Upgrade01.php b/src/Upgrades/Upgrade01.php index 6cfb3fd6..3e2edf18 100644 --- a/src/Upgrades/Upgrade01.php +++ b/src/Upgrades/Upgrade01.php @@ -5,36 +5,38 @@ class Upgrade01 implements IUpgrade { public function run(\Szurubooru\DatabaseConnection $databaseConnection) { + $driver = $databaseConnection->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME); + $databaseConnection->getPDO()->exec(' - CREATE TABLE "users" + CREATE TABLE users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - passwordHash TEXT NOT NULL, - email TEXT, - emailUnconfirmed TEXT, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + name VARCHAR(50) NOT NULL, + passwordHash VARCHAR(64) NOT NULL, + email VARCHAR(200), + emailUnconfirmed VARCHAR(200), accessRank INTEGER NOT NULL, - browsingSettings TEXT, - banned INTEGER, - registrationTime INTEGER DEFAULT NULL, - lastLoginTime INTEGER DEFAULT NULL, + browsingSettings VARCHAR(300), + banned BOOLEAN DEFAULT FALSE, + registrationTime DATETIME DEFAULT NULL, + lastLoginTime DATETIME DEFAULT NULL, avatarStyle INTEGER DEFAULT 1 );'); $databaseConnection->getPDO()->exec(' - CREATE TABLE "tokens" + CREATE TABLE tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + name VARCHAR(200) NOT NULL, purpose INTEGER NOT NULL, - additionalData TEXT + additionalData VARCHAR(200) );'); $databaseConnection->getPDO()->exec(' - CREATE TABLE "posts" + CREATE TABLE posts ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + name VARCHAR(200) NOT NULL );'); } } diff --git a/src/Upgrades/Upgrade02.php b/src/Upgrades/Upgrade02.php index 12f45123..d774ba59 100644 --- a/src/Upgrades/Upgrade02.php +++ b/src/Upgrades/Upgrade02.php @@ -6,6 +6,6 @@ class Upgrade02 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $databaseConnection->getPDO()->exec(' - ALTER TABLE "users" ADD COLUMN accountConfirmed BOOLEAN NOT NULL DEFAULT FALSE'); + ALTER TABLE users ADD COLUMN accountConfirmed BOOLEAN NOT NULL DEFAULT FALSE'); } } diff --git a/src/Upgrades/Upgrade03.php b/src/Upgrades/Upgrade03.php index 0650e346..4141595b 100644 --- a/src/Upgrades/Upgrade03.php +++ b/src/Upgrades/Upgrade03.php @@ -5,37 +5,40 @@ class Upgrade03 implements IUpgrade { public function run(\Szurubooru\DatabaseConnection $databaseConnection) { - $databaseConnection->getPDO()->exec('DROP TABLE "posts"'); + $pdo = $databaseConnection->getPDO(); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); - $databaseConnection->getPDO()->exec(' - CREATE TABLE "posts" + $pdo->exec('DROP TABLE IF EXISTS posts'); + + $pdo->exec(' + CREATE TABLE posts ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + name VARCHAR(40) NOT NULL, userId INTEGER, - uploadTime TIMESTAMP NOT NULL, - lastEditTime TIMESTAMP, + uploadTime DATETIME NOT NULL, + lastEditTime DATETIME, safety INTEGER NOT NULL, contentType INTEGER NOT NULL, - contentChecksum TEXT NOT NULL, - source TEXT, + contentChecksum VARCHAR(64) NOT NULL, + source VARCHAR(200), imageWidth INTEGER, imageHeight INTEGER, originalFileSize INTEGER, - originalFileName TEXT + originalFileName VARCHAR(200) )'); - $databaseConnection->getPDO()->exec(' - CREATE TABLE "tags" + $pdo->exec(' + CREATE TABLE tags ( - name TEXT PRIMARY KEY NOT NULL + name VARCHAR(64) PRIMARY KEY NOT NULL )'); - $databaseConnection->getPDO()->exec(' - CREATE TABLE "postTags" + $pdo->exec(' + CREATE TABLE postTags ( postId INTEGER NOT NULL, - tagName TEXT NOT NULL, + tagName VARCHAR(64) NOT NULL, PRIMARY KEY (postId, tagName) )'); } diff --git a/src/Upgrades/Upgrade04.php b/src/Upgrades/Upgrade04.php index ca879ea6..b9f884a0 100644 --- a/src/Upgrades/Upgrade04.php +++ b/src/Upgrades/Upgrade04.php @@ -19,7 +19,7 @@ class Upgrade04 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { - $databaseConnection->getPDO()->exec('ALTER TABLE "posts" ADD COLUMN contentMimeType TEXT DEFAULT NULL'); + $databaseConnection->getPDO()->exec('ALTER TABLE posts ADD COLUMN contentMimeType VARCHAR(64) DEFAULT NULL'); $posts = $this->postDao->findAll(); foreach ($posts as $post) diff --git a/src/Upgrades/Upgrade05.php b/src/Upgrades/Upgrade05.php index 04347c6d..521e81b9 100644 --- a/src/Upgrades/Upgrade05.php +++ b/src/Upgrades/Upgrade05.php @@ -6,12 +6,13 @@ class Upgrade05 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec(' CREATE TABLE tags2 ( - id INTEGER PRIMARY KEY NOT NULL, - name TEXT UNIQUE NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + name VARCHAR(64) UNIQUE NOT NULL, usages INTEGER NOT NULL DEFAULT 0 )'); $pdo->exec('INSERT INTO tags2(name, usages) SELECT name, (SELECT COUNT(1) FROM postTags WHERE tagName = tags.name) FROM tags'); diff --git a/src/Upgrades/Upgrade06.php b/src/Upgrades/Upgrade06.php index b603de05..815f27b5 100644 --- a/src/Upgrades/Upgrade06.php +++ b/src/Upgrades/Upgrade06.php @@ -7,8 +7,6 @@ class Upgrade06 implements IUpgrade { $pdo = $databaseConnection->getPDO(); - $pdo->exec('ALTER TABLE posts ADD COLUMN tagCount INTEGER NOT NULL DEFAULT 0'); - $pdo->exec(' CREATE TRIGGER postTagsDelete BEFORE DELETE ON postTags FOR EACH ROW @@ -34,5 +32,7 @@ class Upgrade06 implements IUpgrade UPDATE tags SET usages = usages + 1 WHERE tags.id = NEW.tagId; UPDATE tags SET usages = usages - 1 WHERE tags.id = OLD.tagId; END'); + + $pdo->exec('ALTER TABLE posts ADD COLUMN tagCount INTEGER NOT NULL DEFAULT 0'); } } diff --git a/src/Upgrades/Upgrade07.php b/src/Upgrades/Upgrade07.php index ee863ce5..0cc49a76 100644 --- a/src/Upgrades/Upgrade07.php +++ b/src/Upgrades/Upgrade07.php @@ -6,15 +6,16 @@ class Upgrade07 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - - $pdo->exec('ALTER TABLE posts ADD COLUMN featureCount INTEGER NOT NULL DEFAULT 0'); - $pdo->exec('ALTER TABLE posts ADD COLUMN lastFeatureTime TIMESTAMP'); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec('CREATE TABLE globals ( - id INTEGER PRIMARY KEY NOT NULL, - key TEXT UNIQUE NOT NULL, - value TEXT + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + dataKey VARCHAR(32) UNIQUE NOT NULL, + dataValue VARCHAR(64) )'); + + $pdo->exec('ALTER TABLE posts ADD COLUMN featureCount INTEGER NOT NULL DEFAULT 0'); + $pdo->exec('ALTER TABLE posts ADD COLUMN lastFeatureTime DATETIME'); } } diff --git a/src/Upgrades/Upgrade08.php b/src/Upgrades/Upgrade08.php index db0d8dac..a4e5cebf 100644 --- a/src/Upgrades/Upgrade08.php +++ b/src/Upgrades/Upgrade08.php @@ -6,10 +6,11 @@ class Upgrade08 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec('CREATE TABLE postRelations ( - id INTEGER PRIMARY KEY NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', post1id INTEGER NOT NULL, post2id INTEGER NOT NULL, UNIQUE (post1id, post2id) diff --git a/src/Upgrades/Upgrade09.php b/src/Upgrades/Upgrade09.php index 11a66b12..85d36516 100644 --- a/src/Upgrades/Upgrade09.php +++ b/src/Upgrades/Upgrade09.php @@ -17,13 +17,14 @@ class Upgrade09 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec('DROP TABLE IF EXISTS snapshots'); $pdo->exec('CREATE TABLE snapshots ( - id INTEGER PRIMARY KEY NOT NULL, - time TIMESTAMP NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', + time DATETIME NOT NULL, type INTEGER NOT NULL, primaryKey TEXT NOT NULL, operation INTEGER NOT NULL, diff --git a/src/Upgrades/Upgrade10.php b/src/Upgrades/Upgrade10.php index 1815f49a..db54b55e 100644 --- a/src/Upgrades/Upgrade10.php +++ b/src/Upgrades/Upgrade10.php @@ -6,16 +6,14 @@ class Upgrade10 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - - $pdo->exec('ALTER TABLE posts ADD COLUMN favCount INTEGER NOT NULL DEFAULT 0'); - $pdo->exec('ALTER TABLE posts ADD COLUMN lastFavTime TIMESTAMP'); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec('CREATE TABLE favorites ( - id INTEGER PRIMARY KEY NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', userId INTEGER NOT NULL, postId INTEGER NOT NULL, - time TIMESTAMP NOT NULL, + time DATETIME NOT NULL, UNIQUE (userId, postId) )'); @@ -55,5 +53,8 @@ class Upgrade10 implements IUpgrade WHERE favorites.postId = posts.id) WHERE posts.id IN (OLD.postId, NEW.postId); END'); + + $pdo->exec('ALTER TABLE posts ADD COLUMN favCount INTEGER NOT NULL DEFAULT 0'); + $pdo->exec('ALTER TABLE posts ADD COLUMN lastFavTime DATETIME'); } } diff --git a/src/Upgrades/Upgrade11.php b/src/Upgrades/Upgrade11.php index 1db7e057..bf10bb77 100644 --- a/src/Upgrades/Upgrade11.php +++ b/src/Upgrades/Upgrade11.php @@ -6,15 +6,14 @@ class Upgrade11 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - - $pdo->exec('ALTER TABLE posts ADD COLUMN score INTEGER NOT NULL DEFAULT 0'); + $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $pdo->exec('CREATE TABLE postScores ( - id INTEGER PRIMARY KEY NOT NULL, + id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ', userId INTEGER NOT NULL, postId INTEGER NOT NULL, - time TIMESTAMP NOT NULL, + time DATETIME NOT NULL, score INTEGER NOT NULL, UNIQUE (userId, postId) )'); @@ -54,5 +53,7 @@ class Upgrade11 implements IUpgrade WHERE postScores.postId = posts.id) WHERE posts.id = NEW.postId; END'); + + $pdo->exec('ALTER TABLE posts ADD COLUMN score INTEGER NOT NULL DEFAULT 0'); } } diff --git a/src/Validator.php b/src/Validator.php index 26e14c65..4941a5af 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -93,6 +93,8 @@ class Validator throw new \DomainException('Tags cannot be empty.'); } + $this->validateMaxLength($tag, 64, 'Tag'); + foreach ($illegalCharacters as $char) { if (strpos($tag, $char) !== false) diff --git a/tests/AbstractDatabaseTestCase.php b/tests/AbstractDatabaseTestCase.php index 18d55c83..7cd9cf6e 100644 --- a/tests/AbstractDatabaseTestCase.php +++ b/tests/AbstractDatabaseTestCase.php @@ -10,6 +10,8 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa parent::setUp(); $config = $this->mockConfig($this->createTestDirectory()); $config->set('database/dsn', 'sqlite::memory:'); + $config->set('database/user', ''); + $config->set('database/password', ''); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config); \Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection); diff --git a/tests/Dao/FavoritesDaoTest.php b/tests/Dao/FavoritesDaoTest.php index ea673891..e303b002 100644 --- a/tests/Dao/FavoritesDaoTest.php +++ b/tests/Dao/FavoritesDaoTest.php @@ -26,7 +26,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $favorite = new \Szurubooru\Entities\Favorite(); $favorite->setUser($user); $favorite->setPost($post); - $favorite->setTime('whatever'); + $favorite->setTime(date('c')); $favoritesDao = $this->getFavoritesDao(); $favoritesDao->save($favorite); @@ -36,7 +36,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $savedFavorite = $favoritesDao->findById($favorite->getId()); $this->assertEquals(1, $savedFavorite->getUserId()); $this->assertEquals(2, $savedFavorite->getPostId()); - $this->assertEquals('whatever', $savedFavorite->getTime()); + $this->assertEquals($favorite->getTime(), $savedFavorite->getTime()); $this->assertEntitiesEqual($user, $savedFavorite->getUser()); $this->assertEntitiesEqual($post, $savedFavorite->getPost()); } diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index 95226395..5bba25c2 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -293,7 +293,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $post = new \Szurubooru\Entities\Post(); $post->setName('test'); - $post->setUploadTime('whatever'); + $post->setUploadTime(date('c')); $post->setSafety(\Szurubooru\Entities\Post::POST_SAFETY_SAFE); $post->setContentType(\Szurubooru\Entities\Post::POST_TYPE_YOUTUBE); $post->setContentChecksum('whatever'); diff --git a/tests/Dao/PostScoreDaoTest.php b/tests/Dao/PostScoreDaoTest.php index f4342807..6cf2059c 100644 --- a/tests/Dao/PostScoreDaoTest.php +++ b/tests/Dao/PostScoreDaoTest.php @@ -26,7 +26,7 @@ class PostScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postScore = new \Szurubooru\Entities\PostScore(); $postScore->setUser($user); $postScore->setPost($post); - $postScore->setTime('whatever'); + $postScore->setTime(date('c')); $postScore->setScore(1); $postScoreDao = $this->getPostScoreDao(); $postScoreDao->save($postScore); @@ -37,7 +37,7 @@ class PostScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $savedPostScore = $postScoreDao->findById($postScore->getId()); $this->assertEquals(1, $savedPostScore->getUserId()); $this->assertEquals(2, $savedPostScore->getPostId()); - $this->assertEquals('whatever', $savedPostScore->getTime()); + $this->assertEquals($postScore->getTime(), $savedPostScore->getTime()); $this->assertEntitiesEqual($user, $savedPostScore->getUser()); $this->assertEntitiesEqual($post, $savedPostScore->getPost()); } @@ -52,19 +52,19 @@ class PostScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $postScore1 = new \Szurubooru\Entities\PostScore(); $postScore1->setUser($user1); $postScore1->setPost($post1); - $postScore1->setTime('time1'); + $postScore1->setTime(date('c', mktime(1))); $postScore1->setScore(1); $postScore2 = new \Szurubooru\Entities\PostScore(); $postScore2->setUser($user2); $postScore2->setPost($post2); - $postScore2->setTime('time2'); + $postScore2->setTime(date('c', mktime(2))); $postScore2->setScore(0); $postScore3 = new \Szurubooru\Entities\PostScore(); $postScore3->setUser($user1); $postScore3->setPost($post2); - $postScore3->setTime('time3'); + $postScore3->setTime(date('c', mktime(3))); $postScore3->setScore(-1); $postScoreDao = $this->getPostScoreDao(); diff --git a/tests/Dao/SnapshotDaoTest.php b/tests/Dao/SnapshotDaoTest.php index e9475dca..a245a0c1 100644 --- a/tests/Dao/SnapshotDaoTest.php +++ b/tests/Dao/SnapshotDaoTest.php @@ -40,7 +40,7 @@ class SnapshotDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $snapshot->setType(\Szurubooru\Entities\Snapshot::TYPE_POST); $snapshot->setData(['wake up', 'neo', ['follow' => 'white rabbit']]); $snapshot->setPrimaryKey(1); - $snapshot->setTime('whateveer'); + $snapshot->setTime(date('c', mktime(1, 2, 3))); $snapshot->setUserId(null); $snapshot->setOperation(\Szurubooru\Entities\Snapshot::OPERATION_CHANGE); return $snapshot; diff --git a/tests/Dao/UserDaoFilterTest.php b/tests/Dao/UserDaoFilterTest.php index 3d703398..14e82f10 100644 --- a/tests/Dao/UserDaoFilterTest.php +++ b/tests/Dao/UserDaoFilterTest.php @@ -151,8 +151,8 @@ class UserDaoFilterTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $user = new \Szurubooru\Entities\User(); $user->setName($userName); $user->setPasswordHash('whatever'); - $user->setLastLoginTime('whatever'); - $user->setRegistrationTime('whatever'); + $user->setLastLoginTime(date('c', mktime(1, 2, 3))); + $user->setRegistrationTime(date('c', mktime(3, 2, 1))); $user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER); return $user; } diff --git a/tests/Dao/UserDaoTest.php b/tests/Dao/UserDaoTest.php index 8ec4c217..2d7c1c78 100644 --- a/tests/Dao/UserDaoTest.php +++ b/tests/Dao/UserDaoTest.php @@ -114,8 +114,8 @@ final class UserDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $user = new \Szurubooru\Entities\User(); $user->setName('test'); $user->setPasswordHash('whatever'); - $user->setLastLoginTime('whatever'); - $user->setRegistrationTime('whatever'); + $user->setLastLoginTime(date('c', mktime(1, 2, 3))); + $user->setRegistrationTime(date('c', mktime(3, 2, 1))); $user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER); return $user; }