]>
git.wh0rd.org - tt-rss.git/blob - plugins/googlereaderimport/init.php
2 class GoogleReaderImport
extends Plugin
{
10 "Import starred/shared items from Google Reader takeout",
16 function init($host) {
17 $this->link
= $host->get_link();
20 $host->add_command("greader-import",
21 "import data in Google Reader JSON format",
24 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
27 function greader_import($args) {
28 $file = $args['greader_import'];
30 if (!file_exists($file)) {
31 _debug("file not found: $file");
35 _debug("please enter your username:");
37 $username = db_escape_string($this->link
, trim(read_stdin()));
39 _debug("looking up user: $username...");
41 $result = db_query($this->link
, "SELECT id FROM ttrss_users
42 WHERE login = '$username'");
44 if (db_num_rows($result) == 0) {
45 _debug("user not found.");
49 $owner_uid = db_fetch_result($result, 0, "id");
51 _debug("processing: $file (owner_uid: $owner_uid)");
53 $this->import($file, $owner_uid);
56 function get_prefs_js() {
57 return file_get_contents(dirname(__FILE__
) . "/init.js");
60 function import($file = false, $owner_uid = 0) {
63 header("Content-Type: text/html");
65 $owner_uid = $_SESSION["uid"];
67 if (is_file($_FILES['starred_file']['tmp_name'])) {
68 $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true);
70 print_error(__('No file uploaded.'));
74 $doc = json_decode(file_get_contents($file), true);
78 $sql_set_marked = strtolower(basename($file)) == 'starred.json' ?
'true' : 'false';
79 _debug("will set articles as starred: $sql_set_marked");
82 $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ?
'true' : 'false';
86 if (isset($doc['items'])) {
89 foreach ($doc['items'] as $item) {
92 $guid = db_escape_string($this->link
, mb_substr($item['id'], 0, 250));
93 $title = db_escape_string($this->link
, $item['title']);
94 $updated = date('Y-m-d h:i:s', $item['updated']);
97 $author = db_escape_string($this->link
, $item['author']);
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']);
108 if (is_array($item['content'])) {
109 $content = db_escape_string($this->link
,
110 $item['content']['content'], false);
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));
123 $imported +
= (int) $this->create_article($owner_uid, $guid, $title,
124 $updated, $link, $content, $author, $sql_set_marked, $tags);
129 _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
131 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
135 print_error(__('The document has incorrect format.'));
139 print_error(__('Error while parsing document.'));
143 print "<div align='center'>";
144 print "<button dojoType=\"dijit.form.Button\"
145 onclick=\"dijit.byId('starredImportDlg').execute()\">".
146 __('Close this window')."</button>";
151 // expects ESCAPED data
152 private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked, $tags) {
154 if (!$guid) $guid = sha1($link);
156 $guid = "$owner_uid,$guid";
158 $content_hash = sha1($content);
160 if (filter_var($link, FILTER_VALIDATE_URL
) === FALSE) return false;
162 db_query($this->link
, "BEGIN");
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");
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)
172 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
174 $result = db_query($this->link
, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
176 if (db_num_rows($result) != 0) {
177 $ref_id = db_fetch_result($result, 0, "id");
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)
183 ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
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");
188 if (db_num_rows($result) != 0 && is_array($tags)) {
190 $entry_int_id = db_fetch_result($result, 0, "int_id");
191 $tags_to_cache = array();
193 foreach ($tags as $tag) {
195 $tag = db_escape_string($this->link
, sanitize_tag($tag));
197 if (!tag_is_valid($tag)) continue;
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");
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')");
209 array_push($tags_to_cache, $tag);
212 /* update the cache */
214 $tags_to_cache = array_unique($tags_to_cache);
215 $tags_str = db_escape_string($this->link
, join(",", $tags_to_cache));
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");
226 db_query($this->link
, "COMMIT");
231 function hook_prefs_tab($args) {
232 if ($args != "prefFeeds") return;
234 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
236 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
238 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
240 print "<iframe id=\"starred_upload_iframe\"
241 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
242 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
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\">
250 <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
251 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
252 __('Import my Starred items') . "</button>";
255 print "</div>"; #pane