1
0
www.mikescher.com/www/internals/modules/blog.php

153 lines
4.0 KiB
PHP
Raw Normal View History

2017-11-08 17:39:50 +01:00
<?php
2020-01-19 13:16:40 +01:00
class Blog implements IWebsiteModule
2017-11-08 17:39:50 +01:00
{
2020-01-15 00:29:49 +01:00
/** @var array */
private $staticData;
public function __construct()
{
$this->load();
}
private function load()
2017-11-08 17:39:50 +01:00
{
2020-01-15 02:50:23 +01:00
$all = require (__DIR__ . '/../../statics/blog/__all.php');
2017-12-30 23:46:27 +01:00
2020-01-15 00:29:49 +01:00
$this->staticData = array_map(function($a){return self::readSingle($a);}, $all);
2017-11-08 17:39:50 +01:00
}
2017-12-28 22:53:41 +01:00
2019-11-02 20:19:34 +01:00
private static function readSingle($d)
2017-12-30 23:46:27 +01:00
{
if ($d['cat']==='blog')
$d['url'] = "/blog/" . $d['id'] . "/" . destructiveUrlEncode($d['title']);
else if ($d['cat']==='log')
$d['url'] = "/log/" . $d['id'] . "/" . destructiveUrlEncode($d['title']);
$d['canonical'] = "https://www.mikescher.com" . $d['url'];
2020-01-16 10:50:18 +01:00
$d['file_fragment'] = __DIR__ . '/../../statics/blog/' . $d['fragment'];
2018-01-27 14:21:34 +01:00
2019-11-02 20:19:34 +01:00
if (!array_key_exists('extras', $d)) $d['extras'] = [];
2017-12-30 23:46:27 +01:00
return $d;
}
2020-01-15 00:29:49 +01:00
public function listAll()
2017-12-30 23:46:27 +01:00
{
2020-01-15 00:29:49 +01:00
return $this->staticData;
}
public function listAllNewestFirst()
{
$data = $this->staticData;
2017-12-28 22:53:41 +01:00
usort($data, function($a, $b) { return strcasecmp($b['date'], $a['date']); });
return $data;
}
2017-12-30 23:11:51 +01:00
2020-01-15 00:29:49 +01:00
public function getBlogpost($id)
2017-12-30 23:11:51 +01:00
{
2020-01-15 00:29:49 +01:00
foreach ($this->staticData as $post) {
2017-12-30 23:11:51 +01:00
if ($post['id'] == $id) return $post;
}
return null;
}
2020-01-16 10:50:18 +01:00
/**
* @param string $id
* @param string $subview
* @param string $error
* @return array|null
*/
2020-01-15 00:29:49 +01:00
public function getFullBlogpost($id, $subview, &$error)
2019-12-02 14:57:01 +01:00
{
2020-01-15 00:29:49 +01:00
$post = $this->getBlogpost($id);
2019-12-02 14:57:01 +01:00
if ($post === null) { $error="Blogpost not found"; return null; }
$post['issubview'] = false;
$isSubEuler = ($post['type'] === 'euler' && $subview !== '');
$eulerproblem = null;
if ($isSubEuler)
{
2020-01-15 02:50:23 +01:00
$eulerproblem = Website::inst()->modules->Euler()->getEulerProblemFromStrIdent($subview);
2019-12-02 14:57:01 +01:00
if ($eulerproblem === null) { $error="Project Euler entry not found"; return null; }
$post['submodel'] = $eulerproblem;
$post['issubview'] = true;
}
$isSubAdventOfCode = ($post['type'] === 'aoc' && $subview !== '');
$adventofcodeday = null;
if ($isSubAdventOfCode)
{
2020-01-15 02:50:23 +01:00
$adventofcodeday = Website::inst()->modules->AdventOfCode()->getDayFromStrIdent($post['extras']['aoc:year'], $subview);
2019-12-02 14:57:01 +01:00
if ($adventofcodeday === null) { $error="AdventOfCode entry not found"; return null; }
$post['submodel'] = $adventofcodeday;
$post['issubview'] = true;
}
if ($isSubEuler) $post['title'] = $eulerproblem['title'];
if ($isSubAdventOfCode) $post['title'] = $adventofcodeday['title'];
if ($isSubEuler) $post['canonical'] = $eulerproblem['canonical'];
if ($isSubAdventOfCode) $post['canonical'] = $adventofcodeday['canonical'];
return $post;
}
2020-01-15 00:29:49 +01:00
public function getPostFragment($post)
2017-12-30 23:11:51 +01:00
{
2018-01-27 14:21:34 +01:00
return file_get_contents($post['file_fragment']);
}
2020-01-15 00:29:49 +01:00
public function checkConsistency()
2018-01-27 14:21:34 +01:00
{
$keys = [];
2020-01-15 00:29:49 +01:00
$this->load();
foreach ($this->staticData as $post)
2018-01-27 14:21:34 +01:00
{
if (in_array($post['id'], $keys)) return ['result'=>'err', 'message' => 'Duplicate key ' . $post['id']];
$keys []= $post['id'];
if ($post['cat'] !== 'log' && $post['cat'] !== 'blog') return ['result'=>'err', 'message' => 'Unknown cat ' . $post['cat']];
if ($post['type'] === 'markdown') {
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
} else if ($post['type'] === 'plain') {
2018-01-27 14:21:34 +01:00
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
} else if ($post['type'] === 'html') {
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
2018-01-27 14:21:34 +01:00
} else if ($post['type'] === 'euler') {
// aok
2019-11-02 20:19:34 +01:00
} else if ($post['type'] === 'aoc') {
2020-12-02 08:32:04 +01:00
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
2019-11-02 20:19:34 +01:00
if (!array_key_exists('aoc:year', $post['extras'])) return ['result'=>'err', 'message' => 'AdventOfCode metadata [aoc:year] missing: ' . $post['title']];
// aok
2018-01-27 14:21:34 +01:00
} else {
return ['result'=>'err', 'message' => 'Unknown type ' . $post['type']];
}
}
return ['result'=>'ok', 'message' => ''];
2017-12-30 23:11:51 +01:00
}
2017-11-08 17:39:50 +01:00
}