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