]> git.wh0rd.org - tt-rss.git/blame - update.php
rework update.php to use getopt; allow --task parameter
[tt-rss.git] / update.php
CommitLineData
ece78711 1#!/usr/bin/env php
fecd57c8 2<?php
88e8fb3a
AD
3 set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4 get_include_path());
107d0cf3 5
661135c7
AD
6 define('DISABLE_SESSIONS', true);
7
9b27cec8
AD
8 chdir(dirname(__FILE__));
9
fb074239 10 require_once "functions.php";
2c08214a 11 require_once "rssfuncs.php";
81596c66 12 require_once "config.php";
f02713bb 13 require_once "sanity_check.php";
81596c66 14 require_once "db.php";
661135c7 15 require_once "db-prefs.php";
661135c7 16
3de78afd
AD
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
19
73f28fe9
AD
20 // Create a database connection.
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 init_connection($link);
24
764555ff
AD
25 $longopts = array("feeds",
26 "feedbrowser",
27 "daemon",
28 "daemon-loop",
29 "task:",
30 "cleanup-tags",
31 "quiet",
32 "indexes",
33 "convert-filters",
34 "force-update",
35 "list-plugins",
36 "help");
37
38 foreach ($pluginhost->get_commands() as $command => $data) {
39 array_push($longopts, $command);
40 }
41
42 $options = getopt("", $longopts);
3de78afd 43
764555ff 44 if (count($options) == 0 && !defined('STDIN')) {
366f06f7
AD
45 ?> <html>
46 <head>
3de78afd 47 <title>Tiny Tiny RSS data update script.</title>
366f06f7
AD
48 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
49 <link rel="stylesheet" type="text/css" href="utility.css">
50 </head>
51
52 <body>
53 <div class="floatingLogo"><img src="images/logo_wide.png"></div>
3de78afd 54 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
366f06f7 55
3de78afd 56 <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
366f06f7
AD
57
58 </body></html>
59 <?php
60 exit;
61 }
62
764555ff
AD
63
64 if (count($options) == 0 || isset($options["help"]) ) {
661135c7
AD
65 print "Tiny Tiny RSS data update script.\n\n";
66 print "Options:\n";
764555ff
AD
67 print " --feeds - update feeds\n";
68 print " --feedbrowser - update feedbrowser\n";
69 print " --daemon - start single-process update daemon\n";
70 print " --task N - create lockfile using this task id\n";
71 print " --cleanup-tags - perform tags table maintenance\n";
72 print " --quiet - don't show messages\n";
73 print " --indexes - recreate missing schema indexes\n";
74 print " --convert-filters - convert type1 filters to type2\n";
75 print " --force-update - force update of all feeds\n";
76 print " --list-plugins - list all available plugins\n";
77 print " --help - show this help\n";
73f28fe9
AD
78 print "Plugin options:\n";
79
80 foreach ($pluginhost->get_commands() as $command => $data) {
764555ff 81 printf(" --%-19s - %s\n", "$command", $data["description"]);
73f28fe9
AD
82 }
83
661135c7 84 return;
81596c66 85 }
87b9fb65 86
764555ff 87 define('QUIET', isset($options['quiet']));
5439d333 88
764555ff 89 if (!isset($options["daemon"])) {
661135c7
AD
90 $lock_filename = "update.lock";
91 } else {
92 $lock_filename = "update_daemon.lock";
93 }
fecd57c8 94
764555ff
AD
95 if (isset($options["task"])) {
96 _debug("Using task id " . $options["task"]);
97 $lock_filename = $lock_filename . "-task_" . $options["task"];
98 }
99
661135c7
AD
100 $lock_handle = make_lockfile($lock_filename);
101 $must_exit = false;
fecd57c8 102
661135c7
AD
103 // Try to lock a file in order to avoid concurrent update.
104 if (!$lock_handle) {
105 die("error: Can't create lockfile ($lock_filename). ".
106 "Maybe another update process is already running.\n");
107 }
fecd57c8 108
764555ff
AD
109 if (isset($options["force-update"])) {
110 _debug("marking all feeds as needing update...");
111
112 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
113 last_updated = '1970-01-01'");
114 }
115
116 if (isset($options["feeds"])) {
661135c7
AD
117 // Update all feeds needing a update.
118 update_daemon_common($link);
84e9a8c7
AD
119
120 // Update feedbrowser
121 $count = update_feedbrowser_cache($link);
122 _debug("Feedbrowser updated, $count feeds processed.");
123
124 // Purge orphans and cleanup tags
125 purge_orphans($link, true);
126
127 $rc = cleanup_tags($link, 14, 50000);
128 _debug("Cleaned $rc cached tags.");
f32eb194 129
41b82aa4
AD
130 global $pluginhost;
131 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
661135c7 132 }
fecd57c8 133
764555ff 134 if (isset($options["feedbrowser"])) {
661135c7
AD
135 $count = update_feedbrowser_cache($link);
136 print "Finished, $count feeds processed.\n";
fecd57c8 137 }
661135c7 138
764555ff 139 if (isset($options["daemon"])) {
661135c7 140 while (true) {
764555ff 141 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop");
661135c7
AD
142 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
143 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 144 }
81596c66 145 }
9e21a571 146
764555ff 147 if (isset($options["daemon-loop"])) {
661135c7
AD
148 if (!make_stampfile('update_daemon.stamp')) {
149 die("error: unable to create stampfile\n");
150 }
9e21a571 151
dbaa4e4a 152 // Call to the feed batch update function
661135c7 153 // or regenerate feedbrowser cache
9e21a571 154
661135c7
AD
155 if (rand(0,100) > 30) {
156 update_daemon_common($link);
9e21a571 157 } else {
661135c7 158 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
159 _debug("Feedbrowser updated, $count feeds processed.");
160
161 purge_orphans($link, true);
dbaa4e4a 162
e3b42c5a
AD
163 $rc = cleanup_tags($link, 14, 50000);
164
165 _debug("Cleaned $rc cached tags.");
f32eb194 166
41b82aa4
AD
167 global $pluginhost;
168 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
9e21a571 169 }
ef59e6e8 170
fecd57c8 171 }
fecd57c8 172
764555ff 173 if (isset($options["cleanup-tags"])) {
868650e4 174 $rc = cleanup_tags($link, 14, 50000);
5439d333 175 _debug("$rc tags deleted.\n");
868650e4
AD
176 }
177
764555ff 178 if (isset($options["indexes"])) {
871f0a7a
AD
179 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
180 _debug("Type 'yes' to continue.");
181
182 if (read_stdin() != 'yes')
183 exit;
184
185 _debug("clearing existing indexes...");
186
187 if (DB_TYPE == "pgsql") {
188 $result = db_query($link, "SELECT relname FROM
189 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
190 AND relname NOT LIKE '%_pkey'
191 AND relkind = 'i'");
192 } else {
193 $result = db_query($link, "SELECT index_name,table_name FROM
194 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
195 }
196
197 while ($line = db_fetch_assoc($result)) {
198 if (DB_TYPE == "pgsql") {
199 $statement = "DROP INDEX " . $line["relname"];
200 _debug($statement);
201 } else {
202 $statement = "ALTER TABLE ".
203 $line['table_name']." DROP INDEX ".$line['index_name'];
204 _debug($statement);
205 }
206 db_query($link, $statement, false);
207 }
208
209 _debug("reading indexes from schema for: " . DB_TYPE);
210
211 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
212 if ($fp) {
213 while ($line = fgets($fp)) {
214 $matches = array();
215
216 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
217 $index = $matches[1];
218 $table = $matches[2];
219
220 $statement = "CREATE INDEX $index ON $table";
221
222 _debug($statement);
223 db_query($link, $statement);
224 }
225 }
226 fclose($fp);
227 } else {
228 _debug("unable to open schema file.");
229 }
230 _debug("all done.");
231 }
232
764555ff 233 if (isset($options["convert-filters"])) {
6aff7845
AD
234 _debug("WARNING: this will remove all existing type2 filters.");
235 _debug("Type 'yes' to continue.");
236
237 if (read_stdin() != 'yes')
238 exit;
239
240 _debug("converting filters...");
241
242 db_query($link, "DELETE FROM ttrss_filters2");
243
244 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
245
246 while ($line = db_fetch_assoc($result)) {
247 $owner_uid = $line["owner_uid"];
248
37f78940
AD
249 // date filters are removed
250 if ($line["filter_type"] != 5) {
251 $filter = array();
252
253 if (sql_bool_to_bool($line["cat_filter"])) {
254 $feed_id = "CAT:" . (int)$line["cat_id"];
255 } else {
256 $feed_id = (int)$line["feed_id"];
257 }
6aff7845 258
37f78940
AD
259 $filter["enabled"] = $line["enabled"] ? "on" : "off";
260 $filter["rule"] = array(
261 json_encode(array(
262 "reg_exp" => $line["reg_exp"],
263 "feed_id" => $feed_id,
264 "filter_type" => $line["filter_type"])));
6aff7845 265
37f78940
AD
266 $filter["action"] = array(
267 json_encode(array(
268 "action_id" => $line["action_id"],
269 "action_param_label" => $line["action_param"],
270 "action_param" => $line["action_param"])));
6aff7845 271
37f78940 272 // Oh god it's full of hacks
5451903c 273
37f78940
AD
274 $_REQUEST = $filter;
275 $_SESSION["uid"] = $owner_uid;
6aff7845 276
37f78940
AD
277 $filters = new Pref_Filters($link, $_REQUEST);
278 $filters->add();
279 }
6aff7845
AD
280 }
281
282 }
283
764555ff 284 if (isset($options["list-plugins"])) {
7a866114 285 $tmppluginhost = new PluginHost($link);
d2a421e3 286 $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
20b86c79
AD
287 $enabled = array_map("trim", explode(",", PLUGINS));
288
289 echo "List of all available plugins:\n";
290
7a866114 291 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
d2a421e3 292 $about = $plugin->about();
7a866114 293
20b86c79
AD
294 $status = $about[3] ? "system" : "user";
295
296 if (in_array($name, $enabled)) $name .= "*";
297
298 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
299 $name, $status, $about[0], $about[2], $about[1]);
7a866114 300 }
20b86c79
AD
301
302 echo "Plugins marked by * are currently enabled for all users.\n";
303
7a866114
AD
304 }
305
764555ff 306 $pluginhost->run_commands($options);
73f28fe9 307
661135c7 308 db_close($link);
107d0cf3 309