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