link = $host->get_link(); $this->host = $host; $host->add_command("greader-import", "import data in Google Reader JSON format", $this, ":", "FILE"); $host->add_hook($host::HOOK_PREFS_TAB, $this); } function greader_import($args) { $file = $args['greader_import']; if (!file_exists($file)) { _debug("file not found: $file"); return; } _debug("please enter your username:"); $username = db_escape_string($this->link, trim(read_stdin())); _debug("looking up user: $username..."); $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$username'"); if (db_num_rows($result) == 0) { _debug("user not found."); return; } $owner_uid = db_fetch_result($result, 0, "id"); _debug("processing: $file (owner_uid: $owner_uid)"); $this->import($file, $owner_uid); } function get_prefs_js() { return file_get_contents(dirname(__FILE__) . "/init.js"); } function import($file = false, $owner_uid = 0) { if (!$file) { header("Content-Type: text/html"); $owner_uid = $_SESSION["uid"]; if (is_file($_FILES['starred_file']['tmp_name'])) { $doc = json_decode(file_get_contents($_FILES['starred_file']['tmp_name']), true); } else { print_error(__('No file uploaded.')); return; } } else { $doc = json_decode(file_get_contents($file), true); } if ($file) { $sql_set_marked = strtolower(basename($file)) == 'starred.json' ? 'true' : 'false'; _debug("will set articles as starred: $sql_set_marked"); } else { $sql_set_marked = strtolower($_FILES['starred_file']['name']) == 'starred.json' ? 'true' : 'false'; } if ($doc) { if (isset($doc['items'])) { $processed = 0; foreach ($doc['items'] as $item) { // print_r($item); $guid = db_escape_string($this->link, mb_substr($item['id'], 0, 250)); $title = db_escape_string($this->link, $item['title']); $updated = date('Y-m-d h:i:s', $item['updated']); $link = ''; $content = ''; $author = db_escape_string($this->link, $item['author']); if (is_array($item['alternate'])) { foreach ($item['alternate'] as $alt) { if (isset($alt['type']) && $alt['type'] == 'text/html') { $link = db_escape_string($this->link, $alt['href']); } } } if (is_array($item['content'])) { $content = db_escape_string($this->link, $item['content']['content'], false); } $processed++; $imported += (int) $this->create_article($owner_uid, $guid, $title, $updated, $link, $content, $author, $sql_set_marked); } if ($file) { _debug(sprintf("All done. %d of %d articles imported.", $imported, $processed)); } else { print "
" . T_sprintf("All done. %d out of %d articles imported.", $imported, $processed) . "
"; } } else { print_error(__('The document has incorrect format.')); } } else { print_error(__('Error while parsing document.')); } if (!$file) { print "".__("Paste your starred.json or shared.json into the form below."). "
"; print ""; print "