]>
git.wh0rd.org - tt-rss.git/blob - plugins/greaderstarredimport/init.php
2 class GreaderStarredImport
extends Plugin
{
10 "Import starred items from Google Reader takeout",
16 function init($host) {
17 $this->link
= $host->get_link();
20 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
23 function get_prefs_js() {
24 return file_get_contents(dirname(__FILE__
) . "/init.js");
29 header("Content-Type: text/html");
31 if (is_file($_FILES['starred_file']['tmp_name'])) {
32 $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true);
34 print_error(__('No file uploaded.'));
39 if (isset($doc['items'])) {
42 foreach ($doc['items'] as $item) {
45 $guid = db_escape_string($this->link
, $item['id']);
46 $title = db_escape_string($this->link
, $item['title']);
47 $updated = date('Y-m-d h:i:s', $item['updated']);
50 $author = db_escape_string($this->link
, $item['author']);
52 if (is_array($item['alternate'])) {
53 foreach ($item['alternate'] as $alt) {
54 if (isset($alt['type']) && $alt['type'] == 'text/html') {
55 $link = db_escape_string($this->link
, $alt['href']);
60 if (is_array($item['content'])) {
61 $content = db_escape_string($this->link
,
62 $item['content']['content'], false);
67 $imported +
= (int) $this->create_article($guid, $title,
68 $updated, $link, $content, $author);
72 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
75 print_error(__('The document has incorrect format.'));
79 print_error(__('Error while parsing document.'));
82 print "<div align='center'>";
83 print "<button dojoType=\"dijit.form.Button\"
84 onclick=\"dijit.byId('starredImportDlg').execute()\">".
85 __('Close this window')."</button>";
90 // expects ESCAPED data
91 private function create_article($guid, $title, $updated, $link, $content, $author) {
93 $owner_uid = $_SESSION["uid"];
95 if (!$guid) $guid = sha1($link);
97 $guid = "$owner_uid,$guid";
99 $content_hash = sha1($content);
101 if (filter_var($link, FILTER_VALIDATE_URL
) === FALSE) return false;
103 db_query($this->link
, "BEGIN");
105 // only check for our user data here, others might have shared this with different content etc
106 $result = db_query($this->link
, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
107 guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
109 if (db_num_rows($result) == 0) {
110 $result = db_query($this->link
, "INSERT INTO ttrss_entries
111 (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
113 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
115 $result = db_query($this->link
, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
117 if (db_num_rows($result) != 0) {
118 $ref_id = db_fetch_result($result, 0, "id");
120 db_query($this->link
, "INSERT INTO ttrss_user_entries
121 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
122 last_read, note, unread, last_marked)
124 ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
126 if (count($labels) != 0) {
127 foreach ($labels as $label) {
128 label_add_article($link, $ref_id, trim($label), $owner_uid);
136 db_query($this->link
, "COMMIT");
141 function hook_prefs_tab($args) {
142 if ($args != "prefFeeds") return;
144 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred items from Google Reader")."\">";
146 print_notice("Your imported articles will appear in Starred and Archived feeds.");
148 print "<p>".__("Paste your starred.json into the form below."). "</p>";
150 print "<iframe id=\"starred_upload_iframe\"
151 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
152 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
154 print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
155 enctype=\"multipart/form-data\" method=\"POST\"
156 action=\"backend.php\">
157 <input id=\"starred_file\" name=\"starred_file\" type=\"file\">
158 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
159 <input type=\"hidden\" name=\"method\" value=\"import\">
160 <input type=\"hidden\" name=\"plugin\" value=\"greaderstarredimport\">
161 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
162 __('Import my Starred items') . "</button>";
165 print "</div>"; #pane