update.php 9.7 KB

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