init.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. class GoogleReaderImport extends Plugin {
  3. private $link;
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Import starred/shared items from Google Reader takeout",
  8. "fox",
  9. false,
  10. "");
  11. }
  12. function init($host) {
  13. $this->link = $host->get_link();
  14. $this->host = $host;
  15. $host->add_command("greader-import",
  16. "import data in Google Reader JSON format",
  17. $this, ":", "FILE");
  18. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  19. }
  20. function greader_import($args) {
  21. $file = $args['greader_import'];
  22. if (!file_exists($file)) {
  23. _debug("file not found: $file");
  24. return;
  25. }
  26. _debug("please enter your username:");
  27. $username = db_escape_string($this->link, trim(read_stdin()));
  28. _debug("looking up user: $username...");
  29. $result = db_query($this->link, "SELECT id FROM ttrss_users
  30. WHERE login = '$username'");
  31. if (db_num_rows($result) == 0) {
  32. _debug("user not found.");
  33. return;
  34. }
  35. $owner_uid = db_fetch_result($result, 0, "id");
  36. _debug("processing: $file (owner_uid: $owner_uid)");
  37. $this->import($file, $owner_uid);
  38. }
  39. function get_prefs_js() {
  40. return file_get_contents(dirname(__FILE__) . "/init.js");
  41. }
  42. function import($file = false, $owner_uid = 0) {
  43. purge_orphans($this->link);
  44. if (!$file) {
  45. header("Content-Type: text/html");
  46. $owner_uid = $_SESSION["uid"];
  47. if ($_FILES['starred_file']['error'] != 0) {
  48. print_error(T_sprintf("Upload failed with error code %d",
  49. $_FILES['starred_file']['error']));
  50. return;
  51. }
  52. $tmp_file = false;
  53. if (is_uploaded_file($_FILES['starred_file']['tmp_name'])) {
  54. $tmp_file = tempnam(CACHE_DIR . '/upload', 'starred');
  55. $result = move_uploaded_file($_FILES['starred_file']['tmp_name'],
  56. $tmp_file);
  57. if (!$result) {
  58. print_error(__("Unable to move uploaded file."));
  59. return;
  60. }
  61. } else {
  62. print_error(__('Error: please upload OPML file.'));
  63. return;
  64. }
  65. if (is_file($tmp_file)) {
  66. $doc = json_decode(file_get_contents($tmp_file), true);
  67. unlink($tmp_file);
  68. } else {
  69. print_error(__('No file uploaded.'));
  70. return;
  71. }
  72. } else {
  73. $doc = json_decode(file_get_contents($file), true);
  74. }
  75. if ($file) {
  76. $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false';
  77. _debug("will set articles as starred: $sql_set_marked");
  78. } else {
  79. $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false';
  80. }
  81. if ($doc) {
  82. if (isset($doc['items'])) {
  83. $processed = 0;
  84. foreach ($doc['items'] as $item) {
  85. // print_r($item);
  86. $guid = db_escape_string($this->link, mb_substr($item['id'], 0, 250));
  87. $title = db_escape_string($this->link, $item['title']);
  88. $updated = date('Y-m-d h:i:s', $item['updated']);
  89. $link = '';
  90. $content = '';
  91. $author = db_escape_string($this->link, $item['author']);
  92. $tags = array();
  93. $orig_feed_data = array();
  94. if (is_array($item['alternate'])) {
  95. foreach ($item['alternate'] as $alt) {
  96. if (isset($alt['type']) && $alt['type'] == 'text/html') {
  97. $link = db_escape_string($this->link, $alt['href']);
  98. }
  99. }
  100. }
  101. if (is_array($item['summary'])) {
  102. $content = db_escape_string($this->link,
  103. $item['summary']['content'], false);
  104. }
  105. if (is_array($item['content'])) {
  106. $content = db_escape_string($this->link,
  107. $item['content']['content'], false);
  108. }
  109. if (is_array($item['categories'])) {
  110. foreach ($item['categories'] as $cat) {
  111. if (strstr($cat, "com.google/") === FALSE) {
  112. array_push($tags, sanitize_tag($cat));
  113. }
  114. }
  115. }
  116. if (is_array($item['origin'])) {
  117. if (strpos($item['origin']['streamId'], 'feed/') === 0) {
  118. $orig_feed_data['feed_url'] = db_escape_string($this->link,
  119. mb_substr(preg_replace("/^feed\//",
  120. "", $item['origin']['streamId']), 0, 200));
  121. $orig_feed_data['title'] = db_escape_string($this->link,
  122. mb_substr($item['origin']['title'], 0, 200));
  123. $orig_feed_data['site_url'] = db_escape_string($this->link,
  124. mb_substr($item['origin']['htmlUrl'], 0, 200));
  125. }
  126. }
  127. $processed++;
  128. $imported += (int) $this->create_article($owner_uid, $guid, $title,
  129. $updated, $link, $content, $author, $sql_set_marked, $tags,
  130. $orig_feed_data);
  131. if ($file && $processed % 25 == 0) {
  132. _debug("processed $processed articles...");
  133. }
  134. }
  135. if ($file) {
  136. _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
  137. } else {
  138. print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
  139. }
  140. } else {
  141. print_error(__('The document has incorrect format.'));
  142. }
  143. } else {
  144. print_error(__('Error while parsing document.'));
  145. }
  146. if (!$file) {
  147. print "<div align='center'>";
  148. print "<button dojoType=\"dijit.form.Button\"
  149. onclick=\"dijit.byId('starredImportDlg').execute()\">".
  150. __('Close this window')."</button>";
  151. print "</div>";
  152. }
  153. }
  154. // expects ESCAPED data
  155. private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked, $tags, $orig_feed_data) {
  156. if (!$guid) $guid = sha1($link);
  157. $create_archived_feeds = true;
  158. $guid = "$owner_uid,$guid";
  159. $content_hash = sha1($content);
  160. if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false;
  161. db_query($this->link, "BEGIN");
  162. $feed_id = 'NULL';
  163. // let's check for archived feed entry
  164. $feed_inserted = false;
  165. // before dealing with archived feeds we must check ttrss_feeds to maintain id consistency
  166. if ($orig_feed_data['feed_url'] && $create_archived_feeds) {
  167. $result = db_query($this->link,
  168. "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
  169. AND owner_uid = $owner_uid");
  170. if (db_num_rows($result) != 0) {
  171. $feed_id = db_fetch_result($result, 0, "id");
  172. } else {
  173. // let's insert it
  174. if (!$orig_feed_data['title']) $orig_feed_data['title'] = '[Unknown]';
  175. $result = db_query($this->link,
  176. "INSERT INTO ttrss_feeds
  177. (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
  178. VALUES ($owner_uid,
  179. '".$orig_feed_data['feed_url']."',
  180. '".$orig_feed_data['site_url']."',
  181. '".$orig_feed_data['title']."',
  182. NULL, '', '', 0)");
  183. $result = db_query($this->link,
  184. "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
  185. AND owner_uid = $owner_uid");
  186. if (db_num_rows($result) != 0) {
  187. $feed_id = db_fetch_result($result, 0, "id");
  188. $feed_inserted = true;
  189. }
  190. }
  191. }
  192. if ($feed_id && $feed_id != 'NULL') {
  193. // locate archived entry to file entries in, we don't want to file them in actual feeds because of purging
  194. // maybe file marked in real feeds because eh
  195. $result = db_query($this->link, "SELECT id FROM ttrss_archived_feeds WHERE
  196. feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
  197. if (db_num_rows($result) != 0) {
  198. $orig_feed_id = db_fetch_result($result, 0, "id");
  199. } else {
  200. db_query($this->link, "INSERT INTO ttrss_archived_feeds
  201. (id, owner_uid, title, feed_url, site_url)
  202. SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
  203. WHERE id = '$feed_id'");
  204. $result = db_query($this->link, "SELECT id FROM ttrss_archived_feeds WHERE
  205. feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
  206. if (db_num_rows($result) != 0) {
  207. $orig_feed_id = db_fetch_result($result, 0, "id");
  208. }
  209. }
  210. }
  211. // delete temporarily inserted feed
  212. if ($feed_id && $feed_inserted) {
  213. db_query($this->link, "DELETE FROM ttrss_feeds WHERE id = $feed_id");
  214. }
  215. if (!$orig_feed_id) $orig_feed_id = 'NULL';
  216. $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  217. guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
  218. if (db_num_rows($result) == 0) {
  219. $result = db_query($this->link, "INSERT INTO ttrss_entries
  220. (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
  221. VALUES
  222. ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
  223. $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
  224. if (db_num_rows($result) != 0) {
  225. $ref_id = db_fetch_result($result, 0, "id");
  226. db_query($this->link, "INSERT INTO ttrss_user_entries
  227. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
  228. last_read, note, unread, last_marked)
  229. VALUES
  230. ('$ref_id', '', NULL, $orig_feed_id, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
  231. $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries, ttrss_entries
  232. WHERE owner_uid = $owner_uid AND ref_id = id AND ref_id = $ref_id");
  233. if (db_num_rows($result) != 0 && is_array($tags)) {
  234. $entry_int_id = db_fetch_result($result, 0, "int_id");
  235. $tags_to_cache = array();
  236. foreach ($tags as $tag) {
  237. $tag = db_escape_string($this->link, sanitize_tag($tag));
  238. if (!tag_is_valid($tag)) continue;
  239. $result = db_query($this->link, "SELECT id FROM ttrss_tags
  240. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  241. owner_uid = '$owner_uid' LIMIT 1");
  242. if ($result && db_num_rows($result) == 0) {
  243. db_query($this->link, "INSERT INTO ttrss_tags
  244. (owner_uid,tag_name,post_int_id)
  245. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  246. }
  247. array_push($tags_to_cache, $tag);
  248. }
  249. /* update the cache */
  250. $tags_to_cache = array_unique($tags_to_cache);
  251. $tags_str = db_escape_string($this->link, join(",", $tags_to_cache));
  252. db_query($this->link, "UPDATE ttrss_user_entries
  253. SET tag_cache = '$tags_str' WHERE ref_id = '$ref_id'
  254. AND owner_uid = $owner_uid");
  255. }
  256. $rc = true;
  257. }
  258. }
  259. db_query($this->link, "COMMIT");
  260. return $rc;
  261. }
  262. function hook_prefs_tab($args) {
  263. if ($args != "prefFeeds") return;
  264. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
  265. print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
  266. print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
  267. print "<iframe id=\"starred_upload_iframe\"
  268. name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
  269. style=\"width: 400px; height: 100px; display: none;\"></iframe>";
  270. print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
  271. enctype=\"multipart/form-data\" method=\"POST\"
  272. action=\"backend.php\">
  273. <input id=\"starred_file\" name=\"starred_file\" type=\"file\">&nbsp;
  274. <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
  275. <input type=\"hidden\" name=\"method\" value=\"import\">
  276. <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
  277. <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
  278. __('Import my Starred items') . "</button>";
  279. print "</div>"; #pane
  280. }
  281. }
  282. ?>