From f9b73c6067e3d7e3a8e085576b03ee5241f179e0 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 12 Oct 2023 20:00:08 +0000 Subject: [PATCH] Updated query usage. --- public/index.php | 46 +++++++++++++++---------- src/Contacts.php | 51 +++++++++++++-------------- src/Languages.php | 53 ---------------------------- src/ProjectInfo.php | 22 ++++++------ src/Projects.php | 84 +++++++++++++++++++++++++++++++++++---------- src/SSHKeys.php | 44 +++++++++++++++--------- 6 files changed, 158 insertions(+), 142 deletions(-) delete mode 100644 src/Languages.php diff --git a/public/index.php b/public/index.php index 8dd75da..ec0ba1f 100644 --- a/public/index.php +++ b/public/index.php @@ -127,48 +127,56 @@ $router->get('/', function() use ($ctx) { $dbConn = $ctx->getDatabase(); - $projectInfos = (new Projects($dbConn))->getFeatured(); - $langs = new Languages($dbConn); + $projects = new Projects($dbConn); + $projectInfos = $projects->getProjects( + featuredOnly: true, + deleted: false, + take: 3, + random: true, + ); - $projects = []; + $projectItems = []; foreach($projectInfos as $projectInfo) - $projects[] = [ + $projectItems[] = [ 'info' => $projectInfo, - 'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $langs->getProjectColour($projectInfo), + 'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $projects->getProjectColour($projectInfo), ]; - $contacts = (new Contacts($dbConn))->getHomePage(); + $contacts = (new Contacts($dbConn))->getContacts( + homePageOnly: true, + take: 3, + ); return $ctx->getTemplating()->render('index', [ - 'projects' => $projects, + 'projects' => $projectItems, 'contacts' => $contacts, ]); }); $router->get('/contact', function() use ($ctx) { return $ctx->getTemplating()->render('contact', [ - 'contacts' => (new Contacts($ctx->getDatabase()))->getAll(), + 'contacts' => (new Contacts($ctx->getDatabase()))->getContacts(), ]); }); $router->get('/projects', function() use ($ctx) { $dbConn = $ctx->getDatabase(); - $langs = new Languages($dbConn); - $projectInfos = (new Projects($dbConn))->getAll(); + $projects = new Projects($dbConn); + $projectInfos = $projects->getProjects(deleted: false); - $projects = []; + $items = []; foreach($projectInfos as $projectInfo) - $projects[] = [ + $items[] = [ 'info' => $projectInfo, - 'langs' => $langs->getByProject($projectInfo), - 'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $langs->getProjectColour($projectInfo), + 'langs' => $projects->getLanguages(projectInfo: $projectInfo), + 'colour' => $projectInfo->hasColour() ? $projectInfo->getColour() : $projects->getProjectColour($projectInfo), ]; $sections = [ 'projects' => [ 'title' => 'Projects', 'desc' => '', - 'items' => $projects, + 'items' => $items, ], ]; @@ -227,7 +235,7 @@ $router->get('/ssh_keys', function() use ($db) { $includeComment = !empty($_GET['c']); $json = !empty($_GET['j']); - $keys = (new SSHKeys($db))->getKeys($minLevel); + $keys = (new SSHKeys($db))->getKeys(minLevel: $minLevel, deprecated: false); if($json) { $items = []; @@ -257,7 +265,7 @@ $router->get('/ssh_keys', function() use ($db) { }); $router->get('/authorized_keys', function() use ($db) { - $keys = (new SSHKeys($db))->getKeys(500); + $keys = (new SSHKeys($db))->getKeys(minLevel: 500, deprecated: false); header('Content-Type: text/plain; charset=us-ascii'); @@ -269,7 +277,7 @@ $router->get('/authorized_keys', function() use ($db) { }); $router->get('/git_keys_ro', function() use ($db) { - $keys = (new SSHKeys($db))->getKeys(100); + $keys = (new SSHKeys($db))->getKeys(minLevel: 100, deprecated: false); header('Content-Type: text/plain; charset=us-ascii'); @@ -281,7 +289,7 @@ $router->get('/git_keys_ro', function() use ($db) { }); $router->get('/git_keys_rw', function() use ($db) { - $keys = (new SSHKeys($db))->getKeys(200); + $keys = (new SSHKeys($db))->getKeys(minLevel: 200, deprecated: false); header('Content-Type: text/plain; charset=us-ascii'); diff --git a/src/Contacts.php b/src/Contacts.php index 27f0479..1b6d2b8 100644 --- a/src/Contacts.php +++ b/src/Contacts.php @@ -1,41 +1,42 @@ 0 ORDER BY `cont_order` LIMIT 3'; - private const QUERY_ALL = self::QUERY . ' ORDER BY `cont_order`'; - - private IDbConnection $conn; - - public function __construct(IDbConnection $conn) { - $this->conn = $conn; + public function __construct(IDbConnection $dbConn) { + $this->dbConn = $dbConn; + $this->cache = new DbStatementCache($dbConn); } - public function getAll(): array { - $stmt = $this->conn->prepare(self::QUERY_ALL); + public function getContacts( + bool $homePageOnly = false, + ?int $take = null + ): array { + $hasTake = $take !== null; + + $query = 'SELECT cont_name, cont_homepage, cont_order, cont_title, cont_icon, cont_colour, cont_display, cont_link FROM fm_contacts'; + if($homePageOnly) + $query .= ' WHERE cont_homepage <> 0'; + $query .= ' ORDER BY cont_order'; + if($hasTake) + $query .= ' LIMIT ?'; + + $stmt = $this->cache->get($query); + if($hasTake) + $stmt->addParameter(1, $take); $stmt->execute(); + $result = $stmt->getResult(); - $objs = []; + $contacts = []; while($result->next()) - $objs[] = new ContactInfo($result); + $contacts[] = new ContactInfo($result); - return $objs; - } - - public function getHomePage(): array { - $stmt = $this->conn->prepare(self::QUERY_HOME); - $stmt->execute(); - $result = $stmt->getResult(); - $objs = []; - - while($result->next()) - $objs[] = new ContactInfo($result); - - return $objs; + return $contacts; } } diff --git a/src/Languages.php b/src/Languages.php deleted file mode 100644 index d9f3954..0000000 --- a/src/Languages.php +++ /dev/null @@ -1,53 +0,0 @@ -conn = $conn; - } - - public function getByProject(ProjectInfo $project): array { - $stmt = $this->conn->prepare(self::QUERY_PROJECT); - $stmt->addParameter(1, $project->getId()); - $stmt->execute(); - $result = $stmt->getResult(); - $objs = []; - - while($result->next()) - $objs[] = new LanguageInfo($result); - - return $objs; - } - - public function getProjectColour(ProjectInfo $project): Colour { - $raw = $this->getProjectColourRaw($project); - if($raw === null) - return Colour::none(); - - return ColourRGB::fromRawRGB($raw); - } - - public function getProjectColourRaw(ProjectInfo $project): ?int { - $stmt = $this->conn->prepare(self::QUERY_PROJECT_COLOUR); - $stmt->addParameter(1, $project->getId()); - $stmt->execute(); - $result = $stmt->getResult(); - - if(!$result->next()) - return null; - - return $result->isNull(0) ? null : $result->getInteger(0); - } -} diff --git a/src/ProjectInfo.php b/src/ProjectInfo.php index fc7f40a..e3d6923 100644 --- a/src/ProjectInfo.php +++ b/src/ProjectInfo.php @@ -12,10 +12,10 @@ class ProjectInfo { private string $nameClean; private ?string $summary; private ?string $description; - private bool $featured; private int $order; - private ?int $colour; private string $type; + private bool $featured; + private ?int $colour; private ?string $homepage; private ?string $source; private ?string $discussion; @@ -29,16 +29,16 @@ class ProjectInfo { $this->nameClean = $result->getString(2); $this->summary = $result->isNull(3) ? null : $result->getString(3); $this->description = $result->isNull(4) ? null : $result->getString(4); - $this->featured = $result->getInteger(5) !== 0; - $this->order = $result->getInteger(6); - $this->colour = $result->isNull(13) ? null : $result->getInteger(13); - $this->type = $result->getString(11); - $this->homepage = $result->isNull(7) ? null : $result->getString(7); - $this->source = $result->isNull(8) ? null : $result->getString(8); - $this->discussion = $result->isNull(9) ? null : $result->getString(9); + $this->order = $result->getInteger(5); + $this->type = $result->getString(6); + $this->featured = $result->getInteger(7) !== 0; + $this->colour = $result->isNull(8) ? null : $result->getInteger(8); + $this->homepage = $result->isNull(9) ? null : $result->getString(9); + $this->source = $result->isNull(10) ? null : $result->getString(10); + $this->discussion = $result->isNull(11) ? null : $result->getString(11); $this->createdAt = $result->getInteger(12); - $this->archivedAt = $result->isNull(10) ? null : $result->getInteger(10); - $this->deletedAt = null; + $this->archivedAt = $result->isNull(13) ? null : $result->getInteger(13); + $this->deletedAt = $result->isNull(14) ? null : $result->getInteger(14); } public function getId(): string { diff --git a/src/Projects.php b/src/Projects.php index 7d46f5c..c951397 100644 --- a/src/Projects.php +++ b/src/Projects.php @@ -1,41 +1,89 @@ 0 ORDER BY RAND() LIMIT 3'; - - private IDbConnection $conn; - - public function __construct(IDbConnection $conn) { - $this->conn = $conn; + public function __construct(IDbConnection $dbConn) { + $this->dbConn = $dbConn; + $this->cache = new DbStatementCache($dbConn); } - public function getAll(): array { - $stmt = $this->conn->prepare(self::QUERY_ALL); + public function getProjects( + bool $featuredOnly = false, + ?bool $deleted = null, + ?int $take = null, + bool $random = false + ): array { + $hasDeleted = $deleted !== null; + $hasTake = $take !== null; + + $args = 0; + $query = 'SELECT project_id, project_name, COALESCE(project_name_clean, REPLACE(LOWER(project_name), \' \', \'-\')), project_summary, project_description, project_order, project_type, project_featured, project_colour, project_homepage, project_repository, project_forum, UNIX_TIMESTAMP(project_created), UNIX_TIMESTAMP(project_deleted), UNIX_TIMESTAMP(project_archived) FROM fm_projects'; + if($featuredOnly) { + ++$args; + $query .= ' WHERE project_featured <> 0'; + } + if($hasDeleted) + $query .= sprintf(' %s project_deleted %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deleted ? 'IS NOT' : 'IS'); + $query .= sprintf(' ORDER BY %s', $random ? 'RAND()' : 'project_order DESC'); + if($hasTake) + $query .= ' LIMIT ?'; + + $stmt = $this->cache->get($query); + if($hasTake) + $stmt->addParameter(1, $take); $stmt->execute(); + $result = $stmt->getResult(); - $objs = []; + $projects = []; while($result->next()) - $objs[] = new ProjectInfo($result); + $projects[] = new ProjectInfo($result); - return $objs; + return $projects; } - public function getFeatured(): array { - $stmt = $this->conn->prepare(self::QUERY_FEATURED); + public function getLanguages( + ProjectInfo|string|null $projectInfo = null + ): array { + $hasProjectInfo = $projectInfo !== null; + + $query = 'SELECT l.language_id, l.language_name, l.language_colour FROM fm_proglangs AS l'; + if($hasProjectInfo) + $query .= ' LEFT JOIN fm_projects_proglangs AS pl ON pl.language_id = l.language_id WHERE pl.project_id = ? ORDER BY pl.priority ASC'; + + $stmt = $this->cache->get($query); + if($hasProjectInfo) + $stmt->addParameter(1, $projectInfo instanceof ProjectInfo ? $projectInfo->getId() : $projectInfo); $stmt->execute(); + $result = $stmt->getResult(); - $objs = []; + $langs = []; while($result->next()) - $objs[] = new ProjectInfo($result); + $langs[] = new LanguageInfo($result); - return $objs; + return $langs; + } + + public function getProjectColour(ProjectInfo|string $projectInfo): Colour { + $query = 'SELECT language_colour FROM fm_proglangs WHERE language_id = (SELECT language_id FROM fm_projects_proglangs WHERE project_id = ? ORDER BY priority ASC)'; + + $stmt = $this->cache->get($query); + $stmt->addParameter(1, $projectInfo instanceof ProjectInfo ? $projectInfo->getId() : $projectInfo); + $stmt->execute(); + $result = $stmt->getResult(); + + if(!$result->next() || $result->isNull(0)) + return Colour::none(); + + return ColourRGB::fromRawRGB($result->getInteger(0)); } } diff --git a/src/SSHKeys.php b/src/SSHKeys.php index 3c1afd8..be4259c 100644 --- a/src/SSHKeys.php +++ b/src/SSHKeys.php @@ -1,34 +1,46 @@ = ?'; + private IDbConnection $dbConn; + private DbStatementCache $cache; - private IDbConnection $conn; - - public function __construct(IDbConnection $conn) { - $this->conn = $conn; + public function __construct(IDbConnection $dbConn) { + $this->dbConn = $dbConn; + $this->cache = new DbStatementCache($dbConn); } - public function getKeys(int $minLevel, bool $includeDeprecated = false): array { - $query = self::QUERY; + public function getKeys( + ?int $minLevel = null, + ?bool $deprecated = null, + ): array { + $hasMinLevel = $minLevel !== null; + $hasDeprecated = $deprecated !== null; - if(!$includeDeprecated) - $query .= ' AND `key_deprecated` IS NULL'; + $args = 0; + $query = 'SELECT key_id, key_level, key_algo, key_body, key_comment, UNIX_TIMESTAMP(key_created), UNIX_TIMESTAMP(key_deprecated) FROM fm_public_keys'; + if($hasMinLevel) { + ++$args; + $query .= ' WHERE key_level >= ?'; + } + if($hasDeprecated) + $query .= sprintf(' %s key_deprecated %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deprecated ? 'IS NOT' : 'IS'); + $query .= ' ORDER BY key_level DESC, key_id ASC'; - $query .= ' ORDER BY `key_level` DESC, `key_id`'; - - $stmt = $this->conn->prepare($query); - $stmt->addParameter(1, $minLevel); + $stmt = $this->cache->get($query); + if($hasMinLevel) + $stmt->addParameter(1, $minLevel); $stmt->execute(); + $result = $stmt->getResult(); - $objs = []; + $keys = []; while($result->next()) - $objs[] = new SSHKeyInfo($result); + $keys[] = new SSHKeyInfo($result); - return $objs; + return $keys; } }