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