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

89 lines
2.1 KiB
PHP
Raw Normal View History

2017-11-08 17:39:50 +01:00
<?php
class Blog
{
public static function listAll()
{
2018-01-03 22:08:56 +01:00
$all = require (__DIR__ . '/../statics/blog/__all.php');
2017-12-30 23:46:27 +01:00
2019-11-02 20:19:34 +01:00
return array_map('self::readSingle', $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'];
2018-01-27 14:21:34 +01:00
$d['file_fragment'] = __DIR__ . '/../statics/blog/' . $d['fragment'];
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;
}
2018-01-26 21:12:04 +01:00
public static function listAllNewestFirst()
2017-12-30 23:46:27 +01:00
{
2017-12-28 22:53:41 +01:00
$data = self::listAll();
usort($data, function($a, $b) { return strcasecmp($b['date'], $a['date']); });
return $data;
}
2017-12-30 23:11:51 +01:00
public static function getBlogpost($id)
{
foreach (self::listAll() as $post) {
if ($post['id'] == $id) return $post;
}
return null;
}
public static function getPostFragment($post)
{
2018-01-27 14:21:34 +01:00
return file_get_contents($post['file_fragment']);
}
public static function checkConsistency()
{
$keys = [];
foreach (self::listAll() as $post)
{
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') {
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
} else if ($post['type'] === 'euler') {
// aok
2019-11-02 20:19:34 +01:00
} else if ($post['type'] === 'aoc') {
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
}