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