]>
Commit | Line | Data |
---|---|---|
97aba8ec AD |
1 | <? |
2 | require_once "config.php"; | |
3 | require_once "functions.php"; | |
4 | require_once "db.php"; | |
5 | ||
db22a6e9 | 6 | define('MAX_SOURCE_SCHEMA_VERSION', 2); |
97aba8ec AD |
7 | define('TARGET_SCHEMA_VERSION', 2); |
8 | ||
9 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
10 | ||
11 | login_sequence($link); | |
12 | ||
13 | if (!$link) { | |
14 | if (DB_TYPE == "mysql") { | |
15 | print mysql_error(); | |
16 | } | |
17 | // PG seems to display its own errors just fine by default. | |
18 | return; | |
19 | } | |
20 | ||
21 | if (DB_TYPE == "pgsql") { | |
22 | pg_query("set client_encoding = 'utf-8'"); | |
23 | } | |
24 | ||
25 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
26 | ||
27 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
28 | ||
29 | if ($schema_version != TARGET_SCHEMA_VERSION) { | |
30 | print "Error: database schema is invalid | |
31 | (got version $schema_version; expected ".TARGET_SCHEMA_VERSION.")"; | |
32 | return; | |
33 | } | |
34 | ||
35 | function import_article($link, $data) { | |
36 | ||
a654a595 AD |
37 | print "Processing article <b>".$data["title"]. |
38 | "</b> (".$data["feed_title"].")<br>"; | |
97aba8ec AD |
39 | |
40 | $owner_uid = $_SESSION["uid"]; | |
41 | ||
42 | db_query($link, "BEGIN"); | |
43 | ||
44 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE feed_url = '". | |
a654a595 | 45 | db_escape_string($data["feed_url"]) . "' AND owner_uid = '$owner_uid'"); |
97aba8ec AD |
46 | |
47 | if (db_num_rows($result) == 0) { | |
48 | return false; | |
49 | } | |
50 | ||
51 | $feed_id = db_fetch_result($result, 0, "id"); | |
52 | ||
53 | $result = db_query($link, "SELECT id FROM ttrss_entries WHERE | |
54 | guid = '".$data["guid"]."'"); | |
55 | ||
56 | if (db_num_rows($result) == 0) { | |
57 | ||
58 | print "Not found, adding base entry...<br>"; | |
59 | ||
a654a595 AD |
60 | $entry_title = db_escape_string($data["title"]); |
61 | $entry_guid = db_escape_string($data["guid"]); | |
62 | $entry_link = db_escape_string($data["link"]); | |
63 | $updated = db_escape_string($data["updated"]); | |
64 | $date_entered = db_escape_string($data["date_entered"]); | |
65 | $entry_content = db_escape_string($data["content"]); | |
97aba8ec | 66 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
a654a595 | 67 | $entry_comments = db_escape_string($data["comments"]); |
97aba8ec AD |
68 | |
69 | $result = db_query($link, | |
70 | "INSERT INTO ttrss_entries | |
71 | (title, | |
72 | guid, | |
73 | link, | |
74 | updated, | |
75 | content, | |
76 | content_hash, | |
77 | no_orig_date, | |
78 | date_entered, | |
79 | comments) | |
80 | VALUES | |
81 | ('$entry_title', | |
82 | '$entry_guid', | |
83 | '$entry_link', | |
84 | '$updated', | |
85 | '$entry_content', | |
86 | '$content_hash', | |
87 | false, | |
88 | '$date_entered', | |
89 | '$entry_comments')"); | |
90 | } | |
91 | ||
92 | $result = db_query($link, "SELECT id FROM ttrss_entries WHERE | |
93 | guid = '".$data["guid"]."'"); | |
94 | ||
95 | if (db_num_rows($result) == 0) { return false; } | |
96 | ||
97 | $entry_id = db_fetch_result($result, 0, "id"); | |
98 | ||
99 | print "Found base ID: $entry_id<br>"; | |
100 | ||
101 | $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE | |
5a664259 | 102 | ref_id = '$entry_id' AND owner_uid = '$owner_uid'"); |
97aba8ec AD |
103 | |
104 | if (db_num_rows($result) == 0) { | |
105 | print "User table entry not found, creating...<br>"; | |
106 | ||
a654a595 AD |
107 | $unread = sql_bool_to_string(db_escape_string($data["unread"])); |
108 | $marked = sql_bool_to_string(db_escape_string($data["marked"])); | |
109 | $last_read = db_escape_string($data["last_read"]); | |
97aba8ec AD |
110 | |
111 | if (!$last_read) { | |
112 | $last_read_qpart = 'NULL'; | |
113 | } else { | |
114 | $last_read_qpart = "'$last_read'"; | |
115 | } | |
116 | ||
117 | $result = db_query($link, | |
118 | "INSERT INTO ttrss_user_entries | |
119 | (ref_id, owner_uid, feed_id, unread, marked, last_read) | |
a654a595 | 120 | VALUES ('$entry_id', '$owner_uid', '$feed_id', $unread, $marked, |
97aba8ec AD |
121 | $last_read_qpart)"); |
122 | ||
123 | } else { | |
124 | print "User table entry already exists, nothing to do.<br>"; | |
125 | } | |
126 | ||
127 | db_query($link, "COMMIT"); | |
128 | ||
129 | } | |
130 | ||
131 | ?> | |
132 | <html> | |
133 | <body> | |
134 | ||
135 | <? if ($_REQUEST["op"] != "Import") { ?> | |
136 | ||
df08c5bb | 137 | <h1>Import XMLDB (your login is <?= $_SESSION["name"] ?>)</h1> |
97aba8ec AD |
138 | |
139 | <form enctype="multipart/form-data" method="POST" action="xml-import.php"> | |
140 | File: <input name="xmldb" type="file"> | |
141 | <input class="button" name="op" type="submit" value="Import"> | |
142 | </form> | |
143 | ||
144 | <? } else { | |
145 | ||
146 | print "<h1>Importing data (your login is ".$_SESSION["name"].")</h1>"; | |
147 | ||
148 | if (is_file($_FILES['xmldb']['tmp_name'])) { | |
149 | $dom = domxml_open_file($_FILES['xmldb']['tmp_name']); | |
150 | // $dom = domxml_open_file('xmldb.xml'); | |
151 | ||
152 | if ($dom) { | |
153 | $root = $dom->document_element(); | |
154 | ||
155 | $schema_version = $root->get_elements_by_tagname('schema_version'); | |
156 | $schema_version = $schema_version[0]->get_content(); | |
157 | ||
db22a6e9 | 158 | if ($schema_version != MAX_SOURCE_SCHEMA_VERSION) { |
97aba8ec AD |
159 | die("Incorrect source schema version"); |
160 | } | |
161 | ||
162 | $articles = $root->get_elements_by_tagname("article"); | |
163 | ||
164 | foreach ($articles as $article) { | |
165 | $child_nodes = $article->child_nodes(); | |
166 | ||
167 | $article_data = array(); | |
168 | ||
169 | foreach ($child_nodes as $child) { | |
170 | $article_data[$child->tagname()] = $child->get_content(); | |
171 | } | |
172 | ||
173 | import_article($link, $article_data); | |
174 | } | |
175 | } else { | |
176 | print "Error: could not parse document."; | |
177 | } | |
178 | } else { | |
179 | print "<p>Error: please upload XMLDB.</p>"; | |
180 | } | |
181 | ||
182 | } ?> | |
183 | </body> | |
184 | </html> | |
185 |