]>
Commit | Line | Data |
---|---|---|
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 "autoload.php"; | |
11 | require_once "functions.php"; | |
12 | require_once "rssfuncs.php"; | |
13 | require_once "config.php"; | |
14 | require_once "sanity_check.php"; | |
15 | require_once "db.php"; | |
16 | require_once "db-prefs.php"; | |
17 | ||
18 | if (!defined('PHP_EXECUTABLE')) | |
19 | define('PHP_EXECUTABLE', '/usr/bin/php'); | |
20 | ||
21 | init_plugins(); | |
22 | ||
23 | $longopts = array("feeds", | |
24 | "feedbrowser", | |
25 | "daemon", | |
26 | "daemon-loop", | |
27 | "task:", | |
28 | "cleanup-tags", | |
29 | "quiet", | |
30 | "log:", | |
31 | "indexes", | |
32 | "pidlock:", | |
33 | "update-schema", | |
34 | "convert-filters", | |
35 | "force-update", | |
36 | "list-plugins", | |
37 | "help"); | |
38 | ||
39 | foreach (PluginHost::getInstance()->get_commands() as $command => $data) { | |
40 | array_push($longopts, $command . $data["suffix"]); | |
41 | } | |
42 | ||
43 | $options = getopt("", $longopts); | |
44 | ||
45 | if (count($options) == 0 && !defined('STDIN')) { | |
46 | ?> <html> | |
47 | <head> | |
48 | <title>Tiny Tiny RSS data update script.</title> | |
49 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
50 | <link rel="stylesheet" type="text/css" href="css/utility.css"> | |
51 | </head> | |
52 | ||
53 | <body> | |
54 | <div class="floatingLogo"><img src="images/logo_small.png"></div> | |
55 | <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1> | |
56 | ||
57 | <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?> | |
58 | ||
59 | </body></html> | |
60 | <?php | |
61 | exit; | |
62 | } | |
63 | ||
64 | if (count($options) == 0 || isset($options["help"]) ) { | |
65 | print "Tiny Tiny RSS data update script.\n\n"; | |
66 | print "Options:\n"; | |
67 | print " --feeds - update feeds\n"; | |
68 | print " --feedbrowser - update feedbrowser\n"; | |
69 | print " --daemon - start single-process update daemon\n"; | |
70 | print " --task N - create lockfile using this task id\n"; | |
71 | print " --cleanup-tags - perform tags table maintenance\n"; | |
72 | print " --quiet - don't output messages to stdout\n"; | |
73 | print " --log FILE - log messages to FILE\n"; | |
74 | print " --indexes - recreate missing schema indexes\n"; | |
75 | print " --update-schema - update database schema\n"; | |
76 | print " --convert-filters - convert type1 filters to type2\n"; | |
77 | print " --force-update - force update of all feeds\n"; | |
78 | print " --list-plugins - list all available plugins\n"; | |
79 | print " --help - show this help\n"; | |
80 | print "Plugin options:\n"; | |
81 | ||
82 | foreach (PluginHost::getInstance()->get_commands() as $command => $data) { | |
83 | $args = $data['arghelp']; | |
84 | printf(" --%-19s - %s\n", "$command $args", $data["description"]); | |
85 | } | |
86 | ||
87 | return; | |
88 | } | |
89 | ||
90 | if (!isset($options['daemon'])) { | |
91 | require_once "errorhandler.php"; | |
92 | } | |
93 | ||
94 | if (!isset($options['update-schema'])) { | |
95 | $schema_version = get_schema_version(); | |
96 | ||
97 | if ($schema_version != SCHEMA_VERSION) { | |
98 | die("Schema version is wrong, please upgrade the database.\n"); | |
99 | } | |
100 | } | |
101 | ||
102 | define('QUIET', isset($options['quiet'])); | |
103 | ||
104 | if (isset($options["log"])) { | |
105 | _debug("Logging to " . $options["log"]); | |
106 | define('LOGFILE', $options["log"]); | |
107 | } | |
108 | ||
109 | if (!isset($options["daemon"])) { | |
110 | $lock_filename = "update.lock"; | |
111 | } else { | |
112 | $lock_filename = "update_daemon.lock"; | |
113 | } | |
114 | ||
115 | if (isset($options["task"])) { | |
116 | _debug("Using task id " . $options["task"]); | |
117 | $lock_filename = $lock_filename . "-task_" . $options["task"]; | |
118 | } | |
119 | ||
120 | if (isset($options["pidlock"])) { | |
121 | $my_pid = $options["pidlock"]; | |
122 | $lock_filename = "update_daemon-$my_pid.lock"; | |
123 | ||
124 | } | |
125 | ||
126 | _debug("Lock: $lock_filename"); | |
127 | ||
128 | $lock_handle = make_lockfile($lock_filename); | |
129 | $must_exit = false; | |
130 | ||
131 | if (isset($options["task"]) && isset($options["pidlock"])) { | |
132 | $waits = $options["task"] * 5; | |
133 | _debug("Waiting before update ($waits)"); | |
134 | sleep($waits); | |
135 | } | |
136 | ||
137 | // Try to lock a file in order to avoid concurrent update. | |
138 | if (!$lock_handle) { | |
139 | die("error: Can't create lockfile ($lock_filename). ". | |
140 | "Maybe another update process is already running.\n"); | |
141 | } | |
142 | ||
143 | if (isset($options["force-update"])) { | |
144 | _debug("marking all feeds as needing update..."); | |
145 | ||
146 | db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01', | |
147 | last_updated = '1970-01-01'"); | |
148 | } | |
149 | ||
150 | if (isset($options["feeds"])) { | |
151 | update_daemon_common(); | |
152 | housekeeping_common(true); | |
153 | ||
154 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
155 | } | |
156 | ||
157 | if (isset($options["feedbrowser"])) { | |
158 | $count = update_feedbrowser_cache(); | |
159 | print "Finished, $count feeds processed.\n"; | |
160 | } | |
161 | ||
162 | if (isset($options["daemon"])) { | |
163 | while (true) { | |
164 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; | |
165 | $log = isset($options['log']) ? '--log '.$options['log'] : ''; | |
166 | ||
167 | passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log"); | |
168 | _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); | |
169 | sleep(DAEMON_SLEEP_INTERVAL); | |
170 | } | |
171 | } | |
172 | ||
173 | if (isset($options["daemon-loop"])) { | |
174 | if (!make_stampfile('update_daemon.stamp')) { | |
175 | _debug("warning: unable to create stampfile\n"); | |
176 | } | |
177 | ||
178 | update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT); | |
179 | ||
180 | if (!isset($options["pidlock"]) || $options["task"] == 0) | |
181 | housekeeping_common(true); | |
182 | ||
183 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op); | |
184 | } | |
185 | ||
186 | if (isset($options["cleanup-tags"])) { | |
187 | $rc = cleanup_tags( 14, 50000); | |
188 | _debug("$rc tags deleted.\n"); | |
189 | } | |
190 | ||
191 | if (isset($options["indexes"])) { | |
192 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); | |
193 | _debug("Type 'yes' to continue."); | |
194 | ||
195 | if (read_stdin() != 'yes') | |
196 | exit; | |
197 | ||
198 | _debug("clearing existing indexes..."); | |
199 | ||
200 | if (DB_TYPE == "pgsql") { | |
201 | $result = db_query( "SELECT relname FROM | |
202 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' | |
203 | AND relname NOT LIKE '%_pkey' | |
204 | AND relkind = 'i'"); | |
205 | } else { | |
206 | $result = db_query( "SELECT index_name,table_name FROM | |
207 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); | |
208 | } | |
209 | ||
210 | while ($line = db_fetch_assoc($result)) { | |
211 | if (DB_TYPE == "pgsql") { | |
212 | $statement = "DROP INDEX " . $line["relname"]; | |
213 | _debug($statement); | |
214 | } else { | |
215 | $statement = "ALTER TABLE ". | |
216 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
217 | _debug($statement); | |
218 | } | |
219 | db_query( $statement, false); | |
220 | } | |
221 | ||
222 | _debug("reading indexes from schema for: " . DB_TYPE); | |
223 | ||
224 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
225 | if ($fp) { | |
226 | while ($line = fgets($fp)) { | |
227 | $matches = array(); | |
228 | ||
229 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
230 | $index = $matches[1]; | |
231 | $table = $matches[2]; | |
232 | ||
233 | $statement = "CREATE INDEX $index ON $table"; | |
234 | ||
235 | _debug($statement); | |
236 | db_query( $statement); | |
237 | } | |
238 | } | |
239 | fclose($fp); | |
240 | } else { | |
241 | _debug("unable to open schema file."); | |
242 | } | |
243 | _debug("all done."); | |
244 | } | |
245 | ||
246 | if (isset($options["convert-filters"])) { | |
247 | _debug("WARNING: this will remove all existing type2 filters."); | |
248 | _debug("Type 'yes' to continue."); | |
249 | ||
250 | if (read_stdin() != 'yes') | |
251 | exit; | |
252 | ||
253 | _debug("converting filters..."); | |
254 | ||
255 | db_query( "DELETE FROM ttrss_filters2"); | |
256 | ||
257 | $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id"); | |
258 | ||
259 | while ($line = db_fetch_assoc($result)) { | |
260 | $owner_uid = $line["owner_uid"]; | |
261 | ||
262 | // date filters are removed | |
263 | if ($line["filter_type"] != 5) { | |
264 | $filter = array(); | |
265 | ||
266 | if (sql_bool_to_bool($line["cat_filter"])) { | |
267 | $feed_id = "CAT:" . (int)$line["cat_id"]; | |
268 | } else { | |
269 | $feed_id = (int)$line["feed_id"]; | |
270 | } | |
271 | ||
272 | $filter["enabled"] = $line["enabled"] ? "on" : "off"; | |
273 | $filter["rule"] = array( | |
274 | json_encode(array( | |
275 | "reg_exp" => $line["reg_exp"], | |
276 | "feed_id" => $feed_id, | |
277 | "filter_type" => $line["filter_type"]))); | |
278 | ||
279 | $filter["action"] = array( | |
280 | json_encode(array( | |
281 | "action_id" => $line["action_id"], | |
282 | "action_param_label" => $line["action_param"], | |
283 | "action_param" => $line["action_param"]))); | |
284 | ||
285 | // Oh god it's full of hacks | |
286 | ||
287 | $_REQUEST = $filter; | |
288 | $_SESSION["uid"] = $owner_uid; | |
289 | ||
290 | $filters = new Pref_Filters($_REQUEST); | |
291 | $filters->add(); | |
292 | } | |
293 | } | |
294 | ||
295 | } | |
296 | ||
297 | if (isset($options["update-schema"])) { | |
298 | _debug("checking for updates (" . DB_TYPE . ")..."); | |
299 | ||
300 | $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION); | |
301 | ||
302 | if ($updater->isUpdateRequired()) { | |
303 | _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION); | |
304 | _debug("WARNING: please backup your database before continuing."); | |
305 | _debug("Type 'yes' to continue."); | |
306 | ||
307 | if (read_stdin() != 'yes') | |
308 | exit; | |
309 | ||
310 | for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { | |
311 | _debug("performing update up to version $i..."); | |
312 | ||
313 | $result = $updater->performUpdateTo($i); | |
314 | ||
315 | _debug($result ? "OK!" : "FAILED!"); | |
316 | ||
317 | if (!$result) return; | |
318 | ||
319 | } | |
320 | } else { | |
321 | _debug("update not required."); | |
322 | } | |
323 | ||
324 | } | |
325 | ||
326 | if (isset($options["list-plugins"])) { | |
327 | $tmppluginhost = new PluginHost(); | |
328 | $tmppluginhost->load_all($tmppluginhost::KIND_ALL); | |
329 | $enabled = array_map("trim", explode(",", PLUGINS)); | |
330 | ||
331 | echo "List of all available plugins:\n"; | |
332 | ||
333 | foreach ($tmppluginhost->get_plugins() as $name => $plugin) { | |
334 | $about = $plugin->about(); | |
335 | ||
336 | $status = $about[3] ? "system" : "user"; | |
337 | ||
338 | if (in_array($name, $enabled)) $name .= "*"; | |
339 | ||
340 | printf("%-50s %-10s v%.2f (by %s)\n%s\n\n", | |
341 | $name, $status, $about[0], $about[2], $about[1]); | |
342 | } | |
343 | ||
344 | echo "Plugins marked by * are currently enabled for all users.\n"; | |
345 | ||
346 | } | |
347 | ||
348 | PluginHost::getInstance()->run_commands($options); | |
349 | ||
350 | if (file_exists(LOCK_DIRECTORY . "/$lock_filename")) | |
351 | unlink(LOCK_DIRECTORY . "/$lock_filename"); | |
352 | ?> |