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