Updated query usage.

This commit is contained in:
flash 2023-10-12 20:00:08 +00:00
parent 7321e72e89
commit f9b73c6067
6 changed files with 158 additions and 142 deletions

View file

@ -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');

View file

@ -1,41 +1,42 @@
<?php
namespace Makai;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class Contacts {
private const QUERY = 'SELECT `cont_name`, `cont_homepage`, `cont_order`, `cont_title`, `cont_icon`, `cont_colour`, `cont_display`, `cont_link` FROM `fm_contacts`';
private IDbConnection $dbConn;
private DbStatementCache $cache;
private const QUERY_HOME = self::QUERY . ' WHERE `cont_homepage` <> 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;
}
}

View file

@ -1,53 +0,0 @@
<?php
namespace Makai;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
use Index\Data\IDbConnection;
class Languages {
private const QUERY = 'SELECT pl.`language_id`, pl.`language_name`, pl.`language_colour` FROM `fm_proglangs` AS pl';
private const QUERY_PROJECT = self::QUERY . ' LEFT JOIN `fm_projects_proglangs` AS ppl ON ppl.`language_id` = pl.`language_id` WHERE ppl.`project_id` = ? ORDER BY ppl.`priority`';
private const QUERY_PROJECT_COLOUR = 'SELECT pl.`language_colour` FROM `fm_proglangs` AS pl LEFT JOIN `fm_projects_proglangs` AS ppl ON ppl.`language_id` = pl.`language_id` WHERE pl.`language_colour` IS NOT NULL AND ppl.`project_id` = ? ORDER BY ppl.`priority` LIMIT 1';
private IDbConnection $conn;
public function __construct(IDbConnection $conn) {
$this->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);
}
}

View file

@ -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 {

View file

@ -1,41 +1,89 @@
<?php
namespace Makai;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class Projects {
private const QUERY = 'SELECT `project_id`, `project_name`, COALESCE(`project_name_clean`, REPLACE(LOWER(`project_name`), \' \', \'-\')), `project_summary`, `project_description`, `project_featured`, `project_order`, `project_homepage`, `project_repository`, `project_forum`, UNIX_TIMESTAMP(`project_archived`), `project_type`, UNIX_TIMESTAMP(`project_created`), `project_colour` FROM `fm_projects` WHERE `project_deleted` IS NULL';
private IDbConnection $dbConn;
private DbStatementCache $cache;
private const QUERY_ALL = self::QUERY . ' ORDER BY `project_order` DESC';
private const QUERY_FEATURED = self::QUERY . ' AND `project_featured` <> 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));
}
}

View file

@ -1,34 +1,46 @@
<?php
namespace Makai;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class SSHKeys {
private const QUERY = 'SELECT `key_id`, `key_level`, `key_algo`, `key_body`, `key_comment`, UNIX_TIMESTAMP(`key_created`), UNIX_TIMESTAMP(`key_deprecated`) FROM `fm_public_keys` WHERE `key_level` >= ?';
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;
}
}