update.php 9.4 KB

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