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