]> git.wh0rd.org - tt-rss.git/blame - plugins/googlereaderimport/init.php
Merge pull request #98 from Joschasa/master
[tt-rss.git] / plugins / googlereaderimport / init.php
CommitLineData
493c2f88 1<?php
93e79595 2class GoogleReaderImport extends Plugin {
493c2f88
AD
3
4
5 private $link;
6 private $host;
7
8 function about() {
9 return array(1.0,
4c92878f 10 "Import Starred or Shared items from Google Reader takeout",
493c2f88
AD
11 "fox",
12 false,
13 "");
14 }
15
16 function init($host) {
17 $this->link = $host->get_link();
18 $this->host = $host;
19
4cf0f9a9
AD
20 $host->add_command("greader-import",
21 "import data in Google Reader JSON format",
22 $this, ":", "FILE");
23
493c2f88
AD
24 $host->add_hook($host::HOOK_PREFS_TAB, $this);
25 }
26
4cf0f9a9
AD
27 function greader_import($args) {
28 $file = $args['greader_import'];
29
30 if (!file_exists($file)) {
31 _debug("file not found: $file");
32 return;
33 }
34
35 _debug("please enter your username:");
36
37 $username = db_escape_string($this->link, trim(read_stdin()));
38
39 _debug("looking up user: $username...");
40
41 $result = db_query($this->link, "SELECT id FROM ttrss_users
42 WHERE login = '$username'");
43
44 if (db_num_rows($result) == 0) {
45 _debug("user not found.");
46 return;
47 }
48
49 $owner_uid = db_fetch_result($result, 0, "id");
50
51 _debug("processing: $file (owner_uid: $owner_uid)");
52
53 $this->import($file, $owner_uid);
54 }
55
493c2f88
AD
56 function get_prefs_js() {
57 return file_get_contents(dirname(__FILE__) . "/init.js");
58 }
59
4cf0f9a9 60 function import($file = false, $owner_uid = 0) {
493c2f88 61
4cf0f9a9
AD
62 if (!$file) {
63 header("Content-Type: text/html");
493c2f88 64
93e79595
AD
65 $owner_uid = $_SESSION["uid"];
66
4cf0f9a9
AD
67 if (is_file($_FILES['starred_file']['tmp_name'])) {
68 $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true);
69 } else {
70 print_error(__('No file uploaded.'));
71 return;
72 }
493c2f88 73 } else {
4cf0f9a9 74 $doc = json_decode(file_get_contents($file), true);
493c2f88
AD
75 }
76
4cf0f9a9
AD
77 if ($file) {
78 $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false';
79 _debug("will set articles as starred: $sql_set_marked");
80
81 } else {
82 $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false';
83 }
4c92878f 84
493c2f88
AD
85 if ($doc) {
86 if (isset($doc['items'])) {
87 $processed = 0;
88
89 foreach ($doc['items'] as $item) {
90// print_r($item);
91
92 $guid = db_escape_string($this->link, $item['id']);
93 $title = db_escape_string($this->link, $item['title']);
94 $updated = date('Y-m-d h:i:s', $item['updated']);
95 $link = '';
96 $content = '';
97 $author = db_escape_string($this->link, $item['author']);
98
99 if (is_array($item['alternate'])) {
100 foreach ($item['alternate'] as $alt) {
101 if (isset($alt['type']) && $alt['type'] == 'text/html') {
102 $link = db_escape_string($this->link, $alt['href']);
103 }
104 }
105 }
106
107 if (is_array($item['content'])) {
108 $content = db_escape_string($this->link,
109 $item['content']['content'], false);
110 }
111
112 $processed++;
113
4cf0f9a9 114 $imported += (int) $this->create_article($owner_uid, $guid, $title,
4c92878f 115 $updated, $link, $content, $author, $sql_set_marked);
493c2f88
AD
116
117 }
118
4cf0f9a9
AD
119 if ($file) {
120 _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
121 } else {
122 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
123 }
493c2f88
AD
124
125 } else {
126 print_error(__('The document has incorrect format.'));
127 }
128
129 } else {
130 print_error(__('Error while parsing document.'));
131 }
132
4cf0f9a9
AD
133 if (!$file) {
134 print "<div align='center'>";
135 print "<button dojoType=\"dijit.form.Button\"
136 onclick=\"dijit.byId('starredImportDlg').execute()\">".
137 __('Close this window')."</button>";
138 print "</div>";
139 }
493c2f88
AD
140 }
141
142 // expects ESCAPED data
4cf0f9a9 143 private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked) {
493c2f88
AD
144
145 if (!$guid) $guid = sha1($link);
146
147 $guid = "$owner_uid,$guid";
148
149 $content_hash = sha1($content);
150
151 if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false;
152
153 db_query($this->link, "BEGIN");
154
155 // only check for our user data here, others might have shared this with different content etc
156 $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
157 guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
158
159 if (db_num_rows($result) == 0) {
160 $result = db_query($this->link, "INSERT INTO ttrss_entries
161 (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
162 VALUES
163 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
164
165 $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
166
167 if (db_num_rows($result) != 0) {
168 $ref_id = db_fetch_result($result, 0, "id");
169
170 db_query($this->link, "INSERT INTO ttrss_user_entries
171 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
172 last_read, note, unread, last_marked)
173 VALUES
4c92878f 174 ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
493c2f88
AD
175
176 if (count($labels) != 0) {
177 foreach ($labels as $label) {
178 label_add_article($link, $ref_id, trim($label), $owner_uid);
179 }
180 }
181
182 $rc = true;
183 }
184 }
185
186 db_query($this->link, "COMMIT");
187
188 return $rc;
189 }
190
191 function hook_prefs_tab($args) {
192 if ($args != "prefFeeds") return;
193
4c92878f 194 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
493c2f88 195
4c92878f 196 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
493c2f88 197
4c92878f 198 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
493c2f88
AD
199
200 print "<iframe id=\"starred_upload_iframe\"
201 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
202 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
203
204 print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
205 enctype=\"multipart/form-data\" method=\"POST\"
206 action=\"backend.php\">
207 <input id=\"starred_file\" name=\"starred_file\" type=\"file\">&nbsp;
208 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
209 <input type=\"hidden\" name=\"method\" value=\"import\">
93e79595 210 <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
493c2f88
AD
211 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
212 __('Import my Starred items') . "</button>";
213
214
215 print "</div>"; #pane
216 }
217}
218?>