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