]>
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 | ||
8fb9acee | 37 | print "<p>Article: <b>".$data["title"]. |
a654a595 | 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 | ||
8fb9acee | 58 | print "Adding base entry...<br>"; |
97aba8ec | 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> | |
648cbec8 AD |
133 | <head> |
134 | <title>XML Import</title> | |
135 | <link rel="stylesheet" href="opml.css" type="text/css"> | |
136 | </head> | |
97aba8ec AD |
137 | <body> |
138 | ||
648cbec8 AD |
139 | <h1><img src="images/ttrss_logo.png"></h1> |
140 | ||
648cbec8 | 141 | <div class="opmlBody"> |
8fb9acee AD |
142 | |
143 | <? if ($_REQUEST["op"] != "Import") { ?> | |
648cbec8 AD |
144 | |
145 | <h2>Import XMLDB</h2> | |
97aba8ec AD |
146 | |
147 | <form enctype="multipart/form-data" method="POST" action="xml-import.php"> | |
148 | File: <input name="xmldb" type="file"> | |
149 | <input class="button" name="op" type="submit" value="Import"> | |
150 | </form> | |
151 | ||
152 | <? } else { | |
153 | ||
648cbec8 | 154 | print "<h2>Importing data</h2>"; |
97aba8ec AD |
155 | |
156 | if (is_file($_FILES['xmldb']['tmp_name'])) { | |
157 | $dom = domxml_open_file($_FILES['xmldb']['tmp_name']); | |
158 | // $dom = domxml_open_file('xmldb.xml'); | |
159 | ||
160 | if ($dom) { | |
161 | $root = $dom->document_element(); | |
162 | ||
163 | $schema_version = $root->get_elements_by_tagname('schema_version'); | |
164 | $schema_version = $schema_version[0]->get_content(); | |
165 | ||
db22a6e9 | 166 | if ($schema_version != MAX_SOURCE_SCHEMA_VERSION) { |
97aba8ec AD |
167 | die("Incorrect source schema version"); |
168 | } | |
169 | ||
170 | $articles = $root->get_elements_by_tagname("article"); | |
171 | ||
172 | foreach ($articles as $article) { | |
173 | $child_nodes = $article->child_nodes(); | |
174 | ||
175 | $article_data = array(); | |
176 | ||
177 | foreach ($child_nodes as $child) { | |
178 | $article_data[$child->tagname()] = $child->get_content(); | |
179 | } | |
180 | ||
8fb9acee | 181 | $is_imported = import_article($link, $article_data); |
97aba8ec | 182 | } |
8fb9acee AD |
183 | |
184 | print "<p><a class=\"button\" href=\"prefs.php\">Return to preferences</a>"; | |
97aba8ec AD |
185 | } else { |
186 | print "Error: could not parse document."; | |
187 | } | |
188 | } else { | |
189 | print "<p>Error: please upload XMLDB.</p>"; | |
190 | } | |
191 | ||
192 | } ?> | |
648cbec8 | 193 | </div> |
97aba8ec AD |
194 | </body> |
195 | </html> | |
196 |