update.php 8.4 KB

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