]> git.wh0rd.org - tt-rss.git/blob - xml-import.php
remove comment from xml-export.php
[tt-rss.git] / xml-import.php
1 <?
2 require_once "config.php";
3 require_once "functions.php";
4 require_once "db.php";
5
6 define('MAX_SOURCE_SCHEMA_VERSION', 2);
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 "<p>Article: <b>".$data["title"].
38 "</b> (".$data["feed_title"].")<br>";
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 = '".
45 db_escape_string($data["feed_url"]) . "' AND owner_uid = '$owner_uid'");
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 "Adding base entry...<br>";
59
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"]);
66 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
67 $entry_comments = db_escape_string($data["comments"]);
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
102 ref_id = '$entry_id' AND owner_uid = '$owner_uid'");
103
104 if (db_num_rows($result) == 0) {
105 print "User table entry not found, creating...<br>";
106
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"]);
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)
120 VALUES ('$entry_id', '$owner_uid', '$feed_id', $unread, $marked,
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 <head>
134 <title>XML Import</title>
135 <link rel="stylesheet" href="opml.css" type="text/css">
136 </head>
137 <body>
138
139 <h1><img src="images/ttrss_logo.png"></h1>
140
141 <div class="opmlBody">
142
143 <? if ($_REQUEST["op"] != "Import") { ?>
144
145 <h2>Import XMLDB</h2>
146
147 <form enctype="multipart/form-data" method="POST" action="xml-import.php">
148 File: <input name="xmldb" type="file">&nbsp;
149 <input class="button" name="op" type="submit" value="Import">
150 </form>
151
152 <? } else {
153
154 print "<h2>Importing data</h2>";
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
166 if ($schema_version != MAX_SOURCE_SCHEMA_VERSION) {
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
181 $is_imported = import_article($link, $article_data);
182 }
183
184 print "<p><a class=\"button\" href=\"prefs.php\">Return to preferences</a>";
185 } else {
186 print "Error: could not parse document.";
187 }
188 } else {
189 print "<p>Error: please upload XMLDB.</p>";
190 }
191
192 } ?>
193 </div>
194 </body>
195 </html>
196