1
0
This commit is contained in:
Mike Schwörer 2020-01-15 00:29:49 +01:00
parent c9b6223a87
commit f06948851c
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -1,12 +1,22 @@
<?php <?php
require_once 'website.php';
class Blog class Blog
{ {
public static function listAll() /** @var array */
private $staticData;
public function __construct()
{
$this->load();
}
private function load()
{ {
$all = require (__DIR__ . '/../statics/blog/__all.php'); $all = require (__DIR__ . '/../statics/blog/__all.php');
return array_map('self::readSingle', $all); $this->staticData = array_map(function($a){return self::readSingle($a);}, $all);
} }
private static function readSingle($d) private static function readSingle($d)
@ -25,24 +35,29 @@ class Blog
return $d; return $d;
} }
public static function listAllNewestFirst() public function listAll()
{ {
$data = self::listAll(); return $this->staticData;
}
public function listAllNewestFirst()
{
$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 getBlogpost($id) public function getBlogpost($id)
{ {
foreach (self::listAll() as $post) { foreach ($this->staticData as $post) {
if ($post['id'] == $id) return $post; if ($post['id'] == $id) return $post;
} }
return null; return null;
} }
public static function getFullBlogpost($id, $subview, &$error) public function getFullBlogpost($id, $subview, &$error)
{ {
$post = self::getBlogpost($id); $post = $this->getBlogpost($id);
if ($post === null) { $error="Blogpost not found"; return null; } if ($post === null) { $error="Blogpost not found"; return null; }
$post['issubview'] = false; $post['issubview'] = false;
@ -52,7 +67,7 @@ class Blog
if ($isSubEuler) if ($isSubEuler)
{ {
require_once(__DIR__ . '/../internals/euler.php'); require_once(__DIR__ . '/../internals/euler.php');
$eulerproblem = Euler::getEulerProblemFromStrIdent($subview); $eulerproblem = Website::inst()->Euler()->getEulerProblemFromStrIdent($subview);
if ($eulerproblem === null) { $error="Project Euler entry not found"; return null; } if ($eulerproblem === null) { $error="Project Euler entry not found"; return null; }
$post['submodel'] = $eulerproblem; $post['submodel'] = $eulerproblem;
$post['issubview'] = true; $post['issubview'] = true;
@ -63,7 +78,7 @@ class Blog
if ($isSubAdventOfCode) if ($isSubAdventOfCode)
{ {
require_once(__DIR__ . '/../internals/adventofcode.php'); require_once(__DIR__ . '/../internals/adventofcode.php');
$adventofcodeday = AdventOfCode::getDayFromStrIdent($post['extras']['aoc:year'], $subview); $adventofcodeday = Website::inst()->AdventOfCode()->getDayFromStrIdent($post['extras']['aoc:year'], $subview);
if ($adventofcodeday === null) { $error="AdventOfCode entry not found"; return null; } if ($adventofcodeday === null) { $error="AdventOfCode entry not found"; return null; }
$post['submodel'] = $adventofcodeday; $post['submodel'] = $adventofcodeday;
$post['issubview'] = true; $post['issubview'] = true;
@ -79,16 +94,18 @@ class Blog
} }
public static function getPostFragment($post) public function getPostFragment($post)
{ {
return file_get_contents($post['file_fragment']); return file_get_contents($post['file_fragment']);
} }
public static function checkConsistency() public function checkConsistency()
{ {
$keys = []; $keys = [];
foreach (self::listAll() as $post) $this->load();
foreach ($this->staticData as $post)
{ {
if (in_array($post['id'], $keys)) return ['result'=>'err', 'message' => 'Duplicate key ' . $post['id']]; if (in_array($post['id'], $keys)) return ['result'=>'err', 'message' => 'Duplicate key ' . $post['id']];
$keys []= $post['id']; $keys []= $post['id'];