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