]>
Commit | Line | Data |
---|---|---|
493c2f88 AD |
1 | <?php |
2 | class GreaderStarredImport extends Plugin { | |
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 | |
4cf0f9a9 AD |
65 | if (is_file($_FILES['starred_file']['tmp_name'])) { |
66 | $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true); | |
67 | } else { | |
68 | print_error(__('No file uploaded.')); | |
69 | return; | |
70 | } | |
493c2f88 | 71 | } else { |
4cf0f9a9 | 72 | $doc = json_decode(file_get_contents($file), true); |
493c2f88 AD |
73 | } |
74 | ||
4cf0f9a9 AD |
75 | if ($file) { |
76 | $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false'; | |
77 | _debug("will set articles as starred: $sql_set_marked"); | |
78 | ||
79 | } else { | |
80 | $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false'; | |
81 | } | |
4c92878f | 82 | |
493c2f88 AD |
83 | if ($doc) { |
84 | if (isset($doc['items'])) { | |
85 | $processed = 0; | |
86 | ||
87 | foreach ($doc['items'] as $item) { | |
88 | // print_r($item); | |
89 | ||
90 | $guid = db_escape_string($this->link, $item['id']); | |
91 | $title = db_escape_string($this->link, $item['title']); | |
92 | $updated = date('Y-m-d h:i:s', $item['updated']); | |
93 | $link = ''; | |
94 | $content = ''; | |
95 | $author = db_escape_string($this->link, $item['author']); | |
96 | ||
97 | if (is_array($item['alternate'])) { | |
98 | foreach ($item['alternate'] as $alt) { | |
99 | if (isset($alt['type']) && $alt['type'] == 'text/html') { | |
100 | $link = db_escape_string($this->link, $alt['href']); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | if (is_array($item['content'])) { | |
106 | $content = db_escape_string($this->link, | |
107 | $item['content']['content'], false); | |
108 | } | |
109 | ||
110 | $processed++; | |
111 | ||
4cf0f9a9 | 112 | $imported += (int) $this->create_article($owner_uid, $guid, $title, |
4c92878f | 113 | $updated, $link, $content, $author, $sql_set_marked); |
493c2f88 AD |
114 | |
115 | } | |
116 | ||
4cf0f9a9 AD |
117 | if ($file) { |
118 | _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed)); | |
119 | } else { | |
120 | print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>"; | |
121 | } | |
493c2f88 AD |
122 | |
123 | } else { | |
124 | print_error(__('The document has incorrect format.')); | |
125 | } | |
126 | ||
127 | } else { | |
128 | print_error(__('Error while parsing document.')); | |
129 | } | |
130 | ||
4cf0f9a9 AD |
131 | if (!$file) { |
132 | print "<div align='center'>"; | |
133 | print "<button dojoType=\"dijit.form.Button\" | |
134 | onclick=\"dijit.byId('starredImportDlg').execute()\">". | |
135 | __('Close this window')."</button>"; | |
136 | print "</div>"; | |
137 | } | |
493c2f88 AD |
138 | } |
139 | ||
140 | // expects ESCAPED data | |
4cf0f9a9 | 141 | private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked) { |
493c2f88 AD |
142 | |
143 | if (!$guid) $guid = sha1($link); | |
144 | ||
145 | $guid = "$owner_uid,$guid"; | |
146 | ||
147 | $content_hash = sha1($content); | |
148 | ||
149 | if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false; | |
150 | ||
151 | db_query($this->link, "BEGIN"); | |
152 | ||
153 | // only check for our user data here, others might have shared this with different content etc | |
154 | $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE | |
155 | guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1"); | |
156 | ||
157 | if (db_num_rows($result) == 0) { | |
158 | $result = db_query($this->link, "INSERT INTO ttrss_entries | |
159 | (title, guid, link, updated, content, content_hash, date_entered, date_updated, author) | |
160 | VALUES | |
161 | ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')"); | |
162 | ||
163 | $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'"); | |
164 | ||
165 | if (db_num_rows($result) != 0) { | |
166 | $ref_id = db_fetch_result($result, 0, "id"); | |
167 | ||
168 | db_query($this->link, "INSERT INTO ttrss_user_entries | |
169 | (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache, | |
170 | last_read, note, unread, last_marked) | |
171 | VALUES | |
4c92878f | 172 | ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())"); |
493c2f88 AD |
173 | |
174 | if (count($labels) != 0) { | |
175 | foreach ($labels as $label) { | |
176 | label_add_article($link, $ref_id, trim($label), $owner_uid); | |
177 | } | |
178 | } | |
179 | ||
180 | $rc = true; | |
181 | } | |
182 | } | |
183 | ||
184 | db_query($this->link, "COMMIT"); | |
185 | ||
186 | return $rc; | |
187 | } | |
188 | ||
189 | function hook_prefs_tab($args) { | |
190 | if ($args != "prefFeeds") return; | |
191 | ||
4c92878f | 192 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">"; |
493c2f88 | 193 | |
4c92878f | 194 | print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds."); |
493c2f88 | 195 | |
4c92878f | 196 | print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>"; |
493c2f88 AD |
197 | |
198 | print "<iframe id=\"starred_upload_iframe\" | |
199 | name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\" | |
200 | style=\"width: 400px; height: 100px; display: none;\"></iframe>"; | |
201 | ||
202 | print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\" | |
203 | enctype=\"multipart/form-data\" method=\"POST\" | |
204 | action=\"backend.php\"> | |
205 | <input id=\"starred_file\" name=\"starred_file\" type=\"file\"> | |
206 | <input type=\"hidden\" name=\"op\" value=\"pluginhandler\"> | |
207 | <input type=\"hidden\" name=\"method\" value=\"import\"> | |
208 | <input type=\"hidden\" name=\"plugin\" value=\"greaderstarredimport\"> | |
209 | <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" . | |
210 | __('Import my Starred items') . "</button>"; | |
211 | ||
212 | ||
213 | print "</div>"; #pane | |
214 | } | |
215 | } | |
216 | ?> |