]>
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']);
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']);
107 if (is_array($item['content'])) {
108 $content = db_escape_string($this->link
,
109 $item['content']['content'], false);
114 $imported +
= (int) $this->create_article($owner_uid, $guid, $title,
115 $updated, $link, $content, $author, $sql_set_marked);
120 _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
122 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
126 print_error(__('The document has incorrect format.'));
130 print_error(__('Error while parsing document.'));
134 print "<div align='center'>";
135 print "<button dojoType=\"dijit.form.Button\"
136 onclick=\"dijit.byId('starredImportDlg').execute()\">".
137 __('Close this window')."</button>";
142 // expects ESCAPED data
143 private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked) {
145 if (!$guid) $guid = sha1($link);
147 $guid = "$owner_uid,$guid";
149 $content_hash = sha1($content);
151 if (filter_var($link, FILTER_VALIDATE_URL
) === FALSE) return false;
153 db_query($this->link
, "BEGIN");
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");
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)
163 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
165 $result = db_query($this->link
, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
167 if (db_num_rows($result) != 0) {
168 $ref_id = db_fetch_result($result, 0, "id");
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)
174 ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
176 if (count($labels) != 0) {
177 foreach ($labels as $label) {
178 label_add_article($link, $ref_id, trim($label), $owner_uid);
186 db_query($this->link
, "COMMIT");
191 function hook_prefs_tab($args) {
192 if ($args != "prefFeeds") return;
194 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
196 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
198 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
200 print "<iframe id=\"starred_upload_iframe\"
201 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
202 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
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\">
208 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
209 <input type=\"hidden\" name=\"method\" value=\"import\">
210 <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
211 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
212 __('Import my Starred items') . "</button>";
215 print "</div>"; #pane