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