]> git.wh0rd.org - tt-rss.git/blame - update.php
db updates, remove init_connection()
[tt-rss.git] / update.php
CommitLineData
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";
2a649c44 16 require_once "errorhandler.php";
661135c7 17
3de78afd
AD
18 if (!defined('PHP_EXECUTABLE'))
19 define('PHP_EXECUTABLE', '/usr/bin/php');
20
73f28fe9
AD
21 // Create a database connection.
22 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
23
ba68b681 24 init_plugins($link);
73f28fe9 25
764555ff
AD
26 $longopts = array("feeds",
27 "feedbrowser",
28 "daemon",
29 "daemon-loop",
30 "task:",
31 "cleanup-tags",
32 "quiet",
2191eb7a 33 "log:",
764555ff 34 "indexes",
b4c47f7e 35 "update-schema",
764555ff
AD
36 "convert-filters",
37 "force-update",
38 "list-plugins",
39 "help");
40
41 foreach ($pluginhost->get_commands() as $command => $data) {
4cf0f9a9 42 array_push($longopts, $command . $data["suffix"]);
764555ff
AD
43 }
44
45 $options = getopt("", $longopts);
3de78afd 46
764555ff 47 if (count($options) == 0 && !defined('STDIN')) {
366f06f7
AD
48 ?> <html>
49 <head>
3de78afd 50 <title>Tiny Tiny RSS data update script.</title>
366f06f7
AD
51 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
52 <link rel="stylesheet" type="text/css" href="utility.css">
53 </head>
54
55 <body>
884d1650 56 <div class="floatingLogo"><img src="images/logo_small.png"></div>
3de78afd 57 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
366f06f7 58
3de78afd 59 <?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
60
61 </body></html>
62 <?php
63 exit;
64 }
65
764555ff 66 if (count($options) == 0 || isset($options["help"]) ) {
661135c7
AD
67 print "Tiny Tiny RSS data update script.\n\n";
68 print "Options:\n";
764555ff
AD
69 print " --feeds - update feeds\n";
70 print " --feedbrowser - update feedbrowser\n";
71 print " --daemon - start single-process update daemon\n";
72 print " --task N - create lockfile using this task id\n";
73 print " --cleanup-tags - perform tags table maintenance\n";
dc24b520 74 print " --quiet - don't output messages to stdout\n";
2191eb7a 75 print " --log FILE - log messages to FILE\n";
764555ff 76 print " --indexes - recreate missing schema indexes\n";
b4c47f7e 77 print " --update-schema - update database schema\n";
764555ff
AD
78 print " --convert-filters - convert type1 filters to type2\n";
79 print " --force-update - force update of all feeds\n";
80 print " --list-plugins - list all available plugins\n";
81 print " --help - show this help\n";
73f28fe9
AD
82 print "Plugin options:\n";
83
84 foreach ($pluginhost->get_commands() as $command => $data) {
4cf0f9a9
AD
85 $args = $data['arghelp'];
86 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
73f28fe9
AD
87 }
88
661135c7 89 return;
81596c66 90 }
87b9fb65 91
857efe49
AD
92 if (!isset($options['update-schema'])) {
93 $schema_version = get_schema_version($link);
94
95 if ($schema_version != SCHEMA_VERSION) {
96 die("Schema version is wrong, please upgrade the database.\n");
97 }
98 }
99
dc24b520
AD
100 define('QUIET', isset($options['quiet']));
101
2191eb7a
AD
102 if (isset($options["log"])) {
103 _debug("Logging to " . $options["log"]);
104 define('LOGFILE', $options["log"]);
105 }
106
764555ff 107 if (!isset($options["daemon"])) {
661135c7
AD
108 $lock_filename = "update.lock";
109 } else {
110 $lock_filename = "update_daemon.lock";
111 }
fecd57c8 112
764555ff
AD
113 if (isset($options["task"])) {
114 _debug("Using task id " . $options["task"]);
115 $lock_filename = $lock_filename . "-task_" . $options["task"];
116 }
117
661135c7
AD
118 $lock_handle = make_lockfile($lock_filename);
119 $must_exit = false;
fecd57c8 120
661135c7
AD
121 // Try to lock a file in order to avoid concurrent update.
122 if (!$lock_handle) {
123 die("error: Can't create lockfile ($lock_filename). ".
124 "Maybe another update process is already running.\n");
125 }
fecd57c8 126
764555ff
AD
127 if (isset($options["force-update"])) {
128 _debug("marking all feeds as needing update...");
129
130 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
131 last_updated = '1970-01-01'");
132 }
133
134 if (isset($options["feeds"])) {
661135c7
AD
135 // Update all feeds needing a update.
136 update_daemon_common($link);
84e9a8c7
AD
137
138 // Update feedbrowser
139 $count = update_feedbrowser_cache($link);
140 _debug("Feedbrowser updated, $count feeds processed.");
141
142 // Purge orphans and cleanup tags
143 purge_orphans($link, true);
144
145 $rc = cleanup_tags($link, 14, 50000);
146 _debug("Cleaned $rc cached tags.");
f32eb194 147
41b82aa4
AD
148 global $pluginhost;
149 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
661135c7 150 }
fecd57c8 151
764555ff 152 if (isset($options["feedbrowser"])) {
661135c7
AD
153 $count = update_feedbrowser_cache($link);
154 print "Finished, $count feeds processed.\n";
fecd57c8 155 }
661135c7 156
764555ff 157 if (isset($options["daemon"])) {
661135c7 158 while (true) {
0ae9f746
AD
159 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
160
161 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet");
661135c7
AD
162 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
163 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 164 }
81596c66 165 }
9e21a571 166
764555ff 167 if (isset($options["daemon-loop"])) {
661135c7 168 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 169 _debug("warning: unable to create stampfile\n");
661135c7 170 }
9e21a571 171
dbaa4e4a 172 // Call to the feed batch update function
661135c7 173 // or regenerate feedbrowser cache
9e21a571 174
661135c7
AD
175 if (rand(0,100) > 30) {
176 update_daemon_common($link);
9e21a571 177 } else {
661135c7 178 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
179 _debug("Feedbrowser updated, $count feeds processed.");
180
181 purge_orphans($link, true);
dbaa4e4a 182
e3b42c5a
AD
183 $rc = cleanup_tags($link, 14, 50000);
184
185 _debug("Cleaned $rc cached tags.");
f32eb194 186
41b82aa4
AD
187 global $pluginhost;
188 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
9e21a571 189 }
ef59e6e8 190
fecd57c8 191 }
fecd57c8 192
764555ff 193 if (isset($options["cleanup-tags"])) {
868650e4 194 $rc = cleanup_tags($link, 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") {
208 $result = db_query($link, "SELECT relname FROM
209 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
210 AND relname NOT LIKE '%_pkey'
211 AND relkind = 'i'");
212 } else {
213 $result = db_query($link, "SELECT index_name,table_name FROM
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 }
226 db_query($link, $statement, false);
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);
243 db_query($link, $statement);
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
262 db_query($link, "DELETE FROM ttrss_filters2");
263
264 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
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
37f78940
AD
297 $filters = new Pref_Filters($link, $_REQUEST);
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
307 $updater = new DbUpdater($link, DB_TYPE, SCHEMA_VERSION);
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"])) {
7a866114 334 $tmppluginhost = new PluginHost($link);
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
764555ff 355 $pluginhost->run_commands($options);
73f28fe9 356
661135c7 357 db_close($link);
107d0cf3 358