Www.etext.org swish-e.php

From dftwiki3
Revision as of 11:24, 23 January 2010 by Thiebaut (talk | contribs) (New page: <code><pre> <?php // D. Thiebaut // Script file to interface Web request to swishs-e if ($argc > 0) { for ($i=1;$i < $argc;$i++) { parse_str($argv[$i],$tmp); $_REQ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
<?php

// D. Thiebaut
// Script file to interface Web request to swishs-e

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

//--- 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 );

//--- 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";
}
?>