The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WWW::Search - Virtual base class for WWW searches

SYNOPSIS

    require WWW::Search;
    $search_engine = "AltaVista";
    $search = new WWW::Search($search_engine);

DESCRIPTION

This class is the parent for all access methods supported by the WWW::Search library. This library implements a Perl API to web-based search engines.

Current search engines supported include AltaVista (both web and news), Dejanews, Excite (web only), Gopher, HotBot (web only), Infoseek (e-mail, web, and news), Lycos, Magellan, PLweb, SFgate, Simple (retrieve links on a page), Verity.

Search results are limited and there is a pause between each request for results to avoid overloading either the client or the server.

Sample program

Using the library should be straightforward. Here is a sample program:

    my($search) = new WWW::Search('AltaVista');
    $search->native_query(WWW::Search::escape_query($query));
    my($result);
    while ($result = $search->next_result()) {
        print $result->url, "\n";
    };

Results are objects of type WWW::SearchResult (see WWW::SearchResult for details). Note that different back-ends support different result fields. All back-ends are required to support title and url.

SEE ALSO

For more details see LWP.

For specific search engines, see WWW::Search::TheEngineName (replacing TheEngineName with a particular search engine).

For details about the results of a search, see WWW::SearchResult.

METHODS AND FUNCTIONS

new

To create a new WWW::Search, call $search = new WWW::Search('SearchEngineName'); where SearchEngineName is replaced with a particular search engine. For example: $search = new WWW::Search('AltaVista');

If no search engine is specified a default will be chosen for you.

The next step is usually: $search->native_query('search-engine-specific+query+string');

test_cases

Returns the value of the $TEST_CASES variable of the back-end engine. All backends should set $TEST_CASES to a string containing perl code which will be eval-ed during 'make test'. See Excite.pm for an example.

version

Returns the value of the $VERSION variable of the back-end engine, or $WWW::Search::VERSION if the backend does not contain $VERSION.

native_query

Specify a query (and optional options) to the current search object. The query and options must be escaped; call "escape_query" in WWW::Search to escape a plain query. The actual search is not actually begun until results or next_result is called.

Example:

        $search->native_query('search-engine-specific+query+string',
                { option1 => 'able', option2 => 'baker' } );

The hash of options following the query string is optional. The query string is back-end specific. There are two kinds of options: options specific to the back-end and generic options applicable to mutliple back-ends.

Generic options all begin with 'search_'. Currently a few are supported:

search_url Specifies the root of the URL for the
search_debug Enables back-end debugging.
search_parse_debug Enables back-end parser debugging.
search_method Specifies the HTTP method (GET or POST) for HTTP-based queries.
search_to_file FILE Causes the search results to be saved in a set of files prefixed by FILE. (Used internally by the test-suite, not intended for general use.)
search_from_file FILE Reads a search from a set of files prefixed by FILE. (Used internally by the test-suite, not intended for general use.)

Some back-ends may not implement generic options, but any which do implement them must provide these semantics.

Back-end-specific options are described in the documentation for each back-ends. Typically they are packed together to create the query portion of the final URL.

Details about how the search string and option hash are interpreted in the search-engine-specific manual pages (WWW::Search::SearchEngineName).

After native_query, the next step is usually:

    @results = $search->results();

or

    while ($result = $search->next_result()) {
        # do_something;
    };

approximate_result_count

Some back-ends indicate how many hits they have found. Typically this is an approximate value.

results

Return all the results of a query as a reference to array of SearchResult objects.

Example: @results = $search->results(); foreach $result (@results) { print $result->url(), "\n"; };

On error, results() will return undef and set response() to the HTTP response code.

next_result

Return each result of a query as a SearchResult object.

Example: while ($result = $search->next_result()) { print $result->url(), "\n"; };

On error, results() will return undef and set response() to the HTTP response code.

response

Return the HTTP Response code for the last query (see HTTP::Response). If the query returns undef, errors could be reported like this:

    my($response) = $search->response();
    if ($response->is_success) {
        print "no search results\n";
    } else {
        print "error:  " . $response->as_string() . "\n";
    };

Note: even if the back-end does not involve the web it should return HTTP::Response-style codes.

seek_result($offset)

Set which result next_result should return (like lseek in Unix). Results are zero-indexed.

The only guaranteed valid offset is 0 which will replay the results from the beginning. In particular, seeking past the end of the current cached results probably will not do what you might think it should.

Results are cached, so this does not re-issue the query or cause IO (unless you go off the end of the results). To re-do the query, create a new search object.

Example: $search->seek_result(0);

maximum_to_retrieve

The maximum number of hits to return. Queries resulting in more than this many hits will return the first hits, up to this limit. Although this specifies a maximum limit, search engines may impose a lower limit.

Defaults to 500.

Example: $max = $search->maximum_to_retrieve(100);

timeout

The maximum length of time any portion of the query should take, in seconds.

Defaults to 60.

Example: $search->timeout(120);

opaque

This function provides an application a place to store one opaque data element (or many via a Perl reference). This facility is useful to (for example), maintain client-specific information in each active query when you have multiple concurrent queries.

escape_query

Escape a query. Before queries are made special characters must be escaped so that a proper URL can be formed.

This is like escaping a URL but all non-alphanumeric characters are escaped and and spaces are converted to "+"s.

Example: $escaped = Search::escape_query('+lsam +replication'); (Returns "%22lsam+replication%22").

See also unescape_query.

unescape_query

Unescape a query. See escape_query for details.

Example: $unescaped = Search::unescape_query('%22lsam+replication%22'); (Returns "+lsam +replication").

See also unescape_query.

strip_tags

Given a string, returns a copy of that string with HTML tags removed. This should be used by each backend as they insert the title and description values into the SearchResults.

http_proxy

Set-up an HTTP proxy (Perhaps for connections from behind a firewall.)

This routine should be called before the first retrival is attempted.

Example:

    $search->http_proxy("http://gateway:8080");

http_request($method, $url)

Return the response from an http request, handling debugging. Requires that user_agent be setup. For POST methods, query is split off of the URL and passed in the request body.

split_lines (PRIVATE)

This internal routine splits data (typically the result of the web page retrieval) into lines in a way that is OS independent.

generic_option (PRIVATE)

This internal routine checks if an option is generic or back-end specific. Currently all generic options begin with 'search_'. This routine is not a method.

setup_search (PRIVATE)

This internal routine does generic Search setup. It calls native_setup_search to do back-end specific setup.

user_agent($NON_ROBOT) (PRIVATE)

This internal routine creates a user-agent for dervived classes that query the web. If $NON_ROBOT, a normal user-agent (rather than a robot-style user-agent) is used.

Back-ends should use robot-style user-agents whereever possible. Also, back-ends should call user_agent_delay every page retrival to avoid swamping search-engines.

user_agent_delay (PRIVATE)

Derived classes should call this between requests to remote servers to avoid overloading them with many, fast back-to-back requests.

absurl (PRIVATE)

An internal routine to convert a relative URL into a absolute URL. It takes two arguments, the 'base' url (usually the search engine CGI URL) and the URL to be converted. Returns a URI::URL object.

retrieve_some (PRIVATE)

An internal routine to interface with native_retrieve_some. Checks for overflow.

IMPLEMENTING NEW BACK-ENDS

WWW::Search supports back-ends to separate search engines. Each back-end is implemented as a subclass of WWW::Search. WWW::Search::AltaVista provides a good sample back-end.

A back-end must have the two routines native_retrieve_some and native_setup_search.

native_retrieve_some is the core of a back-end. It will be called periodically to fetch URLs. It should retrieve several hits from the search service and add them to the cache. It should return the number of hits found, or undef when there are no more hits.

Internally, native_retrieve_some typically sends an HTTP request to the search service, parse the HTML, extract the links and descriptions, then save the URL for the next page of results. See the code for the AltaVista implementation for an example.

native_setup_search is invoked before the search. It is passed a single argument: the escaped, native version of the query.

The front- and back-ends share a single object (a hash). The back-end can change any hash element beginning with underscore, and {response} (an HTTP::Response code) and {cache} (the array of WWW::SearchResult objects caching all results). Again, look at one of the existing web search backends as an example.

If you implement a new back-end, please let the authors know.

BUGS AND DESIRED FEATURES

The bugs are there for you to find (some people call them Easter Eggs).

Desired features:

A portable query language.

A portable language would easily allow you to move queries easily between different search engines. A query abstraction is non-trivial and unfortunately will not be done anytime soon by the current maintainers. If you want to take a shot at it, please let me know.

AUTHOR

WWW::Search was written by John Heidemann, <johnh@isi.edu>. WWW::Search is currently maintained by Martin Thurn, <MartinThurn@iname.com<gt>.

Back-ends and applications for WWW::Search were originally written by John Heidemann, Wm. L. Scheding, Cesare Feroldi de Rosa, and GLen Pringle.

COPYRIGHT

Copyright (c) 1996 University of Southern California. All rights reserved.

Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the University of Southern California, Information Sciences Institute. The name of the University may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.