]> git.wh0rd.org - tt-rss.git/blob - plugins/greaderstarredimport/init.php
7faa87295b7d30fffb4568db5ecdb99e3beb2cf7
[tt-rss.git] / plugins / greaderstarredimport / init.php
1 <?php
2 class GreaderStarredImport extends Plugin {
3
4
5 private $link;
6 private $host;
7
8 function about() {
9 return array(1.0,
10 "Import Starred or Shared items from Google Reader takeout",
11 "fox",
12 false,
13 "");
14 }
15
16 function init($host) {
17 $this->link = $host->get_link();
18 $this->host = $host;
19
20 $host->add_hook($host::HOOK_PREFS_TAB, $this);
21 }
22
23 function get_prefs_js() {
24 return file_get_contents(dirname(__FILE__) . "/init.js");
25 }
26
27 function import() {
28
29 header("Content-Type: text/html");
30
31 if (is_file($_FILES['starred_file']['tmp_name'])) {
32 $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true);
33 } else {
34 print_error(__('No file uploaded.'));
35 return;
36 }
37
38 $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false';
39
40 if ($doc) {
41 if (isset($doc['items'])) {
42 $processed = 0;
43
44 foreach ($doc['items'] as $item) {
45 // print_r($item);
46
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']);
50 $link = '';
51 $content = '';
52 $author = db_escape_string($this->link, $item['author']);
53
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']);
58 }
59 }
60 }
61
62 if (is_array($item['content'])) {
63 $content = db_escape_string($this->link,
64 $item['content']['content'], false);
65 }
66
67 $processed++;
68
69 $imported += (int) $this->create_article($guid, $title,
70 $updated, $link, $content, $author, $sql_set_marked);
71
72 }
73
74 print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
75
76 } else {
77 print_error(__('The document has incorrect format.'));
78 }
79
80 } else {
81 print_error(__('Error while parsing document.'));
82 }
83
84 print "<div align='center'>";
85 print "<button dojoType=\"dijit.form.Button\"
86 onclick=\"dijit.byId('starredImportDlg').execute()\">".
87 __('Close this window')."</button>";
88 print "</div>";
89
90 }
91
92 // expects ESCAPED data
93 private function create_article($guid, $title, $updated, $link, $content, $author, $marked) {
94
95 $owner_uid = $_SESSION["uid"];
96
97 if (!$guid) $guid = sha1($link);
98
99 $guid = "$owner_uid,$guid";
100
101 $content_hash = sha1($content);
102
103 if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false;
104
105 db_query($this->link, "BEGIN");
106
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");
110
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)
114 VALUES
115 ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
116
117 $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
118
119 if (db_num_rows($result) != 0) {
120 $ref_id = db_fetch_result($result, 0, "id");
121
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)
125 VALUES
126 ('$ref_id', '', NULL, NULL, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
127
128 if (count($labels) != 0) {
129 foreach ($labels as $label) {
130 label_add_article($link, $ref_id, trim($label), $owner_uid);
131 }
132 }
133
134 $rc = true;
135 }
136 }
137
138 db_query($this->link, "COMMIT");
139
140 return $rc;
141 }
142
143 function hook_prefs_tab($args) {
144 if ($args != "prefFeeds") return;
145
146 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
147
148 print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
149
150 print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
151
152 print "<iframe id=\"starred_upload_iframe\"
153 name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
154 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
155
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\">&nbsp;
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>";
165
166
167 print "</div>"; #pane
168 }
169 }
170 ?>