]> git.wh0rd.org - tt-rss.git/blobdiff - include/rssfuncs.php
add auto_assign_labels plugin; allow article filter plugins to add labels to articles
[tt-rss.git] / include / rssfuncs.php
index 8f71f950afbfa79d128f708fe512ca1e81ee0996..1a26e0485a3dac320df9fed0fd533d6681ab513e 100644 (file)
 
                        if (!$registered_title || $registered_title == "[Unknown]") {
 
-                               $feed_title = db_escape_string($rss->get_title());
+                               $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199));
 
                                if ($feed_title) {
                                        _debug("registering title: $feed_title", $debug_enabled);
                                        $entry_language = $lang->detect($entry_title . " " . $entry_content, 1);
 
                                        if (count($entry_language) > 0) {
-                                               @$entry_language = array_keys($entry_language)[0];
+                                               $possible = array_keys($entry_language);
+                                               $entry_language = $possible[0];
 
                                                _debug("detected language: $entry_language", $debug_enabled);
                                        } else {
                                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,
-                                       "language" => $entry_language,
+                                       "force_catchup" => false, // ugly hack for the time being
+                                       "language" => $entry_language, // read only
                                        "feed" => array("id" => $feed,
                                                "fetch_url" => $fetch_url,
                                                "site_url" => $site_url)
 
                                _debug("article hash: $entry_current_hash [stored=$entry_stored_hash]", $debug_enabled);
 
-                               if ($entry_current_hash == $entry_stored_hash) {
+                               if ($entry_current_hash == $entry_stored_hash && !isset($_REQUEST["force_rehash"])) {
                                        _debug("stored article seems up to date [IID: $base_entry_id], updating timestamp only", $debug_enabled);
 
                                        // we keep encountering the entry in feeds, so we need to
                                        db_query("UPDATE ttrss_entries SET date_updated = NOW()
                                                WHERE id = '$base_entry_id'");
 
-                                       continue;
+                    // 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;
+                    }
                                }
 
                                _debug("hash differs, applying plugin filters:", $debug_enabled);
                                $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"];
+
+                               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
 
                                                _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) {