update.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/php
  2. <?php
  3. define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
  4. define('DISABLE_SESSIONS', true);
  5. if (!defined('PHP_EXECUTABLE'))
  6. define('PHP_EXECUTABLE', '/usr/bin/php');
  7. error_reporting(DEFAULT_ERROR_LEVEL);
  8. require_once "sanity_check.php";
  9. require_once "config.php";
  10. require_once "db.php";
  11. require_once "db-prefs.php";
  12. require_once "functions.php";
  13. $op = $argv[1];
  14. if (!$op || $op == "-help") {
  15. print "Tiny Tiny RSS data update script.\n\n";
  16. print "Options:\n";
  17. print " -feeds - update feeds\n";
  18. print " -feedbrowser - update feedbrowser\n";
  19. print " -daemon - start single-process update daemon\n";
  20. print " -help - show this help\n";
  21. return;
  22. }
  23. if ($op != "-daemon") {
  24. $lock_filename = "update.lock";
  25. } else {
  26. $lock_filename = "update_daemon.lock";
  27. }
  28. $lock_handle = make_lockfile($lock_filename);
  29. $must_exit = false;
  30. // Try to lock a file in order to avoid concurrent update.
  31. if (!$lock_handle) {
  32. die("error: Can't create lockfile ($lock_filename). ".
  33. "Maybe another update process is already running.\n");
  34. }
  35. // Create a database connection.
  36. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  37. if (!$link) {
  38. if (DB_TYPE == "mysql") {
  39. print mysql_error();
  40. }
  41. // PG seems to display its own errors just fine by default.
  42. return;
  43. }
  44. init_connection($link);
  45. if ($op == "-feeds") {
  46. // Update all feeds needing a update.
  47. update_daemon_common($link);
  48. }
  49. if ($op == "-feedbrowser") {
  50. $count = update_feedbrowser_cache($link);
  51. print "Finished, $count feeds processed.\n";
  52. }
  53. if ($op == "-daemon") {
  54. if (!ENABLE_UPDATE_DAEMON)
  55. die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
  56. while (true) {
  57. passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
  58. _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
  59. sleep(DAEMON_SLEEP_INTERVAL);
  60. }
  61. }
  62. if ($op == "-daemon-loop") {
  63. if (!make_stampfile('update_daemon.stamp')) {
  64. die("error: unable to create stampfile\n");
  65. }
  66. // Call to the feed batch update function
  67. // or regenerate feedbrowser cache
  68. if (rand(0,100) > 30) {
  69. update_daemon_common($link);
  70. } else {
  71. $count = update_feedbrowser_cache($link);
  72. _debug("Finished, $count feeds processed.");
  73. }
  74. }
  75. db_close($link);
  76. unlink(LOCK_DIRECTORY . "/$lock_filename");
  77. ?>