PDO::ERRMODE_EXCEPTION, ]); //------------------------------- // Check if any post was updated //------------------------------- $sql = "select MAX(updated) upd from posts"; $stmt = $pdo->query($sql); $row = $stmt->fetch(PDO::FETCH_ASSOC); $upd = trim($row['upd']); $TIMEFILE = sys_get_temp_dir().DIRECTORY_SEPARATOR.'writefreely_to_gemini_stamp'; echo "Using timestamp file:$TIMEFILE\n\n"; $prev = trim(file_get_contents($TIMEFILE)); if ($upd==$prev) die("No updated needed, Latest stamp is equal to : [ $upd ] \n\n"); echo "Last updated in database: [ $upd ] NOT EQUAL to disk stamp [ $prev ] in - WILL UPDATE \n\n"; file_put_contents($TIMEFILE,$upd); //------------------------------- // Process all posts each time //------------------------------- $sql = <<query($sql); $prevusername =''; $username =''; $index =[]; $users =[]; $descs =[]; foreach ($stmt as $row) { $username = clean($row['username'] ?? 'nouser'); $slug = clean($row['slug'] ?? 'no-slug'); $description = $row['description'] ?? '--'; echo "\n-----------------------------------"; echo "\n# Getting posts for user: $username"; echo "\n-----------------------------------"; $userDir = $baseDir . DIRECTORY_SEPARATOR . $username; if ($username!=$prevusername) { $users[]=$username; $descs[$username]=$description; if (count($index)>0) saveOneBlogIndex($prevusername, $index); $index = []; $prevusername=$username; } $index[] = $slug; $txt = (string)$row['content']; saveBothFormats($userDir . DIRECTORY_SEPARATOR . $slug, $txt ); } #save last index after loop saveOneBlogIndex($username, $index); saveUserList($users,$descs); saveRecentPosts(); saveRootIndex(); echo "\n### Done.\n"; function saveBothFormats($path, $txt) { $mdPath = $path.'.md'; $gmiPath = $path.'.gmi'; echo "\n...Saving: $gmiPath / .md\n"; createMissingDirsForFilePath($mdPath); $txtSpaceHack = str_replace("\n\n", '|||', $txt); $txtSpaceHack = str_replace("\n", '$$$', $txtSpaceHack); file_put_contents($mdPath,$txtSpaceHack); #Convert from .md to.gmi shell_exec("./bin/gemgen -a below -p below $mdPath"); $cleanMe = file_get_contents($gmiPath); $cleanMe = str_replace('|||',"\n\n", $cleanMe); $cleanMe = str_replace('$$$',"\n", $cleanMe); //Cleaned up gmi with propre new lines file_put_contents($gmiPath,$cleanMe); //Overwrite again with clean original .md file file_put_contents($mdPath,$txt); } function saveUserList($users,$descs) { echo "# Generating blog list..\n"; global $baseDir; #save user list in /blogs/index.gmi $userFile = $baseDir . DIRECTORY_SEPARATOR . 'blogs'.DIRECTORY_SEPARATOR.'index.gmi'; $body = "# Lista dos blogs...\n\n"; sort($users, SORT_STRING | SORT_FLAG_CASE); foreach ($users as $user) $body .= "=> /$user"." $user :: ".$descs[$user]."\n"; createMissingDirsForFilePath($userFile); file_put_contents($userFile,$body); } function saveRootIndex() { global $baseDir; global $pdo; $outFile = $baseDir . DIRECTORY_SEPARATOR . 'index'; createMissingDirsForFilePath($outFile); echo "\n## Saved Root index: $outFile"; $sql = "select * from appcontent where id='about'"; $stmt = $pdo->query($sql); $row = $stmt->fetch(PDO::FETCH_ASSOC); #Create .md as we convert it $body = "# ".$row['title']."\n\n"; $body .= $row['content']; global $titleRecentPosts; global $titleBlogList ; $body .= "[$titleRecentPosts](/read)\n"; $body .= "[$titleBlogList](/blogs)\n\n"; saveBothFormats($outFile,$body); } function saveRecentPosts() { global $baseDir; global $pdo; $outFile = $baseDir . DIRECTORY_SEPARATOR . 'read'.DIRECTORY_SEPARATOR.'index'; $sql = <<query($sql); $row = $stmt->fetch(PDO::FETCH_ASSOC); $body = "# Posts recentes \n"; foreach ($stmt as $row) { $username = clean($row['username'] ?? 'nouser'); $slug = clean($row['slug'] ?? 'no-slug'); $body .= "=> /$username/$slug".".gmi $slug \n"; } saveBothFormats($outFile,$body); echo "\n## Saved Recent index: $outFile"; } function saveOneBlogIndex($username,$index) { global $baseDir; $prevuserDir = $baseDir . DIRECTORY_SEPARATOR . $username; $body = "# $username\n"; foreach ($index as $link) $body .= "=> $link".".gmi $link\n"; $outIndexFile = $prevuserDir . DIRECTORY_SEPARATOR . 'index.gmi'; createMissingDirsForFilePath($outIndexFile); echo "\nSaved index: $outIndexFile"; file_put_contents($outIndexFile, $body); } function clean($s) { $s = (string)$s; $s = trim($s); #cleanup $s = preg_replace('~[^A-Za-z0-9._-]+~', '_', $s); return $s === '' ? 'unknown' : $s; }; function createMissingDirsForFilePath($filePath) { $dir = dirname($filePath); if (!is_dir($dir)) { if (!mkdir($dir, 0775, true)) { throw new RuntimeException("Failed to create dir: $dir"); } } } ?>