]>
Commit | Line | Data |
---|---|---|
84fb5b46 MKG |
1 | #!/usr/bin/perl |
2 | # BEGIN BPS TAGGED BLOCK {{{ | |
3 | # | |
4 | # COPYRIGHT: | |
5 | # | |
403d7b0b | 6 | # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC |
84fb5b46 MKG |
7 | # <sales@bestpractical.com> |
8 | # | |
9 | # (Except where explicitly superseded by other copyright notices) | |
10 | # | |
11 | # | |
12 | # LICENSE: | |
13 | # | |
14 | # This work is made available to you under the terms of Version 2 of | |
15 | # the GNU General Public License. A copy of that license should have | |
16 | # been provided with this software, but in any event can be snarfed | |
17 | # from www.gnu.org. | |
18 | # | |
19 | # This work is distributed in the hope that it will be useful, but | |
20 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | # General Public License for more details. | |
23 | # | |
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program; if not, write to the Free Software | |
26 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
27 | # 02110-1301 or visit their web page on the internet at | |
28 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. | |
29 | # | |
30 | # | |
31 | # CONTRIBUTION SUBMISSION POLICY: | |
32 | # | |
33 | # (The following paragraph is not intended to limit the rights granted | |
34 | # to you to modify and distribute this software under the terms of | |
35 | # the GNU General Public License and is only of importance to you if | |
36 | # you choose to contribute your changes and enhancements to the | |
37 | # community by submitting them to Best Practical Solutions, LLC.) | |
38 | # | |
39 | # By intentionally submitting any modifications, corrections or | |
40 | # derivatives to this work, or any other work intended for use with | |
41 | # Request Tracker, to Best Practical Solutions, LLC, you confirm that | |
42 | # you are the copyright holder for those contributions and you grant | |
43 | # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable, | |
44 | # royalty-free, perpetual, license to use, copy, create derivative | |
45 | # works based on those contributions, and sublicense and distribute | |
46 | # those contributions and any derivatives thereof. | |
47 | # | |
48 | # END BPS TAGGED BLOCK }}} | |
49 | use strict; | |
50 | use warnings; | |
51 | no warnings 'once'; | |
52 | ||
53 | # fix lib paths, some may be relative | |
af59614d | 54 | BEGIN { # BEGIN RT CMD BOILERPLATE |
84fb5b46 | 55 | require File::Spec; |
af59614d | 56 | require Cwd; |
84fb5b46 MKG |
57 | my @libs = ("lib", "local/lib"); |
58 | my $bin_path; | |
59 | ||
60 | for my $lib (@libs) { | |
61 | unless ( File::Spec->file_name_is_absolute($lib) ) { | |
af59614d | 62 | $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1]; |
84fb5b46 MKG |
63 | $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib ); |
64 | } | |
65 | unshift @INC, $lib; | |
66 | } | |
af59614d | 67 | |
84fb5b46 MKG |
68 | } |
69 | ||
70 | BEGIN { | |
71 | use RT; | |
72 | RT::LoadConfig(); | |
73 | RT::Init(); | |
74 | }; | |
75 | use RT::Interface::CLI (); | |
76 | ||
77 | my %OPT = ( | |
78 | help => 0, | |
79 | debug => 0, | |
80 | ); | |
81 | my @OPT_LIST = qw(help|h! debug!); | |
82 | ||
83 | my $db_type = RT->Config->Get('DatabaseType'); | |
84 | if ( $db_type eq 'Pg' ) { | |
85 | %OPT = ( | |
86 | %OPT, | |
87 | limit => 0, | |
88 | all => 0, | |
89 | ); | |
90 | push @OPT_LIST, 'limit=i', 'all!'; | |
91 | } | |
92 | elsif ( $db_type eq 'mysql' ) { | |
93 | %OPT = ( | |
94 | %OPT, | |
95 | limit => 0, | |
96 | all => 0, | |
97 | xmlpipe2 => 0, | |
98 | ); | |
99 | push @OPT_LIST, 'limit=i', 'all!', 'xmlpipe2!'; | |
100 | } | |
101 | elsif ( $db_type eq 'Oracle' ) { | |
102 | %OPT = ( | |
103 | %OPT, | |
104 | memory => '2M', | |
105 | ); | |
106 | push @OPT_LIST, qw(memory=s); | |
107 | } | |
108 | ||
109 | use Getopt::Long qw(GetOptions); | |
110 | GetOptions( \%OPT, @OPT_LIST ); | |
111 | ||
112 | if ( $OPT{'help'} ) { | |
113 | RT::Interface::CLI->ShowHelp( | |
114 | Sections => 'NAME|DESCRIPTION|'. uc($db_type), | |
115 | ); | |
116 | } | |
117 | ||
118 | my $fts_config = RT->Config->Get('FullTextSearch') || {}; | |
119 | unless ( $fts_config->{'Enable'} ) { | |
120 | print STDERR <<EOT; | |
121 | ||
122 | Full text search is disabled in your RT configuration. Run | |
b5747ff2 | 123 | /www/var/rt/sbin/rt-setup-fulltext-index to configure and enable it. |
84fb5b46 MKG |
124 | |
125 | EOT | |
126 | exit 1; | |
127 | } | |
128 | unless ( $fts_config->{'Indexed'} ) { | |
129 | print STDERR <<EOT; | |
130 | ||
131 | Full text search is enabled in your RT configuration, but not with any | |
132 | full-text database indexing -- hence this tool is not required. Read | |
133 | the documentation for %FullTextSearch in your RT_Config for more details. | |
134 | ||
135 | EOT | |
136 | exit 1; | |
137 | } | |
138 | ||
139 | if ( $db_type eq 'Oracle' ) { | |
140 | my $index = $fts_config->{'IndexName'} || 'rt_fts_index'; | |
141 | $RT::Handle->dbh->do( | |
142 | "begin ctx_ddl.sync_index(?, ?); end;", undef, | |
143 | $index, $OPT{'memory'} | |
144 | ); | |
145 | exit; | |
146 | } elsif ( $db_type eq 'mysql' ) { | |
147 | unless ($OPT{'xmlpipe2'}) { | |
148 | print STDERR <<EOT; | |
149 | ||
150 | Updates to the external Sphinx index are done via running the sphinx | |
151 | `indexer` tool: | |
152 | ||
153 | indexer rt | |
154 | ||
155 | EOT | |
156 | exit 1; | |
157 | } | |
158 | } | |
159 | ||
160 | my @types = qw(text html); | |
161 | foreach my $type ( @types ) { | |
162 | REDO: | |
163 | my $attachments = attachments($type); | |
164 | $attachments->Limit( | |
165 | FIELD => 'id', | |
166 | OPERATOR => '>', | |
167 | VALUE => last_indexed($type) | |
168 | ); | |
169 | $attachments->OrderBy( FIELD => 'id', ORDER => 'asc' ); | |
170 | $attachments->RowsPerPage( $OPT{'limit'} || 100 ); | |
171 | ||
172 | my $found = 0; | |
173 | while ( my $a = $attachments->Next ) { | |
174 | next if filter( $type, $a ); | |
175 | debug("Found attachment #". $a->id ); | |
176 | my $txt = extract($type, $a) or next; | |
177 | $found++; | |
178 | process( $type, $a, $txt ); | |
179 | debug("Processed attachment #". $a->id ); | |
180 | } | |
181 | finalize( $type, $attachments ) if $found; | |
182 | clean( $type ); | |
183 | goto REDO if $OPT{'all'} and $attachments->Count == ($OPT{'limit'} || 100) | |
184 | } | |
185 | ||
186 | sub attachments { | |
187 | my $type = shift; | |
188 | my $res = RT::Attachments->new( RT->SystemUser ); | |
189 | my $txn_alias = $res->Join( | |
190 | ALIAS1 => 'main', | |
191 | FIELD1 => 'TransactionId', | |
192 | TABLE2 => 'Transactions', | |
193 | FIELD2 => 'id', | |
194 | ); | |
195 | $res->Limit( | |
196 | ALIAS => $txn_alias, | |
197 | FIELD => 'ObjectType', | |
198 | VALUE => 'RT::Ticket', | |
199 | ); | |
200 | my $ticket_alias = $res->Join( | |
201 | ALIAS1 => $txn_alias, | |
202 | FIELD1 => 'ObjectId', | |
203 | TABLE2 => 'Tickets', | |
204 | FIELD2 => 'id', | |
205 | ); | |
206 | $res->Limit( | |
207 | ALIAS => $ticket_alias, | |
208 | FIELD => 'Status', | |
209 | OPERATOR => '!=', | |
210 | VALUE => 'deleted' | |
211 | ); | |
212 | ||
213 | return goto_specific( | |
214 | suffix => $type, | |
215 | error => "Don't know how to find $type attachments", | |
216 | arguments => [$res], | |
217 | ); | |
218 | } | |
219 | ||
220 | sub last_indexed { | |
221 | my ($type) = (@_); | |
222 | return goto_specific( | |
223 | suffix => $db_type, | |
224 | error => "Don't know how to find last indexed $type attachment for $db_type DB", | |
225 | arguments => \@_, | |
226 | ); | |
227 | } | |
228 | ||
229 | sub filter { | |
230 | my $type = shift; | |
231 | return goto_specific( | |
232 | suffix => $type, | |
233 | arguments => \@_, | |
234 | ); | |
235 | } | |
236 | ||
237 | sub extract { | |
238 | my $type = shift; | |
239 | return goto_specific( | |
240 | suffix => $type, | |
241 | error => "No way to convert $type attachment into text", | |
242 | arguments => \@_, | |
243 | ); | |
244 | } | |
245 | ||
246 | sub process { | |
247 | return goto_specific( | |
248 | suffix => $db_type, | |
249 | error => "No processer for $db_type DB", | |
250 | arguments => \@_, | |
251 | ); | |
252 | } | |
253 | ||
254 | sub finalize { | |
255 | return goto_specific( | |
256 | suffix => $db_type, | |
257 | arguments => \@_, | |
258 | ); | |
259 | } | |
260 | ||
261 | sub clean { | |
262 | return goto_specific( | |
263 | suffix => $db_type, | |
264 | arguments => \@_, | |
265 | ); | |
266 | } | |
267 | ||
268 | { | |
269 | sub last_indexed_mysql { | |
270 | my $type = shift; | |
271 | my $attr = $RT::System->FirstAttribute('LastIndexedAttachments'); | |
272 | return 0 unless $attr; | |
273 | return 0 unless exists $attr->{ $type }; | |
274 | return $attr->{ $type } || 0; | |
275 | } | |
276 | ||
277 | sub process_mysql { | |
278 | my ($type, $attachment, $text) = (@_); | |
279 | ||
280 | my $doc = sphinx_template(); | |
281 | ||
282 | my $element = $doc->createElement('sphinx:document'); | |
283 | $element->setAttribute( id => $attachment->id ); | |
284 | $element->appendTextChild( content => $$text ); | |
285 | ||
286 | $doc->documentElement->appendChild( $element ); | |
287 | } | |
288 | ||
289 | my $doc = undef; | |
290 | sub sphinx_template { | |
291 | return $doc if $doc; | |
292 | ||
293 | require XML::LibXML; | |
294 | $doc = XML::LibXML::Document->new('1.0', 'UTF-8'); | |
295 | my $root = $doc->createElement('sphinx:docset'); | |
296 | $doc->setDocumentElement( $root ); | |
297 | ||
298 | my $schema = $doc->createElement('sphinx:schema'); | |
299 | $root->appendChild( $schema ); | |
300 | foreach ( qw(content) ) { | |
301 | my $field = $doc->createElement('sphinx:field'); | |
302 | $field->setAttribute( name => $_ ); | |
303 | $schema->appendChild( $field ); | |
304 | } | |
305 | ||
306 | return $doc; | |
307 | } | |
308 | ||
309 | sub finalize_mysql { | |
310 | my ($type, $attachments) = @_; | |
311 | sphinx_template()->toFH(*STDOUT, 1); | |
312 | } | |
313 | ||
314 | sub clean_mysql { | |
315 | $doc = undef; | |
316 | } | |
317 | ||
318 | } | |
319 | ||
320 | sub last_indexed_pg { | |
321 | my $type = shift; | |
322 | my $attachments = attachments( $type ); | |
323 | my $alias = 'main'; | |
324 | if ( $fts_config->{'Table'} && $fts_config->{'Table'} ne 'Attachments' ) { | |
325 | $alias = $attachments->Join( | |
326 | TYPE => 'left', | |
327 | FIELD1 => 'id', | |
328 | TABLE2 => $fts_config->{'Table'}, | |
329 | FIELD2 => 'id', | |
330 | ); | |
331 | } | |
332 | $attachments->Limit( | |
333 | ALIAS => $alias, | |
334 | FIELD => $fts_config->{'Column'}, | |
335 | OPERATOR => 'IS NOT', | |
336 | VALUE => 'NULL', | |
337 | ); | |
338 | $attachments->OrderBy( FIELD => 'id', ORDER => 'desc' ); | |
339 | $attachments->RowsPerPage( 1 ); | |
340 | my $res = $attachments->First; | |
341 | return 0 unless $res; | |
342 | return $res->id; | |
343 | } | |
344 | ||
345 | sub process_pg { | |
346 | my ($type, $attachment, $text) = (@_); | |
347 | ||
348 | my $dbh = $RT::Handle->dbh; | |
349 | my $table = $fts_config->{'Table'}; | |
350 | my $column = $fts_config->{'Column'}; | |
351 | ||
352 | my $query; | |
353 | if ( $table ) { | |
354 | if ( my ($id) = $dbh->selectrow_array("SELECT id FROM $table WHERE id = ?", undef, $attachment->id) ) { | |
355 | $query = "UPDATE $table SET $column = to_tsvector(?) WHERE id = ?"; | |
356 | } else { | |
357 | $query = "INSERT INTO $table($column, id) VALUES(to_tsvector(?), ?)"; | |
358 | } | |
359 | } else { | |
360 | $query = "UPDATE Attachments SET $column = to_tsvector(?) WHERE id = ?"; | |
361 | } | |
362 | ||
363 | my $status = eval { $dbh->do( $query, undef, $$text, $attachment->id ) }; | |
364 | unless ( $status ) { | |
5b0d0914 | 365 | if ( $dbh->err == 7 && $dbh->state eq '54000' ) { |
01e3b242 | 366 | warn "Attachment @{[$attachment->id]} cannot be indexed. Most probably it contains too many unique words. Error: ". $dbh->errstr; |
5b0d0914 | 367 | } elsif ( $dbh->err == 7 && $dbh->state eq '22021' ) { |
01e3b242 | 368 | warn "Attachment @{[$attachment->id]} cannot be indexed. Most probably it contains invalid UTF8 bytes. Error: ". $dbh->errstr; |
84fb5b46 MKG |
369 | } else { |
370 | die "error: ". $dbh->errstr; | |
371 | } | |
403d7b0b MKG |
372 | |
373 | # Insert an empty tsvector, so we count this row as "indexed" | |
374 | # for purposes of knowing where to pick up | |
375 | eval { $dbh->do( $query, undef, "", $attachment->id ) } | |
376 | or die "Failed to insert empty tsvector: " . $dbh->errstr; | |
84fb5b46 MKG |
377 | } |
378 | } | |
379 | ||
380 | sub attachments_text { | |
381 | my $res = shift; | |
382 | $res->Limit( FIELD => 'ContentType', VALUE => 'text/plain' ); | |
383 | return $res; | |
384 | } | |
385 | ||
386 | sub extract_text { | |
387 | my $attachment = shift; | |
388 | my $text = $attachment->Content; | |
389 | return undef unless defined $text && length($text); | |
390 | return \$text; | |
391 | } | |
392 | ||
393 | sub attachments_html { | |
394 | my $res = shift; | |
395 | $res->Limit( FIELD => 'ContentType', VALUE => 'text/html' ); | |
396 | return $res; | |
397 | } | |
398 | ||
399 | sub filter_html { | |
400 | my $attachment = shift; | |
401 | if ( my $parent = $attachment->ParentObj ) { | |
402 | # skip html parts that are alternatives | |
403 | return 1 if $parent->id | |
404 | && $parent->ContentType eq 'mulitpart/alternative'; | |
405 | } | |
406 | return 0; | |
407 | } | |
408 | ||
409 | sub extract_html { | |
410 | my $attachment = shift; | |
411 | my $text = $attachment->Content; | |
412 | return undef unless defined $text && length($text); | |
413 | # TODO: html -> text | |
414 | return \$text; | |
415 | } | |
416 | ||
417 | sub goto_specific { | |
418 | my %args = (@_); | |
419 | ||
420 | my $func = (caller(1))[3]; | |
421 | $func =~ s/.*:://; | |
422 | my $call = $func ."_". lc $args{'suffix'}; | |
423 | unless ( defined &$call ) { | |
424 | return undef unless $args{'error'}; | |
425 | require Carp; Carp::croak( $args{'error'} ); | |
426 | } | |
427 | @_ = @{ $args{'arguments'} }; | |
428 | goto &$call; | |
429 | } | |
430 | ||
431 | ||
432 | # helper functions | |
433 | sub debug { print @_, "\n" if $OPT{debug}; 1 } | |
434 | sub error { $RT::Logger->error(_(@_)); 1 } | |
435 | sub warning { $RT::Logger->warn(_(@_)); 1 } | |
436 | ||
437 | =head1 NAME | |
438 | ||
439 | rt-fulltext-indexer - Indexer for full text search | |
440 | ||
441 | =head1 DESCRIPTION | |
442 | ||
443 | This is a helper script to keep full text indexes in sync with data. | |
444 | Read F<docs/full_text_indexing.pod> for complete details on how and when | |
445 | to run it. | |
446 | ||
447 | =head1 AUTHOR | |
448 | ||
449 | Ruslan Zakirov E<lt>ruz@bestpractical.comE<gt>, | |
450 | Alex Vandiver E<lt>alexmv@bestpractical.comE<gt> | |
451 | ||
452 | =cut | |
453 |