init.php 11 KB

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