]>
Commit | Line | Data |
---|---|---|
493c2f88 AD |
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 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 | if ($doc) { | |
39 | if (isset($doc['items'])) { | |
40 | $processed = 0; | |
41 | ||
42 | foreach ($doc['items'] as $item) { | |
43 | // print_r($item); | |
44 | ||
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']); | |
48 | $link = ''; | |
49 | $content = ''; | |
50 | $author = db_escape_string($this->link, $item['author']); | |
51 | ||
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']); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | if (is_array($item['content'])) { | |
61 | $content = db_escape_string($this->link, | |
62 | $item['content']['content'], false); | |
63 | } | |
64 | ||
65 | $processed++; | |
66 | ||
67 | $imported += (int) $this->create_article($guid, $title, | |
68 | $updated, $link, $content, $author); | |
69 | ||
70 | } | |
71 | ||
72 | print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>"; | |
73 | ||
74 | } else { | |
75 | print_error(__('The document has incorrect format.')); | |
76 | } | |
77 | ||
78 | } else { | |
79 | print_error(__('Error while parsing document.')); | |
80 | } | |
81 | ||
82 | print "<div align='center'>"; | |
83 | print "<button dojoType=\"dijit.form.Button\" | |
84 | onclick=\"dijit.byId('starredImportDlg').execute()\">". | |
85 | __('Close this window')."</button>"; | |
86 | print "</div>"; | |
87 | ||
88 | } | |
89 | ||
90 | // expects ESCAPED data | |
91 | private function create_article($guid, $title, $updated, $link, $content, $author) { | |
92 | ||
93 | $owner_uid = $_SESSION["uid"]; | |
94 | ||
95 | if (!$guid) $guid = sha1($link); | |
96 | ||
97 | $guid = "$owner_uid,$guid"; | |
98 | ||
99 | $content_hash = sha1($content); | |
100 | ||
101 | if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false; | |
102 | ||
103 | db_query($this->link, "BEGIN"); | |
104 | ||
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"); | |
108 | ||
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) | |
112 | VALUES | |
113 | ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')"); | |
114 | ||
115 | $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'"); | |
116 | ||
117 | if (db_num_rows($result) != 0) { | |
118 | $ref_id = db_fetch_result($result, 0, "id"); | |
119 | ||
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) | |
123 | VALUES | |
124 | ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())"); | |
125 | ||
126 | if (count($labels) != 0) { | |
127 | foreach ($labels as $label) { | |
128 | label_add_article($link, $ref_id, trim($label), $owner_uid); | |
129 | } | |
130 | } | |
131 | ||
132 | $rc = true; | |
133 | } | |
134 | } | |
135 | ||
136 | db_query($this->link, "COMMIT"); | |
137 | ||
138 | return $rc; | |
139 | } | |
140 | ||
141 | function hook_prefs_tab($args) { | |
142 | if ($args != "prefFeeds") return; | |
143 | ||
144 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred items from Google Reader")."\">"; | |
145 | ||
146 | print_notice("Your imported articles will appear in Starred and Archived feeds."); | |
147 | ||
148 | print "<p>".__("Paste your starred.json into the form below."). "</p>"; | |
149 | ||
150 | print "<iframe id=\"starred_upload_iframe\" | |
151 | name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\" | |
152 | style=\"width: 400px; height: 100px; display: none;\"></iframe>"; | |
153 | ||
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>"; | |
163 | ||
164 | ||
165 | print "</div>"; #pane | |
166 | } | |
167 | } | |
168 | ?> |