| Filename | /home/vagrant/kohaclone/C4/Search/History.pm |
| Statements | Executed 6 statements in 18µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 4 | 1 | 1 | 180µs | 28.1ms | C4::Search::History::get_from_session |
| 1 | 1 | 1 | 49µs | 152µs | C4::Search::History::BEGIN@3 |
| 1 | 1 | 1 | 18µs | 68µs | C4::Search::History::BEGIN@7 |
| 1 | 1 | 1 | 15µs | 15µs | C4::Search::History::BEGIN@5 |
| 1 | 1 | 1 | 14µs | 70µs | C4::Search::History::BEGIN@11 |
| 1 | 1 | 1 | 12µs | 35µs | C4::Search::History::BEGIN@10 |
| 1 | 1 | 1 | 12µs | 101µs | C4::Search::History::BEGIN@9 |
| 1 | 1 | 1 | 11µs | 14µs | C4::Search::History::BEGIN@6 |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::add |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::add_to_session |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::delete |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::delete_from_cookie |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::get |
| 0 | 0 | 0 | 0s | 0s | C4::Search::History::set_to_session |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package C4::Search::History; | ||||
| 2 | |||||
| 3 | 2 | 255µs | # spent 152µs (49+103) within C4::Search::History::BEGIN@3 which was called:
# once (49µs+103µs) by C4::Auth::BEGIN@32 at line 3 # spent 152µs making 1 call to C4::Search::History::BEGIN@3
# spent 103µs making 1 call to Modern::Perl::import | ||
| 4 | |||||
| 5 | 1 | 15µs | # spent 15µs within C4::Search::History::BEGIN@5 which was called:
# once (15µs+0s) by C4::Auth::BEGIN@32 at line 5 # spent 15µs making 1 call to C4::Search::History::BEGIN@5 | ||
| 6 | 2 | 17µs | # spent 14µs (11+3) within C4::Search::History::BEGIN@6 which was called:
# once (11µs+3µs) by C4::Auth::BEGIN@32 at line 6 # spent 14µs making 1 call to C4::Search::History::BEGIN@6
# spent 3µs making 1 call to C4::Context::import | ||
| 7 | 2 | 119µs | # spent 68µs (18+50) within C4::Search::History::BEGIN@7 which was called:
# once (18µs+50µs) by C4::Auth::BEGIN@32 at line 7 # spent 68µs making 1 call to C4::Search::History::BEGIN@7
# spent 50µs making 1 call to Exporter::import | ||
| 8 | |||||
| 9 | 2 | 190µs | # spent 101µs (12+89) within C4::Search::History::BEGIN@9 which was called:
# once (12µs+89µs) by C4::Auth::BEGIN@32 at line 9 # spent 101µs making 1 call to C4::Search::History::BEGIN@9
# spent 89µs making 1 call to JSON::import | ||
| 10 | 2 | 59µs | # spent 35µs (12+23) within C4::Search::History::BEGIN@10 which was called:
# once (12µs+23µs) by C4::Auth::BEGIN@32 at line 10 # spent 35µs making 1 call to C4::Search::History::BEGIN@10
# spent 23µs making 1 call to Exporter::import | ||
| 11 | 2 | 126µs | # spent 70µs (14+56) within C4::Search::History::BEGIN@11 which was called:
# once (14µs+56µs) by C4::Auth::BEGIN@32 at line 11 # spent 70µs making 1 call to C4::Search::History::BEGIN@11
# spent 56µs making 1 call to Exporter::import | ||
| 12 | |||||
| 13 | sub add { | ||||
| 14 | my ($params) = @_; | ||||
| 15 | my $userid = $params->{userid}; | ||||
| 16 | my $sessionid = $params->{sessionid}; | ||||
| 17 | my $query_desc = $params->{query_desc}; | ||||
| 18 | my $query_cgi = $params->{query_cgi}; | ||||
| 19 | my $total = $params->{total} // 0; | ||||
| 20 | my $type = $params->{type} || 'biblio'; | ||||
| 21 | my $time = $params->{time}; | ||||
| 22 | |||||
| 23 | my $dbh = C4::Context->dbh; | ||||
| 24 | |||||
| 25 | # Add the request the user just made | ||||
| 26 | my $query = q{ | ||||
| 27 | INSERT INTO search_history( | ||||
| 28 | userid, sessionid, query_desc, query_cgi, type, total, time | ||||
| 29 | ) VALUES( | ||||
| 30 | ?, ?, ?, ?, ?, ?, ? | ||||
| 31 | ) | ||||
| 32 | }; | ||||
| 33 | my $sth = $dbh->prepare($query); | ||||
| 34 | $sth->execute( $userid, $sessionid, $query_desc, $query_cgi, $type, | ||||
| 35 | $total, $time ); | ||||
| 36 | } | ||||
| 37 | |||||
| 38 | sub add_to_session { | ||||
| 39 | my ($params) = @_; | ||||
| 40 | my $cgi = $params->{cgi}; | ||||
| 41 | my $query_desc = $params->{query_desc} || "unknown"; | ||||
| 42 | my $query_cgi = $params->{query_cgi} || "unknown"; | ||||
| 43 | my $total = $params->{total}; | ||||
| 44 | my $type = $params->{type} || 'biblio'; | ||||
| 45 | |||||
| 46 | # To a cookie (the user is not logged in) | ||||
| 47 | my $now = dt_from_string; | ||||
| 48 | my $id = $now->year . $now->month . $now->day . $now->hour . $now->minute . $now->second . int(rand(100)); | ||||
| 49 | my @recent_searches = get_from_session( { cgi => $cgi } ); | ||||
| 50 | push @recent_searches, { | ||||
| 51 | query_desc => $query_desc, | ||||
| 52 | query_cgi => $query_cgi, | ||||
| 53 | total => "$total", | ||||
| 54 | type => $type, | ||||
| 55 | time => output_pref( { dt => $now, dateformat => 'iso', timeformat => '24hr' } ), | ||||
| 56 | id => $id, | ||||
| 57 | }; | ||||
| 58 | |||||
| 59 | shift @recent_searches if ( @recent_searches > 15 ); | ||||
| 60 | set_to_session( { cgi => $cgi, search_history => \@recent_searches } ); | ||||
| 61 | } | ||||
| 62 | |||||
| 63 | sub delete { | ||||
| 64 | my ($params) = @_; | ||||
| 65 | my $id = $params->{id}; | ||||
| 66 | my $userid = $params->{userid}; | ||||
| 67 | my $sessionid = $params->{sessionid}; | ||||
| 68 | my $type = $params->{type} || q{}; | ||||
| 69 | my $previous = $params->{previous} || 0; | ||||
| 70 | my $interval = $params->{interval} || 0; | ||||
| 71 | |||||
| 72 | unless ( ref( $id ) ) { | ||||
| 73 | $id = $id ? [ $id ] : []; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | unless ( $userid or @$id or $interval ) { | ||||
| 77 | warn "ERROR: userid, id or interval is required for history deletion"; | ||||
| 78 | return; | ||||
| 79 | } | ||||
| 80 | |||||
| 81 | my $dbh = C4::Context->dbh; | ||||
| 82 | my $query = q{ | ||||
| 83 | DELETE FROM search_history | ||||
| 84 | WHERE 1 | ||||
| 85 | }; | ||||
| 86 | |||||
| 87 | $query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @$id ) . q{ )} | ||||
| 88 | if @$id; | ||||
| 89 | |||||
| 90 | $query .= q{ | ||||
| 91 | AND userid = ? | ||||
| 92 | } if $userid; | ||||
| 93 | |||||
| 94 | if ($sessionid) { | ||||
| 95 | $query .= | ||||
| 96 | $previous | ||||
| 97 | ? q{ AND sessionid != ?} | ||||
| 98 | : q{ AND sessionid = ?}; | ||||
| 99 | } | ||||
| 100 | |||||
| 101 | $query .= q{ AND type = ?} | ||||
| 102 | if $type; | ||||
| 103 | |||||
| 104 | # FIXME DATE_SUB is a Mysql-ism. Postgres uses: datefield - INTERVAL '6 months' | ||||
| 105 | $query .= q{ AND time < DATE_SUB( NOW(), INTERVAL ? DAY )} | ||||
| 106 | if $interval; | ||||
| 107 | |||||
| 108 | $dbh->do( | ||||
| 109 | $query, {}, | ||||
| 110 | ( @$id ? ( @$id ) : () ), | ||||
| 111 | ( $userid ? $userid : () ), | ||||
| 112 | ( $sessionid ? $sessionid : () ), | ||||
| 113 | ( $type ? $type : () ), | ||||
| 114 | ( $interval ? $interval : () ), | ||||
| 115 | ); | ||||
| 116 | } | ||||
| 117 | |||||
| 118 | sub delete_from_cookie { | ||||
| 119 | my ($params) = @_; | ||||
| 120 | my $cookie = $params->{cookie}; | ||||
| 121 | my $id = $params->{id}; | ||||
| 122 | |||||
| 123 | return unless $cookie; | ||||
| 124 | |||||
| 125 | unless ( ref( $id ) ) { | ||||
| 126 | $id = $id ? [ $id ] : []; | ||||
| 127 | } | ||||
| 128 | return unless @$id; | ||||
| 129 | |||||
| 130 | my @searches; | ||||
| 131 | if ( $cookie ){ | ||||
| 132 | $cookie = uri_unescape( $cookie ); | ||||
| 133 | if (decode_json( $cookie )) { | ||||
| 134 | @searches = @{decode_json( $cookie )} | ||||
| 135 | } | ||||
| 136 | } | ||||
| 137 | |||||
| 138 | @searches = map { | ||||
| 139 | my $search = $_; | ||||
| 140 | ( grep { $_ != $search->{id} } @$id ) ? $search : () | ||||
| 141 | } @searches; | ||||
| 142 | |||||
| 143 | return uri_escape( encode_json( \@searches ) ); | ||||
| 144 | |||||
| 145 | } | ||||
| 146 | |||||
| 147 | sub get { | ||||
| 148 | my ($params) = @_; | ||||
| 149 | my $id = $params->{id}; | ||||
| 150 | my $userid = $params->{userid}; | ||||
| 151 | my $sessionid = $params->{sessionid}; | ||||
| 152 | my $type = $params->{type}; | ||||
| 153 | my $previous = $params->{previous}; | ||||
| 154 | |||||
| 155 | unless ( ref( $id ) ) { | ||||
| 156 | $id = $id ? [ $id ] : []; | ||||
| 157 | } | ||||
| 158 | |||||
| 159 | unless ( $userid or @$id ) { | ||||
| 160 | warn "ERROR: userid is required for history search"; | ||||
| 161 | return; | ||||
| 162 | } | ||||
| 163 | |||||
| 164 | my $query = q{ | ||||
| 165 | SELECT * | ||||
| 166 | FROM search_history | ||||
| 167 | WHERE 1 | ||||
| 168 | }; | ||||
| 169 | |||||
| 170 | $query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @$id ) . q{ )} | ||||
| 171 | if @$id; | ||||
| 172 | |||||
| 173 | $query .= q{ | ||||
| 174 | AND userid = ? | ||||
| 175 | } if $userid; | ||||
| 176 | |||||
| 177 | if ($sessionid) { | ||||
| 178 | $query .= | ||||
| 179 | $previous | ||||
| 180 | ? q{ AND sessionid != ?} | ||||
| 181 | : q{ AND sessionid = ?}; | ||||
| 182 | } | ||||
| 183 | |||||
| 184 | $query .= q{ AND type = ?} | ||||
| 185 | if $type; | ||||
| 186 | |||||
| 187 | my $dbh = C4::Context->dbh; | ||||
| 188 | my $sth = $dbh->prepare($query); | ||||
| 189 | $sth->execute( | ||||
| 190 | ( @$id ? ( @$id ) : () ), | ||||
| 191 | ( $userid ? $userid : () ), | ||||
| 192 | ( $sessionid ? $sessionid : () ), | ||||
| 193 | ( $type ? $type : () ) | ||||
| 194 | ); | ||||
| 195 | return $sth->fetchall_arrayref( {} ); | ||||
| 196 | } | ||||
| 197 | |||||
| 198 | # spent 28.1ms (180µs+27.9) within C4::Search::History::get_from_session which was called 4 times, avg 7.02ms/call:
# 4 times (180µs+27.9ms) by C4::Auth::get_template_and_user at line 390 of C4/Auth.pm, avg 7.02ms/call | ||||
| 199 | 1 | 500ns | my ($params) = @_; | ||
| 200 | 1 | 800ns | my $cgi = $params->{cgi}; | ||
| 201 | 1 | 5µs | 4 | 1.97ms | my $sessionID = $cgi->cookie('CGISESSID'); # spent 1.97ms making 4 calls to CGI::cookie, avg 494µs/call |
| 202 | 1 | 200ns | return () unless $sessionID; | ||
| 203 | 1 | 4µs | 4 | 24.5ms | my $session = C4::Auth::get_session($sessionID); # spent 24.5ms making 4 calls to C4::Auth::get_session, avg 6.12ms/call |
| 204 | 1 | 8µs | 4 | 50µs | return () unless $session and $session->param('search_history'); # spent 50µs making 4 calls to CGI::Session::param, avg 12µs/call |
| 205 | my $obj = | ||||
| 206 | eval { decode_json( uri_unescape( $session->param('search_history') ) ) }; | ||||
| 207 | return () unless defined $obj; | ||||
| 208 | return () unless ref $obj eq 'ARRAY'; | ||||
| 209 | return @{$obj}; | ||||
| 210 | } | ||||
| 211 | |||||
| 212 | sub set_to_session { | ||||
| 213 | my ($params) = @_; | ||||
| 214 | my $cgi = $params->{cgi}; | ||||
| 215 | my $search_history = $params->{search_history}; | ||||
| 216 | my $sessionID = $cgi->cookie('CGISESSID'); | ||||
| 217 | return () unless $sessionID; | ||||
| 218 | my $session = C4::Auth::get_session($sessionID); | ||||
| 219 | return () unless $session; | ||||
| 220 | $session->param( 'search_history', | ||||
| 221 | uri_escape( encode_json($search_history) ) ); | ||||
| 222 | } | ||||
| 223 | |||||
| 224 | 1; | ||||
| 225 | |||||
| 226 | __END__ |