SWISH::Filter -- Perl extension for filtering documents with Swish-e
use SWISH::Filter;
# load available filters into memory
my \$filter = SWISH::Filter->new;
# convert a document
my \$doc = \$filter->convert(
document => \\$scalar_ref, # path or ref to a doc
content_type => \$content_type, # content type if doc reference
name => \$real_path, # optional name for this file (useful for debugging)
user_data => \$whatever, # optional data to make available to filters
);
return unless \$doc; # empty doc, zero size, or no filters installed
# Was the document converted by a filter?
my \$was_filtered = \$doc->was_filtered;
# Skip if the file is not text
return if \$doc->is_binary;
# Print out the doc
my \$doc_ref = \$doc->fetch_doc;
print $\$doc_ref;
# Fetch the final content type of the document
my \$content_type = \$doc->content_type;
# Fetch Swish-e parser type (TXT*, XML*, HTML*, or undefined)
my \$doc_type = \$doc->swish_parser_type;
SWISH::Filter provides a unified way to convert documents into a type that Swish-e can index. Individual filters are installed as separate perl modules. For example, there might be a filter that converts from PDF format to HTML format.
Note that this is just a framework for filtering documents. Additional helper programs or Perl module may need to be installed to use SWISH::Filter to filter documents. For example, to filter PDF documents you must install the Xpdf package.
The filters are automatically loaded when SWISH::Filters->new()
is called. Filters define a type and priority that determines the processing order of the filter. Filters are processed in this sort order until a filter accepts the document for filtering. The filter uses the document's content type to determine if the filter should handle the current document. The content-type is determined by the files suffix if not supplied by the calling program.
The individual filters are not designed to be used as separate modules. All access to the filters is through this SWISH::Filter module.
Normally, once a document is filtered processing stops. Filters can filter the document and then set a flag saying that filtering should continue (for example a filter that uncompresses a MS Word document before passing on to the filter that converts from MS Word to text). All this should be transparent to the end user. So, filters can be pipe-lined.
The idea of SWISH::Filter is that new filters can be created, and then downloaded and installed to provide new filtering capabilities. For example, if you needed to index MS Excel documents you might be able to download a filter from the Swish-e site and magically next time you run indexing MS Excel docs would be indexed.
The SWISH::Filter setup can be used with -S prog or -S http. It works best with the -S prog method because the filter modules only need to be loaded and compiled one time. The -S prog program spider.pl will automatically use SWISH::Filter when spidering with default settings (using "default" as the first parameter to spider.pl).
The -S http indexing method uses a Perl helper script called swishspider. swishspider has been updated to work with SWISH::Filter, but (unlike spider.pl) does not contain a "use lib" line to point to the location of SWISH::Filter. This means that by default swishspider will not use SWISH::Filter for filtering. The reason for this is because swishspider runs for every URL fetched, and loading the Filters for each document can be slow. The recommended way of spidering is using -S prog with spider.pl, but if -S http is desired the way to enable SWISH::Filter is to set PERL5LIB before running swish so that swishspider will be able to locate the SWISH::Filter module. Here's one way to set the PERL5LIB with the bash shell:
$ export PERL5LIB=`swish-filter-test -path`
There is currently only one option that can be passed in to new():
my \$filter = SWISH::Filter->new( ignore_filters => ['Pdf2XML'];
content_type
is not set, then the content type will be determined from the file's extension by using the MIME::Types Perl module (available on CPAN). my \$user_data = \$doc_object->user_data;
if ( ref \$user_data && \$user_data->{pdf2html}{title} ) {
...
}
\$doc_object = \$filter->convert(
document => \$doc_ref,
content-type => 'application/pdf',
);
Filters are standard perl modules that are installed into the SWISH::Filters name space. Filters are not complicated -- see the existing filters for examples.
Each filter defines the content-types (or mimetypes) that it can handle. These are specified as a list of regular expressions to match against the document's content-type. If one of the mimetypes of a filter match the incoming document's content-type the filter is called. The filter can then either filter the content or return undefined indicating that it decided not to filter the document for some reason. If the document is converted the filter returns either a reference to a scalar of the content or a file name where the content is stored. The filter also must change the content-type of the document to reflect the new document.
Filters typically use external programs or modules to do that actual work of converting a document from one type to another. For example, programs in the Xpdf packages are used for converting PDF files. The filter can (and should) test for those programs in its new() method.
Filters also can define a type and priority. These attributes are used to set the order filters are tested for a content-type match. This allows you to have more than one filter that can work on the same content-type.
If a filter calls die() then the filter is removed from the chain and will not be called again during the same run. Calling die when running with -S http or -S fs has no effect since the program is run once per document.
Once a filter returns something other than undef no more filters will be called. If the filter calls \$filter->set_continue then processing will continue as if the file was not filtered. For example, a filter can uncompress data and then set \$filter->set_continue and let other filters process the document.
This is the list of methods the filter should or may define (as specificed):
sub new {
my ( \$class ) = @_;
# List of regular expressions
my @mimetypes = (
qr[application/(x-)?msword],
qr[application/worddoc],
);
my %settings = (
mimetypes => \@mimetypes,
# Optional settings
priority => 20,
type => 2,
);
return bless \%settings, \$class;
}
Again, the point of the type() and priority() methods is to allow setting the sort order of the filters. Useful if you have two filters for filtering the same content-type, but prefer to use one over the other. Neither are required.
Here's a module to convert MS Word documents using the program "catdoc":
package SWISH::Filters::Doc2txt;
use vars qw/ \$VERSION /;
\$VERSION = '0.02';
sub new {
my ( \$class ) = @_;
my \$self = bless {
mimetypes => [ qr!application/(x-)?msword! ],
priority => 50,
}, \$class;
# check for helpers
return \$self->set_programs( 'catdoc' );
}
sub filter {
my ( \$self, \$doc ) = @_;
my \$content = \$self->run_catdoc( \$doc->fetch_filename ) || return;
# update the document's content type
\$filter->set_content_type( 'text/plain' );
# return the document
return \\$content;
}
1;
The new() constructor creates a blessed hash which contains an array reference of mimetypes patterns that this filter accepts. The priority sets this filter to run after any other filters that might handle the same type of content. The set_programs() function says that we need to call a program called "catdoc". The function either returns \$self or undefined if catdoc could not be found. The set_programs() function creates a new method for running catdoc.
The filter function runs catdoc passing in the name of the file (if the file is in memory a temporary file is created). That run_catdoc() function was created by the set_programs() call above.
These methods are available to Filter authors, and also provide access to the document after calling the convert() method to end-users of SWISH::Filter.
End users of SWISH::Filter will use a subset of these methods. Mostly:
\$doc_object->fetch_doc # and alias for fetch_document_reference()
\$doc_object->was_filtered # true the document was filtered
\$doc_object->content_type # document's current content type (mime type)
\$doc_object->swish_parser_type # returns a parser type to use with Swish-e -S prog method
\$doc_object->is_binary # returns \$content_type !~ m[^text/];
These methods are also available to the individual filter modules. The filter's "filter" function is also passed a SWISH::Filter::document object. Method calls may be made on this object to check the document's current content type, or to fetch the document as either a file name or a reference to a scalar containing the document content.
return unless \$filter->content_type =~ m!application/pdf!;
my \$name = \$doc_object->name || 'Unknown name';
warn "File '\$name': failed to convert -- file may be corrupt\n";
my \$data = \$doc_object->user_data;
# see if a choice for the <title> was passed in
if ( ref \$data eq 'HASH' && \$data->{pdf2html}{title_field} {
...
...
}
Each filter is a subclass of SWISH::Filters::_BASE. A number of methods are available by default (and some can be overridden). Others are useful when writing your new() constructor.
return \$self->set_programs( qw/ pdftotext pdfinfo / );
my \$content = \$self->run_pdfinfo( \$doc->fetch_filename, [options] );
return unless \$self->use_modules( qw/ Spreadsheet::ParseExcel HTML::Entities / );
Filters can be tested with the swish-filter-test program. Run:
swish-filter-test -man
for documentation.
Please contact the Swish-e discussion list. http://swish-e.org
TBD
Bill Moseley
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.