2 require_once "sessions.php";
4 require_once "config.php";
5 require_once "functions.php";
8 define('MAX_SOURCE_SCHEMA_VERSION', 5);
9 define('TARGET_SCHEMA_VERSION', 5);
11 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
13 login_sequence($link);
16 if (DB_TYPE == "mysql") {
19 // PG seems to display its own errors just fine by default.
23 if (DB_TYPE == "pgsql") {
24 pg_query("set client_encoding = 'utf-8'");
27 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
29 $schema_version = db_fetch_result($result, 0, "schema_version");
31 if ($schema_version != TARGET_SCHEMA_VERSION) {
32 print "Error: database schema is invalid
33 (got version $schema_version; expected ".TARGET_SCHEMA_VERSION.")";
37 function import_article($link, $data) {
39 print "<p>Article: <b>".$data["title"].
40 "</b> (".$data["feed_title"].")<br>";
42 $owner_uid = $_SESSION["uid"];
44 db_query($link, "BEGIN");
46 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE feed_url = '".
47 db_escape_string($data["feed_url"]) . "' AND owner_uid = '$owner_uid'");
49 if (db_num_rows($result) == 0) {
53 $feed_id = db_fetch_result($result, 0, "id");
55 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
56 guid = '".$data["guid"]."'");
58 if (db_num_rows($result) == 0) {
60 print "Adding base entry...<br>";
62 $entry_title = db_escape_string($data["title"]);
63 $entry_guid = db_escape_string($data["guid"]);
64 $entry_link = db_escape_string($data["link"]);
65 $updated = db_escape_string($data["updated"]);
66 $date_entered = db_escape_string($data["date_entered"]);
67 $entry_content = db_escape_string($data["content"]);
68 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
69 $entry_comments = db_escape_string($data["comments"]);
71 $result = db_query($link,
72 "INSERT INTO ttrss_entries
94 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
95 guid = '".$data["guid"]."'");
97 if (db_num_rows($result) == 0) { return false; }
99 $entry_id = db_fetch_result($result, 0, "id");
101 print "Found base ID: $entry_id<br>";
103 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
104 ref_id = '$entry_id' AND owner_uid = '$owner_uid'");
106 if (db_num_rows($result) == 0) {
107 print "User table entry not found, creating...<br>";
109 $unread = sql_bool_to_string(db_escape_string($data["unread"]));
110 $marked = sql_bool_to_string(db_escape_string($data["marked"]));
111 $last_read = db_escape_string($data["last_read"]);
114 $last_read_qpart = 'NULL';
116 $last_read_qpart = "'$last_read'";
119 $result = db_query($link,
120 "INSERT INTO ttrss_user_entries
121 (ref_id, owner_uid, feed_id, unread, marked, last_read)
122 VALUES ('$entry_id', '$owner_uid', '$feed_id', $unread, $marked,
126 print "User table entry already exists, nothing to do.<br>";
129 db_query($link, "COMMIT");
136 <title>XML Import</title>
137 <link rel="stylesheet" href="opml.css" type="text/css">
141 <h1><img src="images/ttrss_logo.png"></h1>
143 <div class="opmlBody">
145 <? if ($_REQUEST["op"] != "Import") { ?>
147 <h2>Import XMLDB</h2>
149 <form enctype="multipart/form-data" method="POST" action="xml-import.php">
150 File: <input name="xmldb" type="file">
151 <input class="button" name="op" type="submit" value="Import">
156 print "<h2>Importing data</h2>";
158 if (is_file($_FILES['xmldb']['tmp_name'])) {
159 $dom = domxml_open_file($_FILES['xmldb']['tmp_name']);
160 // $dom = domxml_open_file('xmldb.xml');
163 $root = $dom->document_element();
165 $schema_version = $root->get_elements_by_tagname('schema_version');
166 $schema_version = $schema_version[0]->get_content();
168 if ($schema_version > MAX_SOURCE_SCHEMA_VERSION) {
169 die("Incorrect source schema version");
172 $articles = $root->get_elements_by_tagname("article");
174 foreach ($articles as $article) {
175 $child_nodes = $article->child_nodes();
177 $article_data = array();
179 foreach ($child_nodes as $child) {
180 $article_data[$child->tagname()] = $child->get_content();
183 $is_imported = import_article($link, $article_data);
186 print "<p><a class=\"button\" href=\"prefs.php\">Return to preferences</a>";
188 print "Error: could not parse document.";
191 print "<p>Error: please upload XMLDB.</p>";