PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/googlereaderimport/init.php

https://github.com/kpadilha/Tiny-Tiny-RSS
PHP | 350 lines | 249 code | 92 blank | 9 comment | 61 complexity | 0019e5d079659b6f24584c0eb1d299d7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-3.0
  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. preg_replace("/^feed\//",
  97. "", $item['origin']['streamId']));
  98. $orig_feed_data['title'] = db_escape_string($this->link,
  99. $item['origin']['title']);
  100. $orig_feed_data['site_url'] = db_escape_string($this->link,
  101. $item['origin']['htmlUrl']);
  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) {
  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. $result = db_query($this->link, "SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  193. guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
  194. if (db_num_rows($result) == 0) {
  195. $result = db_query($this->link, "INSERT INTO ttrss_entries
  196. (title, guid, link, updated, content, content_hash, date_entered, date_updated, author)
  197. VALUES
  198. ('$title', '$guid', '$link', '$updated', '$content', '$content_hash', NOW(), NOW(), '$author')");
  199. $result = db_query($this->link, "SELECT id FROM ttrss_entries WHERE guid = '$guid'");
  200. if (db_num_rows($result) != 0) {
  201. $ref_id = db_fetch_result($result, 0, "id");
  202. db_query($this->link, "INSERT INTO ttrss_user_entries
  203. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, marked, tag_cache, label_cache,
  204. last_read, note, unread, last_marked)
  205. VALUES
  206. ('$ref_id', '', NULL, $orig_feed_id, $owner_uid, $marked, '', '', NOW(), '', false, NOW())");
  207. $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries, ttrss_entries
  208. WHERE owner_uid = $owner_uid AND ref_id = id AND ref_id = $ref_id");
  209. if (db_num_rows($result) != 0 && is_array($tags)) {
  210. $entry_int_id = db_fetch_result($result, 0, "int_id");
  211. $tags_to_cache = array();
  212. foreach ($tags as $tag) {
  213. $tag = db_escape_string($this->link, sanitize_tag($tag));
  214. if (!tag_is_valid($tag)) continue;
  215. $result = db_query($this->link, "SELECT id FROM ttrss_tags
  216. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  217. owner_uid = '$owner_uid' LIMIT 1");
  218. if ($result && db_num_rows($result) == 0) {
  219. db_query($this->link, "INSERT INTO ttrss_tags
  220. (owner_uid,tag_name,post_int_id)
  221. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  222. }
  223. array_push($tags_to_cache, $tag);
  224. }
  225. /* update the cache */
  226. $tags_to_cache = array_unique($tags_to_cache);
  227. $tags_str = db_escape_string($this->link, join(",", $tags_to_cache));
  228. db_query($this->link, "UPDATE ttrss_user_entries
  229. SET tag_cache = '$tags_str' WHERE ref_id = '$ref_id'
  230. AND owner_uid = $owner_uid");
  231. }
  232. $rc = true;
  233. }
  234. }
  235. db_query($this->link, "COMMIT");
  236. return $rc;
  237. }
  238. function hook_prefs_tab($args) {
  239. if ($args != "prefFeeds") return;
  240. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Import starred or shared items from Google Reader")."\">";
  241. print_notice("Your imported articles will appear in Starred (in file is named starred.json) and Archived feeds.");
  242. print "<p>".__("Paste your starred.json or shared.json into the form below."). "</p>";
  243. print "<iframe id=\"starred_upload_iframe\"
  244. name=\"starred_upload_iframe\" onload=\"starredImportComplete(this)\"
  245. style=\"width: 400px; height: 100px; display: none;\"></iframe>";
  246. print "<form name=\"starred_form\" style='display : block' target=\"starred_upload_iframe\"
  247. enctype=\"multipart/form-data\" method=\"POST\"
  248. action=\"backend.php\">
  249. <input id=\"starred_file\" name=\"starred_file\" type=\"file\">&nbsp;
  250. <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
  251. <input type=\"hidden\" name=\"method\" value=\"import\">
  252. <input type=\"hidden\" name=\"plugin\" value=\"googlereaderimport\">
  253. <button dojoType=\"dijit.form.Button\" onclick=\"return starredImport();\" type=\"submit\">" .
  254. __('Import my Starred items') . "</button>";
  255. print "</div>"; #pane
  256. }
  257. }
  258. ?>