]>
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 "config.php"; | |
13 | require_once "sanity_check.php"; | |
14 | require_once "db.php"; | |
15 | require_once "db-prefs.php"; | |
16 | ||
17 | if (!defined('PHP_EXECUTABLE')) | |
18 | define('PHP_EXECUTABLE', '/usr/bin/php'); | |
19 | ||
20 | $pdo = Db::pdo(); | |
21 | ||
22 | init_plugins(); | |
23 | ||
24 | $longopts = array("feeds", | |
25 | "feedbrowser", | |
26 | "daemon", | |
27 | "daemon-loop", | |
28 | "task:", | |
29 | "cleanup-tags", | |
30 | "quiet", | |
31 | "log:", | |
32 | "indexes", | |
33 | "pidlock:", | |
34 | "update-schema", | |
35 | "convert-filters", | |
36 | "force-update", | |
37 | "gen-search-idx", | |
38 | "list-plugins", | |
39 | "debug-feed:", | |
40 | "force-refetch", | |
41 | "force-rehash", | |
42 | "help"); | |
43 | ||
44 | foreach (PluginHost::getInstance()->get_commands() as $command => $data) { | |
45 | array_push($longopts, $command . $data["suffix"]); | |
46 | } | |
47 | ||
48 | $options = getopt("", $longopts); | |
49 | ||
50 | if (!is_array($options)) { | |
51 | die("error: getopt() failed. ". | |
52 | "Most probably you are using PHP CGI to run this script ". | |
53 | "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ". | |
54 | "additional information.\n"); | |
55 | } | |
56 | ||
57 | if (count($options) == 0 && !defined('STDIN')) { | |
58 | ?> <html> | |
59 | <head> | |
60 | <title>Tiny Tiny RSS data update script.</title> | |
61 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
62 | </head> | |
63 | ||
64 | <body> | |
65 | <div class="floatingLogo"><img src="images/logo_small.png"></div> | |
66 | <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1> | |
67 | ||
68 | <?php print_error("Please run this script from the command line. Use option \"--help\" to display command help if this error is displayed erroneously."); ?> | |
69 | ||
70 | </body></html> | |
71 | <?php | |
72 | exit; | |
73 | } | |
74 | ||
75 | if (count($options) == 0 || isset($options["help"]) ) { | |
76 | print "Tiny Tiny RSS data update script.\n\n"; | |
77 | print "Options:\n"; | |
78 | print " --feeds - update feeds\n"; | |
79 | print " --feedbrowser - update feedbrowser\n"; | |
80 | print " --daemon - start single-process update daemon\n"; | |
81 | print " --task N - create lockfile using this task id\n"; | |
82 | print " --cleanup-tags - perform tags table maintenance\n"; | |
83 | print " --quiet - don't output messages to stdout\n"; | |
84 | print " --log FILE - log messages to FILE\n"; | |
85 | print " --indexes - recreate missing schema indexes\n"; | |
86 | print " --update-schema - update database schema\n"; | |
87 | print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n"; | |
88 | print " --convert-filters - convert type1 filters to type2\n"; | |
89 | print " --force-update - force update of all feeds\n"; | |
90 | print " --list-plugins - list all available plugins\n"; | |
91 | print " --debug-feed N - perform debug update of feed N\n"; | |
92 | print " --force-refetch - debug update: force refetch feed data\n"; | |
93 | print " --force-rehash - debug update: force rehash articles\n"; | |
94 | print " --help - show this help\n"; | |
95 | print "Plugin options:\n"; | |
96 | ||
97 | foreach (PluginHost::getInstance()->get_commands() as $command => $data) { | |
98 | $args = $data['arghelp']; | |
99 | printf(" --%-19s - %s\n", "$command $args", $data["description"]); | |
100 | } | |
101 | ||
102 | return; | |
103 | } | |
104 | ||
105 | if (!isset($options['daemon'])) { | |
106 | require_once "errorhandler.php"; | |
107 | } | |
108 | ||
109 | if (!isset($options['update-schema'])) { | |
110 | $schema_version = get_schema_version(); | |
111 | ||
112 | if ($schema_version != SCHEMA_VERSION) { | |
113 | die("Schema version is wrong, please upgrade the database.\n"); | |
114 | } | |
115 | } | |
116 | ||
117 | define('QUIET', isset($options['quiet'])); | |
118 | ||
119 | if (isset($options["log"])) { | |
120 | _debug("Logging to " . $options["log"]); | |
121 | define('LOGFILE', $options["log"]); | |
122 | } | |
123 | ||
124 | if (!isset($options["daemon"])) { | |
125 | $lock_filename = "update.lock"; | |
126 | } else { | |
127 | $lock_filename = "update_daemon.lock"; | |
128 | } | |
129 | ||
130 | if (isset($options["task"])) { | |
131 | _debug("Using task id " . $options["task"]); | |
132 | $lock_filename = $lock_filename . "-task_" . $options["task"]; | |
133 | } | |
134 | ||
135 | if (isset($options["pidlock"])) { | |
136 | $my_pid = $options["pidlock"]; | |
137 | $lock_filename = "update_daemon-$my_pid.lock"; | |
138 | ||
139 | } | |
140 | ||
141 | _debug("Lock: $lock_filename"); | |
142 | ||
143 | $lock_handle = make_lockfile($lock_filename); | |
144 | $must_exit = false; | |
145 | ||
146 | if (isset($options["task"]) && isset($options["pidlock"])) { | |
147 | $waits = $options["task"] * 5; | |
148 | _debug("Waiting before update ($waits)"); | |
149 | sleep($waits); | |
150 | } | |
151 | ||
152 | // Try to lock a file in order to avoid concurrent update. | |
153 | if (!$lock_handle) { | |
154 | die("error: Can't create lockfile ($lock_filename). ". | |
155 | "Maybe another update process is already running.\n"); | |
156 | } | |
157 | ||
158 | if (isset($options["force-update"])) { | |
159 | _debug("marking all feeds as needing update..."); | |
160 | ||
161 | $pdo->query( "UPDATE ttrss_feeds SET | |
162 | last_update_started = '1970-01-01', last_updated = '1970-01-01'"); | |
163 | } | |
164 | ||
165 | if (isset($options["feeds"])) { | |
166 | RSSUtils::update_daemon_common(); | |
167 | RSSUtils::housekeeping_common(true); | |
168 | ||
169 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
170 | } | |
171 | ||
172 | if (isset($options["feedbrowser"])) { | |
173 | $count = RSSUtils::update_feedbrowser_cache(); | |
174 | print "Finished, $count feeds processed.\n"; | |
175 | } | |
176 | ||
177 | if (isset($options["daemon"])) { | |
178 | while (true) { | |
179 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; | |
180 | $log = isset($options['log']) ? '--log '.$options['log'] : ''; | |
181 | ||
182 | passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log"); | |
183 | ||
184 | // let's enforce a minimum spawn interval as to not forkbomb the host | |
185 | $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL); | |
186 | ||
187 | _debug("Sleeping for $spawn_interval seconds..."); | |
188 | sleep($spawn_interval); | |
189 | } | |
190 | } | |
191 | ||
192 | if (isset($options["daemon-loop"])) { | |
193 | if (!make_stampfile('update_daemon.stamp')) { | |
194 | _debug("warning: unable to create stampfile\n"); | |
195 | } | |
196 | ||
197 | RSSUtils::update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT); | |
198 | ||
199 | if (!isset($options["pidlock"]) || $options["task"] == 0) | |
200 | RSSUtils::housekeeping_common(true); | |
201 | ||
202 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
203 | } | |
204 | ||
205 | if (isset($options["cleanup-tags"])) { | |
206 | $rc = cleanup_tags( 14, 50000); | |
207 | _debug("$rc tags deleted.\n"); | |
208 | } | |
209 | ||
210 | if (isset($options["indexes"])) { | |
211 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); | |
212 | _debug("Type 'yes' to continue."); | |
213 | ||
214 | if (read_stdin() != 'yes') | |
215 | exit; | |
216 | ||
217 | _debug("clearing existing indexes..."); | |
218 | ||
219 | if (DB_TYPE == "pgsql") { | |
220 | $sth = $pdo->query( "SELECT relname FROM | |
221 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' | |
222 | AND relname NOT LIKE '%_pkey' | |
223 | AND relkind = 'i'"); | |
224 | } else { | |
225 | $sth = $pdo->query( "SELECT index_name,table_name FROM | |
226 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); | |
227 | } | |
228 | ||
229 | while ($line = $sth->fetch()) { | |
230 | if (DB_TYPE == "pgsql") { | |
231 | $statement = "DROP INDEX " . $line["relname"]; | |
232 | _debug($statement); | |
233 | } else { | |
234 | $statement = "ALTER TABLE ". | |
235 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
236 | _debug($statement); | |
237 | } | |
238 | $pdo->query($statement); | |
239 | } | |
240 | ||
241 | _debug("reading indexes from schema for: " . DB_TYPE); | |
242 | ||
243 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
244 | if ($fp) { | |
245 | while ($line = fgets($fp)) { | |
246 | $matches = array(); | |
247 | ||
248 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
249 | $index = $matches[1]; | |
250 | $table = $matches[2]; | |
251 | ||
252 | $statement = "CREATE INDEX $index ON $table"; | |
253 | ||
254 | _debug($statement); | |
255 | $pdo->query($statement); | |
256 | } | |
257 | } | |
258 | fclose($fp); | |
259 | } else { | |
260 | _debug("unable to open schema file."); | |
261 | } | |
262 | _debug("all done."); | |
263 | } | |
264 | ||
265 | if (isset($options["convert-filters"])) { | |
266 | _debug("WARNING: this will remove all existing type2 filters."); | |
267 | _debug("Type 'yes' to continue."); | |
268 | ||
269 | if (read_stdin() != 'yes') | |
270 | exit; | |
271 | ||
272 | _debug("converting filters..."); | |
273 | ||
274 | $pdo->query("DELETE FROM ttrss_filters2"); | |
275 | ||
276 | $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id"); | |
277 | ||
278 | while ($line = $res->fetch()) { | |
279 | $owner_uid = $line["owner_uid"]; | |
280 | ||
281 | // date filters are removed | |
282 | if ($line["filter_type"] != 5) { | |
283 | $filter = array(); | |
284 | ||
285 | if (sql_bool_to_bool($line["cat_filter"])) { | |
286 | $feed_id = "CAT:" . (int)$line["cat_id"]; | |
287 | } else { | |
288 | $feed_id = (int)$line["feed_id"]; | |
289 | } | |
290 | ||
291 | $filter["enabled"] = $line["enabled"] ? "on" : "off"; | |
292 | $filter["rule"] = array( | |
293 | json_encode(array( | |
294 | "reg_exp" => $line["reg_exp"], | |
295 | "feed_id" => $feed_id, | |
296 | "filter_type" => $line["filter_type"]))); | |
297 | ||
298 | $filter["action"] = array( | |
299 | json_encode(array( | |
300 | "action_id" => $line["action_id"], | |
301 | "action_param_label" => $line["action_param"], | |
302 | "action_param" => $line["action_param"]))); | |
303 | ||
304 | // Oh god it's full of hacks | |
305 | ||
306 | $_REQUEST = $filter; | |
307 | $_SESSION["uid"] = $owner_uid; | |
308 | ||
309 | $filters = new Pref_Filters($_REQUEST); | |
310 | $filters->add(); | |
311 | } | |
312 | } | |
313 | ||
314 | } | |
315 | ||
316 | if (isset($options["update-schema"])) { | |
317 | _debug("checking for updates (" . DB_TYPE . ")..."); | |
318 | ||
319 | $updater = new DbUpdater(Db::pdo(), DB_TYPE, SCHEMA_VERSION); | |
320 | ||
321 | if ($updater->isUpdateRequired()) { | |
322 | _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION); | |
323 | _debug("WARNING: please backup your database before continuing."); | |
324 | _debug("Type 'yes' to continue."); | |
325 | ||
326 | if (read_stdin() != 'yes') | |
327 | exit; | |
328 | ||
329 | for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { | |
330 | _debug("performing update up to version $i..."); | |
331 | ||
332 | $result = $updater->performUpdateTo($i, false); | |
333 | ||
334 | _debug($result ? "OK!" : "FAILED!"); | |
335 | ||
336 | if (!$result) return; | |
337 | ||
338 | } | |
339 | } else { | |
340 | _debug("update not required."); | |
341 | } | |
342 | ||
343 | } | |
344 | ||
345 | if (isset($options["gen-search-idx"])) { | |
346 | echo "Generating search index (stemming set to English)...\n"; | |
347 | ||
348 | $res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL"); | |
349 | $row = $res->fetch(); | |
350 | $count = $row['count']; | |
351 | ||
352 | print "Articles to process: $count.\n"; | |
353 | ||
354 | $limit = 500; | |
355 | $processed = 0; | |
356 | ||
357 | $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE | |
358 | tsvector_combined IS NULL ORDER BY id LIMIT ?"); | |
359 | $sth->execute([$limit]); | |
360 | ||
361 | $usth = $pdo->prepare("UPDATE ttrss_entries | |
362 | SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?"); | |
363 | ||
364 | while (true) { | |
365 | ||
366 | while ($line = $sth->fetch()) { | |
367 | $tsvector_combined = mb_substr(strip_tags($line["title"] . " " . $line["content"]), 0, 1000000); | |
368 | ||
369 | $usth->execute([$tsvector_combined, $line['id']]); | |
370 | ||
371 | $processed++; | |
372 | } | |
373 | ||
374 | print "Processed $processed articles...\n"; | |
375 | ||
376 | if ($processed < $limit) { | |
377 | echo "All done.\n"; | |
378 | break; | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | if (isset($options["list-plugins"])) { | |
384 | $tmppluginhost = new PluginHost(); | |
385 | $tmppluginhost->load_all($tmppluginhost::KIND_ALL, false); | |
386 | $enabled = array_map("trim", explode(",", PLUGINS)); | |
387 | ||
388 | echo "List of all available plugins:\n"; | |
389 | ||
390 | foreach ($tmppluginhost->get_plugins() as $name => $plugin) { | |
391 | $about = $plugin->about(); | |
392 | ||
393 | $status = $about[3] ? "system" : "user"; | |
394 | ||
395 | if (in_array($name, $enabled)) $name .= "*"; | |
396 | ||
397 | printf("%-50s %-10s v%.2f (by %s)\n%s\n\n", | |
398 | $name, $status, $about[0], $about[2], $about[1]); | |
399 | } | |
400 | ||
401 | echo "Plugins marked by * are currently enabled for all users.\n"; | |
402 | ||
403 | } | |
404 | ||
405 | if (isset($options["debug-feed"])) { | |
406 | $feed = $options["debug-feed"]; | |
407 | ||
408 | if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true; | |
409 | if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true; | |
410 | ||
411 | $_REQUEST['xdebug'] = 1; | |
412 | ||
413 | $rc = RSSUtils::update_rss_feed($feed) != false ? 0 : 1; | |
414 | ||
415 | exit($rc); | |
416 | } | |
417 | ||
418 | PluginHost::getInstance()->run_commands($options); | |
419 | ||
420 | if (file_exists(LOCK_DIRECTORY . "/$lock_filename")) | |
421 | if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') | |
422 | fclose($lock_handle); | |
423 | unlink(LOCK_DIRECTORY . "/$lock_filename"); | |
424 | ?> |