]>
git.wh0rd.org - tt-rss.git/blob - xml-import.php
2 require_once "config.php";
3 require_once "functions.php";
6 define('SOURCE_SCHEMA_VERSION', 1);
7 define('TARGET_SCHEMA_VERSION', 2);
9 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
11 login_sequence($link);
14 if (DB_TYPE
== "mysql") {
17 // PG seems to display its own errors just fine by default.
21 if (DB_TYPE
== "pgsql") {
22 pg_query("set client_encoding = 'utf-8'");
25 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
27 $schema_version = db_fetch_result($result, 0, "schema_version");
29 if ($schema_version != TARGET_SCHEMA_VERSION
) {
30 print "Error: database schema is invalid
31 (got version $schema_version; expected ".TARGET_SCHEMA_VERSION
.")";
35 function import_article($link, $data) {
37 print "Processing article " . $data["title"] . "<br>";
39 $owner_uid = $_SESSION["uid"];
41 db_query($link, "BEGIN");
43 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE feed_url = '".
44 $data["feed_url"] . "' AND owner_uid = '$owner_uid'");
46 if (db_num_rows($result) == 0) {
50 $feed_id = db_fetch_result($result, 0, "id");
52 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
53 guid = '".$data["guid"]."'");
55 if (db_num_rows($result) == 0) {
57 print "Not found, adding base entry...<br>";
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"];
68 $result = db_query($link,
69 "INSERT INTO ttrss_entries
91 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
92 guid = '".$data["guid"]."'");
94 if (db_num_rows($result) == 0) { return false; }
96 $entry_id = db_fetch_result($result, 0, "id");
98 print "Found base ID: $entry_id<br>";
100 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
101 ref_id = '$entry_id'");
103 if (db_num_rows($result) == 0) {
104 print "User table entry not found, creating...<br>";
106 $unread = $data["unread"];
107 $marked = $data["marked"];
108 $last_read = $data["last_read"];
111 $last_read_qpart = 'NULL';
113 $last_read_qpart = "'$last_read'";
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',
123 print "User table entry already exists, nothing to do.<br>";
126 db_query($link, "COMMIT");
134 <?
if ($_REQUEST["op"] != "Import") { ?
>
136 <h1
>Import
XMLDB (your login is
<?
= $_SESSION["login"] ?
>)</h1
>
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">
145 print "<h1>Importing data (your login is ".$_SESSION["name"].")</h1>";
147 if (is_file($_FILES['xmldb']['tmp_name'])) {
148 $dom = domxml_open_file($_FILES['xmldb']['tmp_name']);
149 // $dom = domxml_open_file('xmldb.xml');
152 $root = $dom->document_element();
154 $schema_version = $root->get_elements_by_tagname('schema_version');
155 $schema_version = $schema_version[0]->get_content();
157 if ($schema_version != SOURCE_SCHEMA_VERSION
) {
158 die("Incorrect source schema version");
161 $articles = $root->get_elements_by_tagname("article");
163 foreach ($articles as $article) {
164 $child_nodes = $article->child_nodes();
166 $article_data = array();
168 foreach ($child_nodes as $child) {
169 $article_data[$child->tagname()] = $child->get_content();
172 import_article($link, $article_data);
175 print "Error: could not parse document.";
178 print "<p>Error: please upload XMLDB.</p>";