init.php 11 KB

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