svnno****@sourc*****
svnno****@sourc*****
2007年 10月 16日 (火) 13:15:40 JST
Revision: 189 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=slashdotjp&view=rev&rev=189 Author: tach Date: 2007-10-16 13:15:40 +0900 (Tue, 16 Oct 2007) Log Message: ----------- merge senna search code from OTP Modified Paths: -------------- slashjp/trunk/plugins/Search/Search.pm slashjp/trunk/plugins/Search/search.pl slashjp/trunk/plugins/Search/templates/searchform;search;default -------------- next part -------------- Modified: slashjp/trunk/plugins/Search/Search.pm =================================================================== --- slashjp/trunk/plugins/Search/Search.pm 2007-10-16 03:14:55 UTC (rev 188) +++ slashjp/trunk/plugins/Search/Search.pm 2007-10-16 04:15:40 UTC (rev 189) @@ -38,9 +38,10 @@ # select comment ID, comment Title, Author, Email, link to comment # and SID, article title, type and a link to the article $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); + my $query = $self->_fixupQuerySql($form->{query}); my $constants = getCurrentStatic(); my $columns; + $columns .= "SQL_CALC_FOUND_ROWS "; $columns .= "primaryskid, url, discussions.uid AS author_uid, discussions.title AS title, "; $columns .= "pid, subject, ts, date, comments.uid AS uid, cid, "; $columns .= "discussions.id AS did, dkid, discussions.sid"; @@ -91,6 +92,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $search = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); my $desc = $reader->getDescriptions('discussion_kinds'); foreach my $c (@$search) { @@ -111,10 +113,10 @@ # userSearch REALLY doesn't need to be ordered by keyword since you # only care if the substring is found. $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); + my $query = $self->_fixupQuerySql($form->{query}); my $constants = getCurrentStatic(); - my $columns = 'fakeemail,nickname,users.uid as uid,journal_last_entry_date '; + my $columns = 'SQL_CALC_FOUND_ROWS fakeemail,nickname,users.uid as uid,journal_last_entry_date '; $columns .= ", TRUNCATE( " . $self->_score('nickname', $form->{query}, $constants->{search_method}) . ", 1) as score " if $form->{query}; @@ -138,6 +140,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $users = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $users; } @@ -150,9 +153,10 @@ my $constants = getCurrentStatic(); $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); + my $query = $self->_fixupQuerySql($form->{query}); my $columns; - $columns .= "title, stories.stoid AS stoid, sid, "; + $columns .= "SQL_CALC_FOUND_ROWS "; + $columns .= "title, stories.stoid AS stoid, sid, dept, "; $columns .= "time, commentcount, stories.primaryskid AS skid, "; $columns .= "introtext "; if ($form->{query}) { @@ -196,7 +200,7 @@ map { $reader->sqlQuote($_) } grep { $_ != $gSkin->{skid} } # allow searching on THIS skid @{$constants->{search_ignore_skids}}; - $where .= " AND primaryskid NOT IN ($skid_list) "; + $where .= " AND primaryskid NOT IN ($skid_list) " if ($skid_list); } my $skin = $reader->getSkin($form->{section} || $gSkin->{skid}); @@ -269,6 +273,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); # Don't return just one topic id in tid, also return an arrayref # in tids, with all topic ids in the preferred order. @@ -305,8 +310,9 @@ my $constants = getCurrentStatic(); $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); + my $query = $self->_fixupQuerySql($form->{query}); my $columns; + $columns .= "SQL_CALC_FOUND_ROWS "; $columns .= "users.nickname as nickname, journals.description as description, "; $columns .= "journals.id as id, date, users.uid as uid, article"; $columns .= ", TRUNCATE((( " . $self->_score('description', $form->{query}, $constants->{search_method}) . " + " . $self->_score('article', $form->{query}, $constants->{search_method}) .") / 2), 1) as score " @@ -334,6 +340,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $stories; } @@ -345,8 +352,8 @@ my $constants = getCurrentStatic(); $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); - my $columns = "*"; + my $query = $self->_fixupQuerySql($form->{query}); + my $columns = "SQL_CALC_FOUND_ROWS *"; $columns .= ", TRUNCATE( " . $self->_score('question', $form->{query}, $constants->{search_method}) . ", 1) as score " if $form->{query}; my $tables = "pollquestions"; @@ -377,6 +384,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $stories; } @@ -389,8 +397,8 @@ my $reader = getObject('Slash::DB', { db_type => 'reader' }); $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); - my $columns = "*"; + my $query = $self->_fixupQuerySql($form->{query}); + my $columns = "SQL_CALC_FOUND_ROWS *"; $columns .= ", TRUNCATE( " . $self->_score('subj,story', $form->{query}, $constants->{search_method}) . ", 1) as score " if $form->{query}; my $tables = "submissions"; @@ -421,6 +429,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $stories; } @@ -432,8 +441,8 @@ my $constants = getCurrentStatic(); $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); - my $columns = "title, link, description, created"; + my $query = $self->_fixupQuerySql($form->{query}); + my $columns = "SQL_CALC_FOUND_ROWS title, link, description, created"; $columns .= ", TRUNCATE( " . $self->_score('title,description', $form->{query}, $constants->{search_method}) . ", 1) as score " if $form->{query}; my $tables = "rss_raw"; @@ -455,6 +464,7 @@ $other .= " LIMIT $start, $limit" if $limit; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $stories; } @@ -463,11 +473,11 @@ sub findDiscussion { my($self, $form, $start, $limit, $sort) = @_; $form->{query} = $self->_cleanQuery($form->{query}); - my $query = $self->sqlQuote($form->{query}); + my $query = $self->_fixupQuerySql($form->{query}); my $constants = getCurrentStatic(); $start ||= 0; - my $columns = "*"; + my $columns = "SQL_CALC_FOUND_ROWS *"; $columns .= ", TRUNCATE( " . $self->_score('title', $form->{query}, $constants->{search_method}) . ", 1) as score " if $form->{query}; my $tables = "discussions"; @@ -509,6 +519,7 @@ $other .= " LIMIT $start, $limit" if $limit; # print STDERR "select $columns from $tables where $where $other\n"; my $stories = $self->sqlSelectAllHashrefArray($columns, $tables, $where, $other ); + $self->sqlDo('SET @totalhits = FOUND_ROWS();'); return $stories; } @@ -557,7 +568,7 @@ } return "($method($col, $terms))"; } else { - $query = $self->sqlQuote($query); + $query = $self->_fixupQuerySql($query); return "\n(MATCH ($col) AGAINST ($query))\n"; } } @@ -567,17 +578,25 @@ my ($self, $query) = @_; # This next line could be removed -Brian # get rid of bad characters - $query =~ s/[^A-Z0-9'. :\/_]/ /gi; + #$query =~ s/[^A-Z0-9'. :\/_]/ /gi; + $query =~ s/[\x00-\x20\x27\x60\x7f]+/ /gi; # This should be configurable -Brian # truncate query length - if (length($query) > 40) { - $query = substr($query, 0, 40); + if (length($query) > 200) { + $query = substr($query, 0, 200); } return $query; } +sub _fixupQuerySql { + my ($self, $query) = @_; + if (substr($query, 0, 1) ne '*') { + $query = "*D+ ${query}"; + } + return $self->sqlQuote($query) . ' IN BOOLEAN MODE'; +} ################################################################# sub DESTROY { Modified: slashjp/trunk/plugins/Search/search.pl =================================================================== --- slashjp/trunk/plugins/Search/search.pl 2007-10-16 03:14:55 UTC (rev 188) +++ slashjp/trunk/plugins/Search/search.pl 2007-10-16 04:15:40 UTC (rev 189) @@ -81,7 +81,7 @@ $form->{op} = 'stories' if !exists($ops_rss{$form->{op}}); $ops_rss{$form->{op}}->($form, $constants, $slashdb, $searchDB, $gSkin); } else { - my $text = strip_notags($form->{query}); + my $text = strip_attribute($form->{query}); my $header_title = getData('search_header_title', { text => $text }); my $titlebar_title = getData('search_titlebar_title', { text => $text }); header($header_title) or return; @@ -171,6 +171,7 @@ my $start = $form->{start} || 0; my $comments = $searchDB->findComments($form, $start, $constants->{search_default_display} + 1, $form->{sort}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); my $formats = $slashdb->getDescriptions('threshcodes'); my $threshold_select = createSelect( @@ -186,6 +187,7 @@ 'sort' => _sort(), threshhold => 1, threshold_select => $threshold_select, + totalhits => $totalhits, }); if ($comments && @$comments) { @@ -216,6 +218,7 @@ forward => $forward, args => _buildargs($form), start => $start, + totalhits => $totalhits, }); } else { print getData('nocomments'); @@ -228,10 +231,12 @@ my $start = $form->{start} || 0; my $users = $searchDB->findUsers($form, $start, $constants->{search_default_display} + 1, $form->{sort}, $form->{journal_only}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); slashDisplay('searchform', { op => $form->{op}, 'sort' => _sort(), journal_option => 1, + totalhits => $totalhits, }); if ($users && @$users) { @@ -261,6 +266,7 @@ back => $back, forward => $forward, args => _buildargs($form), + totalhits => $totalhits, }); } else { print getData('nousers'); @@ -273,6 +279,7 @@ my $start = $form->{start} || 0; my $stories = $searchDB->findStory($form, $start, $constants->{search_default_display} + 1, $form->{sort}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); my $topic_ref = $form->{tid} ? $slashdb->getTopic($form->{tid}) : { }; slashDisplay('searchform', { @@ -282,6 +289,7 @@ op => $form->{op}, authors => _authors(), 'sort' => _sort(), + totalhits => $totalhits, }); if ($stories && @$stories) { @@ -316,6 +324,7 @@ forward => $forward, args => _buildargs($form), start => $start, + totalhits => $totalhits, }); } else { print getData('nostories'); @@ -328,6 +337,7 @@ my $start = $form->{start} || 0; my $polls = $searchDB->findPollQuestion($form, $start, $constants->{search_default_display} + 1, $form->{sort}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); my $topic_ref = $form->{tid} ? $slashdb->getTopic($form->{tid}) : { }; slashDisplay('searchform', { op => $form->{op}, @@ -335,6 +345,7 @@ # sections => 1, # _skins(), tref => $topic_ref, 'sort' => _sort(), + totalhits => $totalhits, }); if ($polls && @$polls) { @@ -365,6 +376,7 @@ forward => $forward, args => _buildargs($form), start => $start, + totalhits => $totalhits, }); } else { print getData('nopolls'); @@ -522,9 +534,11 @@ my $start = $form->{start} || 0; my $entries = $searchDB->findJournalEntry($form, $start, $constants->{search_default_display} + 1, $form->{sort}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); slashDisplay('searchform', { op => $form->{op}, 'sort' => _sort(), + totalhits => $totalhits, }); # check for extra articles ... we request one more than we need @@ -559,6 +573,7 @@ forward => $forward, args => _buildargs($form), start => $start, + totalhits => $totalhits, }); } else { print getData('nojournals'); @@ -603,6 +618,7 @@ my $start = $form->{start} || 0; my $entries = $searchDB->findSubmission($form, $start, $constants->{search_default_display} + 1, $form->{sort}); + my $totalhits = $searchDB->sqlSelect('@totalhits'); my $topic_ref = $form->{tid} ? $slashdb->getTopic($form->{tid}) : { }; slashDisplay('searchform', { op => $form->{op}, @@ -611,6 +627,7 @@ submission_notes => $slashdb->getDescriptions('submission-notes'), tref => $topic_ref, 'sort' => _sort(), + totalhits => $totalhits, }); # check for extra articles ... we request one more than we need @@ -645,6 +662,7 @@ forward => $forward, args => _buildargs($form), start => $start, + totalhits => $totalhits, }); } else { print getData('nosubmissions'); Modified: slashjp/trunk/plugins/Search/templates/searchform;search;default =================================================================== --- slashjp/trunk/plugins/Search/templates/searchform;search;default 2007-10-16 03:14:55 UTC (rev 188) +++ slashjp/trunk/plugins/Search/templates/searchform;search;default 2007-10-16 04:15:40 UTC (rev 189) @@ -22,9 +22,9 @@ <form action="[% gSkin.rootdir %]/search.pl" method="get"> <fieldset> - <legend>Search [% form.query %]</legend> + <legend>Search [% form.query | strip_attribute %]</legend> <input type="hidden" name="tid" value="[% form.tid %]"> - <input type="text" size="40" name="query" value="[% form.query %]"> + <input type="text" size="40" name="query" value="[% form.query | strip_attribute %]"> [% IF authors %] [% Slash.createSelect("author", authors, form.author, 1, 0, 1) %] [% END %] @@ -58,7 +58,7 @@ [% IF threshhold %] Threshold [% threshold_select %] - <input type="hidden" name="sid" value="[% form.sid %]"> + <input type="hidden" name="sid" value="[% form.sid | strip_attribute %]"> [% END %] </div> </fieldset> @@ -94,7 +94,7 @@ IF thisparent %] :: <a href="[% gSkin.rootdir %]/search.pl?tid=[% thisparent %]">[% topics.$thisparent.textname %]</a> [% END; END; IF form.tid %] -:: <a href="[% gSkin.rootdir %]/search.pl?tid=[% form.tid %]">[% topics.${form.tid}.textname %]</a> +:: <a href="[% gSkin.rootdir %]/search.pl?tid=[% form.tid | strip_attribute %]">[% topics.${form.tid}.textname %]</a> [% END; IF listnames.size %] Topics [% END %]