WebOrbiton
v2.0.0.0

Publisium

53 lines · 1.6 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/database.php';
  6. require_once __DIR__ . '/activity-log.php';
  7. require_once __DIR__ . '/indexnow.php';
  8. require_once __DIR__ . '/read-aloud.php';
  9. ​
  10. final class ArticleScheduler
  11. {
  12. private static bool $running = false;
  13. ​
  14. public static function publishDue(): void
  15. {
  16. if (self::$running) {
  17. return;
  18. }
  19. self::$running = true;
  20. ​
  21. try {
  22. $db = Database::site();
  23. $due = $db->query(
  24. "SELECT id FROM articles WHERE status = 'scheduled' AND scheduled_at IS NOT NULL AND scheduled_at <= NOW() ORDER BY scheduled_at ASC LIMIT 20"
  25. )->fetchAll(PDO::FETCH_COLUMN);
  26. ​
  27. if (empty($due)) {
  28. return;
  29. }
  30. ​
  31. $publish = $db->prepare(
  32. "UPDATE articles SET status = 'published', published_at = scheduled_at, scheduled_at = NULL, updated_at = NOW()
  33. WHERE id = :id AND status = 'scheduled'"
  34. );
  35. ​
  36. foreach ($due as $articleId) {
  37. $publish->execute(['id' => (int) $articleId]);
  38. if ($publish->rowCount() !== 1) {
  39. continue;
  40. }
  41. ​
  42. ActivityLog::record('article.auto_publish', 'article', (int) $articleId, 'scheduled publication', true);
  43. IndexNow::notifyArticle((int) $articleId);
  44. ReadAloud::queueGeneration((int) $articleId);
  45. }
  46. } catch (PDOException $e) {
  47. error_log('Publisium: scheduled publishing failed: ' . $e->getMessage());
  48. } finally {
  49. self::$running = false;
  50. }
  51. }
  52. }
  53. ​