]>
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"; |
661135c7 | 16 | |
86268d8b AD |
17 | if (!defined('PHP_EXECUTABLE')) |
18 | define('PHP_EXECUTABLE', '/usr/bin/php'); | |
19 | ||
5439d333 | 20 | $op = $argv; |
70dcff6b | 21 | |
5439d333 | 22 | if (count($argv) == 1 || in_array("-help", $op) ) { |
661135c7 AD |
23 | print "Tiny Tiny RSS data update script.\n\n"; |
24 | print "Options:\n"; | |
55f34b81 AD |
25 | print " -feeds - update feeds\n"; |
26 | print " -feedbrowser - update feedbrowser\n"; | |
27 | print " -daemon - start single-process update daemon\n"; | |
28 | print " -cleanup-tags - perform tags table maintenance\n"; | |
29 | print " -get-feeds - receive popular feeds from linked instances\n"; | |
30 | print " -import USER FILE - import articles from XML\n"; | |
e3449aa1 | 31 | print " -update-self - update tt-rss installation to latest version\n"; |
5439d333 | 32 | print " -quiet - don't show messages\n"; |
9a57512c | 33 | print " -indexes - recreate missing schema indexes\n"; |
55f34b81 | 34 | print " -help - show this help\n"; |
661135c7 | 35 | return; |
81596c66 | 36 | } |
87b9fb65 | 37 | |
5439d333 RW |
38 | define('QUIET', in_array("-quiet", $op)); |
39 | ||
40 | if (!in_array("-daemon", $op)) { | |
661135c7 AD |
41 | $lock_filename = "update.lock"; |
42 | } else { | |
43 | $lock_filename = "update_daemon.lock"; | |
44 | } | |
fecd57c8 | 45 | |
661135c7 AD |
46 | $lock_handle = make_lockfile($lock_filename); |
47 | $must_exit = false; | |
fecd57c8 | 48 | |
661135c7 AD |
49 | // Try to lock a file in order to avoid concurrent update. |
50 | if (!$lock_handle) { | |
51 | die("error: Can't create lockfile ($lock_filename). ". | |
52 | "Maybe another update process is already running.\n"); | |
53 | } | |
fecd57c8 | 54 | |
661135c7 | 55 | // Create a database connection. |
dbaa4e4a | 56 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
fecd57c8 | 57 | |
661135c7 | 58 | init_connection($link); |
ef59e6e8 | 59 | |
5439d333 | 60 | if (in_array("-feeds", $op)) { |
661135c7 AD |
61 | // Update all feeds needing a update. |
62 | update_daemon_common($link); | |
84e9a8c7 AD |
63 | |
64 | // Update feedbrowser | |
65 | $count = update_feedbrowser_cache($link); | |
66 | _debug("Feedbrowser updated, $count feeds processed."); | |
67 | ||
68 | // Purge orphans and cleanup tags | |
69 | purge_orphans($link, true); | |
70 | ||
71 | $rc = cleanup_tags($link, 14, 50000); | |
72 | _debug("Cleaned $rc cached tags."); | |
f32eb194 AD |
73 | |
74 | get_linked_feeds($link); | |
661135c7 | 75 | } |
fecd57c8 | 76 | |
5439d333 | 77 | if (in_array("-feedbrowser", $op)) { |
661135c7 AD |
78 | $count = update_feedbrowser_cache($link); |
79 | print "Finished, $count feeds processed.\n"; | |
fecd57c8 | 80 | } |
661135c7 | 81 | |
5439d333 RW |
82 | if (in_array("-daemon", $op)) { |
83 | $op = array_diff($op, array("-daemon")); | |
661135c7 | 84 | while (true) { |
5439d333 | 85 | passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop"); |
661135c7 AD |
86 | _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); |
87 | sleep(DAEMON_SLEEP_INTERVAL); | |
fecd57c8 | 88 | } |
81596c66 | 89 | } |
9e21a571 | 90 | |
5439d333 | 91 | if (in_array("-daemon-loop", $op)) { |
661135c7 AD |
92 | if (!make_stampfile('update_daemon.stamp')) { |
93 | die("error: unable to create stampfile\n"); | |
94 | } | |
9e21a571 | 95 | |
dbaa4e4a | 96 | // Call to the feed batch update function |
661135c7 | 97 | // or regenerate feedbrowser cache |
9e21a571 | 98 | |
661135c7 AD |
99 | if (rand(0,100) > 30) { |
100 | update_daemon_common($link); | |
9e21a571 | 101 | } else { |
661135c7 | 102 | $count = update_feedbrowser_cache($link); |
e3b42c5a AD |
103 | _debug("Feedbrowser updated, $count feeds processed."); |
104 | ||
105 | purge_orphans($link, true); | |
dbaa4e4a | 106 | |
e3b42c5a AD |
107 | $rc = cleanup_tags($link, 14, 50000); |
108 | ||
109 | _debug("Cleaned $rc cached tags."); | |
f32eb194 AD |
110 | |
111 | get_linked_feeds($link); | |
9e21a571 | 112 | } |
ef59e6e8 | 113 | |
fecd57c8 | 114 | } |
fecd57c8 | 115 | |
5439d333 | 116 | if (in_array("-cleanup-tags", $op)) { |
868650e4 | 117 | $rc = cleanup_tags($link, 14, 50000); |
5439d333 | 118 | _debug("$rc tags deleted.\n"); |
868650e4 AD |
119 | } |
120 | ||
5439d333 | 121 | if (in_array("-get-feeds", $op)) { |
ae5f7bb1 AD |
122 | get_linked_feeds($link); |
123 | } | |
124 | ||
5439d333 RW |
125 | if (in_array("-import",$op)) { |
126 | $username = $argv[count($argv) - 2]; | |
127 | $filename = $argv[count($argv) - 1]; | |
55f34b81 AD |
128 | |
129 | if (!$username) { | |
130 | print "error: please specify username.\n"; | |
131 | return; | |
132 | } | |
133 | ||
134 | if (!is_file($filename)) { | |
135 | print "error: input filename ($filename) doesn't exist.\n"; | |
136 | return; | |
137 | } | |
138 | ||
5439d333 | 139 | _debug("importing $filename for user $username...\n"); |
55f34b81 AD |
140 | |
141 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'"); | |
142 | ||
143 | if (db_num_rows($result) == 0) { | |
144 | print "error: could not find user $username.\n"; | |
145 | return; | |
146 | } | |
147 | ||
148 | $owner_uid = db_fetch_result($result, 0, "id"); | |
149 | ||
150 | perform_data_import($link, $filename, $owner_uid); | |
151 | ||
152 | } | |
153 | ||
871f0a7a AD |
154 | if (in_array("-indexes", $op)) { |
155 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); | |
156 | _debug("Type 'yes' to continue."); | |
157 | ||
158 | if (read_stdin() != 'yes') | |
159 | exit; | |
160 | ||
161 | _debug("clearing existing indexes..."); | |
162 | ||
163 | if (DB_TYPE == "pgsql") { | |
164 | $result = db_query($link, "SELECT relname FROM | |
165 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' | |
166 | AND relname NOT LIKE '%_pkey' | |
167 | AND relkind = 'i'"); | |
168 | } else { | |
169 | $result = db_query($link, "SELECT index_name,table_name FROM | |
170 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); | |
171 | } | |
172 | ||
173 | while ($line = db_fetch_assoc($result)) { | |
174 | if (DB_TYPE == "pgsql") { | |
175 | $statement = "DROP INDEX " . $line["relname"]; | |
176 | _debug($statement); | |
177 | } else { | |
178 | $statement = "ALTER TABLE ". | |
179 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
180 | _debug($statement); | |
181 | } | |
182 | db_query($link, $statement, false); | |
183 | } | |
184 | ||
185 | _debug("reading indexes from schema for: " . DB_TYPE); | |
186 | ||
187 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
188 | if ($fp) { | |
189 | while ($line = fgets($fp)) { | |
190 | $matches = array(); | |
191 | ||
192 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
193 | $index = $matches[1]; | |
194 | $table = $matches[2]; | |
195 | ||
196 | $statement = "CREATE INDEX $index ON $table"; | |
197 | ||
198 | _debug($statement); | |
199 | db_query($link, $statement); | |
200 | } | |
201 | } | |
202 | fclose($fp); | |
203 | } else { | |
204 | _debug("unable to open schema file."); | |
205 | } | |
206 | _debug("all done."); | |
207 | } | |
208 | ||
e3449aa1 AD |
209 | if (in_array("-update-self", $op)) { |
210 | _debug("Warning: self-updating is experimental. Use at your own risk."); | |
211 | _debug("Please backup your tt-rss directory before continuing. Your database will not be modified."); | |
212 | _debug("Type 'yes' to continue."); | |
213 | ||
214 | if (read_stdin() != 'yes') | |
215 | exit; | |
216 | ||
217 | $work_dir = dirname(__FILE__); | |
218 | $parent_dir = dirname($work_dir); | |
219 | ||
220 | if (!is_writable($work_dir) && !is_writable("$parent_dir")) { | |
221 | _debug("Both current and parent directories should be writable as current user."); | |
222 | exit; | |
223 | } | |
224 | ||
225 | if (!is_writable(sys_get_temp_dir())) { | |
226 | _debug("System temporary directory should be writable as current user."); | |
227 | exit; | |
228 | } | |
229 | ||
230 | _debug("Checking for tar..."); | |
231 | ||
232 | $system_rc = 0; | |
233 | system("tar --version >/dev/null", $system_rc); | |
234 | ||
235 | if ($system_rc != 0) { | |
236 | _debug("Could not run tar executable (RC=$system_rc)."); | |
237 | exit; | |
238 | } | |
239 | ||
240 | _debug("Checking for latest version..."); | |
241 | ||
242 | $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"), | |
243 | true); | |
244 | ||
245 | if (!is_array($version_info)) { | |
246 | _debug("Unable to fetch version information."); | |
247 | exit; | |
248 | } | |
249 | ||
250 | $target_version = $version_info["version"]; | |
251 | $target_dir = "$parent_dir/tt-rss-$target_version"; | |
252 | ||
253 | _debug("Target version: $target_version"); | |
254 | ||
255 | if (version_compare(VERSION, $target_version) != -1 && !in_array("-force", $op)) { | |
256 | _debug("You are on latest version. Update not needed."); | |
257 | exit; | |
258 | } | |
259 | if (file_exists($target_dir)) { | |
260 | _debug("Target directory $target_dir already exists."); | |
261 | exit; | |
262 | } | |
263 | ||
264 | _debug("Downloading checksums..."); | |
265 | $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt"); | |
266 | ||
267 | if (!$md5sum_data) { | |
268 | _debug("Could not download checksums."); | |
269 | exit; | |
270 | } | |
271 | ||
272 | $md5sum_data = explode("\n", $md5sum_data); | |
273 | ||
274 | $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz"; | |
275 | $data = fetch_file_contents($tarball_url); | |
276 | ||
277 | if (!$data) { | |
278 | _debug("Could not download distribution tarball ($tarball_url)."); | |
279 | exit; | |
280 | } | |
281 | ||
282 | _debug("Verifying tarball checksum..."); | |
283 | ||
284 | $target_md5sum = false; | |
285 | ||
286 | foreach ($md5sum_data as $line) { | |
287 | $pair = explode(" ", $line); | |
288 | ||
289 | if ($pair[1] == "tt-rss-$target_version.tar.gz") { | |
290 | $target_md5sum = $pair[0]; | |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
295 | if (!$target_md5sum) { | |
296 | _debug("Unable to locate checksum for target version."); | |
297 | exit; | |
298 | } | |
299 | ||
300 | $test_md5sum = md5($data); | |
301 | ||
302 | if ($test_md5sum != $target_md5sum) { | |
303 | _debug("Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum)."); | |
304 | exit; | |
305 | } | |
306 | ||
307 | $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss'); | |
308 | _debug("Saving download to $tmp_file"); | |
309 | ||
310 | if (!file_put_contents($tmp_file, $data)) { | |
311 | _debug("Unable to save download."); | |
312 | exit; | |
313 | } | |
314 | ||
315 | if (!chdir($parent_dir)) { | |
316 | _debug("Unable to change into parent directory."); | |
317 | exit; | |
318 | } | |
319 | ||
320 | $old_dir = tmpdirname($parent_dir, "tt-rss-old"); | |
321 | ||
322 | _debug("Renaming current directory to ".basename($old_dir)); | |
323 | if (!rename($work_dir, $old_dir)) { | |
324 | _debug("Unable to rename current directory."); | |
325 | exit; | |
326 | } | |
327 | ||
328 | _debug("Extracting tarball..."); | |
329 | system("tar zxf $tmp_file", $system_rc); | |
330 | ||
331 | if ($system_rc != 0) { | |
332 | _debug("Error while extracting tarball (RC=$system_rc)."); | |
333 | exit; | |
334 | } | |
335 | ||
336 | _debug("Renaming target directory..."); | |
337 | if (!rename($target_dir, $work_dir)) { | |
338 | _debug("Unable to rename target directory."); | |
339 | exit; | |
340 | } | |
341 | ||
342 | chdir($work_dir); | |
343 | ||
344 | _debug("Copying config.php..."); | |
345 | if (!copy("$old_dir/config.php", "$work_dir/config.php")) { | |
346 | _debug("Unable to copy config.php to $work_dir."); | |
347 | exit; | |
348 | } | |
349 | ||
350 | _debug("Cleaning up..."); | |
351 | unlink($tmp_file); | |
352 | ||
353 | _debug("Fixing permissions..."); | |
354 | ||
355 | $directories = array( | |
356 | CACHE_DIR, | |
357 | CACHE_DIR . "/htmlpurifier", | |
358 | CACHE_DIR . "/export", | |
359 | CACHE_DIR . "/images", | |
360 | CACHE_DIR . "/magpie", | |
361 | CACHE_DIR . "/simplepie", | |
362 | ICONS_DIR, | |
363 | LOCK_DIRECTORY); | |
364 | ||
365 | foreach ($directories as $dir) { | |
366 | _debug("-> $dir"); | |
367 | chmod($dir, 0777); | |
368 | } | |
369 | ||
370 | _debug("Upgrade completed."); | |
371 | _debug("Your old tt-rss directory is saved at $old_dir. ". | |
372 | "Please migrate locally modified files (if any) and remove it."); | |
373 | _debug("You might need to re-enter current directory in shell to see new files."); | |
374 | } | |
375 | ||
661135c7 | 376 | db_close($link); |
107d0cf3 | 377 | |