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