]>
git.wh0rd.org - tt-rss.git/blob - plugins/greaderstarredimport/init.php
7faa87295b7d30fffb4568db5ecdb99e3beb2cf7
2 class GreaderStarredImport
extends Plugin
{
10 "Import Starred or Shared 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.'));
38 $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ?
'true' : 'false';
41 if (isset($doc['items'])) {
44 foreach ($doc['items'] as $item) {
47 $guid = db_escape_string($this->link
, $item['id']);
48 $title = db_escape_string($this->link
, $item['title']);
49 $updated = date('Y-m-d h:i:s', $item['updated']);
52 $author = db_escape_string($this->link
, $item['author']);
54 if (is_array($item['alternate'])) {
55 foreach ($item['alternate'] as $alt) {
56 if (isset($alt['type']) && $alt['type'] == 'text/html') {
57 $link = db_escape_string($this->link
, $alt['href']);
62 if (is_array($item['content'])) {
63 $content = db_escape_string($this->link
,
64 $item['content']['content'], false);
69 $imported +
= (int) $this->create_article($guid, $title,
70 $updated, $link, $content, $author, $sql_set_marked);
74 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
77 print_error(__('The document has incorrect format.'));
81 print_error(__('Error while parsing document.'));
84 print "<div align='center'>";
85 print "<button dojoType=\"dijit.form.Button\"
86 onclick=\"dijit.byId('starredImportDlg').execute()\">".
87 __('Close this window')."</button>";
92 // expects ESCAPED data
93 private function create_article($guid, $title, $updated, $link, $content, $author, $marked) {
95 $owner_uid = $_SESSION["uid"];
97 if (!$guid) $guid = sha1($link);
99 $guid = "$owner_uid,$guid";
101 $content_hash = sha1($content);
103 if (filter_var($link, FILTER_VALIDATE_URL
) === FALSE) return false;
105 db_query($this->link
, "BEGIN");
107 // only check for our user data here, others might have shared this with different content etc
108 $result = db_query($this->link
, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
109 guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
111 if (db_num_rows($result) == 0) {
112 $result = db_query($this->link
, "INSERT INTO ttrss_entries
113 (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
115 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
117 $result = db_query($this->link
, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
119 if (db_num_rows($result) != 0) {
120 $ref_id = db_fetch_result($result, 0, "id");
122 db_query($this->link
, "INSERT INTO ttrss_user_entries
123 (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
124 last_read, note, unread, last_marked)
126 ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
128 if (count($labels) != 0) {
129 foreach ($labels as $label) {
130 label_add_article($link, $ref_id, trim($label), $owner_uid);
138 db_query($this->link
, "COMMIT");
143 function hook_prefs_tab($args) {
144 if ($args != "prefFeeds") return;
146 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
148 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
150 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
152 print "<iframe id=\"starred_upload_iframe\"
153 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
154 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
156 print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
157 enctype=\"multipart/form-data\" method=\"POST\"
158 action=\"backend.php\">
159 <input id=\"starred_file\" name=\"starred_file\" type=\"file\">
160 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
161 <input type=\"hidden\" name=\"method\" value=\"import\">
162 <input type=\"hidden\" name=\"plugin\" value=\"greaderstarredimport\">
163 <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
164 __('Import my Starred items') . "</button>";
167 print "</div>"; #pane