update.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env php
  2. <?php
  3. set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
  4. get_include_path());
  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. // Create a database connection.
  16. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  17. init_connection($link);
  18. $op = $argv;
  19. if (count($argv) == 0 && !defined('STDIN')) {
  20. ?> <html>
  21. <head>
  22. <title>Tiny Tiny RSS data update script.</title>
  23. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  24. <link rel="stylesheet" type="text/css" href="utility.css">
  25. </head>
  26. <body>
  27. <div class="floatingLogo"><img src="images/logo_wide.png"></div>
  28. <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
  29. <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
  30. </body></html>
  31. <?php
  32. exit;
  33. }
  34. if (count($argv) == 1 || in_array("-help", $op) ) {
  35. print "Tiny Tiny RSS data update script.\n\n";
  36. print "Options:\n";
  37. print " -feeds - update feeds\n";
  38. print " -feedbrowser - update feedbrowser\n";
  39. print " -daemon - start single-process update daemon\n";
  40. print " -cleanup-tags - perform tags table maintenance\n";
  41. print " -quiet - don't show messages\n";
  42. print " -indexes - recreate missing schema indexes\n";
  43. print " -convert-filters - convert type1 filters to type2\n";
  44. print " -force-update - force update of all feeds\n";
  45. print " -list-plugins - list all available plugins\n";
  46. print " -help - show this help\n";
  47. print "Plugin options:\n";
  48. foreach ($pluginhost->get_commands() as $command => $data) {
  49. printf(" %-19s - %s\n", "$command", $data["description"]);
  50. }
  51. return;
  52. }
  53. define('QUIET', in_array("-quiet", $op));
  54. if (!in_array("-daemon", $op)) {
  55. $lock_filename = "update.lock";
  56. } else {
  57. $lock_filename = "update_daemon.lock";
  58. }
  59. $lock_handle = make_lockfile($lock_filename);
  60. $must_exit = false;
  61. // Try to lock a file in order to avoid concurrent update.
  62. if (!$lock_handle) {
  63. die("error: Can't create lockfile ($lock_filename). ".
  64. "Maybe another update process is already running.\n");
  65. }
  66. if (in_array("-feeds", $op)) {
  67. // Update all feeds needing a update.
  68. update_daemon_common($link);
  69. // Update feedbrowser
  70. $count = update_feedbrowser_cache($link);
  71. _debug("Feedbrowser updated, $count feeds processed.");
  72. // Purge orphans and cleanup tags
  73. purge_orphans($link, true);
  74. $rc = cleanup_tags($link, 14, 50000);
  75. _debug("Cleaned $rc cached tags.");
  76. global $pluginhost;
  77. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  78. }
  79. if (in_array("-feedbrowser", $op)) {
  80. $count = update_feedbrowser_cache($link);
  81. print "Finished, $count feeds processed.\n";
  82. }
  83. if (in_array("-daemon", $op)) {
  84. $op = array_diff($op, array("-daemon"));
  85. while (true) {
  86. passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
  87. _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
  88. sleep(DAEMON_SLEEP_INTERVAL);
  89. }
  90. }
  91. if (in_array("-daemon-loop", $op)) {
  92. if (!make_stampfile('update_daemon.stamp')) {
  93. die("error: unable to create stampfile\n");
  94. }
  95. // Call to the feed batch update function
  96. // or regenerate feedbrowser cache
  97. if (rand(0,100) > 30) {
  98. update_daemon_common($link);
  99. } else {
  100. $count = update_feedbrowser_cache($link);
  101. _debug("Feedbrowser updated, $count feeds processed.");
  102. purge_orphans($link, true);
  103. $rc = cleanup_tags($link, 14, 50000);
  104. _debug("Cleaned $rc cached tags.");
  105. global $pluginhost;
  106. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  107. }
  108. }
  109. if (in_array("-cleanup-tags", $op)) {
  110. $rc = cleanup_tags($link, 14, 50000);
  111. _debug("$rc tags deleted.\n");
  112. }
  113. if (in_array("-indexes", $op)) {
  114. _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
  115. _debug("Type 'yes' to continue.");
  116. if (read_stdin() != 'yes')
  117. exit;
  118. _debug("clearing existing indexes...");
  119. if (DB_TYPE == "pgsql") {
  120. $result = db_query($link, "SELECT relname FROM
  121. pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
  122. AND relname NOT LIKE '%_pkey'
  123. AND relkind = 'i'");
  124. } else {
  125. $result = db_query($link, "SELECT index_name,table_name FROM
  126. information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
  127. }
  128. while ($line = db_fetch_assoc($result)) {
  129. if (DB_TYPE == "pgsql") {
  130. $statement = "DROP INDEX " . $line["relname"];
  131. _debug($statement);
  132. } else {
  133. $statement = "ALTER TABLE ".
  134. $line['table_name']." DROP INDEX ".$line['index_name'];
  135. _debug($statement);
  136. }
  137. db_query($link, $statement, false);
  138. }
  139. _debug("reading indexes from schema for: " . DB_TYPE);
  140. $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
  141. if ($fp) {
  142. while ($line = fgets($fp)) {
  143. $matches = array();
  144. if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
  145. $index = $matches[1];
  146. $table = $matches[2];
  147. $statement = "CREATE INDEX $index ON $table";
  148. _debug($statement);
  149. db_query($link, $statement);
  150. }
  151. }
  152. fclose($fp);
  153. } else {
  154. _debug("unable to open schema file.");
  155. }
  156. _debug("all done.");
  157. }
  158. if (in_array("-convert-filters", $op)) {
  159. _debug("WARNING: this will remove all existing type2 filters.");
  160. _debug("Type 'yes' to continue.");
  161. if (read_stdin() != 'yes')
  162. exit;
  163. _debug("converting filters...");
  164. db_query($link, "DELETE FROM ttrss_filters2");
  165. $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
  166. while ($line = db_fetch_assoc($result)) {
  167. $owner_uid = $line["owner_uid"];
  168. // date filters are removed
  169. if ($line["filter_type"] != 5) {
  170. $filter = array();
  171. if (sql_bool_to_bool($line["cat_filter"])) {
  172. $feed_id = "CAT:" . (int)$line["cat_id"];
  173. } else {
  174. $feed_id = (int)$line["feed_id"];
  175. }
  176. $filter["enabled"] = $line["enabled"] ? "on" : "off";
  177. $filter["rule"] = array(
  178. json_encode(array(
  179. "reg_exp" => $line["reg_exp"],
  180. "feed_id" => $feed_id,
  181. "filter_type" => $line["filter_type"])));
  182. $filter["action"] = array(
  183. json_encode(array(
  184. "action_id" => $line["action_id"],
  185. "action_param_label" => $line["action_param"],
  186. "action_param" => $line["action_param"])));
  187. // Oh god it's full of hacks
  188. $_REQUEST = $filter;
  189. $_SESSION["uid"] = $owner_uid;
  190. $filters = new Pref_Filters($link, $_REQUEST);
  191. $filters->add();
  192. }
  193. }
  194. }
  195. if (in_array("-force-update", $op)) {
  196. _debug("marking all feeds as needing update...");
  197. db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
  198. last_updated = '1970-01-01'");
  199. }
  200. if (in_array("-list-plugins", $op)) {
  201. $tmppluginhost = new PluginHost($link);
  202. $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
  203. $enabled = array_map("trim", explode(",", PLUGINS));
  204. echo "List of all available plugins:\n";
  205. foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
  206. $about = $plugin->about();
  207. $status = $about[3] ? "system" : "user";
  208. if (in_array($name, $enabled)) $name .= "*";
  209. printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
  210. $name, $status, $about[0], $about[2], $about[1]);
  211. }
  212. echo "Plugins marked by * are currently enabled for all users.\n";
  213. }
  214. $pluginhost->run_commands($op);
  215. db_close($link);
  216. if ($lock_handle != false) {
  217. fclose($lock_handle);
  218. }
  219. if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
  220. unlink(LOCK_DIRECTORY . "/$lock_filename");
  221. ?>