init.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 (is_file($_FILES['starred_file']['tmp_name'])) {
  48. $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true);
  49. } else {
  50. print_error(__('No file uploaded.'));
  51. return;
  52. }
  53. } else {
  54. $doc = json_decode(file_get_contents($file), true);
  55. }
  56. if ($file) {
  57. $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false';
  58. _debug("will set articles as starred: $sql_set_marked");
  59. } else {
  60. $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false';
  61. }
  62. if ($doc) {
  63. if (isset($doc['items'])) {
  64. $processed = 0;
  65. foreach ($doc['items'] as $item) {
  66. // print_r($item);
  67. $guid = db_escape_string($this->link, mb_substr($item['id'], 0, 250));
  68. $title = db_escape_string($this->link, $item['title']);
  69. $updated = date('Y-m-d h:i:s', $item['updated']);
  70. $link = '';
  71. $content = '';
  72. $author = db_escape_string($this->link, $item['author']);
  73. $tags = array();
  74. $orig_feed_data = array();
  75. if (is_array($item['alternate'])) {
  76. foreach ($item['alternate'] as $alt) {
  77. if (isset($alt['type']) && $alt['type'] == 'text/html') {
  78. $link = db_escape_string($this->link, $alt['href']);
  79. }
  80. }
  81. }
  82. if (is_array($item['content'])) {
  83. $content = db_escape_string($this->link,
  84. $item['content']['content'], false);
  85. }
  86. if (is_array($item['categories'])) {
  87. foreach ($item['categories'] as $cat) {
  88. if (strstr($cat, "com.google/") === FALSE) {
  89. array_push($tags, sanitize_tag($cat));
  90. }
  91. }
  92. }
  93. if (is_array($item['origin'])) {
  94. if (strpos($item['origin']['streamId'], 'feed/') === 0) {
  95. $orig_feed_data['feed_url'] = db_escape_string($this->link,
  96. mb_substr(preg_replace("/^feed\//",
  97. "", $item['origin']['streamId']), 0, 200));
  98. $orig_feed_data['title'] = db_escape_string($this->link,
  99. mb_substr($item['origin']['title'], 0, 200));
  100. $orig_feed_data['site_url'] = db_escape_string($this->link,
  101. mb_substr($item['origin']['htmlUrl'], 0, 200));
  102. }
  103. }
  104. $processed++;
  105. $imported += (int) $this->create_article($owner_uid, $guid, $title,
  106. $updated, $link, $content, $author, $sql_set_marked, $tags,
  107. $orig_feed_data);
  108. if ($file && $processed % 25 == 0) {
  109. _debug("processed $processed articles...");
  110. }
  111. }
  112. if ($file) {
  113. _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed));
  114. } else {
  115. print "<p style='text-align : center'>" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "</p>";
  116. }
  117. } else {
  118. print_error(__('The document has incorrect format.'));
  119. }
  120. } else {
  121. print_error(__('Error while parsing document.'));
  122. }
  123. if (!$file) {
  124. print "<div align='center'>";
  125. print "<button dojoType=\"dijit.form.Button\"
  126. onclick=\"dijit.byId('starredImportDlg').execute()\">".
  127. __('Close this window')."</button>";
  128. print "</div>";
  129. }
  130. }
  131. // expects ESCAPED data
  132. private function create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $marked, $tags, $orig_feed_data) {
  133. if (!$guid) $guid = sha1($link);
  134. $create_archived_feeds = true;
  135. $guid = "$owner_uid,$guid";
  136. $content_hash = sha1($content);
  137. if (filter_var($link, FILTER_VALIDATE_URL) === FALSE) return false;
  138. db_query($this->link, "BEGIN");
  139. $feed_id = 'NULL';
  140. // let's check for archived feed entry
  141. $feed_inserted = false;
  142. // before dealing with archived feeds we must check ttrss_feeds to maintain id consistency
  143. if ($orig_feed_data['feed_url'] && $create_archived_feeds) {
  144. $result = db_query($this->link,
  145. "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
  146. AND owner_uid = $owner_uid");
  147. if (db_num_rows($result) != 0) {
  148. $feed_id = db_fetch_result($result, 0, "id");
  149. } else {
  150. // let's insert it
  151. if (!$orig_feed_data['title']) $orig_feed_data['title'] = '[Unknown]';
  152. $result = db_query($this->link,
  153. "INSERT INTO ttrss_feeds
  154. (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
  155. VALUES ($owner_uid,
  156. '".$orig_feed_data['feed_url']."',
  157. '".$orig_feed_data['site_url']."',
  158. '".$orig_feed_data['title']."',
  159. NULL, '', '', 0)");
  160. $result = db_query($this->link,
  161. "SELECT id FROM ttrss_feeds WHERE feed_url = '".$orig_feed_data['feed_url']."'
  162. AND owner_uid = $owner_uid");
  163. if (db_num_rows($result) != 0) {
  164. $feed_id = db_fetch_result($result, 0, "id");
  165. $feed_inserted = true;
  166. }
  167. }
  168. }
  169. if ($feed_id && $feed_id != 'NULL') {
  170. // locate archived entry to file entries in, we don't want to file them in actual feeds because of purging
  171. // maybe file marked in real feeds because eh
  172. $result = db_query($this->link, "SELECT id FROM ttrss_archived_feeds WHERE
  173. feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
  174. if (db_num_rows($result) != 0) {
  175. $orig_feed_id = db_fetch_result($result, 0, "id");
  176. } else {
  177. db_query($this->link, "INSERT INTO ttrss_archived_feeds
  178. (id, owner_uid, title, feed_url, site_url)
  179. SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
  180. WHERE id = '$feed_id'");
  181. $result = db_query($this->link, "SELECT id FROM ttrss_archived_feeds WHERE
  182. feed_url = '".$orig_feed_data['feed_url']."' AND owner_uid = $owner_uid");
  183. if (db_num_rows($result) != 0) {
  184. $orig_feed_id = db_fetch_result($result, 0, "id");
  185. }
  186. }
  187. }
  188. // delete temporarily inserted feed
  189. if ($feed_id && $feed_inserted) {
  190. db_query($this->link, "DELETE FROM ttrss_feeds WHERE id = $feed_id");
  191. }
  192. if (!$orig_feed_id) $orig_feed_id = 'NULL';
  193. $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  194. guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
  195. if (db_num_rows($result) == 0) {
  196. $result = db_query($this->link, "INSERT INTO ttrss_entries
  197. (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
  198. VALUES
  199. ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
  200. $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
  201. if (db_num_rows($result) != 0) {
  202. $ref_id = db_fetch_result($result, 0, "id");
  203. db_query($this->link, "INSERT INTO ttrss_user_entries
  204. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
  205. last_read, note, unread, last_marked)
  206. VALUES
  207. ('$ref_id', '', NULL, $orig_feed_id, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
  208. $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries, ttrss_entries
  209. WHERE owner_uid = $owner_uid AND ref_id = id AND ref_id = $ref_id");
  210. if (db_num_rows($result) != 0 && is_array($tags)) {
  211. $entry_int_id = db_fetch_result($result, 0, "int_id");
  212. $tags_to_cache = array();
  213. foreach ($tags as $tag) {
  214. $tag = db_escape_string($this->link, sanitize_tag($tag));
  215. if (!tag_is_valid($tag)) continue;
  216. $result = db_query($this->link, "SELECT id FROM ttrss_tags
  217. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  218. owner_uid = '$owner_uid' LIMIT 1");
  219. if ($result && db_num_rows($result) == 0) {
  220. db_query($this->link, "INSERT INTO ttrss_tags
  221. (owner_uid,tag_name,post_int_id)
  222. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  223. }
  224. array_push($tags_to_cache, $tag);
  225. }
  226. /* update the cache */
  227. $tags_to_cache = array_unique($tags_to_cache);
  228. $tags_str = db_escape_string($this->link, join(",", $tags_to_cache));
  229. db_query($this->link, "UPDATE ttrss_user_entries
  230. SET tag_cache = '$tags_str' WHERE ref_id = '$ref_id'
  231. AND owner_uid = $owner_uid");
  232. }
  233. $rc = true;
  234. }
  235. }
  236. db_query($this->link, "COMMIT");
  237. return $rc;
  238. }
  239. function hook_prefs_tab($args) {
  240. if ($args != "prefFeeds") return;
  241. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
  242. print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
  243. print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
  244. print "<iframe id=\"starred_upload_iframe\"
  245. name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
  246. style=\"width: 400px; height: 100px; display: none;\"></iframe>";
  247. print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
  248. enctype=\"multipart/form-data\" method=\"POST\"
  249. action=\"backend.php\">
  250. <input id=\"starred_file\" name=\"starred_file\" type=\"file\">&nbsp;
  251. <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
  252. <input type=\"hidden\" name=\"method\" value=\"import\">
  253. <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
  254. <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
  255. __('Import my Starred items') . "</button>";
  256. print "</div>"; #pane
  257. }
  258. }
  259. ?>