]> git.wh0rd.org - tt-rss.git/blob - update.php
rework class system to use subdirectories
[tt-rss.git] / update.php
1 #!/usr/bin/env php
2 <?php
3 set_include_path(get_include_path() . PATH_SEPARATOR .
4 dirname(__FILE__) . "/include");
5
6 define('DISABLE_SESSIONS', true);
7
8 chdir(dirname(__FILE__));
9
10 require_once "functions.php";
11 require_once "rssfuncs.php";
12 require_once "sanity_check.php";
13 require_once "config.php";
14 require_once "db.php";
15 require_once "db-prefs.php";
16
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
19
20 $op = $argv;
21
22 if (count($argv) == 1 || in_array("-help", $op) ) {
23 print "Tiny Tiny RSS data update script.\n\n";
24 print "Options:\n";
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";
31 print " -quiet - don't show messages\n";
32 print " -indexes - recreate missing schema indexes\n";
33 print " -help - show this help\n";
34 return;
35 }
36
37 define('QUIET', in_array("-quiet", $op));
38
39 if (!in_array("-daemon", $op)) {
40 $lock_filename = "update.lock";
41 } else {
42 $lock_filename = "update_daemon.lock";
43 }
44
45 $lock_handle = make_lockfile($lock_filename);
46 $must_exit = false;
47
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 }
53
54 // Create a database connection.
55 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
56
57 init_connection($link);
58
59 $plugins = new Plugins($link);
60
61 if (in_array("-feeds", $op)) {
62 // Update all feeds needing a update.
63 update_daemon_common($link);
64
65 // Update feedbrowser
66 $count = update_feedbrowser_cache($link);
67 _debug("Feedbrowser updated, $count feeds processed.");
68
69 // Purge orphans and cleanup tags
70 purge_orphans($link, true);
71
72 $rc = cleanup_tags($link, 14, 50000);
73 _debug("Cleaned $rc cached tags.");
74
75 get_linked_feeds($link);
76 }
77
78 if (in_array("-feedbrowser", $op)) {
79 $count = update_feedbrowser_cache($link);
80 print "Finished, $count feeds processed.\n";
81 }
82
83 if (in_array("-daemon", $op)) {
84 $op = array_diff($op, array("-daemon"));
85 while (true) {
86 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
87 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
88 sleep(DAEMON_SLEEP_INTERVAL);
89 }
90 }
91
92 if (in_array("-daemon-loop", $op)) {
93 if (!make_stampfile('update_daemon.stamp')) {
94 die("error: unable to create stampfile\n");
95 }
96
97 // Call to the feed batch update function
98 // or regenerate feedbrowser cache
99
100 if (rand(0,100) > 30) {
101 update_daemon_common($link);
102 } else {
103 $count = update_feedbrowser_cache($link);
104 _debug("Feedbrowser updated, $count feeds processed.");
105
106 purge_orphans($link, true);
107
108 $rc = cleanup_tags($link, 14, 50000);
109
110 _debug("Cleaned $rc cached tags.");
111
112 get_linked_feeds($link);
113 }
114
115 }
116
117 if (in_array("-cleanup-tags", $op)) {
118 $rc = cleanup_tags($link, 14, 50000);
119 _debug("$rc tags deleted.\n");
120 }
121
122 if (in_array("-get-feeds", $op)) {
123 get_linked_feeds($link);
124 }
125
126 if (in_array("-import",$op)) {
127 $username = $argv[count($argv) - 2];
128 $filename = $argv[count($argv) - 1];
129
130 if (!$username) {
131 print "error: please specify username.\n";
132 return;
133 }
134
135 if (!is_file($filename)) {
136 print "error: input filename ($filename) doesn't exist.\n";
137 return;
138 }
139
140 _debug("importing $filename for user $username...\n");
141
142 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
143
144 if (db_num_rows($result) == 0) {
145 print "error: could not find user $username.\n";
146 return;
147 }
148
149 $owner_uid = db_fetch_result($result, 0, "id");
150
151 perform_data_import($link, $filename, $owner_uid);
152
153 }
154
155 if (in_array("-indexes", $op)) {
156 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
157 _debug("Type 'yes' to continue.");
158
159 if (read_stdin() != 'yes')
160 exit;
161
162 _debug("clearing existing indexes...");
163
164 if (DB_TYPE == "pgsql") {
165 $result = db_query($link, "SELECT relname FROM
166 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
167 AND relname NOT LIKE '%_pkey'
168 AND relkind = 'i'");
169 } else {
170 $result = db_query($link, "SELECT index_name,table_name FROM
171 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
172 }
173
174 while ($line = db_fetch_assoc($result)) {
175 if (DB_TYPE == "pgsql") {
176 $statement = "DROP INDEX " . $line["relname"];
177 _debug($statement);
178 } else {
179 $statement = "ALTER TABLE ".
180 $line['table_name']." DROP INDEX ".$line['index_name'];
181 _debug($statement);
182 }
183 db_query($link, $statement, false);
184 }
185
186 _debug("reading indexes from schema for: " . DB_TYPE);
187
188 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
189 if ($fp) {
190 while ($line = fgets($fp)) {
191 $matches = array();
192
193 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
194 $index = $matches[1];
195 $table = $matches[2];
196
197 $statement = "CREATE INDEX $index ON $table";
198
199 _debug($statement);
200 db_query($link, $statement);
201 }
202 }
203 fclose($fp);
204 } else {
205 _debug("unable to open schema file.");
206 }
207 _debug("all done.");
208 }
209
210 db_close($link);
211
212 if ($lock_handle != false) {
213 fclose($lock_handle);
214 }
215
216 unlink(LOCK_DIRECTORY . "/$lock_filename");
217 ?>