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