root/utils/xml-import.php

Revision 905, 4.8 kB (checked in by Andrew Dolgov <fox@madoka.spb.ru>, 3 years ago)

[project @ database backed sessions]

Line 
1 <?
2     require_once "sessions.php";
3     
4     require_once "config.php";
5     require_once "functions.php";
6     require_once "db.php";
7
8     define('MAX_SOURCE_SCHEMA_VERSION', 5);
9     define('TARGET_SCHEMA_VERSION', 5);
10
11     $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
12
13     login_sequence($link);
14
15     if (!$link) {
16         if (DB_TYPE == "mysql") {
17             print mysql_error();
18         }
19         // PG seems to display its own errors just fine by default.       
20         return;
21     }
22
23     if (DB_TYPE == "pgsql") {
24         pg_query("set client_encoding = 'utf-8'");
25     }
26
27     $result = db_query($link, "SELECT schema_version FROM ttrss_version");
28
29     $schema_version = db_fetch_result($result, 0, "schema_version");
30
31     if ($schema_version != TARGET_SCHEMA_VERSION) {
32         print "Error: database schema is invalid
33             (got version $schema_version; expected ".TARGET_SCHEMA_VERSION.")";
34         return;
35     }
36
37     function import_article($link, $data) {
38
39         print "<p>Article: <b>".$data["title"].
40         "</b> (".$data["feed_title"].")<br>";
41
42         $owner_uid = $_SESSION["uid"];
43
44         db_query($link, "BEGIN");
45
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'");
48
49         if (db_num_rows($result) == 0) {
50             return false;
51         }
52
53         $feed_id = db_fetch_result($result, 0, "id");       
54
55         $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
56             guid = '".$data["guid"]."'");
57
58         if (db_num_rows($result) == 0) {
59
60             print "Adding base entry...<br>";
61
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"]);
70
71             $result = db_query($link,
72                 "INSERT INTO ttrss_entries
73                     (title,
74                     guid,
75                     link,
76                     updated,
77                     content,
78                     content_hash,
79                     no_orig_date,
80                     date_entered,
81                     comments)
82                 VALUES
83                     ('$entry_title',
84                     '$entry_guid',
85                     '$entry_link',
86                     '$updated',
87                     '$entry_content',
88                     '$content_hash',
89                     false,
90                     '$date_entered',
91                     '$entry_comments')");
92         }
93
94         $result = db_query($link, "SELECT id FROM ttrss_entries WHERE
95             guid = '".$data["guid"]."'");
96
97         if (db_num_rows($result) == 0) { return false; }
98
99         $entry_id = db_fetch_result($result, 0, "id");
100
101         print "Found base ID: $entry_id<br>";
102
103         $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
104             ref_id = '$entry_id' AND owner_uid = '$owner_uid'");
105
106         if (db_num_rows($result) == 0) {
107             print "User table entry not found, creating...<br>";
108
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"]);
112
113             if (!$last_read) {
114                 $last_read_qpart = 'NULL';
115             } else {
116                 $last_read_qpart = "'$last_read'";
117             }
118
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,
123                     $last_read_qpart)");
124
125         } else {
126             print "User table entry already exists, nothing to do.<br>";
127         }
128
129         db_query($link, "COMMIT");
130
131     }
132
133 ?>
134 <html>
135 <head>
136     <title>XML Import</title>
137     <link rel="stylesheet" href="opml.css" type="text/css">
138 </head>
139 <body>
140
141     <h1><img src="images/ttrss_logo.png"></h1>
142
143     <div class="opmlBody">
144
145     <? if ($_REQUEST["op"] != "Import") { ?>
146    
147     <h2>Import XMLDB</h2>
148
149     <form    enctype="multipart/form-data" method="POST" action="xml-import.php">
150     File: <input name="xmldb" type="file">&nbsp;
151     <input class="button" name="op" type="submit" value="Import">
152     </form>
153
154     <? } else {
155
156         print "<h2>Importing data</h2>";
157
158         if (is_file($_FILES['xmldb']['tmp_name'])) {
159             $dom = domxml_open_file($_FILES['xmldb']['tmp_name']);
160 //            $dom = domxml_open_file('xmldb.xml');
161
162             if ($dom) {
163                 $root = $dom->document_element();
164
165                 $schema_version = $root->get_elements_by_tagname('schema_version');
166                 $schema_version = $schema_version[0]->get_content();
167
168                 if ($schema_version > MAX_SOURCE_SCHEMA_VERSION) {
169                     die("Incorrect source schema version");
170                 }
171
172                 $articles = $root->get_elements_by_tagname("article");
173
174                 foreach ($articles as $article) {
175                     $child_nodes = $article->child_nodes();
176
177                     $article_data = array();
178
179                     foreach ($child_nodes as $child) {
180                         $article_data[$child->tagname()] = $child->get_content();
181                     }
182
183                     $is_imported = import_article($link, $article_data);
184                 }
185
186                 print "<p><a class=\"button\" href=\"prefs.php\">Return to preferences</a>";
187             } else {
188                 print "Error: could not parse document.";
189             }
190         } else {
191             print "<p>Error: please upload XMLDB.</p>";
192         }
193
194     } ?>
195 </div>
196 </body>
197 </html>
198
199
Note: See TracBrowser for help on using the browser.