]> git.wh0rd.org - tt-rss.git/blob - update.php
remove errorhandler from CLI scripts for the time being
[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 "log:",
33 "indexes",
34 "update-schema",
35 "convert-filters",
36 "force-update",
37 "list-plugins",
38 "help");
39
40 foreach ($pluginhost->get_commands() as $command => $data) {
41 array_push($longopts, $command . $data["suffix"]);
42 }
43
44 $options = getopt("", $longopts);
45
46 if (count($options) == 0 && !defined('STDIN')) {
47 ?> <html>
48 <head>
49 <title>Tiny Tiny RSS data update script.</title>
50 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
51 <link rel="stylesheet" type="text/css" href="utility.css">
52 </head>
53
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
58 <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
59
60 </body></html>
61 <?php
62 exit;
63 }
64
65 if (count($options) == 0 || isset($options["help"]) ) {
66 print "Tiny Tiny RSS data update script.\n\n";
67 print "Options:\n";
68 print " --feeds - update feeds\n";
69 print " --feedbrowser - update feedbrowser\n";
70 print " --daemon - start single-process update daemon\n";
71 print " --task N - create lockfile using this task id\n";
72 print " --cleanup-tags - perform tags table maintenance\n";
73 print " --quiet - don't output messages to stdout\n";
74 print " --log FILE - log messages to FILE\n";
75 print " --indexes - recreate missing schema indexes\n";
76 print " --update-schema - update database schema\n";
77 print " --convert-filters - convert type1 filters to type2\n";
78 print " --force-update - force update of all feeds\n";
79 print " --list-plugins - list all available plugins\n";
80 print " --help - show this help\n";
81 print "Plugin options:\n";
82
83 foreach ($pluginhost->get_commands() as $command => $data) {
84 $args = $data['arghelp'];
85 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
86 }
87
88 return;
89 }
90
91 if (!isset($options['update-schema'])) {
92 $schema_version = get_schema_version($link);
93
94 if ($schema_version != SCHEMA_VERSION) {
95 die("Schema version is wrong, please upgrade the database.\n");
96 }
97 }
98
99 define('QUIET', isset($options['quiet']));
100
101 if (isset($options["log"])) {
102 _debug("Logging to " . $options["log"]);
103 define('LOGFILE', $options["log"]);
104 }
105
106 if (!isset($options["daemon"])) {
107 $lock_filename = "update.lock";
108 } else {
109 $lock_filename = "update_daemon.lock";
110 }
111
112 if (isset($options["task"])) {
113 _debug("Using task id " . $options["task"]);
114 $lock_filename = $lock_filename . "-task_" . $options["task"];
115 }
116
117 $lock_handle = make_lockfile($lock_filename);
118 $must_exit = false;
119
120 // Try to lock a file in order to avoid concurrent update.
121 if (!$lock_handle) {
122 die("error: Can't create lockfile ($lock_filename). ".
123 "Maybe another update process is already running.\n");
124 }
125
126 if (isset($options["force-update"])) {
127 _debug("marking all feeds as needing update...");
128
129 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
130 last_updated = '1970-01-01'");
131 }
132
133 if (isset($options["feeds"])) {
134 // Update all feeds needing a update.
135 update_daemon_common($link);
136
137 // Update feedbrowser
138 $count = update_feedbrowser_cache($link);
139 _debug("Feedbrowser updated, $count feeds processed.");
140
141 // Purge orphans and cleanup tags
142 purge_orphans($link, true);
143
144 $rc = cleanup_tags($link, 14, 50000);
145 _debug("Cleaned $rc cached tags.");
146
147 global $pluginhost;
148 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
149 }
150
151 if (isset($options["feedbrowser"])) {
152 $count = update_feedbrowser_cache($link);
153 print "Finished, $count feeds processed.\n";
154 }
155
156 if (isset($options["daemon"])) {
157 while (true) {
158 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
159
160 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet");
161 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
162 sleep(DAEMON_SLEEP_INTERVAL);
163 }
164 }
165
166 if (isset($options["daemon-loop"])) {
167 if (!make_stampfile('update_daemon.stamp')) {
168 _debug("warning: unable to create stampfile\n");
169 }
170
171 // Call to the feed batch update function
172 // or regenerate feedbrowser cache
173
174 if (rand(0,100) > 30) {
175 update_daemon_common($link);
176 } else {
177 $count = update_feedbrowser_cache($link);
178 _debug("Feedbrowser updated, $count feeds processed.");
179
180 purge_orphans($link, true);
181
182 $rc = cleanup_tags($link, 14, 50000);
183
184 _debug("Cleaned $rc cached tags.");
185
186 global $pluginhost;
187 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
188 }
189
190 }
191
192 if (isset($options["cleanup-tags"])) {
193 $rc = cleanup_tags($link, 14, 50000);
194 _debug("$rc tags deleted.\n");
195 }
196
197 if (isset($options["indexes"])) {
198 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
199 _debug("Type 'yes' to continue.");
200
201 if (read_stdin() != 'yes')
202 exit;
203
204 _debug("clearing existing indexes...");
205
206 if (DB_TYPE == "pgsql") {
207 $result = db_query($link, "SELECT relname FROM
208 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
209 AND relname NOT LIKE '%_pkey'
210 AND relkind = 'i'");
211 } else {
212 $result = db_query($link, "SELECT index_name,table_name FROM
213 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
214 }
215
216 while ($line = db_fetch_assoc($result)) {
217 if (DB_TYPE == "pgsql") {
218 $statement = "DROP INDEX " . $line["relname"];
219 _debug($statement);
220 } else {
221 $statement = "ALTER TABLE ".
222 $line['table_name']." DROP INDEX ".$line['index_name'];
223 _debug($statement);
224 }
225 db_query($link, $statement, false);
226 }
227
228 _debug("reading indexes from schema for: " . DB_TYPE);
229
230 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
231 if ($fp) {
232 while ($line = fgets($fp)) {
233 $matches = array();
234
235 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
236 $index = $matches[1];
237 $table = $matches[2];
238
239 $statement = "CREATE INDEX $index ON $table";
240
241 _debug($statement);
242 db_query($link, $statement);
243 }
244 }
245 fclose($fp);
246 } else {
247 _debug("unable to open schema file.");
248 }
249 _debug("all done.");
250 }
251
252 if (isset($options["convert-filters"])) {
253 _debug("WARNING: this will remove all existing type2 filters.");
254 _debug("Type 'yes' to continue.");
255
256 if (read_stdin() != 'yes')
257 exit;
258
259 _debug("converting filters...");
260
261 db_query($link, "DELETE FROM ttrss_filters2");
262
263 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
264
265 while ($line = db_fetch_assoc($result)) {
266 $owner_uid = $line["owner_uid"];
267
268 // date filters are removed
269 if ($line["filter_type"] != 5) {
270 $filter = array();
271
272 if (sql_bool_to_bool($line["cat_filter"])) {
273 $feed_id = "CAT:" . (int)$line["cat_id"];
274 } else {
275 $feed_id = (int)$line["feed_id"];
276 }
277
278 $filter["enabled"] = $line["enabled"] ? "on" : "off";
279 $filter["rule"] = array(
280 json_encode(array(
281 "reg_exp" => $line["reg_exp"],
282 "feed_id" => $feed_id,
283 "filter_type" => $line["filter_type"])));
284
285 $filter["action"] = array(
286 json_encode(array(
287 "action_id" => $line["action_id"],
288 "action_param_label" => $line["action_param"],
289 "action_param" => $line["action_param"])));
290
291 // Oh god it's full of hacks
292
293 $_REQUEST = $filter;
294 $_SESSION["uid"] = $owner_uid;
295
296 $filters = new Pref_Filters($link, $_REQUEST);
297 $filters->add();
298 }
299 }
300
301 }
302
303 if (isset($options["update-schema"])) {
304 _debug("checking for updates (" . DB_TYPE . ")...");
305
306 $updater = new DbUpdater($link, DB_TYPE, SCHEMA_VERSION);
307
308 if ($updater->isUpdateRequired()) {
309 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
310 _debug("WARNING: please backup your database before continuing.");
311 _debug("Type 'yes' to continue.");
312
313 if (read_stdin() != 'yes')
314 exit;
315
316 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
317 _debug("performing update up to version $i...");
318
319 $result = $updater->performUpdateTo($i);
320
321 _debug($result ? "OK!" : "FAILED!");
322
323 if (!$result) return;
324
325 }
326 } else {
327 _debug("update not required.");
328 }
329
330 }
331
332 if (isset($options["list-plugins"])) {
333 $tmppluginhost = new PluginHost($link);
334 $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
335 $enabled = array_map("trim", explode(",", PLUGINS));
336
337 echo "List of all available plugins:\n";
338
339 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
340 $about = $plugin->about();
341
342 $status = $about[3] ? "system" : "user";
343
344 if (in_array($name, $enabled)) $name .= "*";
345
346 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
347 $name, $status, $about[0], $about[2], $about[1]);
348 }
349
350 echo "Plugins marked by * are currently enabled for all users.\n";
351
352 }
353
354 $pluginhost->run_commands($options);
355
356 db_close($link);
357
358 if ($lock_handle != false) {
359 fclose($lock_handle);
360 }
361
362 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
363 unlink(LOCK_DIRECTORY . "/$lock_filename");
364 ?>