update.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 "autoload.php";
  8. require_once "functions.php";
  9. require_once "config.php";
  10. require_once "sanity_check.php";
  11. require_once "db.php";
  12. require_once "db-prefs.php";
  13. function cleanup_tags($days = 14, $limit = 1000) {
  14. $days = (int) $days;
  15. if (DB_TYPE == "pgsql") {
  16. $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
  17. } else if (DB_TYPE == "mysql") {
  18. $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
  19. }
  20. $tags_deleted = 0;
  21. $pdo = Db::pdo();
  22. while ($limit > 0) {
  23. $limit_part = 500;
  24. $sth = $pdo->prepare("SELECT ttrss_tags.id AS id
  25. FROM ttrss_tags, ttrss_user_entries, ttrss_entries
  26. WHERE post_int_id = int_id AND $interval_query AND
  27. ref_id = ttrss_entries.id AND tag_cache != '' LIMIT ?");
  28. $sth->bindValue(1, $limit_part, PDO::PARAM_INT);
  29. $sth->execute();
  30. $ids = array();
  31. while ($line = $sth->fetch()) {
  32. array_push($ids, $line['id']);
  33. }
  34. if (count($ids) > 0) {
  35. $ids = join(",", $ids);
  36. $usth = $pdo->query("DELETE FROM ttrss_tags WHERE id IN ($ids)");
  37. $tags_deleted = $usth->rowCount();
  38. } else {
  39. break;
  40. }
  41. $limit -= $limit_part;
  42. }
  43. return $tags_deleted;
  44. }
  45. if (!defined('PHP_EXECUTABLE'))
  46. define('PHP_EXECUTABLE', '/usr/bin/php');
  47. $pdo = Db::pdo();
  48. init_plugins();
  49. $longopts = array("feeds",
  50. "daemon",
  51. "daemon-loop",
  52. "send-digests",
  53. "task:",
  54. "cleanup-tags",
  55. "quiet",
  56. "log:",
  57. "log-level:",
  58. "indexes",
  59. "pidlock:",
  60. "update-schema",
  61. "convert-filters",
  62. "force-update",
  63. "gen-search-idx",
  64. "list-plugins",
  65. "debug-feed:",
  66. "force-refetch",
  67. "force-rehash",
  68. "opml-export:",
  69. "help");
  70. foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
  71. array_push($longopts, $command . $data["suffix"]);
  72. }
  73. $options = getopt("", $longopts);
  74. if (!is_array($options)) {
  75. die("error: getopt() failed. ".
  76. "Most probably you are using PHP CGI to run this script ".
  77. "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
  78. "additional information.\n");
  79. }
  80. if (count($options) == 0 && !defined('STDIN')) {
  81. ?>
  82. <!DOCTYPE html>
  83. <html>
  84. <head>
  85. <title>Tiny Tiny RSS data update script.</title>
  86. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  87. </head>
  88. <body>
  89. <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
  90. <?php print_error("Please run this script from the command line. Use option \"--help\" to display command help if this error is displayed erroneously."); ?>
  91. </body></html>
  92. <?php
  93. exit;
  94. }
  95. if (count($options) == 0 || isset($options["help"]) ) {
  96. print "Tiny Tiny RSS data update script.\n\n";
  97. print "Options:\n";
  98. print " --feeds - update feeds\n";
  99. print " --daemon - start single-process update daemon\n";
  100. print " --task N - create lockfile using this task id\n";
  101. print " --cleanup-tags - perform tags table maintenance\n";
  102. print " --quiet - don't output messages to stdout\n";
  103. print " --log FILE - log messages to FILE\n";
  104. print " --log-level N - log verbosity level\n";
  105. print " --indexes - recreate missing schema indexes\n";
  106. print " --update-schema - update database schema\n";
  107. print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
  108. print " --convert-filters - convert type1 filters to type2\n";
  109. print " --send-digests - send pending email digests\n";
  110. print " --force-update - force update of all feeds\n";
  111. print " --list-plugins - list all available plugins\n";
  112. print " --debug-feed N - perform debug update of feed N\n";
  113. print " --force-refetch - debug update: force refetch feed data\n";
  114. print " --force-rehash - debug update: force rehash articles\n";
  115. print " --opml-export \"USER FILE\" - export feeds of selected user to OPML\n";
  116. print " --help - show this help\n";
  117. print "Plugin options:\n";
  118. foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
  119. $args = $data['arghelp'];
  120. printf(" --%-26s - %s\n", "$command $args", $data["description"]);
  121. }
  122. return;
  123. }
  124. if (!isset($options['daemon'])) {
  125. require_once "errorhandler.php";
  126. }
  127. if (!isset($options['update-schema'])) {
  128. $schema_version = get_schema_version();
  129. if ($schema_version != SCHEMA_VERSION) {
  130. die("Schema version is wrong, please upgrade the database (--update-schema).\n");
  131. }
  132. }
  133. Debug::set_enabled(true);
  134. if (isset($options["log-level"])) {
  135. Debug::set_loglevel((int)$options["log-level"]);
  136. }
  137. if (isset($options["log"])) {
  138. Debug::set_quiet(isset($options['quiet']));
  139. Debug::set_logfile($options["log"]);
  140. Debug::log("Logging to " . $options["log"]);
  141. } else {
  142. if (isset($options['quiet'])) {
  143. Debug::set_loglevel(Debug::$LOG_DISABLED);
  144. }
  145. }
  146. if (!isset($options["daemon"])) {
  147. $lock_filename = "update.lock";
  148. } else {
  149. $lock_filename = "update_daemon.lock";
  150. }
  151. if (isset($options["task"])) {
  152. Debug::log("Using task id " . $options["task"]);
  153. $lock_filename = $lock_filename . "-task_" . $options["task"];
  154. }
  155. if (isset($options["pidlock"])) {
  156. $my_pid = $options["pidlock"];
  157. $lock_filename = "update_daemon-$my_pid.lock";
  158. }
  159. Debug::log("Lock: $lock_filename");
  160. $lock_handle = make_lockfile($lock_filename);
  161. $must_exit = false;
  162. if (isset($options["task"]) && isset($options["pidlock"])) {
  163. $waits = $options["task"] * 5;
  164. Debug::log("Waiting before update ($waits)");
  165. sleep($waits);
  166. }
  167. // Try to lock a file in order to avoid concurrent update.
  168. if (!$lock_handle) {
  169. die("error: Can't create lockfile ($lock_filename). ".
  170. "Maybe another update process is already running.\n");
  171. }
  172. if (isset($options["force-update"])) {
  173. Debug::log("marking all feeds as needing update...");
  174. $pdo->query( "UPDATE ttrss_feeds SET
  175. last_update_started = '1970-01-01', last_updated = '1970-01-01'");
  176. }
  177. if (isset($options["feeds"])) {
  178. RSSUtils::update_daemon_common();
  179. RSSUtils::housekeeping_common(true);
  180. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  181. }
  182. if (isset($options["daemon"])) {
  183. while (true) {
  184. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  185. $log = isset($options['log']) ? '--log '.$options['log'] : '';
  186. $log_level = isset($options['log-level']) ? '--log-level '.$options['log-level'] : '';
  187. passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log $log_level");
  188. // let's enforce a minimum spawn interval as to not forkbomb the host
  189. $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL);
  190. Debug::log("Sleeping for $spawn_interval seconds...");
  191. sleep($spawn_interval);
  192. }
  193. }
  194. if (isset($options["daemon-loop"])) {
  195. if (!make_stampfile('update_daemon.stamp')) {
  196. Debug::log("warning: unable to create stampfile\n");
  197. }
  198. RSSUtils::update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT);
  199. if (!isset($options["pidlock"]) || $options["task"] == 0)
  200. RSSUtils::housekeeping_common(true);
  201. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  202. }
  203. if (isset($options["cleanup-tags"])) {
  204. $rc = cleanup_tags( 14, 50000);
  205. Debug::log("$rc tags deleted.\n");
  206. }
  207. if (isset($options["indexes"])) {
  208. Debug::log("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
  209. Debug::log("Type 'yes' to continue.");
  210. if (read_stdin() != 'yes')
  211. exit;
  212. Debug::log("clearing existing indexes...");
  213. if (DB_TYPE == "pgsql") {
  214. $sth = $pdo->query( "SELECT relname FROM
  215. pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
  216. AND relname NOT LIKE '%_pkey'
  217. AND relkind = 'i'");
  218. } else {
  219. $sth = $pdo->query( "SELECT index_name,table_name FROM
  220. information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
  221. }
  222. while ($line = $sth->fetch()) {
  223. if (DB_TYPE == "pgsql") {
  224. $statement = "DROP INDEX " . $line["relname"];
  225. Debug::log($statement);
  226. } else {
  227. $statement = "ALTER TABLE ".
  228. $line['table_name']." DROP INDEX ".$line['index_name'];
  229. Debug::log($statement);
  230. }
  231. $pdo->query($statement);
  232. }
  233. Debug::log("reading indexes from schema for: " . DB_TYPE);
  234. $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
  235. if ($fp) {
  236. while ($line = fgets($fp)) {
  237. $matches = array();
  238. if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
  239. $index = $matches[1];
  240. $table = $matches[2];
  241. $statement = "CREATE INDEX $index ON $table";
  242. Debug::log($statement);
  243. $pdo->query($statement);
  244. }
  245. }
  246. fclose($fp);
  247. } else {
  248. Debug::log("unable to open schema file.");
  249. }
  250. Debug::log("all done.");
  251. }
  252. if (isset($options["convert-filters"])) {
  253. Debug::log("WARNING: this will remove all existing type2 filters.");
  254. Debug::log("Type 'yes' to continue.");
  255. if (read_stdin() != 'yes')
  256. exit;
  257. Debug::log("converting filters...");
  258. $pdo->query("DELETE FROM ttrss_filters2");
  259. $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id");
  260. while ($line = $res->fetch()) {
  261. $owner_uid = $line["owner_uid"];
  262. // date filters are removed
  263. if ($line["filter_type"] != 5) {
  264. $filter = array();
  265. if (sql_bool_to_bool($line["cat_filter"])) {
  266. $feed_id = "CAT:" . (int)$line["cat_id"];
  267. } else {
  268. $feed_id = (int)$line["feed_id"];
  269. }
  270. $filter["enabled"] = $line["enabled"] ? "on" : "off";
  271. $filter["rule"] = array(
  272. json_encode(array(
  273. "reg_exp" => $line["reg_exp"],
  274. "feed_id" => $feed_id,
  275. "filter_type" => $line["filter_type"])));
  276. $filter["action"] = array(
  277. json_encode(array(
  278. "action_id" => $line["action_id"],
  279. "action_param_label" => $line["action_param"],
  280. "action_param" => $line["action_param"])));
  281. // Oh god it's full of hacks
  282. $_REQUEST = $filter;
  283. $_SESSION["uid"] = $owner_uid;
  284. $filters = new Pref_Filters($_REQUEST);
  285. $filters->add();
  286. }
  287. }
  288. }
  289. if (isset($options["update-schema"])) {
  290. Debug::log("Checking for updates (" . DB_TYPE . ")...");
  291. $updater = new DbUpdater(Db::pdo(), DB_TYPE, SCHEMA_VERSION);
  292. if ($updater->isUpdateRequired()) {
  293. Debug::log("Schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
  294. if (DB_TYPE == "mysql")
  295. Debug::Log("READ THIS: Due to MySQL limitations, your database is not completely protected while updating.\n".
  296. "Errors may put it in an inconsistent state requiring manual rollback.\nBACKUP YOUR DATABASE BEFORE CONTINUING.");
  297. else
  298. Debug::log("WARNING: please backup your database before continuing.");
  299. Debug::log("Type 'yes' to continue.");
  300. if (read_stdin() != 'yes')
  301. exit;
  302. Debug::log("Performing updates to version " . SCHEMA_VERSION);
  303. for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
  304. Debug::log("* Updating to version $i...");
  305. $result = $updater->performUpdateTo($i, false);
  306. if ($result) {
  307. Debug::log("* Completed.");
  308. } else {
  309. Debug::log("One of the updates failed. Either retry the process or perform updates manually.");
  310. return;
  311. }
  312. }
  313. } else {
  314. Debug::log("Update not required.");
  315. }
  316. }
  317. if (isset($options["gen-search-idx"])) {
  318. echo "Generating search index (stemming set to English)...\n";
  319. $res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
  320. $row = $res->fetch();
  321. $count = $row['count'];
  322. print "Articles to process: $count.\n";
  323. $limit = 500;
  324. $processed = 0;
  325. $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE
  326. tsvector_combined IS NULL ORDER BY id LIMIT ?");
  327. $sth->execute([$limit]);
  328. $usth = $pdo->prepare("UPDATE ttrss_entries
  329. SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
  330. while (true) {
  331. while ($line = $sth->fetch()) {
  332. $tsvector_combined = mb_substr(strip_tags($line["title"] . " " . $line["content"]), 0, 1000000);
  333. $usth->execute([$tsvector_combined, $line['id']]);
  334. $processed++;
  335. }
  336. print "Processed $processed articles...\n";
  337. if ($processed < $limit) {
  338. echo "All done.\n";
  339. break;
  340. }
  341. }
  342. }
  343. if (isset($options["list-plugins"])) {
  344. $tmppluginhost = new PluginHost();
  345. $tmppluginhost->load_all($tmppluginhost::KIND_ALL, false);
  346. $enabled = array_map("trim", explode(",", PLUGINS));
  347. echo "List of all available plugins:\n";
  348. foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
  349. $about = $plugin->about();
  350. $status = $about[3] ? "system" : "user";
  351. if (in_array($name, $enabled)) $name .= "*";
  352. printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
  353. $name, $status, $about[0], $about[2], $about[1]);
  354. }
  355. echo "Plugins marked by * are currently enabled for all users.\n";
  356. }
  357. if (isset($options["debug-feed"])) {
  358. $feed = $options["debug-feed"];
  359. if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
  360. if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
  361. Debug::set_loglevel(Debug::$LOG_EXTENDED);
  362. $rc = RSSUtils::update_rss_feed($feed) != false ? 0 : 1;
  363. exit($rc);
  364. }
  365. if (isset($options["send-digests"])) {
  366. Digest::send_headlines_digests();
  367. }
  368. if (isset($options["opml-export"])) {
  369. list ($user, $filename) = explode(" ", $options["opml-export"], 2);
  370. Debug::log("Exporting feeds of user $user to $filename as OPML...");
  371. $sth = $pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
  372. $sth->execute([$user]);
  373. if ($res = $sth->fetch()) {
  374. $opml = new OPML("");
  375. $rc = $opml->opml_export($filename, $res["id"], false, true, true);
  376. Debug::log($rc ? "Success." : "Failed.");
  377. } else {
  378. Debug::log("User not found: $user");
  379. }
  380. }
  381. PluginHost::getInstance()->run_commands($options);
  382. if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
  383. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  384. fclose($lock_handle);
  385. unlink(LOCK_DIRECTORY . "/$lock_filename");
  386. ?>