]>
Commit | Line | Data |
---|---|---|
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) { | |
4cf0f9a9 | 40 | array_push($longopts, $command . $data["suffix"]); |
764555ff AD |
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) { | |
4cf0f9a9 AD |
82 | $args = $data['arghelp']; |
83 | printf(" --%-19s - %s\n", "$command $args", $data["description"]); | |
73f28fe9 AD |
84 | } |
85 | ||
661135c7 | 86 | return; |
81596c66 | 87 | } |
87b9fb65 | 88 | |
dc24b520 AD |
89 | define('QUIET', isset($options['quiet'])); |
90 | ||
2191eb7a AD |
91 | if (isset($options["log"])) { |
92 | _debug("Logging to " . $options["log"]); | |
93 | define('LOGFILE', $options["log"]); | |
94 | } | |
95 | ||
764555ff | 96 | if (!isset($options["daemon"])) { |
661135c7 AD |
97 | $lock_filename = "update.lock"; |
98 | } else { | |
99 | $lock_filename = "update_daemon.lock"; | |
100 | } | |
fecd57c8 | 101 | |
764555ff AD |
102 | if (isset($options["task"])) { |
103 | _debug("Using task id " . $options["task"]); | |
104 | $lock_filename = $lock_filename . "-task_" . $options["task"]; | |
105 | } | |
106 | ||
661135c7 AD |
107 | $lock_handle = make_lockfile($lock_filename); |
108 | $must_exit = false; | |
fecd57c8 | 109 | |
661135c7 AD |
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 | } | |
fecd57c8 | 115 | |
764555ff AD |
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"])) { | |
661135c7 AD |
124 | // Update all feeds needing a update. |
125 | update_daemon_common($link); | |
84e9a8c7 AD |
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."); | |
f32eb194 | 136 | |
41b82aa4 AD |
137 | global $pluginhost; |
138 | $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
661135c7 | 139 | } |
fecd57c8 | 140 | |
764555ff | 141 | if (isset($options["feedbrowser"])) { |
661135c7 AD |
142 | $count = update_feedbrowser_cache($link); |
143 | print "Finished, $count feeds processed.\n"; | |
fecd57c8 | 144 | } |
661135c7 | 145 | |
764555ff | 146 | if (isset($options["daemon"])) { |
661135c7 | 147 | while (true) { |
0ae9f746 AD |
148 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; |
149 | ||
150 | passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet"); | |
661135c7 AD |
151 | _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); |
152 | sleep(DAEMON_SLEEP_INTERVAL); | |
fecd57c8 | 153 | } |
81596c66 | 154 | } |
9e21a571 | 155 | |
764555ff | 156 | if (isset($options["daemon-loop"])) { |
661135c7 | 157 | if (!make_stampfile('update_daemon.stamp')) { |
e81610d9 | 158 | _debug("warning: unable to create stampfile\n"); |
661135c7 | 159 | } |
9e21a571 | 160 | |
dbaa4e4a | 161 | // Call to the feed batch update function |
661135c7 | 162 | // or regenerate feedbrowser cache |
9e21a571 | 163 | |
661135c7 AD |
164 | if (rand(0,100) > 30) { |
165 | update_daemon_common($link); | |
9e21a571 | 166 | } else { |
661135c7 | 167 | $count = update_feedbrowser_cache($link); |
e3b42c5a AD |
168 | _debug("Feedbrowser updated, $count feeds processed."); |
169 | ||
170 | purge_orphans($link, true); | |
dbaa4e4a | 171 | |
e3b42c5a AD |
172 | $rc = cleanup_tags($link, 14, 50000); |
173 | ||
174 | _debug("Cleaned $rc cached tags."); | |
f32eb194 | 175 | |
41b82aa4 AD |
176 | global $pluginhost; |
177 | $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
9e21a571 | 178 | } |
ef59e6e8 | 179 | |
fecd57c8 | 180 | } |
fecd57c8 | 181 | |
764555ff | 182 | if (isset($options["cleanup-tags"])) { |
868650e4 | 183 | $rc = cleanup_tags($link, 14, 50000); |
5439d333 | 184 | _debug("$rc tags deleted.\n"); |
868650e4 AD |
185 | } |
186 | ||
764555ff | 187 | if (isset($options["indexes"])) { |
871f0a7a AD |
188 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); |
189 | _debug("Type 'yes' to continue."); | |
190 | ||
191 | if (read_stdin() != 'yes') | |
192 | exit; | |
193 | ||
194 | _debug("clearing existing indexes..."); | |
195 | ||
196 | if (DB_TYPE == "pgsql") { | |
197 | $result = db_query($link, "SELECT relname FROM | |
198 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' | |
199 | AND relname NOT LIKE '%_pkey' | |
200 | AND relkind = 'i'"); | |
201 | } else { | |
202 | $result = db_query($link, "SELECT index_name,table_name FROM | |
203 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); | |
204 | } | |
205 | ||
206 | while ($line = db_fetch_assoc($result)) { | |
207 | if (DB_TYPE == "pgsql") { | |
208 | $statement = "DROP INDEX " . $line["relname"]; | |
209 | _debug($statement); | |
210 | } else { | |
211 | $statement = "ALTER TABLE ". | |
212 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
213 | _debug($statement); | |
214 | } | |
215 | db_query($link, $statement, false); | |
216 | } | |
217 | ||
218 | _debug("reading indexes from schema for: " . DB_TYPE); | |
219 | ||
220 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
221 | if ($fp) { | |
222 | while ($line = fgets($fp)) { | |
223 | $matches = array(); | |
224 | ||
225 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
226 | $index = $matches[1]; | |
227 | $table = $matches[2]; | |
228 | ||
229 | $statement = "CREATE INDEX $index ON $table"; | |
230 | ||
231 | _debug($statement); | |
232 | db_query($link, $statement); | |
233 | } | |
234 | } | |
235 | fclose($fp); | |
236 | } else { | |
237 | _debug("unable to open schema file."); | |
238 | } | |
239 | _debug("all done."); | |
240 | } | |
241 | ||
764555ff | 242 | if (isset($options["convert-filters"])) { |
6aff7845 AD |
243 | _debug("WARNING: this will remove all existing type2 filters."); |
244 | _debug("Type 'yes' to continue."); | |
245 | ||
246 | if (read_stdin() != 'yes') | |
247 | exit; | |
248 | ||
249 | _debug("converting filters..."); | |
250 | ||
251 | db_query($link, "DELETE FROM ttrss_filters2"); | |
252 | ||
253 | $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id"); | |
254 | ||
255 | while ($line = db_fetch_assoc($result)) { | |
256 | $owner_uid = $line["owner_uid"]; | |
257 | ||
37f78940 AD |
258 | // date filters are removed |
259 | if ($line["filter_type"] != 5) { | |
260 | $filter = array(); | |
261 | ||
262 | if (sql_bool_to_bool($line["cat_filter"])) { | |
263 | $feed_id = "CAT:" . (int)$line["cat_id"]; | |
264 | } else { | |
265 | $feed_id = (int)$line["feed_id"]; | |
266 | } | |
6aff7845 | 267 | |
37f78940 AD |
268 | $filter["enabled"] = $line["enabled"] ? "on" : "off"; |
269 | $filter["rule"] = array( | |
270 | json_encode(array( | |
271 | "reg_exp" => $line["reg_exp"], | |
272 | "feed_id" => $feed_id, | |
273 | "filter_type" => $line["filter_type"]))); | |
6aff7845 | 274 | |
37f78940 AD |
275 | $filter["action"] = array( |
276 | json_encode(array( | |
277 | "action_id" => $line["action_id"], | |
278 | "action_param_label" => $line["action_param"], | |
279 | "action_param" => $line["action_param"]))); | |
6aff7845 | 280 | |
37f78940 | 281 | // Oh god it's full of hacks |
5451903c | 282 | |
37f78940 AD |
283 | $_REQUEST = $filter; |
284 | $_SESSION["uid"] = $owner_uid; | |
6aff7845 | 285 | |
37f78940 AD |
286 | $filters = new Pref_Filters($link, $_REQUEST); |
287 | $filters->add(); | |
288 | } | |
6aff7845 AD |
289 | } |
290 | ||
291 | } | |
292 | ||
764555ff | 293 | if (isset($options["list-plugins"])) { |
7a866114 | 294 | $tmppluginhost = new PluginHost($link); |
d2a421e3 | 295 | $tmppluginhost->load_all($tmppluginhost::KIND_ALL); |
20b86c79 AD |
296 | $enabled = array_map("trim", explode(",", PLUGINS)); |
297 | ||
298 | echo "List of all available plugins:\n"; | |
299 | ||
7a866114 | 300 | foreach ($tmppluginhost->get_plugins() as $name => $plugin) { |
d2a421e3 | 301 | $about = $plugin->about(); |
7a866114 | 302 | |
20b86c79 AD |
303 | $status = $about[3] ? "system" : "user"; |
304 | ||
305 | if (in_array($name, $enabled)) $name .= "*"; | |
306 | ||
307 | printf("%-50s %-10s v%.2f (by %s)\n%s\n\n", | |
308 | $name, $status, $about[0], $about[2], $about[1]); | |
7a866114 | 309 | } |
20b86c79 AD |
310 | |
311 | echo "Plugins marked by * are currently enabled for all users.\n"; | |
312 | ||
7a866114 AD |
313 | } |
314 | ||
764555ff | 315 | $pluginhost->run_commands($options); |
73f28fe9 | 316 | |
661135c7 | 317 | db_close($link); |
107d0cf3 | 318 | |