]>
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 | ||
404e2e36 | 10 | require_once "autoload.php"; |
fb074239 | 11 | require_once "functions.php"; |
2c08214a | 12 | require_once "rssfuncs.php"; |
81596c66 | 13 | require_once "config.php"; |
f02713bb | 14 | require_once "sanity_check.php"; |
81596c66 | 15 | require_once "db.php"; |
661135c7 | 16 | require_once "db-prefs.php"; |
661135c7 | 17 | |
3de78afd AD |
18 | if (!defined('PHP_EXECUTABLE')) |
19 | define('PHP_EXECUTABLE', '/usr/bin/php'); | |
20 | ||
6322ac79 | 21 | init_plugins(); |
73f28fe9 | 22 | |
764555ff AD |
23 | $longopts = array("feeds", |
24 | "feedbrowser", | |
25 | "daemon", | |
26 | "daemon-loop", | |
27 | "task:", | |
28 | "cleanup-tags", | |
29 | "quiet", | |
2191eb7a | 30 | "log:", |
764555ff | 31 | "indexes", |
7440a7fe | 32 | "pidlock:", |
b4c47f7e | 33 | "update-schema", |
764555ff AD |
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 | 74 | print " --indexes - recreate missing schema indexes\n"; |
b4c47f7e | 75 | print " --update-schema - update database schema\n"; |
764555ff AD |
76 | print " --convert-filters - convert type1 filters to type2\n"; |
77 | print " --force-update - force update of all feeds\n"; | |
78 | print " --list-plugins - list all available plugins\n"; | |
79 | print " --help - show this help\n"; | |
73f28fe9 AD |
80 | print "Plugin options:\n"; |
81 | ||
82 | foreach ($pluginhost->get_commands() as $command => $data) { | |
4cf0f9a9 AD |
83 | $args = $data['arghelp']; |
84 | printf(" --%-19s - %s\n", "$command $args", $data["description"]); | |
73f28fe9 AD |
85 | } |
86 | ||
661135c7 | 87 | return; |
81596c66 | 88 | } |
87b9fb65 | 89 | |
0186be6a AD |
90 | if (!isset($options['daemon'])) { |
91 | require_once "errorhandler.php"; | |
92 | } | |
93 | ||
857efe49 | 94 | if (!isset($options['update-schema'])) { |
6322ac79 | 95 | $schema_version = get_schema_version(); |
857efe49 AD |
96 | |
97 | if ($schema_version != SCHEMA_VERSION) { | |
98 | die("Schema version is wrong, please upgrade the database.\n"); | |
99 | } | |
100 | } | |
101 | ||
dc24b520 AD |
102 | define('QUIET', isset($options['quiet'])); |
103 | ||
2191eb7a AD |
104 | if (isset($options["log"])) { |
105 | _debug("Logging to " . $options["log"]); | |
106 | define('LOGFILE', $options["log"]); | |
107 | } | |
108 | ||
764555ff | 109 | if (!isset($options["daemon"])) { |
661135c7 AD |
110 | $lock_filename = "update.lock"; |
111 | } else { | |
112 | $lock_filename = "update_daemon.lock"; | |
113 | } | |
fecd57c8 | 114 | |
764555ff AD |
115 | if (isset($options["task"])) { |
116 | _debug("Using task id " . $options["task"]); | |
117 | $lock_filename = $lock_filename . "-task_" . $options["task"]; | |
118 | } | |
119 | ||
7440a7fe AD |
120 | if (isset($options["pidlock"])) { |
121 | $my_pid = $options["pidlock"]; | |
122 | $lock_filename = "update_daemon-$my_pid.lock"; | |
123 | ||
124 | } | |
125 | ||
126 | _debug("Lock: $lock_filename"); | |
127 | ||
661135c7 AD |
128 | $lock_handle = make_lockfile($lock_filename); |
129 | $must_exit = false; | |
fecd57c8 | 130 | |
7440a7fe AD |
131 | if (isset($options["task"])) { |
132 | $waits = $options["task"] * 5; | |
133 | _debug("Waiting before update ($waits)"); | |
134 | sleep($waits); | |
135 | } | |
136 | ||
661135c7 AD |
137 | // Try to lock a file in order to avoid concurrent update. |
138 | if (!$lock_handle) { | |
139 | die("error: Can't create lockfile ($lock_filename). ". | |
140 | "Maybe another update process is already running.\n"); | |
141 | } | |
fecd57c8 | 142 | |
764555ff AD |
143 | if (isset($options["force-update"])) { |
144 | _debug("marking all feeds as needing update..."); | |
145 | ||
6322ac79 | 146 | db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01', |
764555ff AD |
147 | last_updated = '1970-01-01'"); |
148 | } | |
149 | ||
150 | if (isset($options["feeds"])) { | |
661135c7 | 151 | // Update all feeds needing a update. |
6322ac79 | 152 | update_daemon_common(); |
84e9a8c7 AD |
153 | |
154 | // Update feedbrowser | |
6322ac79 | 155 | $count = update_feedbrowser_cache(); |
84e9a8c7 AD |
156 | _debug("Feedbrowser updated, $count feeds processed."); |
157 | ||
158 | // Purge orphans and cleanup tags | |
6322ac79 | 159 | purge_orphans( true); |
84e9a8c7 | 160 | |
6322ac79 | 161 | $rc = cleanup_tags( 14, 50000); |
84e9a8c7 | 162 | _debug("Cleaned $rc cached tags."); |
f32eb194 | 163 | |
41b82aa4 AD |
164 | global $pluginhost; |
165 | $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
661135c7 | 166 | } |
fecd57c8 | 167 | |
764555ff | 168 | if (isset($options["feedbrowser"])) { |
6322ac79 | 169 | $count = update_feedbrowser_cache(); |
661135c7 | 170 | print "Finished, $count feeds processed.\n"; |
fecd57c8 | 171 | } |
661135c7 | 172 | |
764555ff | 173 | if (isset($options["daemon"])) { |
661135c7 | 174 | while (true) { |
0ae9f746 AD |
175 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; |
176 | ||
177 | passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet"); | |
661135c7 AD |
178 | _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); |
179 | sleep(DAEMON_SLEEP_INTERVAL); | |
fecd57c8 | 180 | } |
81596c66 | 181 | } |
9e21a571 | 182 | |
764555ff | 183 | if (isset($options["daemon-loop"])) { |
661135c7 | 184 | if (!make_stampfile('update_daemon.stamp')) { |
e81610d9 | 185 | _debug("warning: unable to create stampfile\n"); |
661135c7 | 186 | } |
9e21a571 | 187 | |
dbaa4e4a | 188 | // Call to the feed batch update function |
661135c7 | 189 | // or regenerate feedbrowser cache |
9e21a571 | 190 | |
661135c7 | 191 | if (rand(0,100) > 30) { |
6322ac79 | 192 | update_daemon_common(); |
9e21a571 | 193 | } else { |
6322ac79 | 194 | $count = update_feedbrowser_cache(); |
e3b42c5a AD |
195 | _debug("Feedbrowser updated, $count feeds processed."); |
196 | ||
6322ac79 | 197 | purge_orphans( true); |
dbaa4e4a | 198 | |
6322ac79 | 199 | $rc = cleanup_tags( 14, 50000); |
e3b42c5a AD |
200 | |
201 | _debug("Cleaned $rc cached tags."); | |
f32eb194 | 202 | |
41b82aa4 AD |
203 | global $pluginhost; |
204 | $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
9e21a571 | 205 | } |
ef59e6e8 | 206 | |
fecd57c8 | 207 | } |
fecd57c8 | 208 | |
764555ff | 209 | if (isset($options["cleanup-tags"])) { |
6322ac79 | 210 | $rc = cleanup_tags( 14, 50000); |
5439d333 | 211 | _debug("$rc tags deleted.\n"); |
868650e4 AD |
212 | } |
213 | ||
764555ff | 214 | if (isset($options["indexes"])) { |
871f0a7a AD |
215 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); |
216 | _debug("Type 'yes' to continue."); | |
217 | ||
218 | if (read_stdin() != 'yes') | |
219 | exit; | |
220 | ||
221 | _debug("clearing existing indexes..."); | |
222 | ||
223 | if (DB_TYPE == "pgsql") { | |
6322ac79 | 224 | $result = db_query( "SELECT relname FROM |
871f0a7a AD |
225 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' |
226 | AND relname NOT LIKE '%_pkey' | |
227 | AND relkind = 'i'"); | |
228 | } else { | |
6322ac79 | 229 | $result = db_query( "SELECT index_name,table_name FROM |
871f0a7a AD |
230 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); |
231 | } | |
232 | ||
233 | while ($line = db_fetch_assoc($result)) { | |
234 | if (DB_TYPE == "pgsql") { | |
235 | $statement = "DROP INDEX " . $line["relname"]; | |
236 | _debug($statement); | |
237 | } else { | |
238 | $statement = "ALTER TABLE ". | |
239 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
240 | _debug($statement); | |
241 | } | |
6322ac79 | 242 | db_query( $statement, false); |
871f0a7a AD |
243 | } |
244 | ||
245 | _debug("reading indexes from schema for: " . DB_TYPE); | |
246 | ||
247 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
248 | if ($fp) { | |
249 | while ($line = fgets($fp)) { | |
250 | $matches = array(); | |
251 | ||
252 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
253 | $index = $matches[1]; | |
254 | $table = $matches[2]; | |
255 | ||
256 | $statement = "CREATE INDEX $index ON $table"; | |
257 | ||
258 | _debug($statement); | |
6322ac79 | 259 | db_query( $statement); |
871f0a7a AD |
260 | } |
261 | } | |
262 | fclose($fp); | |
263 | } else { | |
264 | _debug("unable to open schema file."); | |
265 | } | |
266 | _debug("all done."); | |
267 | } | |
268 | ||
764555ff | 269 | if (isset($options["convert-filters"])) { |
6aff7845 AD |
270 | _debug("WARNING: this will remove all existing type2 filters."); |
271 | _debug("Type 'yes' to continue."); | |
272 | ||
273 | if (read_stdin() != 'yes') | |
274 | exit; | |
275 | ||
276 | _debug("converting filters..."); | |
277 | ||
6322ac79 | 278 | db_query( "DELETE FROM ttrss_filters2"); |
6aff7845 | 279 | |
6322ac79 | 280 | $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id"); |
6aff7845 AD |
281 | |
282 | while ($line = db_fetch_assoc($result)) { | |
283 | $owner_uid = $line["owner_uid"]; | |
284 | ||
37f78940 AD |
285 | // date filters are removed |
286 | if ($line["filter_type"] != 5) { | |
287 | $filter = array(); | |
288 | ||
289 | if (sql_bool_to_bool($line["cat_filter"])) { | |
290 | $feed_id = "CAT:" . (int)$line["cat_id"]; | |
291 | } else { | |
292 | $feed_id = (int)$line["feed_id"]; | |
293 | } | |
6aff7845 | 294 | |
37f78940 AD |
295 | $filter["enabled"] = $line["enabled"] ? "on" : "off"; |
296 | $filter["rule"] = array( | |
297 | json_encode(array( | |
298 | "reg_exp" => $line["reg_exp"], | |
299 | "feed_id" => $feed_id, | |
300 | "filter_type" => $line["filter_type"]))); | |
6aff7845 | 301 | |
37f78940 AD |
302 | $filter["action"] = array( |
303 | json_encode(array( | |
304 | "action_id" => $line["action_id"], | |
305 | "action_param_label" => $line["action_param"], | |
306 | "action_param" => $line["action_param"]))); | |
6aff7845 | 307 | |
37f78940 | 308 | // Oh god it's full of hacks |
5451903c | 309 | |
37f78940 AD |
310 | $_REQUEST = $filter; |
311 | $_SESSION["uid"] = $owner_uid; | |
6aff7845 | 312 | |
6322ac79 | 313 | $filters = new Pref_Filters( $_REQUEST); |
37f78940 AD |
314 | $filters->add(); |
315 | } | |
6aff7845 AD |
316 | } |
317 | ||
318 | } | |
319 | ||
b4c47f7e AD |
320 | if (isset($options["update-schema"])) { |
321 | _debug("checking for updates (" . DB_TYPE . ")..."); | |
322 | ||
0630a100 | 323 | $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION); |
b4c47f7e AD |
324 | |
325 | if ($updater->isUpdateRequired()) { | |
326 | _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION); | |
327 | _debug("WARNING: please backup your database before continuing."); | |
328 | _debug("Type 'yes' to continue."); | |
329 | ||
330 | if (read_stdin() != 'yes') | |
331 | exit; | |
332 | ||
333 | for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { | |
334 | _debug("performing update up to version $i..."); | |
335 | ||
336 | $result = $updater->performUpdateTo($i); | |
337 | ||
338 | _debug($result ? "OK!" : "FAILED!"); | |
339 | ||
340 | if (!$result) return; | |
341 | ||
342 | } | |
343 | } else { | |
344 | _debug("update not required."); | |
345 | } | |
346 | ||
347 | } | |
348 | ||
764555ff | 349 | if (isset($options["list-plugins"])) { |
72ff0137 | 350 | $tmppluginhost = new PluginHost(Db::get()); |
d2a421e3 | 351 | $tmppluginhost->load_all($tmppluginhost::KIND_ALL); |
20b86c79 AD |
352 | $enabled = array_map("trim", explode(",", PLUGINS)); |
353 | ||
354 | echo "List of all available plugins:\n"; | |
355 | ||
7a866114 | 356 | foreach ($tmppluginhost->get_plugins() as $name => $plugin) { |
d2a421e3 | 357 | $about = $plugin->about(); |
7a866114 | 358 | |
20b86c79 AD |
359 | $status = $about[3] ? "system" : "user"; |
360 | ||
361 | if (in_array($name, $enabled)) $name .= "*"; | |
362 | ||
363 | printf("%-50s %-10s v%.2f (by %s)\n%s\n\n", | |
364 | $name, $status, $about[0], $about[2], $about[1]); | |
7a866114 | 365 | } |
20b86c79 AD |
366 | |
367 | echo "Plugins marked by * are currently enabled for all users.\n"; | |
368 | ||
7a866114 AD |
369 | } |
370 | ||
764555ff | 371 | $pluginhost->run_commands($options); |
73f28fe9 | 372 | |