root/utils/xml-export.php

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