Www.etext.org swish-e.php

From dftwiki3
Jump to: navigation, search
<?php

// D. Thiebaut
// Script file to interface Web request to swishs-e
// Typical call from browser:
//
// http://xgridmac.dyndns.org/~thiebaut/swish-e/swishe.php?delay=20&search='government'
//
// delay represents max number of 1/10 of seconds to wait (the number is actually random
// but no more than whatever integer is given.  20 means up to 2 seconds.
//
// search is the search string.
// can search for sentences by using input of the form seach="hello world" or
//  search="hello%20world"
//

if ($argc > 0) {
       for ($i=1;$i < $argc;$i++) {
        parse_str($argv[$i],$tmp);
        $_REQUEST = array_merge($_REQUEST, $tmp);
    }
}

// returns true/false depending on whether a string starts with a given 
// string or not.
function startsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}

//--- get the words to search for ---
$words=$_REQUEST[ 'search' ];
$words = escapeshellcmd( $words ); // to be safe

$delay = 0;
if (isset($_REQUEST['delay'] )) {
   $delay = $_REQUEST[ 'delay' ];
}

//--- output header ---
print '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n";

//--- execute swish-e with given search word ---
exec(  '/usr/local/bin/swish-e -w '. $words, $output );

//--- add a random delay ---
usleep( rand( 1, $delay ) * 100000 );

//--- output top 20 lines of output ---
$count = 0;
foreach ( $output as $line ) {
   //--- skip comments ---
   if ( startsWith( $line, '#' ) ) continue;

   //--- parse line ---
   $words = explode( ' ', $line );
   if ( count( $words ) != 4 ) continue;

   //--- only 20 lines ---
   $count += 1;
   if ( $count > 20 ) break;

   //--- change path into url ---
   $url = $words[1];
   $url = str_replace( "/Users/thiebaut/Sites/", "http://xgridmac.dyndns.org/~thiebaut/", $url );

   print "<br>rank:   " . $count . "\n";
   print "<br>score:  " . $words[0] . "\n";
   print "<br>url:    " . $url . "\n";
   print "<br>link:   <a href=\"" . $url . "\">link</a>\n";
   print "<br>file:   " . str_replace( '"', '', $words[2] ) . "\n";
   print "<br>offset: " . $words[3] . "\n";
   print "<br>\n";
}
?>