summaryrefslogtreecommitdiff
path: root/utils/xml-import.php
blob: 0706b0fa2a346f98ca59faf23c868460196e11a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?
	require_once "sessions.php";
	
	require_once "config.php";
	require_once "functions.php";
	require_once "db.php";

	define('MAX_SOURCE_SCHEMA_VERSION', 5);
	define('TARGET_SCHEMA_VERSION', 5);

	$link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);	

	login_sequence($link);

	if (!$link) {
		if (DB_TYPE == "mysql") {
			print mysql_error();
		}
		// PG seems to display its own errors just fine by default.		
		return;
	}

	if (DB_TYPE == "pgsql") {
		pg_query("set client_encoding = 'utf-8'");
	}

	$result = db_query($link, "SELECT schema_version FROM ttrss_version");

	$schema_version = db_fetch_result($result, 0, "schema_version");

	if ($schema_version != TARGET_SCHEMA_VERSION) {
		print "Error: database schema is invalid
			(got version $schema_version; expected ".TARGET_SCHEMA_VERSION.")";
		return;
	}

	function import_article($link, $data) {

		print "<p>Article: <b>".$data["title"].
		"</b> (".$data["feed_title"].")<br>";

		$owner_uid = $_SESSION["uid"];

		db_query($link, "BEGIN");

		$result = db_query($link, "SELECT id FROM ttrss_feeds WHERE feed_url = '".
			db_escape_string($data["feed_url"]) . "' AND owner_uid = '$owner_uid'");

		if (db_num_rows($result) == 0) {
			return false;
		}

		$feed_id = db_fetch_result($result, 0, "id");		

		$result = db_query($link, "SELECT id FROM ttrss_entries WHERE
			guid = '".$data["guid"]."'");

		if (db_num_rows($result) == 0) {

			print "Adding base entry...<br>";

			$entry_title = db_escape_string($data["title"]);
			$entry_guid = db_escape_string($data["guid"]);
			$entry_link = db_escape_string($data["link"]);
			$updated = db_escape_string($data["updated"]);
			$date_entered = db_escape_string($data["date_entered"]);
			$entry_content = db_escape_string($data["content"]);
			$content_hash = "SHA1:" . sha1(strip_tags($entry_content));
			$entry_comments = db_escape_string($data["comments"]);

			$result = db_query($link,
				"INSERT INTO ttrss_entries 
					(title,
					guid,
					link,
					updated,
					content,
					content_hash,
					no_orig_date,
					date_entered,
					comments)
				VALUES
					('$entry_title', 
					'$entry_guid', 
					'$entry_link',
					'$updated', 
					'$entry_content', 
					'$content_hash',
					false, 
					'$date_entered', 
					'$entry_comments')");
		}

		$result = db_query($link, "SELECT id FROM ttrss_entries WHERE
			guid = '".$data["guid"]."'");

		if (db_num_rows($result) == 0) { return false; }

		$entry_id = db_fetch_result($result, 0, "id");

		print "Found base ID: $entry_id<br>";

		$result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
			ref_id = '$entry_id' AND owner_uid = '$owner_uid'");

		if (db_num_rows($result) == 0) {
			print "User table entry not found, creating...<br>";

			$unread = sql_bool_to_string(db_escape_string($data["unread"]));
			$marked = sql_bool_to_string(db_escape_string($data["marked"]));
			$last_read = db_escape_string($data["last_read"]);

			if (!$last_read) {
				$last_read_qpart = 'NULL';
			} else {
				$last_read_qpart = "'$last_read'";
			}

			$result = db_query($link,
				"INSERT INTO ttrss_user_entries 
					(ref_id, owner_uid, feed_id, unread, marked, last_read) 
				VALUES ('$entry_id', '$owner_uid', '$feed_id', $unread, $marked,
					$last_read_qpart)");

		} else {
			print "User table entry already exists, nothing to do.<br>";
		}

		db_query($link, "COMMIT");

	}

?>
<html>
<head>
	<title>XML Import</title>
	<link rel="stylesheet" href="opml.css" type="text/css">
</head>
<body>

	<h1><img src="images/ttrss_logo.png"></h1>

	<div class="opmlBody">

	<? if ($_REQUEST["op"] != "Import") { ?>
	
	<h2>Import XMLDB</h2>

	<form	enctype="multipart/form-data" method="POST" action="xml-import.php">
	File: <input name="xmldb" type="file">&nbsp;
	<input class="button" name="op" type="submit" value="Import">
	</form>

	<? } else {

		print "<h2>Importing data</h2>";

		if (is_file($_FILES['xmldb']['tmp_name'])) {
			$dom = domxml_open_file($_FILES['xmldb']['tmp_name']);
//			$dom = domxml_open_file('xmldb.xml');

			if ($dom) {
				$root = $dom->document_element();

				$schema_version = $root->get_elements_by_tagname('schema_version');
				$schema_version = $schema_version[0]->get_content();

				if ($schema_version > MAX_SOURCE_SCHEMA_VERSION) {
					die("Incorrect source schema version");
				}

				$articles = $root->get_elements_by_tagname("article");

				foreach ($articles as $article) {
					$child_nodes = $article->child_nodes();

					$article_data = array();

					foreach ($child_nodes as $child) {
						$article_data[$child->tagname()] = $child->get_content();
					}

					$is_imported = import_article($link, $article_data);
				}

				print "<p><a class=\"button\" href=\"prefs.php\">Return to preferences</a>";
			} else {
				print "Error: could not parse document.";
			}
		} else {
			print "<p>Error: please upload XMLDB.</p>";
		}

	} ?>
</div>
</body>
</html>