]> git.wh0rd.org - tt-rss.git/blobdiff - include/rssfuncs.php
pluginhost: mention that update task & housekeeping hooks are for global plugins...
[tt-rss.git] / include / rssfuncs.php
index 807b041a36ca64ed9b430595ab63d75704b46cde..cc8a8e3f0b331fd6cbb3025ad909ecc0ea68cba3 100644 (file)
 
                        if (!$registered_title || $registered_title == "[Unknown]") {
 
-                               $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199);
+                               $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199));
 
                                if ($feed_title) {
                                        _debug("registering title: $feed_title", $debug_enabled);
                                if (db_num_rows($result) != 0) {
                                        $base_entry_id = db_fetch_result($result, 0, "id");
                                        $entry_stored_hash = db_fetch_result($result, 0, "content_hash");
+                                       $article_labels = get_article_labels($base_entry_id, $owner_uid);
                                } else {
                                        $base_entry_id = false;
                                        $entry_stored_hash = "";
+                                       $article_labels = array();
                                }
 
                                $article = array("owner_uid" => $owner_uid, // read only
                                        "title" => $entry_title,
                                        "content" => $entry_content,
                                        "link" => $entry_link,
+                                       "labels" => $article_labels, // current limitation: can add labels to article, can't remove them
                                        "tags" => $entry_tags,
                                        "author" => $entry_author,
+                                       "force_catchup" => false, // ugly hack for the time being
+                                       "score_modifier" => 0, // no previous value, plugin should recalculate score modifier based on content if needed
                                        "language" => $entry_language, // read only
                                        "feed" => array("id" => $feed,
                                                "fetch_url" => $fetch_url,
                                        db_query("UPDATE ttrss_entries SET date_updated = NOW()
                                                WHERE id = '$base_entry_id'");
 
-                    // if we allow duplicate posts, we have to continue to 
+                    // if we allow duplicate posts, we have to continue to
                     // create the user entries for this feed
                     if (!get_pref("ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
                         continue;
                                $entry_author = db_escape_string($article["author"]);
                                $entry_link = db_escape_string($article["link"]);
                                $entry_content = $article["content"]; // escaped below
+                               $entry_force_catchup = $article["force_catchup"];
+                               $article_labels = $article["labels"];
+                               $entry_score_modifier = (int) $article["score_modifier"];
+
+                               if ($debug_enabled) {
+                                       _debug("article labels:", $debug_enabled);
+                                       print_r($article_labels);
+                               }
+
+                               _debug("force catchup: $entry_force_catchup");
 
                                if ($cache_images && is_writable(CACHE_DIR . '/images'))
                                        cache_images($entry_content, $site_url, $debug_enabled);
                                                        '$entry_language',
                                                        '$entry_author')");
 
-                                       $article_labels = array();
-
                                } else {
                                        $base_entry_id = db_fetch_result($result, 0, "id");
-
-                                       $article_labels = get_article_labels($base_entry_id, $owner_uid);
                                }
 
                                // now it should exist, if not - bad luck then
                                                continue;
                                        }
 
-                                       $score = calculate_article_score($article_filters);
+                                       $score = calculate_article_score($article_filters) + $entry_score_modifier;
 
-                                       _debug("initial score: $score", $debug_enabled);
+                                       _debug("initial score: $score [including plugin modifier: $entry_score_modifier]", $debug_enabled);
 
                                        $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
                                                        ref_id = '$ref_id' AND owner_uid = '$owner_uid'
 
                                                _debug("user record not found, creating...", $debug_enabled);
 
-                                               if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
+                                               if ($score >= -500 && !find_article_filter($article_filters, 'catchup') && !$entry_force_catchup) {
                                                        $unread = 'true';
                                                        $last_read_qpart = 'NULL';
                                                } else {
 
                                                // N-grams
 
-                                               if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) {
+                                               /* if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) {
 
                                                        $result = db_query("SELECT COUNT(*) AS similar FROM
                                                                        ttrss_entries,ttrss_user_entries
                                                        if ($ngram_similar > 0) {
                                                                $unread = 'false';
                                                        }
-                                               }
+                                               } */
 
                                                $last_marked = ($marked == 'true') ? 'NOW()' : 'NULL';
                                                $last_published = ($published == 'true') ? 'NOW()' : 'NULL';
 
                                db_query("COMMIT");
 
-                               _debug("assigning labels...", $debug_enabled);
+                               _debug("assigning labels [other]...", $debug_enabled);
+
+                               foreach ($article_labels as $label) {
+                                       label_add_article($entry_ref_id, $label[1], $owner_uid);
+                               }
+
+                               _debug("assigning labels [filters]...", $debug_enabled);
 
                                assign_article_to_label_filters($entry_ref_id, $article_filters,
                                        $owner_uid, $article_labels);
                                        db_query("COMMIT");
                                }
 
-                               if (get_pref("AUTO_ASSIGN_LABELS", $owner_uid, false)) {
-                                       _debug("auto-assigning labels...", $debug_enabled);
-
-                                       foreach ($labels as $label) {
-                                               $caption = preg_quote($label["caption"]);
-
-                                               if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) {
-                                                       if (!labels_contains_caption($article_labels, $caption)) {
-                                                               label_add_article($entry_ref_id, $caption, $owner_uid);
-                                                       }
-                                               }
-                                       }
-                               }
-
                                _debug("article processed", $debug_enabled);
                        }
 
                                                file_put_contents($local_filename, $file_content);
                                        }
                                }
-
-                               /* if (file_exists($local_filename)) {
-                                       $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' .
-                                               base64_encode($src));
-                               } */
                        }
                }
-
-               //$node = $doc->getElementsByTagName('body')->item(0);
-               //return $doc->saveXML($node);
        }
 
        function expire_error_log($debug) {
                return $error;
        } */
 
+       function cleanup_counters_cache($debug) {
+               $result = db_query("DELETE FROM ttrss_counters_cache
+                       WHERE feed_id > 0 AND
+                       (SELECT COUNT(id) FROM ttrss_feeds WHERE
+                               id = feed_id AND
+                               ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid) = 0");
+               $frows = db_affected_rows($result);
+
+               $result = db_query("DELETE FROM ttrss_cat_counters_cache
+                       WHERE feed_id > 0 AND
+                       (SELECT COUNT(id) FROM ttrss_feed_categories WHERE
+                               id = feed_id AND
+                               ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid) = 0");
+               $crows = db_affected_rows($result);
+
+               _debug("Removed $frows (feeds) $crows (cats) orphaned counter cache entries.");
+       }
+
        function housekeeping_common($debug) {
                expire_cached_files($debug);
                expire_lock_files($debug);
                _debug("Feedbrowser updated, $count feeds processed.");
 
                purge_orphans( true);
+               cleanup_counters_cache($debug);
                $rc = cleanup_tags( 14, 50000);
 
                _debug("Cleaned $rc cached tags.");