cgi-bin/DW/Hooks/SiteSearch.pm
author fu
Thu Feb 23 02:21:54 2012 +0800
changeset 4315 34b3c4ba3afb
parent 4087 0363ba0c8e9a
permissions -rw-r--r--
http://bugs.dwscoalition.org/show_bug.cgi?id=3859

Tweak for grammar / clarity / wording.

Patch by fu.
     1 #!/usr/bin/perl
     2 #
     3 # DW::Hooks::SiteSearch
     4 #
     5 # Hooks for Site Search functionality.
     6 #
     7 # Authors:
     8 #      Mark Smith <mark@dreamwidth.org>
     9 #
    10 # Copyright (c) 2009 by Dreamwidth Studios, LLC.
    11 #
    12 # This program is free software; you may redistribute it and/or modify it under
    13 # the same terms as Perl itself.  For a copy of the license, please reference
    14 # 'perldoc perlartistic' or 'perldoc perlgpl'.
    15 #
    16 
    17 package DW::Hooks::SiteSearch;
    18 
    19 use strict;
    20 use LJ::Hooks;
    21 
    22 sub _sphinx_db {
    23     # ensure we can talk to our system
    24     return LJ::get_dbh( 'sphinx_search' )
    25         or die "Unable to get sphinx_search database handle.\n";
    26 }
    27 
    28 LJ::Hooks::register_hook( 'setprop', sub {
    29     my %opts = @_;
    30     return unless $opts{prop} eq 'opt_blockglobalsearch';
    31 
    32     my $dbh = _sphinx_db() or return 0;
    33     $dbh->do( 'UPDATE posts_raw SET allow_global_search = ? WHERE journal_id = ?',
    34               undef, $opts{value} eq 'Y' ? 0 : 1, $opts{u}->id );
    35     die $dbh->errstr if $dbh->err;
    36 
    37     # looks good
    38     return 1;
    39 } );
    40 
    41 
    42 # set when the user's status(vis) changes
    43 # the user may still undelete or be unsuspended
    44 # so we don't want to remove from indexing just yet
    45 sub _mark_deleted {
    46     my ( $u, $is_deleted ) = @_;
    47 
    48     my $dbh = _sphinx_db() or return 0;
    49     $dbh->do( 'UPDATE posts_raw SET is_deleted = ? where journal_id = ?',
    50               undef, $is_deleted, $u->id );
    51     die $dbh->errstr if $dbh->err;
    52 
    53     return 1;
    54 }
    55 
    56 LJ::Hooks::register_hook( 'account_delete', sub { _mark_deleted( $_[0], 1 ) } );
    57 LJ::Hooks::register_hook( 'account_cancel', sub { _mark_deleted( $_[0], 1 ) } );
    58 LJ::Hooks::register_hook( 'account_makevisible', sub {
    59     my ( $u, %opts ) = @_;
    60 
    61     my $old = $opts{old_statusvis};
    62     _mark_deleted( $u, 0 ) if $old eq "D" || $old eq "S";
    63 } );
    64 
    65 
    66 LJ::Hooks::register_hook( 'purged_user', sub {
    67     my ( $u ) = @_;
    68 
    69     my $sclient = LJ::theschwartz() or die;
    70 
    71     # queue up a copier job, which will notice that the entries by this user have been deleted...
    72     $sclient->insert_jobs( TheSchwartz::Job->new_from_array( 'DW::Worker::Sphinx::Copier',
    73                                 { userid => $u->id } ) );
    74 
    75 });
    76 
    77 1;