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