]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_psql_trgm/init.php
2 class Af_Psql_Trgm
extends Plugin
{
8 "Marks similar articles as read (requires pg_trgm)" ,
13 $similarity = ( float ) db_escape_string ( $_POST [ "similarity" ]);
14 $min_title_length = ( int ) db_escape_string ( $_POST [ "min_title_length" ]);
16 if ( $similarity < 0 ) $similarity = 0 ;
17 if ( $similarity > 1 ) $similarity = 1 ;
19 if ( $min_title_length < 0 ) $min_title_length = 0 ;
21 $similarity = sprintf ( "%.2f" , $similarity );
23 $this -> host
-> set ( $this , "similarity" , $similarity );
24 $this -> host
-> set ( $this , "min_title_length" , $min_title_length );
26 echo T_sprintf ( "Data saved ( %s )" , $similarity );
29 function init ( $host ) {
32 $host -> add_hook ( $host :: HOOK_ARTICLE_FILTER
, $this );
33 $host -> add_hook ( $host :: HOOK_PREFS_TAB
, $this );
34 $host -> add_hook ( $host :: HOOK_PREFS_EDIT_FEED
, $this );
35 $host -> add_hook ( $host :: HOOK_PREFS_SAVE_FEED
, $this );
39 function hook_prefs_tab ( $args ) {
40 if ( $args != "prefFeeds" ) return ;
42 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Mark similar articles as read' ). " \" >" ;
44 if ( DB_TYPE
!= "pgsql" ) {
45 print_error ( "Database type not supported." );
48 $result = db_query ( "select 'similarity'::regproc" );
50 if ( db_num_rows ( $result ) == 0 ) {
51 print_error ( "pg_trgm extension not found." );
54 $similarity = $this -> host
-> get ( $this , "similarity" );
55 $min_title_length = $this -> host
-> get ( $this , "min_title_length" );
57 if (! $similarity ) $similarity = '0.75' ;
58 if (! $min_title_length ) $min_title_length = '32' ;
60 print "<form dojoType= \" dijit.form.Form \" >" ;
62 print "<script type= \" dojo/method \" event= \" onSubmit \" args= \" evt \" >
64 if (this.validate()) {
65 console.log(dojo.objectToQuery(this.getValues()));
66 new Ajax.Request('backend.php', {
67 parameters: dojo.objectToQuery(this.getValues()),
68 onComplete: function(transport) {
69 notify_info(transport.responseText);
76 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" op \" value= \" pluginhandler \" >" ;
77 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" method \" value= \" save \" >" ;
78 print "<input dojoType= \" dijit.form.TextBox \" style= \" display : none \" name= \" plugin \" value= \" af_psql_trgm \" >" ;
80 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." );
83 print_notice ( "Enable the plugin for specific feeds in the feed editor." );
85 print "<h3>" . __ ( "Global settings" ) . "</h3>" ;
89 print "<tr><td width= \" 40% \" >" . __ ( "Minimum similarity:" ). "</td>" ;
91 <input dojoType= \" dijit.form.ValidationTextBox \"
93 required= \" 1 \" name= \" similarity \" value= \" $similarity\" ></td></tr>" ;
94 print "<tr><td width= \" 40% \" >" . __ ( "Minimum title length:" ). "</td>" ;
96 <input dojoType= \" dijit.form.ValidationTextBox \"
98 required= \" 1 \" name= \" min_title_length \" value= \" $min_title_length\" ></td></tr>" ;
102 print "<p><button dojoType= \" dijit.form.Button \" type= \" submit \" >" .
103 __ ( "Save" ). "</button>" ;
107 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
108 if (! array ( $enabled_feeds )) $enabled_feeds = array ();
110 if ( count ( $enabled_feeds ) > 0 ) {
111 print "<h3>" . __ ( "Currently enabled for (click to edit):" ) . "</h3>" ;
113 print "<ul class= \" browseFeedList \" style= \" border-width : 1px \" >" ;
114 foreach ( $enabled_feeds as $f ) {
116 "<img src='images/pub_set.png'
117 style='vertical-align : middle'> <a href='#'
118 onclick='editFeed( $f )'>" .
119 getFeedTitle ( $f ) . "</a></li>" ;
127 function hook_prefs_edit_feed ( $feed_id ) {
128 print "<div class= \" dlgSec \" >" . __ ( "Similarity (pg_trgm)" ). "</div>" ;
129 print "<div class= \" dlgSecCont \" >" ;
131 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
132 if (! array ( $enabled_feeds )) $enabled_feeds = array ();
134 $key = array_search ( $feed_id , $enabled_feeds );
135 $checked = $key !== FALSE ?
"checked" : "" ;
137 print "<hr/><input dojoType= \" dijit.form.CheckBox \" type= \" checkbox \" id= \" trgm_similarity_enabled \"
138 name= \" trgm_similarity_enabled \"
139 $checked > <label for= \" trgm_similarity_enabled \" >" . __ ( 'Mark similar articles as read' ). "</label>" ;
144 function hook_prefs_save_feed ( $feed_id ) {
145 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
146 if (! is_array ( $enabled_feeds )) $enabled_feeds = array ();
148 $enable = checkbox_to_sql_bool ( $_POST [ "trgm_similarity_enabled" ]) == 'true' ;
149 $key = array_search ( $feed_id , $enabled_feeds );
152 if ( $key === FALSE ) {
153 array_push ( $enabled_feeds , $feed_id );
156 if ( $key !== FALSE ) {
157 unset ( $enabled_feeds [ $key ]);
161 $this -> host
-> set ( $this , "enabled_feeds" , $enabled_feeds );
164 function hook_article_filter ( $article ) {
166 if ( DB_TYPE
!= "pgsql" ) return $article ;
168 $result = db_query ( "select 'similarity'::regproc" );
169 if ( db_num_rows ( $result ) == 0 ) return $article ;
171 $enabled_feeds = $this -> host
-> get ( $this , "enabled_feeds" );
172 $key = array_search ( $article [ "feed" ][ "id" ], $enabled_feeds );
173 if ( $key === FALSE ) return $article ;
175 $similarity = ( float ) $this -> host
-> get ( $this , "similarity" );
176 if ( $similarity < 0.01 ) return $article ;
178 $min_title_length = ( int ) $this -> host
-> get ( $this , "min_length" );
179 if ( mb_strlen ( $article [ "title" ]) < $min_title_length ) return $article ;
181 $owner_uid = $article [ "owner_uid" ];
182 $feed_id = $article [ "feed" ][ "id" ];
184 $title_escaped = db_escape_string ( $article [ "title" ]);
186 $result = db_query ( "SELECT MAX(SIMILARITY(title, ' $title_escaped ')) AS ms
187 FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
188 date_entered >= NOW() - interval '1 day' AND
189 owner_uid = $owner_uid " );
191 $similarity_result = db_fetch_result ( $result , 0 , "ms" );
193 //_debug("similarity result: $similarity_result");
195 if ( $similarity_result >= $similarity ) {
196 $article [ "force_catchup" ] = true ;
203 function api_version () {