]>
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"; | |
5439d333 | 31 | print " -quiet - don't show messages\n"; |
9a57512c | 32 | print " -indexes - recreate missing schema indexes\n"; |
55f34b81 | 33 | print " -help - show this help\n"; |
661135c7 | 34 | return; |
81596c66 | 35 | } |
87b9fb65 | 36 | |
5439d333 RW |
37 | define('QUIET', in_array("-quiet", $op)); |
38 | ||
39 | if (!in_array("-daemon", $op)) { | |
661135c7 AD |
40 | $lock_filename = "update.lock"; |
41 | } else { | |
42 | $lock_filename = "update_daemon.lock"; | |
43 | } | |
fecd57c8 | 44 | |
661135c7 AD |
45 | $lock_handle = make_lockfile($lock_filename); |
46 | $must_exit = false; | |
fecd57c8 | 47 | |
661135c7 AD |
48 | // Try to lock a file in order to avoid concurrent update. |
49 | if (!$lock_handle) { | |
50 | die("error: Can't create lockfile ($lock_filename). ". | |
51 | "Maybe another update process is already running.\n"); | |
52 | } | |
fecd57c8 | 53 | |
661135c7 | 54 | // Create a database connection. |
dbaa4e4a | 55 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
fecd57c8 | 56 | |
661135c7 | 57 | init_connection($link); |
ef59e6e8 | 58 | |
5439d333 | 59 | if (in_array("-feeds", $op)) { |
661135c7 AD |
60 | // Update all feeds needing a update. |
61 | update_daemon_common($link); | |
84e9a8c7 AD |
62 | |
63 | // Update feedbrowser | |
64 | $count = update_feedbrowser_cache($link); | |
65 | _debug("Feedbrowser updated, $count feeds processed."); | |
66 | ||
67 | // Purge orphans and cleanup tags | |
68 | purge_orphans($link, true); | |
69 | ||
70 | $rc = cleanup_tags($link, 14, 50000); | |
71 | _debug("Cleaned $rc cached tags."); | |
f32eb194 AD |
72 | |
73 | get_linked_feeds($link); | |
661135c7 | 74 | } |
fecd57c8 | 75 | |
5439d333 | 76 | if (in_array("-feedbrowser", $op)) { |
661135c7 AD |
77 | $count = update_feedbrowser_cache($link); |
78 | print "Finished, $count feeds processed.\n"; | |
fecd57c8 | 79 | } |
661135c7 | 80 | |
5439d333 RW |
81 | if (in_array("-daemon", $op)) { |
82 | $op = array_diff($op, array("-daemon")); | |
661135c7 | 83 | while (true) { |
5439d333 | 84 | passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop"); |
661135c7 AD |
85 | _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); |
86 | sleep(DAEMON_SLEEP_INTERVAL); | |
fecd57c8 | 87 | } |
81596c66 | 88 | } |
9e21a571 | 89 | |
5439d333 | 90 | if (in_array("-daemon-loop", $op)) { |
661135c7 AD |
91 | if (!make_stampfile('update_daemon.stamp')) { |
92 | die("error: unable to create stampfile\n"); | |
93 | } | |
9e21a571 | 94 | |
dbaa4e4a | 95 | // Call to the feed batch update function |
661135c7 | 96 | // or regenerate feedbrowser cache |
9e21a571 | 97 | |
661135c7 AD |
98 | if (rand(0,100) > 30) { |
99 | update_daemon_common($link); | |
9e21a571 | 100 | } else { |
661135c7 | 101 | $count = update_feedbrowser_cache($link); |
e3b42c5a AD |
102 | _debug("Feedbrowser updated, $count feeds processed."); |
103 | ||
104 | purge_orphans($link, true); | |
dbaa4e4a | 105 | |
e3b42c5a AD |
106 | $rc = cleanup_tags($link, 14, 50000); |
107 | ||
108 | _debug("Cleaned $rc cached tags."); | |
f32eb194 AD |
109 | |
110 | get_linked_feeds($link); | |
9e21a571 | 111 | } |
ef59e6e8 | 112 | |
fecd57c8 | 113 | } |
fecd57c8 | 114 | |
5439d333 | 115 | if (in_array("-cleanup-tags", $op)) { |
868650e4 | 116 | $rc = cleanup_tags($link, 14, 50000); |
5439d333 | 117 | _debug("$rc tags deleted.\n"); |
868650e4 AD |
118 | } |
119 | ||
5439d333 | 120 | if (in_array("-get-feeds", $op)) { |
ae5f7bb1 AD |
121 | get_linked_feeds($link); |
122 | } | |
123 | ||
5439d333 RW |
124 | if (in_array("-import",$op)) { |
125 | $username = $argv[count($argv) - 2]; | |
126 | $filename = $argv[count($argv) - 1]; | |
55f34b81 AD |
127 | |
128 | if (!$username) { | |
129 | print "error: please specify username.\n"; | |
130 | return; | |
131 | } | |
132 | ||
133 | if (!is_file($filename)) { | |
134 | print "error: input filename ($filename) doesn't exist.\n"; | |
135 | return; | |
136 | } | |
137 | ||
5439d333 | 138 | _debug("importing $filename for user $username...\n"); |
55f34b81 AD |
139 | |
140 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'"); | |
141 | ||
142 | if (db_num_rows($result) == 0) { | |
143 | print "error: could not find user $username.\n"; | |
144 | return; | |
145 | } | |
146 | ||
147 | $owner_uid = db_fetch_result($result, 0, "id"); | |
148 | ||
149 | perform_data_import($link, $filename, $owner_uid); | |
150 | ||
151 | } | |
152 | ||
871f0a7a AD |
153 | if (in_array("-indexes", $op)) { |
154 | _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); | |
155 | _debug("Type 'yes' to continue."); | |
156 | ||
157 | if (read_stdin() != 'yes') | |
158 | exit; | |
159 | ||
160 | _debug("clearing existing indexes..."); | |
161 | ||
162 | if (DB_TYPE == "pgsql") { | |
163 | $result = db_query($link, "SELECT relname FROM | |
164 | pg_catalog.pg_class WHERE relname LIKE 'ttrss_%' | |
165 | AND relname NOT LIKE '%_pkey' | |
166 | AND relkind = 'i'"); | |
167 | } else { | |
168 | $result = db_query($link, "SELECT index_name,table_name FROM | |
169 | information_schema.statistics WHERE index_name LIKE 'ttrss_%'"); | |
170 | } | |
171 | ||
172 | while ($line = db_fetch_assoc($result)) { | |
173 | if (DB_TYPE == "pgsql") { | |
174 | $statement = "DROP INDEX " . $line["relname"]; | |
175 | _debug($statement); | |
176 | } else { | |
177 | $statement = "ALTER TABLE ". | |
178 | $line['table_name']." DROP INDEX ".$line['index_name']; | |
179 | _debug($statement); | |
180 | } | |
181 | db_query($link, $statement, false); | |
182 | } | |
183 | ||
184 | _debug("reading indexes from schema for: " . DB_TYPE); | |
185 | ||
186 | $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r"); | |
187 | if ($fp) { | |
188 | while ($line = fgets($fp)) { | |
189 | $matches = array(); | |
190 | ||
191 | if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) { | |
192 | $index = $matches[1]; | |
193 | $table = $matches[2]; | |
194 | ||
195 | $statement = "CREATE INDEX $index ON $table"; | |
196 | ||
197 | _debug($statement); | |
198 | db_query($link, $statement); | |
199 | } | |
200 | } | |
201 | fclose($fp); | |
202 | } else { | |
203 | _debug("unable to open schema file."); | |
204 | } | |
205 | _debug("all done."); | |
206 | } | |
207 | ||
661135c7 | 208 | db_close($link); |
107d0cf3 | 209 | |