update.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 . $data["suffix"]);
  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. $args = $data['arghelp'];
  68. printf(" --%-19s - %s\n", "$command $args", $data["description"]);
  69. }
  70. return;
  71. }
  72. define('QUIET', isset($options['quiet']));
  73. if (isset($options["log"])) {
  74. _debug("Logging to " . $options["log"]);
  75. define('LOGFILE', $options["log"]);
  76. }
  77. if (!isset($options["daemon"])) {
  78. $lock_filename = "update.lock";
  79. } else {
  80. $lock_filename = "update_daemon.lock";
  81. }
  82. if (isset($options["task"])) {
  83. _debug("Using task id " . $options["task"]);
  84. $lock_filename = $lock_filename . "-task_" . $options["task"];
  85. }
  86. $lock_handle = make_lockfile($lock_filename);
  87. $must_exit = false;
  88. // Try to lock a file in order to avoid concurrent update.
  89. if (!$lock_handle) {
  90. die("error: Can't create lockfile ($lock_filename). ".
  91. "Maybe another update process is already running.\n");
  92. }
  93. if (isset($options["force-update"])) {
  94. _debug("marking all feeds as needing update...");
  95. db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
  96. last_updated = '1970-01-01'");
  97. }
  98. if (isset($options["feeds"])) {
  99. // Update all feeds needing a update.
  100. update_daemon_common($link);
  101. // Update feedbrowser
  102. $count = update_feedbrowser_cache($link);
  103. _debug("Feedbrowser updated, $count feeds processed.");
  104. // Purge orphans and cleanup tags
  105. purge_orphans($link, true);
  106. $rc = cleanup_tags($link, 14, 50000);
  107. _debug("Cleaned $rc cached tags.");
  108. global $pluginhost;
  109. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  110. }
  111. if (isset($options["feedbrowser"])) {
  112. $count = update_feedbrowser_cache($link);
  113. print "Finished, $count feeds processed.\n";
  114. }
  115. if (isset($options["daemon"])) {
  116. while (true) {
  117. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  118. passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet");
  119. _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
  120. sleep(DAEMON_SLEEP_INTERVAL);
  121. }
  122. }
  123. if (isset($options["daemon-loop"])) {
  124. if (!make_stampfile('update_daemon.stamp')) {
  125. _debug("warning: unable to create stampfile\n");
  126. }
  127. // Call to the feed batch update function
  128. // or regenerate feedbrowser cache
  129. if (rand(0,100) > 30) {
  130. update_daemon_common($link);
  131. } else {
  132. $count = update_feedbrowser_cache($link);
  133. _debug("Feedbrowser updated, $count feeds processed.");
  134. purge_orphans($link, true);
  135. $rc = cleanup_tags($link, 14, 50000);
  136. _debug("Cleaned $rc cached tags.");
  137. global $pluginhost;
  138. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  139. }
  140. }
  141. if (isset($options["cleanup-tags"])) {
  142. $rc = cleanup_tags($link, 14, 50000);
  143. _debug("$rc tags deleted.\n");
  144. }
  145. if (isset($options["indexes"])) {
  146. _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
  147. _debug("Type 'yes' to continue.");
  148. if (read_stdin() != 'yes')
  149. exit;
  150. _debug("clearing existing indexes...");
  151. if (DB_TYPE == "pgsql") {
  152. $result = db_query($link, "SELECT relname FROM
  153. pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
  154. AND relname NOT LIKE '%_pkey'
  155. AND relkind = 'i'");
  156. } else {
  157. $result = db_query($link, "SELECT index_name,table_name FROM
  158. information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
  159. }
  160. while ($line = db_fetch_assoc($result)) {
  161. if (DB_TYPE == "pgsql") {
  162. $statement = "DROP INDEX " . $line["relname"];
  163. _debug($statement);
  164. } else {
  165. $statement = "ALTER TABLE ".
  166. $line['table_name']." DROP INDEX ".$line['index_name'];
  167. _debug($statement);
  168. }
  169. db_query($link, $statement, false);
  170. }
  171. _debug("reading indexes from schema for: " . DB_TYPE);
  172. $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
  173. if ($fp) {
  174. while ($line = fgets($fp)) {
  175. $matches = array();
  176. if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
  177. $index = $matches[1];
  178. $table = $matches[2];
  179. $statement = "CREATE INDEX $index ON $table";
  180. _debug($statement);
  181. db_query($link, $statement);
  182. }
  183. }
  184. fclose($fp);
  185. } else {
  186. _debug("unable to open schema file.");
  187. }
  188. _debug("all done.");
  189. }
  190. if (isset($options["convert-filters"])) {
  191. _debug("WARNING: this will remove all existing type2 filters.");
  192. _debug("Type 'yes' to continue.");
  193. if (read_stdin() != 'yes')
  194. exit;
  195. _debug("converting filters...");
  196. db_query($link, "DELETE FROM ttrss_filters2");
  197. $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
  198. while ($line = db_fetch_assoc($result)) {
  199. $owner_uid = $line["owner_uid"];
  200. // date filters are removed
  201. if ($line["filter_type"] != 5) {
  202. $filter = array();
  203. if (sql_bool_to_bool($line["cat_filter"])) {
  204. $feed_id = "CAT:" . (int)$line["cat_id"];
  205. } else {
  206. $feed_id = (int)$line["feed_id"];
  207. }
  208. $filter["enabled"] = $line["enabled"] ? "on" : "off";
  209. $filter["rule"] = array(
  210. json_encode(array(
  211. "reg_exp" => $line["reg_exp"],
  212. "feed_id" => $feed_id,
  213. "filter_type" => $line["filter_type"])));
  214. $filter["action"] = array(
  215. json_encode(array(
  216. "action_id" => $line["action_id"],
  217. "action_param_label" => $line["action_param"],
  218. "action_param" => $line["action_param"])));
  219. // Oh god it's full of hacks
  220. $_REQUEST = $filter;
  221. $_SESSION["uid"] = $owner_uid;
  222. $filters = new Pref_Filters($link, $_REQUEST);
  223. $filters->add();
  224. }
  225. }
  226. }
  227. if (isset($options["list-plugins"])) {
  228. $tmppluginhost = new PluginHost($link);
  229. $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
  230. $enabled = array_map("trim", explode(",", PLUGINS));
  231. echo "List of all available plugins:\n";
  232. foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
  233. $about = $plugin->about();
  234. $status = $about[3] ? "system" : "user";
  235. if (in_array($name, $enabled)) $name .= "*";
  236. printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
  237. $name, $status, $about[0], $about[2], $about[1]);
  238. }
  239. echo "Plugins marked by * are currently enabled for all users.\n";
  240. }
  241. $pluginhost->run_commands($options);
  242. db_close($link);
  243. if ($lock_handle != false) {
  244. fclose($lock_handle);
  245. }
  246. if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
  247. unlink(LOCK_DIRECTORY . "/$lock_filename");
  248. ?>