1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?php
$db = new SQLite3('typecho.db');
$result = $db->query("SELECT a.cid, a.title, a.slug, a.text, a.created, a.status, CASE c.type WHEN 'tag' THEN group_concat(c.name) END tags, CASE c.type WHEN 'category' THEN group_concat(c.name) END categories FROM typecho_contents AS a LEFT JOIN typecho_relationships AS b ON b.cid=a.cid LEFT JOIN typecho_metas AS c ON c.mid=b.mid GROUP BY a.cid ORDER BY a.cid DESC" );
while ($row = $result->fetchArray(SQLITE3_ASSOC)) { generate($row); }
function generate($data) { $foler = __DIR__ . '/source/_drafts/';
$tpl = file_get_contents(__DIR__ . '/scaffolds/post.md');
$filename = $data['slug'] . '.md'; if ($data['status'] == 'publish') { $foler = __DIR__ . '/source/_posts/'; }
$tpl = str_replace('{{ title }}', $data['title'], $tpl); $tpl = str_replace('{{ date }}', date('Y-m-d H:i:s', $data['created']), $tpl); $tpl = str_replace('{{ tags }}', $data['tags'] ? '[' . $data['tags'] . ']' : '', $tpl); $tpl = str_replace('{{ categories }}', $data['categories'] ? '[' . $data['categories'] . ']' : '', $tpl);
$tpl .= $data['text']; file_put_contents($foler . $filename, $tpl); }
|