actualize_script.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env php
  2. <?php
  3. // declare(strict_types=1); // Need to wait for PHP 8+ due to https://php.net/ob-implicit-flush
  4. require(__DIR__ . '/../cli/_cli.php');
  5. session_cache_limiter('');
  6. ob_implicit_flush(false);
  7. ob_start();
  8. $begin_date = date_create('now');
  9. // Set the header params ($_GET) to call the FRSS application.
  10. $_GET['c'] = 'feed';
  11. $_GET['a'] = 'actualize';
  12. $_GET['ajax'] = 1;
  13. $_GET['maxFeeds'] = PHP_INT_MAX;
  14. $_SERVER['HTTP_HOST'] = '';
  15. $app = new FreshRSS();
  16. FreshRSS_Context::initSystem();
  17. if (FreshRSS_Context::$system_conf === null) {
  18. throw new FreshRSS_Context_Exception('System configuration not initialised!');
  19. }
  20. FreshRSS_Context::$system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  21. define('SIMPLEPIE_SYSLOG_ENABLED', FreshRSS_Context::$system_conf->simplepie_syslog_enabled);
  22. /**
  23. * Writes to FreshRSS admin log, and if it is not already done by default,
  24. * writes to syslog (only if simplepie_syslog_enabled in FreshRSS configuration) and to STDOUT
  25. */
  26. function notice(string $message): void {
  27. Minz_Log::notice($message, ADMIN_LOG);
  28. if (!COPY_LOG_TO_SYSLOG && SIMPLEPIE_SYSLOG_ENABLED) {
  29. syslog(LOG_NOTICE, $message);
  30. }
  31. if (defined('STDOUT') && !COPY_SYSLOG_TO_STDERR) {
  32. fwrite(STDOUT, $message . "\n"); //Unbuffered
  33. }
  34. }
  35. // <Mutex>
  36. // Avoid having multiple actualization processes at the same time
  37. $mutexFile = TMP_PATH . '/actualize.freshrss.lock';
  38. $mutexTtl = 900; // seconds (refreshed before each new feed)
  39. if (file_exists($mutexFile) && ((time() - (@filemtime($mutexFile) ?: 0)) > $mutexTtl)) {
  40. unlink($mutexFile);
  41. }
  42. if (($handle = @fopen($mutexFile, 'x')) === false) {
  43. notice('FreshRSS feeds actualization was already running, so aborting new run at ' . $begin_date->format('c'));
  44. die();
  45. }
  46. fclose($handle);
  47. register_shutdown_function(static function () use ($mutexFile) {
  48. unlink($mutexFile);
  49. });
  50. // </Mutex>
  51. notice('FreshRSS starting feeds actualization at ' . $begin_date->format('c'));
  52. // make sure the PHP setup of the CLI environment is compatible with FreshRSS as well
  53. echo 'Failed requirements!', "\n";
  54. performRequirementCheck(FreshRSS_Context::$system_conf->db['type'] ?? '');
  55. ob_clean();
  56. echo 'Results: ', "\n"; //Buffered
  57. // Create the list of users to actualize.
  58. // Users are processed in a random order but always start with default user
  59. $users = listUsers();
  60. shuffle($users);
  61. if (FreshRSS_Context::$system_conf->default_user !== '') {
  62. array_unshift($users, FreshRSS_Context::$system_conf->default_user);
  63. $users = array_unique($users);
  64. }
  65. $limits = FreshRSS_Context::$system_conf->limits;
  66. $min_last_activity = time() - $limits['max_inactivity'];
  67. foreach ($users as $user) {
  68. FreshRSS_Context::initUser($user);
  69. if (FreshRSS_Context::$user_conf == null) {
  70. notice('Invalid user ' . $user);
  71. continue;
  72. }
  73. if (!FreshRSS_Context::$user_conf->enabled) {
  74. notice('FreshRSS skip disabled user ' . $user);
  75. continue;
  76. }
  77. if (($user !== FreshRSS_Context::$system_conf->default_user) &&
  78. (FreshRSS_UserDAO::mtime($user) < $min_last_activity)) {
  79. notice('FreshRSS skip inactive user ' . $user);
  80. continue;
  81. }
  82. FreshRSS_Auth::giveAccess();
  83. // NB: Extensions and hooks are reinitialised there
  84. $app->init();
  85. Minz_ExtensionManager::addHook('feed_before_actualize', static function (FreshRSS_Feed $feed) use ($mutexFile) {
  86. touch($mutexFile);
  87. return $feed;
  88. });
  89. notice('FreshRSS actualize ' . $user . '…');
  90. echo $user, ' '; //Buffered
  91. Minz_ExtensionManager::callHook('freshrss_user_maintenance');
  92. $app->run();
  93. if (!invalidateHttpCache()) {
  94. Minz_Log::warning('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME), ADMIN_LOG);
  95. if (defined('STDERR')) {
  96. fwrite(STDERR, 'FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME) . "\n");
  97. }
  98. }
  99. gc_collect_cycles();
  100. }
  101. $end_date = date_create('now');
  102. $duration = date_diff($end_date, $begin_date);
  103. notice('FreshRSS actualization done for ' . count($users) .
  104. ' users, using ' . format_bytes(memory_get_peak_usage(true)) . ' of memory, in ' .
  105. $duration->format('%a day(s), %h hour(s), %i minute(s) and %s seconds.'));
  106. echo 'End.', "\n";
  107. ob_end_flush();