]> git.wh0rd.org Git - tt-rss.git/commitdiff
implement fetching and exporting of shared feeds
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Wed, 20 Apr 2011 11:21:00 +0000 (15:21 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Wed, 20 Apr 2011 11:21:00 +0000 (15:21 +0400)
backend.php
functions.php
modules/backend-rpc.php
modules/popup-dialog.php
modules/pref-instances.php
update.php
update_daemon2.php

index e63d5abd705bc5f791652751fd8b0c02b41c97be..c4edd92eaa62849aa5f92a60e4fffd4f64d1a076 100644 (file)
@@ -60,7 +60,7 @@
 
        if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
                        $op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
-                       $op != "logout" && $op != "pubsub") {
+                       $op != "fbexport" && $op != "logout" && $op != "pubsub") {
 
                header("Content-Type: text/plain");
                print json_encode(array("error" => array("code" => 6)));
                        header("Location: tt-rss.php");
                break; // logout
 
+               case "fbexport":
+
+                       // TODO: change to _POST
+                       $access_key = db_escape_string($_REQUEST["key"]);
+
+                       // TODO: rate limit checking using last_connected
+                       $result = db_query($link, "SELECT id FROM ttrss_linked_instances
+                               WHERE access_key = '$access_key'");
+
+                       if (db_num_rows($result) == 1) {
+
+                               $instance_id = db_fetch_result($result, 0, "id");
+
+                               $result = db_query($link, "SELECT feed_url, title, subscribers
+                                       FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
+
+                               $feeds = array();
+
+                               while ($line = db_fetch_assoc($result)) {
+                                       array_push($feeds, $line);
+                               }
+
+                               db_query($link, "UPDATE ttrss_linked_instances SET last_connected = NOW(),
+                                       last_status_in = 1 WHERE id = '$instance_id'");
+
+                               print json_encode(array("feeds" => $feeds));
+                       } else {
+                               print json_encode(array("error" => array("code" => 6)));
+                       }
+               break; // fbexport
+
                default:
                        header("Content-Type: text/plain");
                        print json_encode(array("error" => array("code" => 7)));
index 0da69094af03060933f4678aca48d416a071ace7..e0c2a664bebb08811ac72e737f0b1dd12a0d8aec 100644 (file)
                }
        }
 
-       function fetch_file_contents($url, $type = false, $login = false, $pass = false) {
+       function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false) {
                $login = urlencode($login);
                $pass = urlencode($pass);
 
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 
+                       if ($post_query) {
+                               curl_setopt($ch, CURLOPT_POST, true);
+                               curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
+                       }
+
                        if ($login && $pass)
                                curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
 
 
                return $query;
        }
+
+       // Status codes:
+       // -1  - never connected
+       // 0   - no data received
+       // 1   - data received successfully
+       // 2   - did not receive valid data
+       // >10 - server error, code + 10 (e.g. 16 means server error 6)
+
+       function get_linked_feeds($link, $instance_id = false) {
+               if ($instance_id)
+                       $instance_qpart = "id = '$instance_id' AND ";
+               else
+                       $instance_qpart = "";
+
+               if (DB_TYPE == "pgsql") {
+                       $date_qpart = "last_connected < NOW() - INTERVAL '1 second'";
+               } else {
+                       $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR";
+               }
+
+               $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances
+                       WHERE $instance_qpart $date_qpart ORDER BY last_connected");
+
+               while ($line = db_fetch_assoc($result)) {
+                       $id = $line['id'];
+
+                       _debug("Updating: " . $line['access_url'] . " ($id)");
+
+                       $fetch_url = $line['access_url'] . '/backend.php?op=fbexport';
+                       $post_query = 'key=' . $line['access_key'];
+
+                       $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
+
+                       if ($feeds) {
+                               $feeds = json_decode($feeds, true);
+
+                               if ($feeds) {
+                                       if ($feeds['error']) {
+                                               $status = $feeds['error']['code'] + 10;
+                                       } else {
+                                               $status = 1;
+
+                                               if (count($feeds['feeds']) > 0) {
+
+                                                       db_query($link, "DELETE FROM ttrss_linked_feeds
+                                                               WHERE instance_id = '$id'");
+
+                                                       foreach ($feeds['feeds'] as $feed) {
+                                                               $feed_url = db_escape_string($feed['feed_url']);
+                                                               $title = db_escape_string($feed['title']);
+                                                               $subscribers = db_escape_string($feed['subscribers']);
+
+                                                               db_query($link, "INSERT INTO ttrss_linked_feeds
+                                                                       (feed_url, title, subscribers, instance_id, created, updated)
+                                                               VALUES
+                                                                       ('$feed_url', '$title', '$subscribers', '$id', NOW(), NOW())");
+                                                       }
+                                               } else {
+                                                       // received 0 feeds, this might indicate that
+                                                       // the instance on the other hand is rebuilding feedbrowser cache
+                                                       // we will try again later
+
+                                                       // TODO: maybe perform expiration based on updated here?
+                                               }
+
+                                               _debug("Processed " . count($feeds['feeds']) . " feeds.");
+                                       }
+                               } else {
+                                       $status = 2;
+                               }
+
+                       } else {
+                               $status = 0;
+                       }
+
+                       _debug("Status: $status");
+
+                       db_query($link, "UPDATE ttrss_linked_instances SET
+                               last_status_out = '$status', last_connected = NOW() WHERE id = '$id'");
+
+               }
+
+       }
 ?>
index 2ff9a6312ba0882f0e4937633e0cdb410c44a16a..75ce6886a0952c6058db837d28df0a3bf94f4040 100644 (file)
                        return;
                }
 
-               if ($subop == "fbExport") {
-
-                       // TODO: change to _POST
-                       $access_key = db_escape_string($_REQUEST["key"]);
-
-                       // TODO: rate limit checking using last_connected
-                       $result = db_query($link, "SELECT id FROM ttrss_linked_instances
-                               WHERE access_key = '$access_key'");
-
-                       if (db_num_rows($result) == 1) {
-
-                               $instance_id = db_fetch_result($result, 0, "id");
-
-                               $result = db_query($link, "SELECT feed_url, title, subscribers
-                                       FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
-
-                               $feeds = array();
-
-                               while ($line = db_fetch_assoc($result)) {
-                                       array_push($feeds, $line);
-                               }
-
-                               db_query($link, "UPDATE ttrss_linked_instances SET last_connected = NOW()
-                                       WHERE id = '$instance_id'");
-
-                               print json_encode(array("feeds" => $feeds));
-                       } else {
-                               print json_encode(array("error" => array("code" => 6)));
-                       }
-                       return;
-               }
-
                if ($subop == "genHash") {
                        $hash = sha1(uniqid(rand(), true));
 
index f5a30a06e07eb653f3665cfd1badc2787ab71803..6cb60eef43fa53edbdb06ade74309e202978c48a 100644 (file)
                                style=\"width: 20em\" name=\"access_key\" id=\"instance_add_key\"
                                value=\"$access_key\">";
 
+                       print "<p class='insensitive'>" . __("Use one access key for both linked instances.");
+
                        print "</div>";
 
                        print "<div class=\"dlgButtons\">
index 45df2eb398bae5a9a59dac27643e281ab80e43dd..d3510c2877ba6a5137b75c152aecd59fd5fdb3dc 100644 (file)
@@ -24,8 +24,9 @@
 
                        if (db_num_rows($result) == 0) {
                                db_query($link, "INSERT INTO ttrss_linked_instances
-                                       (access_url, access_key, last_connected) VALUES
-                                       ('$access_url', '$access_key', '1970-01-01')");
+                                       (access_url, access_key, last_connected, last_status_in, last_status_out)
+                                       VALUES
+                                       ('$access_url', '$access_key', '1970-01-01', -1, -1)");
 
                        }
 
@@ -74,6 +75,8 @@
                                style=\"width: 20em\" name=\"access_key\" id=\"instance_edit_key\"
                                value=\"$access_key\">";
 
+                       print "<p class='insensitive'>" . __("Use one access key for both linked instances.");
+
                        print "</div>";
 
                        print "<div class=\"dlgButtons\">
 
                print "</div>"; #toolbar
 
-               $result = db_query($link, "SELECT * FROM ttrss_linked_instances
+               $result = db_query($link, "SELECT *,
+                       (SELECT COUNT(*) FROM ttrss_linked_feeds
+                               WHERE instance_id = ttrss_linked_instances.id) AS num_feeds
+                       FROM ttrss_linked_instances
                        ORDER BY $sort");
 
                print "<p class=\"insensitive\" style='margin-left : 1em;'>" . __("You can connect other instances of Tiny Tiny RSS to this one to share Popular feeds. Link to this instance of Tiny Tiny RSS by using this URL:");
                        <td align='center' width=\"5%\">&nbsp;</td>
                        <td width=''><a href=\"#\" onclick=\"updateInstanceList('access_url')\">".__('Instance URL')."</a></td>
                        <td width='20%'><a href=\"#\" onclick=\"updateInstanceList('access_key')\">".__('Access key')."</a></td>
-                       <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_connected')\">".__('Last connected')."</a></td>
+                       <td width='10%'><a href=\"#\" onclick=\"updateUsersList('last_connected')\">".__('Last connected')."</a></td>
+                       <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Stored feeds')."</a></td>
                        </tr>";
 
                $lnum = 0;
                        print "<td $onclick>" . htmlspecialchars($line['access_url']) . "</td>";
                        print "<td $onclick>" . htmlspecialchars($access_key) . "</td>";
                        print "<td $onclick>" . htmlspecialchars($line['last_connected']) . "</td>";
+                       print "<td $onclick>" . htmlspecialchars($line['num_feeds']) . "</td>";
 
                        print "</tr>";
 
index 9c9c6ae8de742de18d06cd7b5c36638167e7f8f4..92e2ce6ef085c7090180a6286e0b53ae10ebaa6e 100755 (executable)
@@ -22,6 +22,7 @@
                print "  -feedbrowser   - update feedbrowser\n";
                print "  -daemon        - start single-process update daemon\n";
                print "  -cleanup-tags  - perform tags table maintenance\n";
+               print "  -get-feeds     - receive popular feeds from linked instances\n";
                print "  -help          - show this help\n";
                return;
        }
                print "$rc tags deleted.\n";
        }
 
+       if ($op == "-get-feeds") {
+               get_linked_feeds($link);
+       }
+
        db_close($link);
 
        unlink(LOCK_DIRECTORY . "/$lock_filename");
index 6ccdbcb100e5a7fc2afd614563a1098159b92e76..9ad914307a0019ae122cca5d10e88d029c577c19 100755 (executable)
 
                                                _debug("Cleaned $rc cached tags.");
 
+                                               _debug("Updating linked feeds...");
+                                               get_linked_feeds($link);
+
                                        }
 
                                        _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");