update.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/php
  2. <?php
  3. set_include_path(get_include_path() . PATH_SEPARATOR .
  4. dirname(__FILE__) . "/include");
  5. define('DISABLE_SESSIONS', true);
  6. chdir(dirname(__FILE__));
  7. require_once "functions.php";
  8. require_once "rssfuncs.php";
  9. require_once "sanity_check.php";
  10. require_once "config.php";
  11. require_once "db.php";
  12. require_once "db-prefs.php";
  13. if (!defined('PHP_EXECUTABLE'))
  14. define('PHP_EXECUTABLE', '/usr/bin/php');
  15. $op = $argv[1];
  16. if (!$op || $op == "-help") {
  17. print "Tiny Tiny RSS data update script.\n\n";
  18. print "Options:\n";
  19. print " -feeds - update feeds\n";
  20. print " -feedbrowser - update feedbrowser\n";
  21. print " -daemon - start single-process update daemon\n";
  22. print " -cleanup-tags - perform tags table maintenance\n";
  23. print " -get-feeds - receive popular feeds from linked instances\n";
  24. print " -help - show this help\n";
  25. return;
  26. }
  27. if ($op != "-daemon") {
  28. $lock_filename = "update.lock";
  29. } else {
  30. $lock_filename = "update_daemon.lock";
  31. }
  32. $lock_handle = make_lockfile($lock_filename);
  33. $must_exit = false;
  34. // Try to lock a file in order to avoid concurrent update.
  35. if (!$lock_handle) {
  36. die("error: Can't create lockfile ($lock_filename). ".
  37. "Maybe another update process is already running.\n");
  38. }
  39. // Create a database connection.
  40. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  41. init_connection($link);
  42. if ($op == "-feeds") {
  43. // Update all feeds needing a update.
  44. update_daemon_common($link);
  45. // Update feedbrowser
  46. $count = update_feedbrowser_cache($link);
  47. _debug("Feedbrowser updated, $count feeds processed.");
  48. // Purge orphans and cleanup tags
  49. purge_orphans($link, true);
  50. $rc = cleanup_tags($link, 14, 50000);
  51. _debug("Cleaned $rc cached tags.");
  52. get_linked_feeds($link);
  53. }
  54. if ($op == "-feedbrowser") {
  55. $count = update_feedbrowser_cache($link);
  56. print "Finished, $count feeds processed.\n";
  57. }
  58. if ($op == "-daemon") {
  59. while (true) {
  60. passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
  61. _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
  62. sleep(DAEMON_SLEEP_INTERVAL);
  63. }
  64. }
  65. if ($op == "-daemon-loop") {
  66. if (!make_stampfile('update_daemon.stamp')) {
  67. die("error: unable to create stampfile\n");
  68. }
  69. // Call to the feed batch update function
  70. // or regenerate feedbrowser cache
  71. if (rand(0,100) > 30) {
  72. update_daemon_common($link);
  73. } else {
  74. $count = update_feedbrowser_cache($link);
  75. _debug("Feedbrowser updated, $count feeds processed.");
  76. purge_orphans($link, true);
  77. $rc = cleanup_tags($link, 14, 50000);
  78. _debug("Cleaned $rc cached tags.");
  79. get_linked_feeds($link);
  80. }
  81. }
  82. if ($op == "-cleanup-tags") {
  83. $rc = cleanup_tags($link, 14, 50000);
  84. print "$rc tags deleted.\n";
  85. }
  86. if ($op == "-get-feeds") {
  87. get_linked_feeds($link);
  88. }
  89. db_close($link);
  90. if ($lock_handle != false) {
  91. fclose($lock_handle);
  92. }
  93. unlink(LOCK_DIRECTORY . "/$lock_filename");
  94. ?>