1
0
This commit is contained in:
Mike Schwörer 2020-01-15 00:29:45 +01:00
parent 2b450bdef1
commit c9b6223a87
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 65 additions and 36 deletions

View File

@ -1,9 +1,24 @@
<?php <?php
require_once __DIR__ . '/base.php'; require_once 'website.php';
class Books class Books
{ {
/** @var array */
private $staticData;
public function __construct()
{
$this->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) public static function readSingle($a)
{ {
$a['imgfront_url'] = '/data/images/book_img/' . $a['id'] . '_front.png'; $a['imgfront_url'] = '/data/images/book_img/' . $a['id'] . '_front.png';
@ -31,27 +46,27 @@ class Books
return $a; return $a;
} }
public static function listAll() public function listAll()
{ {
$all = require (__DIR__ . '/../statics/books/__all.php'); return $this->staticData;
return array_map('self::readSingle', $all);
} }
public static function listAllNewestFirst() public function listAllNewestFirst()
{ {
$data = self::listAll(); $data = $this->staticData;
usort($data, function($a, $b) { return strcasecmp($b['date'], $a['date']); }); usort($data, function($a, $b) { return strcasecmp($b['date'], $a['date']); });
return $data; return $data;
} }
public static function checkConsistency() public function checkConsistency()
{ {
$warn = null; $warn = null;
$this->load();
$ids = []; $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']]; if (in_array($prog['id'], $ids)) return ['result'=>'err', 'message' => 'Duplicate id ' . $prog['id']];
$ids []= $prog['id']; $ids []= $prog['id'];
@ -79,7 +94,7 @@ class Books
return ['result'=>'ok', 'message' => '']; return ['result'=>'ok', 'message' => ''];
} }
public static function checkThumbnails() public function checkThumbnails()
{ {
foreach (self::listAll() as $book) foreach (self::listAll() as $book)
{ {
@ -89,7 +104,7 @@ class Books
return ['result'=>'ok', 'message' => '']; return ['result'=>'ok', 'message' => ''];
} }
public static function createPreview($prog) public function createPreview($prog)
{ {
global $CONFIG; global $CONFIG;
@ -103,7 +118,7 @@ class Books
} }
public static function getBook($id) public function getBook($id)
{ {
foreach (self::listAll() as $book) { foreach (self::listAll() as $book) {
if ($book['id'] == $id) return $book; if ($book['id'] == $id) return $book;
@ -111,7 +126,7 @@ class Books
return null; return null;
} }
public static function getRepositoryHost($book) public function getRepositoryHost($book)
{ {
$r = $book['repository']; $r = $book['repository'];
if (startsWith($r, "http://")) $r = substr($r, strlen("http://")); if (startsWith($r, "http://")) $r = substr($r, strlen("http://"));

View File

@ -9,7 +9,12 @@ class Database
public function __construct(Website $site) 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 = $opt =
[ [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
@ -17,14 +22,12 @@ class Database
PDO::ATTR_EMULATE_PREPARES => false, 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) public function sql_query_num($query)
{ {
$r = $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0]; return $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0];
return $r;
} }
public function sql_query_num_prep($query, $params) public function sql_query_num_prep($query, $params)
@ -37,16 +40,13 @@ class Database
} }
$stmt->execute(); $stmt->execute();
$r = $stmt->fetch(PDO::FETCH_NUM)[0];
return $r; return $stmt->fetch(PDO::FETCH_NUM)[0];
} }
public function sql_query_assoc($query) public function sql_query_assoc($query)
{ {
$r = $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC); return $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
return $r;
} }
public function sql_query_assoc_prep($query, $params) public function sql_query_assoc_prep($query, $params)

View File

@ -17,8 +17,10 @@ class Website
/** @var array */ /** @var array */
public $config; public $config;
/** @var Database|null */ /** @var Database|null */ private $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() public function init()
{ {
@ -50,7 +52,7 @@ class Website
} }
} }
public static function getInstance() public static function inst()
{ {
return self::$instance; return self::$instance;
} }
@ -140,10 +142,6 @@ class Website
exit(); exit();
} }
/**
* @param PageFrameOptions $pfo
* @param URLRoute $route
*/
private function output(PageFrameOptions $pfo, URLRoute $route) private function output(PageFrameOptions $pfo, URLRoute $route)
{ {
if ($pfo->contentType !== null) header('Content-Type: ' . $pfo->contentType); if ($pfo->contentType !== null) header('Content-Type: ' . $pfo->contentType);
@ -160,16 +158,32 @@ class Website
require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame;
} }
public function database() public function Database()
{ {
if ($this->database === null) if ($this->database === null) { require_once 'database.php'; $this->database = new Database($this); }
{
require_once 'database.php';
$this->database = new Database($this);
}
return $this->database; 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 * @return bool
*/ */