diff --git a/www/internals/books.php b/www/internals/books.php index cf3ece8..b2199ea 100644 --- a/www/internals/books.php +++ b/www/internals/books.php @@ -1,9 +1,24 @@ load(); + } + + private function load() + { + $all = require (__DIR__ . '/../statics/blog/__all.php'); + + $this->staticData = array_map(function($a){return self::readSingle($a);}, $all); + } + public static function readSingle($a) { $a['imgfront_url'] = '/data/images/book_img/' . $a['id'] . '_front.png'; @@ -31,27 +46,27 @@ class Books return $a; } - public static function listAll() + public function listAll() { - $all = require (__DIR__ . '/../statics/books/__all.php'); - - return array_map('self::readSingle', $all); + return $this->staticData; } - public static function listAllNewestFirst() + public function listAllNewestFirst() { - $data = self::listAll(); + $data = $this->staticData; usort($data, function($a, $b) { return strcasecmp($b['date'], $a['date']); }); return $data; } - public static function checkConsistency() + public function checkConsistency() { $warn = null; + $this->load(); + $ids = []; - foreach (self::listAll() as $prog) + foreach ($this->staticData as $prog) { if (in_array($prog['id'], $ids)) return ['result'=>'err', 'message' => 'Duplicate id ' . $prog['id']]; $ids []= $prog['id']; @@ -79,7 +94,7 @@ class Books return ['result'=>'ok', 'message' => '']; } - public static function checkThumbnails() + public function checkThumbnails() { foreach (self::listAll() as $book) { @@ -89,7 +104,7 @@ class Books return ['result'=>'ok', 'message' => '']; } - public static function createPreview($prog) + public function createPreview($prog) { global $CONFIG; @@ -103,7 +118,7 @@ class Books } - public static function getBook($id) + public function getBook($id) { foreach (self::listAll() as $book) { if ($book['id'] == $id) return $book; @@ -111,7 +126,7 @@ class Books return null; } - public static function getRepositoryHost($book) + public function getRepositoryHost($book) { $r = $book['repository']; if (startsWith($r, "http://")) $r = substr($r, strlen("http://")); diff --git a/www/internals/database.php b/www/internals/database.php index dc607c7..70327e9 100644 --- a/www/internals/database.php +++ b/www/internals/database.php @@ -9,7 +9,12 @@ class Database public function __construct(Website $site) { - $dsn = "mysql:host=" . $site->config['host'] . ";dbname=" . $site->config['database'] . ";charset=utf8"; + $this->connect($site->config); + } + + private function connect(array $config) + { + $dsn = "mysql:host=" . $config['host'] . ";dbname=" . $config['database'] . ";charset=utf8"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, @@ -17,14 +22,12 @@ class Database PDO::ATTR_EMULATE_PREPARES => false, ]; - $this->pdo = new PDO($dsn, $site->config['user'], $site->config['password'], $opt); + $this->pdo = new PDO($dsn, $config['user'], $config['password'], $opt); } public function sql_query_num($query) { - $r = $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0]; - - return $r; + return $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0]; } public function sql_query_num_prep($query, $params) @@ -37,16 +40,13 @@ class Database } $stmt->execute(); - $r = $stmt->fetch(PDO::FETCH_NUM)[0]; - return $r; + return $stmt->fetch(PDO::FETCH_NUM)[0]; } public function sql_query_assoc($query) { - $r = $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC); - - return $r; + return $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC); } public function sql_query_assoc_prep($query, $params) diff --git a/www/internals/website.php b/www/internals/website.php index 125821b..ca91737 100644 --- a/www/internals/website.php +++ b/www/internals/website.php @@ -17,8 +17,10 @@ class Website /** @var array */ public $config; - /** @var Database|null */ - private $database = null; + /** @var Database|null */ private $database = null; + /** @var AdventOfCode|null */ private $adventOfCode = null; + /** @var Blog|null */ private $blog = null; + /** @var Books|null */ private $books = null; public function init() { @@ -50,7 +52,7 @@ class Website } } - public static function getInstance() + public static function inst() { return self::$instance; } @@ -140,10 +142,6 @@ class Website exit(); } - /** - * @param PageFrameOptions $pfo - * @param URLRoute $route - */ private function output(PageFrameOptions $pfo, URLRoute $route) { if ($pfo->contentType !== null) header('Content-Type: ' . $pfo->contentType); @@ -160,16 +158,32 @@ class Website require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; } - public function database() + public function Database() { - if ($this->database === null) - { - require_once 'database.php'; - $this->database = new Database($this); - } + if ($this->database === null) { require_once 'database.php'; $this->database = new Database($this); } return $this->database; } + public function AdventOfCode() + { + if ($this->adventOfCode === null) { require_once 'adventofcode.php'; $this->adventOfCode = new AdventOfCode(); } + return $this->adventOfCode; + } + + public function Blog() + { + if ($this->blog === null) { require_once 'blog.php'; $this->blog = new Blog(); } + return $this->blog; + } + + public function Books() + { + if ($this->books === null) { require_once 'books.php'; $this->books = new Books(); } + return $this->books; + } + + + /** * @return bool */