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