update.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 "rssfuncs.php";
  10. require_once "config.php";
  11. require_once "sanity_check.php";
  12. require_once "db.php";
  13. require_once "db-prefs.php";
  14. if (!defined('PHP_EXECUTABLE'))
  15. define('PHP_EXECUTABLE', '/usr/bin/php');
  16. init_plugins();
  17. $longopts = array("feeds",
  18. "feedbrowser",
  19. "daemon",
  20. "daemon-loop",
  21. "task:",
  22. "cleanup-tags",
  23. "quiet",
  24. "log:",
  25. "indexes",
  26. "pidlock:",
  27. "update-schema",
  28. "convert-filters",
  29. "force-update",
  30. "gen-search-idx",
  31. "list-plugins",
  32. "debug-feed:",
  33. "force-refetch",
  34. "force-rehash",
  35. "decrypt-feeds",
  36. "help");
  37. foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
  38. array_push($longopts, $command . $data["suffix"]);
  39. }
  40. $options = getopt("", $longopts);
  41. if (!is_array($options)) {
  42. die("error: getopt() failed. ".
  43. "Most probably you are using PHP CGI to run this script ".
  44. "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
  45. "additional information.\n");
  46. }
  47. if (count($options) == 0 && !defined('STDIN')) {
  48. ?> <html>
  49. <head>
  50. <title>Tiny Tiny RSS data update script.</title>
  51. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  52. <link rel="stylesheet" type="text/css" href="css/utility.css">
  53. </head>
  54. <body>
  55. <div class="floatingLogo"><img src="images/logo_small.png"></div>
  56. <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
  57. <?php print_error("Please run this script from the command line. Use option \"--help\" to display command help if this error is displayed erroneously."); ?>
  58. </body></html>
  59. <?php
  60. exit;
  61. }
  62. if (count($options) == 0 || isset($options["help"]) ) {
  63. print "Tiny Tiny RSS data update script.\n\n";
  64. print "Options:\n";
  65. print " --feeds - update feeds\n";
  66. print " --feedbrowser - update feedbrowser\n";
  67. print " --daemon - start single-process update daemon\n";
  68. print " --task N - create lockfile using this task id\n";
  69. print " --cleanup-tags - perform tags table maintenance\n";
  70. print " --quiet - don't output messages to stdout\n";
  71. print " --log FILE - log messages to FILE\n";
  72. print " --indexes - recreate missing schema indexes\n";
  73. print " --update-schema - update database schema\n";
  74. print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
  75. print " --convert-filters - convert type1 filters to type2\n";
  76. print " --force-update - force update of all feeds\n";
  77. print " --list-plugins - list all available plugins\n";
  78. print " --debug-feed N - perform debug update of feed N\n";
  79. print " --force-refetch - debug update: force refetch feed data\n";
  80. print " --force-rehash - debug update: force rehash articles\n";
  81. print " --decrypt-feeds - decrypt feed passwords\n";
  82. print " --help - show this help\n";
  83. print "Plugin options:\n";
  84. foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
  85. $args = $data['arghelp'];
  86. printf(" --%-19s - %s\n", "$command $args", $data["description"]);
  87. }
  88. return;
  89. }
  90. if (!isset($options['daemon'])) {
  91. require_once "errorhandler.php";
  92. }
  93. if (!isset($options['update-schema'])) {
  94. $schema_version = get_schema_version();
  95. if ($schema_version != SCHEMA_VERSION) {
  96. die("Schema version is wrong, please upgrade the database.\n");
  97. }
  98. }
  99. define('QUIET', isset($options['quiet']));
  100. if (isset($options["log"])) {
  101. _debug("Logging to " . $options["log"]);
  102. define('LOGFILE', $options["log"]);
  103. }
  104. if (!isset($options["daemon"])) {
  105. $lock_filename = "update.lock";
  106. } else {
  107. $lock_filename = "update_daemon.lock";
  108. }
  109. if (isset($options["task"])) {
  110. _debug("Using task id " . $options["task"]);
  111. $lock_filename = $lock_filename . "-task_" . $options["task"];
  112. }
  113. if (isset($options["pidlock"])) {
  114. $my_pid = $options["pidlock"];
  115. $lock_filename = "update_daemon-$my_pid.lock";
  116. }
  117. _debug("Lock: $lock_filename");
  118. $lock_handle = make_lockfile($lock_filename);
  119. $must_exit = false;
  120. if (isset($options["task"]) && isset($options["pidlock"])) {
  121. $waits = $options["task"] * 5;
  122. _debug("Waiting before update ($waits)");
  123. sleep($waits);
  124. }
  125. // Try to lock a file in order to avoid concurrent update.
  126. if (!$lock_handle) {
  127. die("error: Can't create lockfile ($lock_filename). ".
  128. "Maybe another update process is already running.\n");
  129. }
  130. if (isset($options["force-update"])) {
  131. _debug("marking all feeds as needing update...");
  132. db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
  133. last_updated = '1970-01-01'");
  134. }
  135. if (isset($options["feeds"])) {
  136. update_daemon_common();
  137. housekeeping_common(true);
  138. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  139. }
  140. if (isset($options["feedbrowser"])) {
  141. $count = update_feedbrowser_cache();
  142. print "Finished, $count feeds processed.\n";
  143. }
  144. if (isset($options["daemon"])) {
  145. while (true) {
  146. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  147. $log = isset($options['log']) ? '--log '.$options['log'] : '';
  148. passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log");
  149. _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
  150. sleep(DAEMON_SLEEP_INTERVAL);
  151. }
  152. }
  153. if (isset($options["daemon-loop"])) {
  154. if (!make_stampfile('update_daemon.stamp')) {
  155. _debug("warning: unable to create stampfile\n");
  156. }
  157. update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT);
  158. if (!isset($options["pidlock"]) || $options["task"] == 0)
  159. housekeeping_common(true);
  160. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  161. }
  162. if (isset($options["cleanup-tags"])) {
  163. $rc = cleanup_tags( 14, 50000);
  164. _debug("$rc tags deleted.\n");
  165. }
  166. if (isset($options["indexes"])) {
  167. _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
  168. _debug("Type 'yes' to continue.");
  169. if (read_stdin() != 'yes')
  170. exit;
  171. _debug("clearing existing indexes...");
  172. if (DB_TYPE == "pgsql") {
  173. $result = db_query( "SELECT relname FROM
  174. pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
  175. AND relname NOT LIKE '%_pkey'
  176. AND relkind = 'i'");
  177. } else {
  178. $result = db_query( "SELECT index_name,table_name FROM
  179. information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
  180. }
  181. while ($line = db_fetch_assoc($result)) {
  182. if (DB_TYPE == "pgsql") {
  183. $statement = "DROP INDEX " . $line["relname"];
  184. _debug($statement);
  185. } else {
  186. $statement = "ALTER TABLE ".
  187. $line['table_name']." DROP INDEX ".$line['index_name'];
  188. _debug($statement);
  189. }
  190. db_query( $statement, false);
  191. }
  192. _debug("reading indexes from schema for: " . DB_TYPE);
  193. $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
  194. if ($fp) {
  195. while ($line = fgets($fp)) {
  196. $matches = array();
  197. if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
  198. $index = $matches[1];
  199. $table = $matches[2];
  200. $statement = "CREATE INDEX $index ON $table";
  201. _debug($statement);
  202. db_query( $statement);
  203. }
  204. }
  205. fclose($fp);
  206. } else {
  207. _debug("unable to open schema file.");
  208. }
  209. _debug("all done.");
  210. }
  211. if (isset($options["convert-filters"])) {
  212. _debug("WARNING: this will remove all existing type2 filters.");
  213. _debug("Type 'yes' to continue.");
  214. if (read_stdin() != 'yes')
  215. exit;
  216. _debug("converting filters...");
  217. db_query( "DELETE FROM ttrss_filters2");
  218. $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
  219. while ($line = db_fetch_assoc($result)) {
  220. $owner_uid = $line["owner_uid"];
  221. // date filters are removed
  222. if ($line["filter_type"] != 5) {
  223. $filter = array();
  224. if (sql_bool_to_bool($line["cat_filter"])) {
  225. $feed_id = "CAT:" . (int)$line["cat_id"];
  226. } else {
  227. $feed_id = (int)$line["feed_id"];
  228. }
  229. $filter["enabled"] = $line["enabled"] ? "on" : "off";
  230. $filter["rule"] = array(
  231. json_encode(array(
  232. "reg_exp" => $line["reg_exp"],
  233. "feed_id" => $feed_id,
  234. "filter_type" => $line["filter_type"])));
  235. $filter["action"] = array(
  236. json_encode(array(
  237. "action_id" => $line["action_id"],
  238. "action_param_label" => $line["action_param"],
  239. "action_param" => $line["action_param"])));
  240. // Oh god it's full of hacks
  241. $_REQUEST = $filter;
  242. $_SESSION["uid"] = $owner_uid;
  243. $filters = new Pref_Filters($_REQUEST);
  244. $filters->add();
  245. }
  246. }
  247. }
  248. if (isset($options["update-schema"])) {
  249. _debug("checking for updates (" . DB_TYPE . ")...");
  250. $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION);
  251. if ($updater->isUpdateRequired()) {
  252. _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
  253. _debug("WARNING: please backup your database before continuing.");
  254. _debug("Type 'yes' to continue.");
  255. if (read_stdin() != 'yes')
  256. exit;
  257. for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
  258. _debug("performing update up to version $i...");
  259. $result = $updater->performUpdateTo($i, false);
  260. _debug($result ? "OK!" : "FAILED!");
  261. if (!$result) return;
  262. }
  263. } else {
  264. _debug("update not required.");
  265. }
  266. }
  267. if (isset($options["gen-search-idx"])) {
  268. echo "Generating search index (stemming set to English)...\n";
  269. $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
  270. $count = db_fetch_result($result, 0, "count");
  271. print "Articles to process: $count.\n";
  272. $limit = 500;
  273. $processed = 0;
  274. while (true) {
  275. $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
  276. while ($line = db_fetch_assoc($result)) {
  277. $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags(str_replace('<', ' <', $line['content'])),
  278. 0, 1000000));
  279. db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
  280. }
  281. $processed += db_num_rows($result);
  282. print "Processed $processed articles...\n";
  283. if (db_num_rows($result) != $limit) {
  284. echo "All done.\n";
  285. break;
  286. }
  287. }
  288. }
  289. if (isset($options["list-plugins"])) {
  290. $tmppluginhost = new PluginHost();
  291. $tmppluginhost->load_all($tmppluginhost::KIND_ALL, false);
  292. $enabled = array_map("trim", explode(",", PLUGINS));
  293. echo "List of all available plugins:\n";
  294. foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
  295. $about = $plugin->about();
  296. $status = $about[3] ? "system" : "user";
  297. if (in_array($name, $enabled)) $name .= "*";
  298. printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
  299. $name, $status, $about[0], $about[2], $about[1]);
  300. }
  301. echo "Plugins marked by * are currently enabled for all users.\n";
  302. }
  303. if (isset($options["debug-feed"])) {
  304. $feed = $options["debug-feed"];
  305. if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
  306. if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
  307. $_REQUEST['xdebug'] = 1;
  308. $rc = update_rss_feed($feed) != false ? 0 : 1;
  309. exit($rc);
  310. }
  311. if (isset($options["decrypt-feeds"])) {
  312. $result = db_query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
  313. if (!function_exists("mcrypt_decrypt")) {
  314. _debug("mcrypt functions not available.");
  315. return;
  316. }
  317. require_once "crypt.php";
  318. $total = 0;
  319. db_query("BEGIN");
  320. while ($line = db_fetch_assoc($result)) {
  321. _debug("processing feed id " . $line["id"]);
  322. $auth_pass = db_escape_string(decrypt_string($line["auth_pass"]));
  323. db_query("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = '$auth_pass'
  324. WHERE id = " . $line["id"]);
  325. ++$total;
  326. }
  327. db_query("COMMIT");
  328. _debug("$total feeds processed.");
  329. }
  330. PluginHost::getInstance()->run_commands($options);
  331. if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
  332. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  333. fclose($lock_handle);
  334. unlink(LOCK_DIRECTORY . "/$lock_filename");
  335. ?>