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