]> git.wh0rd.org - tt-rss.git/blame - plugins/googlereaderimport/init.php
remove $link
[tt-rss.git] / plugins / googlereaderimport / init.php
CommitLineData
493c2f88 1<?php
93e79595 2class GoogleReaderImport extends Plugin {
493c2f88
AD
3 private $host;
4
5 function about() {
6 return array(1.0,
7c93962d 7 "Import starred/shared items from Google Reader takeout",
493c2f88
AD
8 "fox",
9 false,
10 "");
11 }
12
13 function init($host) {
493c2f88
AD
14 $this->host = $host;
15
4cf0f9a9
AD
16 $host->add_command("greader-import",
17 "import data in Google Reader JSON format",
18 $this, ":", "FILE");
19
493c2f88
AD
20 $host->add_hook($host::HOOK_PREFS_TAB, $this);
21 }
22
4cf0f9a9
AD
23 function greader_import($args) {
24 $file = $args['greader_import'];
25
26 if (!file_exists($file)) {
27 _debug("file not found: $file");
28 return;
29 }
30
31 _debug("please enter your username:");
32
6322ac79 33 $username = db_escape_string( trim(read_stdin()));
4cf0f9a9
AD
34
35 _debug("looking up user: $username...");
36
6322ac79 37 $result = db_query( "SELECT id FROM ttrss_users
4cf0f9a9
AD
38 WHERE login = '$username'");
39
40 if (db_num_rows($result) == 0) {
41 _debug("user not found.");
42 return;
43 }
44
45 $owner_uid = db_fetch_result($result, 0, "id");
46
47 _debug("processing: $file (owner_uid: $owner_uid)");
48
49 $this->import($file, $owner_uid);
50 }
51
493c2f88
AD
52 function get_prefs_js() {
53 return file_get_contents(dirname(__FILE__) . "/init.js");
54 }
55
4cf0f9a9 56 function import($file = false, $owner_uid = 0) {
493c2f88 57
6322ac79 58 purge_orphans();
749da11b 59
4cf0f9a9
AD
60 if (!$file) {
61 header("Content-Type: text/html");
493c2f88 62
93e79595
AD
63 $owner_uid = $_SESSION["uid"];
64
3306daec
AD
65 if ($_FILES['starred_file']['error'] != 0) {
66 print_error(T_sprintf("Upload failed with error code %d",
67 $_FILES['starred_file']['error']));
68 return;
69 }
70
71 $tmp_file = false;
72
73 if (is_uploaded_file($_FILES['starred_file']['tmp_name'])) {
74 $tmp_file = tempnam(CACHE_DIR . '/upload', 'starred');
75
76 $result = move_uploaded_file($_FILES['starred_file']['tmp_name'],
77 $tmp_file);
78
79 if (!$result) {
80 print_error(__("Unable to move uploaded file."));
81 return;
82 }
83 } else {
84 print_error(__('Error: please upload OPML file.'));
85 return;
86 }
87
88 if (is_file($tmp_file)) {
89 $doc = json_decode(file_get_contents($tmp_file), true);
90 unlink($tmp_file);
4cf0f9a9
AD
91 } else {
92 print_error(__('No file uploaded.'));
93 return;
94 }
493c2f88 95 } else {
4cf0f9a9 96 $doc = json_decode(file_get_contents($file), true);
493c2f88
AD
97 }
98
4cf0f9a9
AD
99 if ($file) {
100 $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false';
101 _debug("will set articles as starred: $sql_set_marked");
102
103 } else {
104 $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false';
105 }
4c92878f 106
493c2f88
AD
107 if ($doc) {
108 if (isset($doc['items'])) {
109 $processed = 0;
110
111 foreach ($doc['items'] as $item) {
112// print_r($item);
113
6322ac79
AD
114 $guid = db_escape_string( mb_substr($item['id'], 0, 250));
115 $title = db_escape_string( $item['title']);
493c2f88
AD
116 $updated = date('Y-m-d h:i:s', $item['updated']);
117 $link = '';
118 $content = '';
6322ac79 119 $author = db_escape_string( $item['author']);
4188225c 120 $tags = array();
749da11b 121 $orig_feed_data = array();
493c2f88
AD
122
123 if (is_array($item['alternate'])) {
124 foreach ($item['alternate'] as $alt) {
125 if (isset($alt['type']) && $alt['type'] == 'text/html') {
6322ac79 126 $link = db_escape_string( $alt['href']);
493c2f88
AD
127 }
128 }
129 }
130
b87bd8ed 131 if (is_array($item['summary'])) {
6322ac79 132 $content = db_escape_string(
b87bd8ed
AD
133 $item['summary']['content'], false);
134 }
135
493c2f88 136 if (is_array($item['content'])) {
6322ac79 137 $content = db_escape_string(
493c2f88
AD
138 $item['content']['content'], false);
139 }
140
4188225c
AD
141 if (is_array($item['categories'])) {
142 foreach ($item['categories'] as $cat) {
143 if (strstr($cat, "com.google/") === FALSE) {
144 array_push($tags, sanitize_tag($cat));
145 }
146 }
147 }
148
749da11b
AD
149 if (is_array($item['origin'])) {
150 if (strpos($item['origin']['streamId'], 'feed/') === 0) {
151
6322ac79 152 $orig_feed_data['feed_url'] = db_escape_string(
61096207
AD
153 mb_substr(preg_replace("/^feed\//",
154 "", $item['origin']['streamId']), 0, 200));
749da11b 155
6322ac79 156 $orig_feed_data['title'] = db_escape_string(
61096207 157 mb_substr($item['origin']['title'], 0, 200));
749da11b 158
6322ac79 159 $orig_feed_data['site_url'] = db_escape_string(
61096207 160 mb_substr($item['origin']['htmlUrl'], 0, 200));
749da11b
AD
161 }
162 }
163
493c2f88
AD
164 $processed++;
165
4cf0f9a9 166 $imported += (int) $this->create_article($owner_uid, $guid, $title,
6322ac79 167 $updated, $content, $author, $sql_set_marked, $tags,
749da11b 168 $orig_feed_data);
b9fc62a7
AD
169
170 if ($file && $processed % 25 == 0) {
171 _debug("processed $processed articles...");
172 }
493c2f88
AD
173 }
174
4cf0f9a9
AD
175 if ($file) {
176 _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
177 } else {
178 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
179 }
493c2f88
AD
180
181 } else {
182 print_error(__('The document has incorrect format.'));
183 }
184
185 } else {
186 print_error(__('Error while parsing document.'));
187 }
188
4cf0f9a9
AD
189 if (!$file) {
190 print "<div align='center'>";
191 print "<button dojoType=\"dijit.form.Button\"
192 onclick=\"dijit.byId('starredImportDlg').execute()\">".
193 __('Close this window')."</button>";
194 print "</div>";
195 }
493c2f88
AD
196 }
197
198 // expects ESCAPED data
6322ac79 199 private function create_article($owner_uid, $guid, $title, $updated, $content, $author, $marked, $tags, $orig_feed_data) {
493c2f88
AD
200
201 if (!$guid) $guid = sha1($link);
202
b9fc62a7 203 $create_archived_feeds = true;
fc5cd158 204
493c2f88
AD
205 $guid = "$owner_uid,$guid";
206
207 $content_hash = sha1($content);
208
6322ac79 209 if (filter_var( FILTER_VALIDATE_URL) === FALSE) return false;
493c2f88 210
6322ac79 211 db_query( "BEGIN");
493c2f88 212
749da11b
AD
213 $feed_id = 'NULL';
214
215 // let's check for archived feed entry
216
217 $feed_inserted = false;
218
219 // before dealing with archived feeds we must check ttrss_feeds to maintain id consistency
220
fc5cd158 221 if ($orig_feed_data['feed_url'] && $create_archived_feeds) {
6322ac79 222 $result = db_query(
749da11b
AD
223 "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
224 AND owner_uid = $owner_uid");
225
226 if (db_num_rows($result) != 0) {
227 $feed_id = db_fetch_result($result, 0, "id");
228 } else {
229 // let's insert it
230
231 if (!$orig_feed_data['title']) $orig_feed_data['title'] = '[Unknown]';
232
6322ac79 233 $result = db_query(
749da11b
AD
234 "INSERT INTO ttrss_feeds
235 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
236 VALUES ($owner_uid,
237 '".$orig_feed_data['feed_url']."',
238 '".$orig_feed_data['site_url']."',
239 '".$orig_feed_data['title']."',
240 NULL, '', '', 0)");
241
6322ac79 242 $result = db_query(
749da11b
AD
243 "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
244 AND owner_uid = $owner_uid");
245
246 if (db_num_rows($result) != 0) {
247 $feed_id = db_fetch_result($result, 0, "id");
248 $feed_inserted = true;
249 }
250 }
251 }
252
61096207 253 if ($feed_id && $feed_id != 'NULL') {
749da11b
AD
254 // locate archived entry to file entries in, we don't want to file them in actual feeds because of purging
255 // maybe file marked in real feeds because eh
256
6322ac79 257 $result = db_query( "SELECT id FROM ttrss_archived_feeds WHERE
749da11b
AD
258 feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
259
260 if (db_num_rows($result) != 0) {
261 $orig_feed_id = db_fetch_result($result, 0, "id");
262 } else {
6322ac79 263 db_query( "INSERT INTO ttrss_archived_feeds
749da11b
AD
264 (id, owner_uid, title, feed_url, site_url)
265 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
266 WHERE id = '$feed_id'");
267
6322ac79 268 $result = db_query( "SELECT id FROM ttrss_archived_feeds WHERE
749da11b
AD
269 feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
270
271 if (db_num_rows($result) != 0) {
272 $orig_feed_id = db_fetch_result($result, 0, "id");
749da11b
AD
273 }
274 }
275 }
276
b9fc62a7
AD
277 // delete temporarily inserted feed
278 if ($feed_id && $feed_inserted) {
6322ac79 279 db_query( "DELETE FROM ttrss_feeds WHERE id = $feed_id");
b9fc62a7
AD
280 }
281
61096207
AD
282 if (!$orig_feed_id) $orig_feed_id = 'NULL';
283
6322ac79 284 $result = db_query( "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
493c2f88
AD
285 guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
286
287 if (db_num_rows($result) == 0) {
6322ac79 288 $result = db_query( "INSERT INTO ttrss_entries
493c2f88
AD
289 (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
290 VALUES
291 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
292
6322ac79 293 $result = db_query( "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
493c2f88
AD
294
295 if (db_num_rows($result) != 0) {
296 $ref_id = db_fetch_result($result, 0, "id");
297
6322ac79 298 db_query( "INSERT INTO ttrss_user_entries
493c2f88
AD
299 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
300 last_read, note, unread, last_marked)
301 VALUES
b9fc62a7 302 ('$ref_id', '', NULL, $orig_feed_id, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
493c2f88 303
6322ac79 304 $result = db_query( "SELECT int_id FROM ttrss_user_entries, ttrss_entries
4188225c
AD
305 WHERE owner_uid = $owner_uid AND ref_id = id AND ref_id = $ref_id");
306
307 if (db_num_rows($result) != 0 && is_array($tags)) {
308
309 $entry_int_id = db_fetch_result($result, 0, "int_id");
310 $tags_to_cache = array();
311
312 foreach ($tags as $tag) {
313
6322ac79 314 $tag = db_escape_string( sanitize_tag($tag));
4188225c
AD
315
316 if (!tag_is_valid($tag)) continue;
317
6322ac79 318 $result = db_query( "SELECT id FROM ttrss_tags
4188225c
AD
319 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
320 owner_uid = '$owner_uid' LIMIT 1");
321
322 if ($result && db_num_rows($result) == 0) {
6322ac79 323 db_query( "INSERT INTO ttrss_tags
4188225c
AD
324 (owner_uid,tag_name,post_int_id)
325 VALUES ('$owner_uid','$tag', '$entry_int_id')");
326 }
327
328 array_push($tags_to_cache, $tag);
493c2f88 329 }
4188225c
AD
330
331 /* update the cache */
332
333 $tags_to_cache = array_unique($tags_to_cache);
6322ac79 334 $tags_str = db_escape_string( join(",", $tags_to_cache));
4188225c 335
6322ac79 336 db_query( "UPDATE ttrss_user_entries
4188225c
AD
337 SET tag_cache = '$tags_str' WHERE ref_id = '$ref_id'
338 AND owner_uid = $owner_uid");
493c2f88
AD
339 }
340
341 $rc = true;
342 }
343 }
344
6322ac79 345 db_query( "COMMIT");
493c2f88
AD
346
347 return $rc;
348 }
349
350 function hook_prefs_tab($args) {
351 if ($args != "prefFeeds") return;
352
4c92878f 353 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
493c2f88 354
4c92878f 355 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
493c2f88 356
4c92878f 357 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
493c2f88
AD
358
359 print "<iframe id=\"starred_upload_iframe\"
360 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
361 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
362
363 print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
364 enctype=\"multipart/form-data\" method=\"POST\"
365 action=\"backend.php\">
366 <input id=\"starred_file\" name=\"starred_file\" type=\"file\">&nbsp;
367 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
368 <input type=\"hidden\" name=\"method\" value=\"import\">
93e79595 369 <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
493c2f88
AD
370 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
371 __('Import my Starred items') . "</button>";
372
b229a184 373 print "</form>";
493c2f88
AD
374
375 print "</div>"; #pane
376 }
377}
378?>