]> git.wh0rd.org - tt-rss.git/blob - update.php
add HOOK_UPDATE_TASK
[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 "functions.php";
11 require_once "rssfuncs.php";
12 require_once "sanity_check.php";
13 require_once "config.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 // Create a database connection.
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 init_connection($link);
24
25
26 $op = $argv;
27
28 if (count($argv) == 0 && !defined('STDIN')) {
29 ?> <html>
30 <head>
31 <title>Tiny Tiny RSS data update script.</title>
32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
33 <link rel="stylesheet" type="text/css" href="utility.css">
34 </head>
35
36 <body>
37 <div class="floatingLogo"><img src="images/logo_wide.png"></div>
38 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
39
40 <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
41
42 </body></html>
43 <?php
44 exit;
45 }
46
47 if (count($argv) == 1 || in_array("-help", $op) ) {
48 print "Tiny Tiny RSS data update script.\n\n";
49 print "Options:\n";
50 print " -feeds - update feeds\n";
51 print " -feedbrowser - update feedbrowser\n";
52 print " -daemon - start single-process update daemon\n";
53 print " -cleanup-tags - perform tags table maintenance\n";
54 print " -import USER FILE - import articles from XML\n";
55 print " -quiet - don't show messages\n";
56 print " -indexes - recreate missing schema indexes\n";
57 print " -convert-filters - convert type1 filters to type2\n";
58 print " -force-update - force update of all feeds\n";
59 print " -help - show this help\n";
60 print "Plugin options:\n";
61
62 foreach ($pluginhost->get_commands() as $command => $data) {
63 printf(" %-19s - %s\n", "$command", $data["description"]);
64 }
65
66 return;
67 }
68
69 define('QUIET', in_array("-quiet", $op));
70
71 if (!in_array("-daemon", $op)) {
72 $lock_filename = "update.lock";
73 } else {
74 $lock_filename = "update_daemon.lock";
75 }
76
77 $lock_handle = make_lockfile($lock_filename);
78 $must_exit = false;
79
80 // Try to lock a file in order to avoid concurrent update.
81 if (!$lock_handle) {
82 die("error: Can't create lockfile ($lock_filename). ".
83 "Maybe another update process is already running.\n");
84 }
85
86 if (in_array("-feeds", $op)) {
87 // Update all feeds needing a update.
88 update_daemon_common($link);
89
90 // Update feedbrowser
91 $count = update_feedbrowser_cache($link);
92 _debug("Feedbrowser updated, $count feeds processed.");
93
94 // Purge orphans and cleanup tags
95 purge_orphans($link, true);
96
97 $rc = cleanup_tags($link, 14, 50000);
98 _debug("Cleaned $rc cached tags.");
99
100 global $pluginhost;
101 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
102 }
103
104 if (in_array("-feedbrowser", $op)) {
105 $count = update_feedbrowser_cache($link);
106 print "Finished, $count feeds processed.\n";
107 }
108
109 if (in_array("-daemon", $op)) {
110 $op = array_diff($op, array("-daemon"));
111 while (true) {
112 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
113 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
114 sleep(DAEMON_SLEEP_INTERVAL);
115 }
116 }
117
118 if (in_array("-daemon-loop", $op)) {
119 if (!make_stampfile('update_daemon.stamp')) {
120 die("error: unable to create stampfile\n");
121 }
122
123 // Call to the feed batch update function
124 // or regenerate feedbrowser cache
125
126 if (rand(0,100) > 30) {
127 update_daemon_common($link);
128 } else {
129 $count = update_feedbrowser_cache($link);
130 _debug("Feedbrowser updated, $count feeds processed.");
131
132 purge_orphans($link, true);
133
134 $rc = cleanup_tags($link, 14, 50000);
135
136 _debug("Cleaned $rc cached tags.");
137
138 global $pluginhost;
139 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
140 }
141
142 }
143
144 if (in_array("-cleanup-tags", $op)) {
145 $rc = cleanup_tags($link, 14, 50000);
146 _debug("$rc tags deleted.\n");
147 }
148
149 if (in_array("-import",$op)) {
150 $username = $argv[count($argv) - 2];
151 $filename = $argv[count($argv) - 1];
152
153 if (!$username) {
154 print "error: please specify username.\n";
155 return;
156 }
157
158 if (!is_file($filename)) {
159 print "error: input filename ($filename) doesn't exist.\n";
160 return;
161 }
162
163 _debug("importing $filename for user $username...\n");
164
165 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
166
167 if (db_num_rows($result) == 0) {
168 print "error: could not find user $username.\n";
169 return;
170 }
171
172 $owner_uid = db_fetch_result($result, 0, "id");
173
174 perform_data_import($link, $filename, $owner_uid);
175
176 }
177
178 if (in_array("-indexes", $op)) {
179 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
180 _debug("Type 'yes' to continue.");
181
182 if (read_stdin() != 'yes')
183 exit;
184
185 _debug("clearing existing indexes...");
186
187 if (DB_TYPE == "pgsql") {
188 $result = db_query($link, "SELECT relname FROM
189 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
190 AND relname NOT LIKE '%_pkey'
191 AND relkind = 'i'");
192 } else {
193 $result = db_query($link, "SELECT index_name,table_name FROM
194 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
195 }
196
197 while ($line = db_fetch_assoc($result)) {
198 if (DB_TYPE == "pgsql") {
199 $statement = "DROP INDEX " . $line["relname"];
200 _debug($statement);
201 } else {
202 $statement = "ALTER TABLE ".
203 $line['table_name']." DROP INDEX ".$line['index_name'];
204 _debug($statement);
205 }
206 db_query($link, $statement, false);
207 }
208
209 _debug("reading indexes from schema for: " . DB_TYPE);
210
211 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
212 if ($fp) {
213 while ($line = fgets($fp)) {
214 $matches = array();
215
216 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
217 $index = $matches[1];
218 $table = $matches[2];
219
220 $statement = "CREATE INDEX $index ON $table";
221
222 _debug($statement);
223 db_query($link, $statement);
224 }
225 }
226 fclose($fp);
227 } else {
228 _debug("unable to open schema file.");
229 }
230 _debug("all done.");
231 }
232
233 if (in_array("-convert-filters", $op)) {
234 _debug("WARNING: this will remove all existing type2 filters.");
235 _debug("Type 'yes' to continue.");
236
237 if (read_stdin() != 'yes')
238 exit;
239
240 _debug("converting filters...");
241
242 db_query($link, "DELETE FROM ttrss_filters2");
243
244 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
245
246 while ($line = db_fetch_assoc($result)) {
247 $owner_uid = $line["owner_uid"];
248
249 // date filters are removed
250 if ($line["filter_type"] != 5) {
251 $filter = array();
252
253 if (sql_bool_to_bool($line["cat_filter"])) {
254 $feed_id = "CAT:" . (int)$line["cat_id"];
255 } else {
256 $feed_id = (int)$line["feed_id"];
257 }
258
259 $filter["enabled"] = $line["enabled"] ? "on" : "off";
260 $filter["rule"] = array(
261 json_encode(array(
262 "reg_exp" => $line["reg_exp"],
263 "feed_id" => $feed_id,
264 "filter_type" => $line["filter_type"])));
265
266 $filter["action"] = array(
267 json_encode(array(
268 "action_id" => $line["action_id"],
269 "action_param_label" => $line["action_param"],
270 "action_param" => $line["action_param"])));
271
272 // Oh god it's full of hacks
273
274 $_REQUEST = $filter;
275 $_SESSION["uid"] = $owner_uid;
276
277 $filters = new Pref_Filters($link, $_REQUEST);
278 $filters->add();
279 }
280 }
281
282 }
283
284 if (in_array("-force-update", $op)) {
285 _debug("marking all feeds as needing update...");
286
287 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
288 last_updated = '1970-01-01'");
289 }
290
291 $pluginhost->run_commands($op);
292
293 db_close($link);
294
295 if ($lock_handle != false) {
296 fclose($lock_handle);
297 }
298
299 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
300 unlink(LOCK_DIRECTORY . "/$lock_filename");
301 ?>