]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_psql_trgm/init.php
2 class Af_Psql_Trgm
extends Plugin
{
5 private $filters = array ();
9 "Marks similar articles as read (requires pg_trgm)" ,
14 $similarity = ( float ) db_escape_string ( $_POST [ "similarity" ]);
15 $min_title_length = ( int ) db_escape_string ( $_POST [ "min_title_length" ]);
17 if ( $similarity < 0 ) $similarity = 0 ;
18 if ( $similarity > 1 ) $similarity = 1 ;
20 if ( $min_title_length < 0 ) $min_title_length = 0 ;
22 $similarity = sprintf ( "%.2f" , $similarity );
24 $this -> host
-> set ( $this , "similarity" , $similarity );
25 $this -> host
-> set ( $this , "min_title_length" , $min_title_length );
27 echo T_sprintf ( "Data saved ( %s )" , $similarity );
30 function init ( $host ) {
33 $host -> add_hook ( $host :: HOOK_ARTICLE_FILTER
, $this );
34 $host -> add_hook ( $host :: HOOK_PREFS_TAB
, $this );
35 $host -> add_hook ( $host :: HOOK_PREFS_EDIT_FEED
, $this );
36 $host -> add_hook ( $host :: HOOK_PREFS_SAVE_FEED
, $this );
40 function hook_prefs_tab ( $args ) {
41 if ( $args != "prefFeeds" ) return ;
43 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Mark similar articles as read' ). " \" >" ;
45 if ( DB_TYPE
!= "pgsql" ) {
46 print_error ( "Database type not supported." );
49 $result = db_query ( "select 'similarity'::regproc" );
51 if ( db_num_rows ( $result ) == 0 ) {
52 print_error ( "pg_trgm extension not found." );
55 $similarity = $this -> host
-> get ( $this , "similarity" );
56 $min_title_length = $this -> host
-> get ( $this , "min_title_length" );
58 if (! $similarity ) $similarity = '0.75' ;
59 if (! $min_title_length ) $min_title_length = '32' ;
61 print "<form dojoType= \" dijit.form.Form \" >" ;
63 print "<script type= \" dojo/method \" event= \" onSubmit \" args= \" evt \" >
65 if (this.validate()) {
66 console.log(dojo.objectToQuery(this.getValues()));
67 new Ajax.Request('backend.php', {
68 parameters: dojo.objectToQuery(this.getValues()),
69 onComplete: function(transport) {
70 notify_info(transport.responseText);
77 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" op \" value= \" pluginhandler \" >" ;
78 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" method \" value= \" save \" >" ;
79 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" plugin \" value= \" af_psql_trgm \" >" ;
81 print_notice ( "PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking." );
84 print_notice ( "Enable the plugin for specific feeds in the feed editor." );
86 print "<h3>" . __ ( "Global settings" ) . "</h3>" ;
90 print "<tr><td width= \" 40% \" >" . __ ( "Minimum similarity:" ). "</td>" ;
92 <input dojoType= \" dijit.form.ValidationTextBox \"
94 required= \" 1 \" name= \" similarity \" value= \" $similarity\" ></td></tr>" ;
95 print "<tr><td width= \" 40% \" >" . __ ( "Minimum title length:" ). "</td>" ;
97 <input dojoType= \" dijit.form.ValidationTextBox \"
99 required= \" 1 \" name= \" min_title_length \" value= \" $min_title_length\" ></td></tr>" ;
104 print "<p><button dojoType= \" dijit.form.Button \" type= \" submit \" >" .
105 __ ( "Save" ). "</button>" ;
112 function hook_prefs_edit_feed ( $feed_id ) {
113 print "<div class= \" dlgSec \" >" . __ ( "Similarity (pg_trgm)" ). "</div>" ;
114 print "<div class= \" dlgSecCont \" >" ;
116 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
117 if (! array ( $enabled_feeds )) $enabled_feeds = array ();
119 $key = array_search ( $feed_id , $enabled_feeds );
120 $checked = $key !== FALSE ?
"checked" : "" ;
122 print "<hr/><input dojoType= \" dijit.form.CheckBox \" type= \" checkbox \" id= \" trgm_similarity_enabled \"
123 name= \" trgm_similarity_enabled \"
124 $checked > <label for= \" trgm_similarity_enabled \" >" . __ ( 'Mark similar articles as read' ). "</label>" ;
129 function hook_prefs_save_feed ( $feed_id ) {
130 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
131 if (! is_array ( $enabled_feeds )) $enabled_feeds = array ();
133 $enable = checkbox_to_sql_bool ( $_POST [ "trgm_similarity_enabled" ]) == 'true' ;
134 $key = array_search ( $feed_id , $enabled_feeds );
137 if ( $key === FALSE ) {
138 array_push ( $enabled_feeds , $feed_id );
141 if ( $key !== FALSE ) {
142 unset ( $enabled_feeds [ $key ]);
146 $this -> host
-> set ( $this , "enabled_feeds" , $enabled_feeds );
149 function hook_article_filter ( $article ) {
151 if ( DB_TYPE
!= "pgsql" ) return $article ;
153 $result = db_query ( "select 'similarity'::regproc" );
154 if ( db_num_rows ( $result ) == 0 ) return $article ;
156 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
157 $key = array_search ( $article [ "feed" ][ "id" ], $enabled_feeds );
158 if ( $key === FALSE ) return $article ;
160 $similarity = ( float ) $this -> host
-> get ( $this , "similarity" );
161 if ( $similarity < 0.01 ) return $article ;
163 $min_title_length = ( int ) $this -> host
-> get ( $this , "min_length" );
164 if ( mb_strlen ( $article [ "title" ]) < $min_title_length ) return $article ;
166 $owner_uid = $article [ "owner_uid" ];
167 $feed_id = $article [ "feed" ][ "id" ];
169 $title_escaped = db_escape_string ( $article [ "title" ]);
171 $result = db_query ( "SELECT MAX(SIMILARITY(title, ' $title_escaped ')) AS ms
172 FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
173 date_entered >= NOW() - interval '1 day' AND
174 owner_uid = $owner_uid " );
176 $similarity_result = db_fetch_result ( $result , 0 , "ms" );
178 //_debug("similarity result: $similarity_result");
180 if ( $similarity_result >= $similarity ) {
181 $article [ "force_catchup" ] = true ;
188 function api_version () {