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