]> git.wh0rd.org - tt-rss.git/blob - update.php
rework update.php to use getopt; allow --task parameter
[tt-rss.git] / update.php
1 #!/usr/bin/env php
2 <?php
3 set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4 get_include_path());
5
6 define('DISABLE_SESSIONS', true);
7
8 chdir(dirname(__FILE__));
9
10 require_once "functions.php";
11 require_once "rssfuncs.php";
12 require_once "config.php";
13 require_once "sanity_check.php";
14 require_once "db.php";
15 require_once "db-prefs.php";
16
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
19
20 // Create a database connection.
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 init_connection($link);
24
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);
43
44 if (count($options) == 0 && !defined('STDIN')) {
45 ?> <html>
46 <head>
47 <title>Tiny Tiny RSS data update script.</title>
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>
54 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
55
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."); ?>
57
58 </body></html>
59 <?php
60 exit;
61 }
62
63
64 if (count($options) == 0 || isset($options["help"]) ) {
65 print "Tiny Tiny RSS data update script.\n\n";
66 print "Options:\n";
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";
78 print "Plugin options:\n";
79
80 foreach ($pluginhost->get_commands() as $command => $data) {
81 printf(" --%-19s - %s\n", "$command", $data["description"]);
82 }
83
84 return;
85 }
86
87 define('QUIET', isset($options['quiet']));
88
89 if (!isset($options["daemon"])) {
90 $lock_filename = "update.lock";
91 } else {
92 $lock_filename = "update_daemon.lock";
93 }
94
95 if (isset($options["task"])) {
96 _debug("Using task id " . $options["task"]);
97 $lock_filename = $lock_filename . "-task_" . $options["task"];
98 }
99
100 $lock_handle = make_lockfile($lock_filename);
101 $must_exit = false;
102
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 }
108
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"])) {
117 // Update all feeds needing a update.
118 update_daemon_common($link);
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.");
129
130 global $pluginhost;
131 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
132 }
133
134 if (isset($options["feedbrowser"])) {
135 $count = update_feedbrowser_cache($link);
136 print "Finished, $count feeds processed.\n";
137 }
138
139 if (isset($options["daemon"])) {
140 while (true) {
141 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop");
142 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
143 sleep(DAEMON_SLEEP_INTERVAL);
144 }
145 }
146
147 if (isset($options["daemon-loop"])) {
148 if (!make_stampfile('update_daemon.stamp')) {
149 die("error: unable to create stampfile\n");
150 }
151
152 // Call to the feed batch update function
153 // or regenerate feedbrowser cache
154
155 if (rand(0,100) > 30) {
156 update_daemon_common($link);
157 } else {
158 $count = update_feedbrowser_cache($link);
159 _debug("Feedbrowser updated, $count feeds processed.");
160
161 purge_orphans($link, true);
162
163 $rc = cleanup_tags($link, 14, 50000);
164
165 _debug("Cleaned $rc cached tags.");
166
167 global $pluginhost;
168 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
169 }
170
171 }
172
173 if (isset($options["cleanup-tags"])) {
174 $rc = cleanup_tags($link, 14, 50000);
175 _debug("$rc tags deleted.\n");
176 }
177
178 if (isset($options["indexes"])) {
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
233 if (isset($options["convert-filters"])) {
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
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 }
258
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"])));
265
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"])));
271
272 // Oh god it's full of hacks
273
274 $_REQUEST = $filter;
275 $_SESSION["uid"] = $owner_uid;
276
277 $filters = new Pref_Filters($link, $_REQUEST);
278 $filters->add();
279 }
280 }
281
282 }
283
284 if (isset($options["list-plugins"])) {
285 $tmppluginhost = new PluginHost($link);
286 $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
287 $enabled = array_map("trim", explode(",", PLUGINS));
288
289 echo "List of all available plugins:\n";
290
291 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
292 $about = $plugin->about();
293
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]);
300 }
301
302 echo "Plugins marked by * are currently enabled for all users.\n";
303
304 }
305
306 $pluginhost->run_commands($options);
307
308 db_close($link);
309
310 if ($lock_handle != false) {
311 fclose($lock_handle);
312 }
313
314 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
315 unlink(LOCK_DIRECTORY . "/$lock_filename");
316 ?>