PageRenderTime 77ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/update.php

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