]>
Commit | Line | Data |
---|---|---|
493c2f88 | 1 | <?php |
93e79595 | 2 | class GoogleReaderImport extends Plugin { |
493c2f88 AD |
3 | |
4 | ||
5 | private $link; | |
6 | private $host; | |
7 | ||
8 | function about() { | |
9 | return array(1.0, | |
7c93962d | 10 | "Import starred/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 | ||
05e1e0ff | 92 | $guid = db_escape_string($this->link, mb_substr($item['id'], 0, 250)); |
493c2f88 AD |
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']); | |
4188225c | 98 | $tags = array(); |
493c2f88 AD |
99 | |
100 | if (is_array($item['alternate'])) { | |
101 | foreach ($item['alternate'] as $alt) { | |
102 | if (isset($alt['type']) && $alt['type'] == 'text/html') { | |
103 | $link = db_escape_string($this->link, $alt['href']); | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | if (is_array($item['content'])) { | |
109 | $content = db_escape_string($this->link, | |
110 | $item['content']['content'], false); | |
111 | } | |
112 | ||
4188225c AD |
113 | if (is_array($item['categories'])) { |
114 | foreach ($item['categories'] as $cat) { | |
115 | if (strstr($cat, "com.google/") === FALSE) { | |
116 | array_push($tags, sanitize_tag($cat)); | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
493c2f88 AD |
121 | $processed++; |
122 | ||
4cf0f9a9 | 123 | $imported += (int) $this->create_article($owner_uid, $guid, $title, |
4188225c | 124 | $updated, $link, $content, $author, $sql_set_marked, $tags); |
493c2f88 AD |
125 | |
126 | } | |
127 | ||
4cf0f9a9 AD |
128 | if ($file) { |
129 | _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed)); | |
130 | } else { | |
131 | print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>"; | |
132 | } | |
493c2f88 AD |
133 | |
134 | } else { | |
135 | print_error(__('The document has incorrect format.')); | |
136 | } | |
137 | ||
138 | } else { | |
139 | print_error(__('Error while parsing document.')); | |
140 | } | |
141 | ||
4cf0f9a9 AD |
142 | if (!$file) { |
143 | print "<div align='center'>"; | |
144 | print "<button dojoType=\"dijit.form.Button\" | |
145 | onclick=\"dijit.byId('starredImportDlg').execute()\">". | |
146 | __('Close this window')."</button>"; | |
147 | print "</div>"; | |
148 | } | |
493c2f88 AD |
149 | } |
150 | ||
151 | // expects ESCAPED data | |
4188225c | 152 | private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked, $tags) { |
493c2f88 AD |
153 | |
154 | if (!$guid) $guid = sha1($link); | |
155 | ||
156 | $guid = "$owner_uid,$guid"; | |
157 | ||
158 | $content_hash = sha1($content); | |
159 | ||
160 | if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false; | |
161 | ||
162 | db_query($this->link, "BEGIN"); | |
163 | ||
164 | // only check for our user data here, others might have shared this with different content etc | |
165 | $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE | |
166 | guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1"); | |
167 | ||
168 | if (db_num_rows($result) == 0) { | |
169 | $result = db_query($this->link, "INSERT INTO ttrss_entries | |
170 | (title, guid, link, updated, content, content_hash, date_entered, date_updated, author) | |
171 | VALUES | |
172 | ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')"); | |
173 | ||
174 | $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'"); | |
175 | ||
176 | if (db_num_rows($result) != 0) { | |
177 | $ref_id = db_fetch_result($result, 0, "id"); | |
178 | ||
179 | db_query($this->link, "INSERT INTO ttrss_user_entries | |
180 | (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache, | |
181 | last_read, note, unread, last_marked) | |
182 | VALUES | |
4c92878f | 183 | ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())"); |
493c2f88 | 184 | |
4188225c AD |
185 | $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries, ttrss_entries |
186 | WHERE owner_uid = $owner_uid AND ref_id = id AND ref_id = $ref_id"); | |
187 | ||
188 | if (db_num_rows($result) != 0 && is_array($tags)) { | |
189 | ||
190 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
191 | $tags_to_cache = array(); | |
192 | ||
193 | foreach ($tags as $tag) { | |
194 | ||
195 | $tag = db_escape_string($this->link, sanitize_tag($tag)); | |
196 | ||
197 | if (!tag_is_valid($tag)) continue; | |
198 | ||
199 | $result = db_query($this->link, "SELECT id FROM ttrss_tags | |
200 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
201 | owner_uid = '$owner_uid' LIMIT 1"); | |
202 | ||
203 | if ($result && db_num_rows($result) == 0) { | |
204 | db_query($this->link, "INSERT INTO ttrss_tags | |
205 | (owner_uid,tag_name,post_int_id) | |
206 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
207 | } | |
208 | ||
209 | array_push($tags_to_cache, $tag); | |
493c2f88 | 210 | } |
4188225c AD |
211 | |
212 | /* update the cache */ | |
213 | ||
214 | $tags_to_cache = array_unique($tags_to_cache); | |
215 | $tags_str = db_escape_string($this->link, join(",", $tags_to_cache)); | |
216 | ||
217 | db_query($this->link, "UPDATE ttrss_user_entries | |
218 | SET tag_cache = '$tags_str' WHERE ref_id = '$ref_id' | |
219 | AND owner_uid = $owner_uid"); | |
493c2f88 AD |
220 | } |
221 | ||
222 | $rc = true; | |
223 | } | |
224 | } | |
225 | ||
226 | db_query($this->link, "COMMIT"); | |
227 | ||
228 | return $rc; | |
229 | } | |
230 | ||
231 | function hook_prefs_tab($args) { | |
232 | if ($args != "prefFeeds") return; | |
233 | ||
4c92878f | 234 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">"; |
493c2f88 | 235 | |
4c92878f | 236 | print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds."); |
493c2f88 | 237 | |
4c92878f | 238 | print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>"; |
493c2f88 AD |
239 | |
240 | print "<iframe id=\"starred_upload_iframe\" | |
241 | name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\" | |
242 | style=\"width: 400px; height: 100px; display: none;\"></iframe>"; | |
243 | ||
244 | print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\" | |
245 | enctype=\"multipart/form-data\" method=\"POST\" | |
246 | action=\"backend.php\"> | |
247 | <input id=\"starred_file\" name=\"starred_file\" type=\"file\"> | |
248 | <input type=\"hidden\" name=\"op\" value=\"pluginhandler\"> | |
249 | <input type=\"hidden\" name=\"method\" value=\"import\"> | |
93e79595 | 250 | <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\"> |
493c2f88 AD |
251 | <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" . |
252 | __('Import my Starred items') . "</button>"; | |
253 | ||
254 | ||
255 | print "</div>"; #pane | |
256 | } | |
257 | } | |
258 | ?> |