]> git.wh0rd.org - tt-rss.git/blob - xml-export.php
TAG 1.1
[tt-rss.git] / xml-export.php
1 <?
2 require_once "config.php";
3 require_once "functions.php";
4 require_once "db.php";
5
6 if ($_GET["export"]) {
7 header("Content-Type: application/xml");
8 }
9 ?>
10
11 <? if (!$_GET["export"]) { ?>
12
13 <html>
14 <head>
15 <title>XML Export</title>
16 <link rel="stylesheet" href="opml.css" type="text/css">
17 </head>
18 <body>
19 <h1><img src="images/ttrss_logo.png"></h1>
20
21 <div class="opmlBody">
22 <h2>XML Export</h2>
23 <form method="GET">
24 Limit to: <input type="checkbox" checked name="marked"> starred,
25 <input type="checkbox" name="unread"> unread.<br>
26 <p><input type="submit" class="button" name="export" value="Export"></p>
27 </form>
28 </div>
29
30 </body>
31 </html>
32
33 <? } else { ?>
34
35 <xmldb>
36
37 <?
38 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
39
40 if (!$link) {
41 if (DB_TYPE == "mysql") {
42 print mysql_error();
43 }
44 // PG seems to display its own errors just fine by default.
45 return;
46 }
47
48 if (DB_TYPE == "pgsql") {
49 pg_query("set client_encoding = 'utf-8'");
50 }
51
52 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
53
54 $schema_version = db_fetch_result($result, 0, "schema_version");
55
56 /* if ($schema_version != SCHEMA_VERSION) {
57 print "<error>Source database schema is invalid
58 (got version $schema_version; expected ".SCHEMA_VERSION.")</error>";
59 print "</xmldb>";
60 return;
61 } */
62
63 print "<schema_version>$schema_version</schema_version>";
64
65 if ($schema_version > 1) {
66 $owner_uid = $_SESSION["uid"];
67 print "<owner_uid>$owner_uid</owner_uid>";
68 }
69
70 print "<exported>" . time() . "</exported>";
71 ?>
72
73 <?
74 if ($_GET["marked"]) {
75 $marked_qpart = "AND marked = true";
76 }
77
78 if ($_GET["unread"]) {
79 $unread_qpart = "AND unread = true";
80 }
81
82 if ($schema_version == 1) {
83
84 $result = db_query($link, "SELECT
85 ttrss_entries.title AS title,
86 content,
87 marked,
88 unread,
89 updated,
90 guid,
91 link,
92 SUBSTRING(date_entered,1,16) AS date_entered,
93 SUBSTRING(last_read,1,16) AS last_read,
94 comments,
95 ttrss_feeds.feed_url AS feed_url,
96 ttrss_feeds.title AS feed_title
97 FROM
98 ttrss_entries,ttrss_feeds
99 WHERE
100 feed_id = ttrss_feeds.id $marked_qpart $unread_qpart
101 ORDER BY ttrss_entries.id");
102
103 } else if ($schema_version == 2) {
104
105 $result = db_query($link, "SELECT
106 ttrss_entries.title AS title,
107 content,
108 marked,
109 unread,
110 updated,
111 guid,
112 link,
113 SUBSTRING(date_entered,1,16) AS date_entered,
114 SUBSTRING(last_read,1,16) AS last_read,
115 comments,
116 ttrss_feeds.feed_url AS feed_url,
117 ttrss_feeds.title AS feed_title
118 FROM
119 ttrss_entries,ttrss_feeds,ttrss_user_entries
120 WHERE
121 ttrss_user_entries.owner_uid = '$owner_uid' AND
122 ref_id = ttrss_entries.id AND
123 feed_id = ttrss_feeds.id $marked_qpart $unread_qpart
124 ORDER BY ttrss_entries.id");
125
126 } else {
127
128 // BAD SCHEMA, NO COOKIE
129
130 print "<error>Source database schema is invalid
131 (got version $schema_version)</error>";
132 }
133
134 print "<total_articles>" . db_num_rows($result) . "</total_articles>";
135
136 ?>
137
138 <articles>
139
140 <?
141 while ($line = db_fetch_assoc($result)) {
142 print "<article>";
143
144 foreach (array_keys($line) as $key) {
145 $line[$key] = str_replace("<![CDATA[", "", $line[$key]);
146 $line[$key] = str_replace("]]>", "", $line[$key]);
147
148 print "<$key><![CDATA[".$line[$key]."]]></$key>";
149
150 }
151
152 print "</article>";
153 }
154
155 ?>
156 </articles>
157
158 </xmldb>
159
160 <? } ?>