PHP Classes

File: htdig-object.php

Recommend this page to a friend!
  Classes of Ted Stresen-Reuter   htdig-php-helper   htdig-object.php   Download  
File: htdig-object.php
Role: Class source
Content type: text/plain
Description: The htdig class
Class: htdig-php-helper
model an htdig search request as a PHP object
Author: By
Last change:
Date: 22 years ago
Size: 7,662 bytes
 

Contents

Class file image Download
<?
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This class expects the following:
 *
 * - that the following files exist:
 * - results-header.html
 * - results-footer.html
 * - results-template.html
 *
 * - that the location of these files is specified below in this->templatesDir
 *
 * - that results-header.html contains at a minimum $(MATCHES) and $(MATCHES_PER_PAGE)
 *
 * - that each template file consists of an htdig variable followed by
 * a new line and/or carriage return
 *
 * - that none of the templates contain variabls that produce form elements (sorry!)
 *
 * - that the search is executed outside this class and the class is passed
 * the results of the search when an new object is created
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
// functions called from within Ht primarily

function reorderWords ( $a, $b )
{
    if (
strlen( $a ) == strlen( $b )) return 0;
    return (
strlen( $a ) > strlen( $b ) ) ? -1 : 1;
}

function
setVars ( &$val, $key )
{
   
trim($val);
   
eregi( "[^a-zA-Z]*([a-zA-Z_]*).*", "$val", $matches );
   
$val = $matches[1];
}

// function for doing case-insensitive search and replace
// found on php.net under str_replace()
function highlight( $needle, $haystack )
{
   
$parts = explode( strtolower($needle), strtolower($haystack) );

   
$pos = 0;

    foreach(
$parts as $key=>$part )
    {
       
$parts[ $key ] = substr($haystack, $pos, strlen($part));
       
$pos += strlen($part);

       
$parts[ $key ] .= '<strong>'.substr($haystack, $pos, strlen($needle)).'</strong>';
       
$pos += strlen($needle);
    }

    return(
join( '', $parts ) );
}

class
Ht
{
    var
$anchor;
    var
$cgi;
    var
$current;
    var
$description;
    var
$descriptions;
    var
$docid;
    var
$excerpt;
    var
$firstdisplayed;
    var
$format;
    var
$hopcount;
    var
$keywords;
    var
$lastdisplayed;
    var
$logical_words;
    var
$match_message;
    var
$matches;
    var
$matches_per_page;
    var
$max_stars;
    var
$metadescription;
    var
$method;
    var
$modified;
    var
$nextpage;
    var
$nstars;
    var
$page;
    var
$pageheader;
    var
$pagelist;
    var
$pages;
    var
$percent;
    var
$plural_matches;
    var
$prevpage;
    var
$score;
    var
$selected_format;
    var
$selected_method;
    var
$selected_sort;
    var
$size;
    var
$sizek;
    var
$sort;
    var
$starsleft;
    var
$starsright;
    var
$startyear;
    var
$startmonth;
    var
$startday;
    var
$endyear;
    var
$endmonth;
    var
$endday;
    var
$syntaxerror;
    var
$title;
    var
$url;
    var
$version;
    var
$words;
    var
$htError;
    var
$template;
    var
$header;
    var
$footer;
    var
$templatesDir;
    var
$results;
    var
$nomatch;
    var
$syntaxerror;
   
    function
Ht ( $result )
    {
        function
setVars ( &$val, $key )
        {
           
trim($val);
           
eregi( "[^a-zA-Z]*([a-zA-Z_]*).*", "$val", $matches );
           
$val = $matches[1];
        }
       
       
$this->results = $result;

       
// make sure there are some results to work with
       
if ( sizeof( $this->results ) < 3 )
        {
           
$this->error = "Unknown error. No results were returned from the search request.";
           
$continue = false;
        }
        else
// something was returned, even if it was no matches
       
{
            if (
eregi( "^nomatch.*", $this->results ) )
            {
               
$this->nomatch = "Sorry. No matches were found. Please try again but with a modified query.";
            }
           
            if (
eregi( "^SYNTAXERROR.*", $this->results ) )
            {
               
$this->syntaxerror = "Sorry. There was a syntax error. It's probably not your fault, but there's nothing more that can be done to fix it at this time. Try contacting the webmaster of this site.";
            }
           
           
// NOTE: even if no results were returned, we want to try and
            // stuff the variables because some of them may be useful
           
            // tell us where to find the template, header, footer files
            // this is relative to the document that this class is included in
           
if ( eregi( ".*\.com$", $GLOBALS["HTTP_HOST"] ) )
            {
               
$this->templatesDir = "/htdocs/dev/search/";
            }
            else
            {
               
$this->templatesDir = "/Library/WebServer/susansexton/search/";
            }

           
// fill the template, header, footer vars with data
           
$header = file($this->templatesDir."results-header.html");
           
array_walk( $header, "setVars" );
           
$this->header = $header;
            for (
$i = 0; $i < sizeof( $this->header ); $i++ )
            {
               
$var = strtolower( $this->header[$i] );
               
$this->$var = $this->results[ $i + 2 ];
            }
           
           
$template = file($this->templatesDir."results-template.html");
           
array_walk( $template, "setVars" );
           
$this->template = $template;
           
           
// process sets of template elements in batches of results
           
for ( $i = 0; $i < $this->matches_per_page; $i++ )
            {
               
// update pointer for template results
               
$pointer = $i * sizeof( $this->template ) + sizeof( $this->header ) + 2;
                for (
$j = 0; $j < sizeof( $this->template ); $j++ )
                {
                   
$var = strtolower( $this->template[$j] );
                    if ( !
is_array( $this->$var ) )
                    {
                       
$this->$var = Array();
                    }
                   
array_push( $this->$var, $this->results[$j+$pointer] );
                }
            }
           
           
$footer = file($this->templatesDir."results-footer.html");
           
array_walk( $footer, "setVars" );
           
$this->footer = $footer;
           
$offset = sizeof( $this->results ) - sizeof( $this->footer );
            for (
$i = 0; $i < sizeof( $this->footer ); $i++ )
            {
               
$var = strtolower( $this->footer[$i] );
               
$this->$var = $this->results[ $i + $offset ];
            }
        }
    }
   
   
// i use this function to append arguments to htdig's prev and next links
   
function appendArgToNextPrevLinks ( $source, $newPart )
    {
       
$href = explode( " ", $source );
       
eregi("\?(.*)\">", $href[1], $matches );
       
$qstring = $matches[1] . $newPart;
       
$href[1] = "href=\"?$qstring\"><img";
       
$href = implode( " ", $href );
        return
$href;
    }

   
// i use this function to append arguments to htdig's pages links
   
function appendArgToPagesLinks ( $source, $newPart, $current )
    {
       
$href = explode( "> <", $source );
        for (
$i = 0; $i < sizeof( $href ); $i++ )
        {
            if (
$current - 1 != $i )
            {
               
// version 3.1.6 produces different page links than 3.2
               
if ( substr( $this->version, 0, 3 ) < 3.2 )
                {
                    if (
eregi( "^<?a href=\"\?(.*)\">(.*)</a", $href[$i]) )
                    {
                       
eregi( "^<?a href=\"\?(.*)\">(.*)</a", $href[$i], $matches);
                    }
                    else
                    {
                        echo
"<br>no match on $i";
                    }
                }
                else
                {
                   
eregi( "^<?a href=\"".$GLOBALS["PHP_SELF"]."\?(.*)\">(.*)</a", $href[$i], $matches );
                }
               
$qstring = $matches[1] . $newPart;
               
$display = $matches[2];
               
$href[$i] = "a href=\"".$GLOBALS["PHP_SELF"]."?$qstring\">$display</a";
            }
        }
       
$href = ($current != 1 ? "<" : "") . implode( "> <", $href );
        return
$href;
    }
   
   
// I use this function to display the description of the image instead of the excerpt, but maintaining the bold
    // believe it or not, it took me three hourse to get this right.
    // main sumbling blocks were: str_replace is not case-sensitive,
    // and referencing a callback function from within a class
   
function swapExcerpt ( $desc )
    {
       
// put logical words into an array
       
$words = substr ( $this->logical_words, 1 , strlen( $this->logical_words ) - 2);
       
$words = explode( " or ", $words );
       
       
// put words in order by length, longest first
        // forces highlighting of entire word (not omitting plural 's')
       
usort( $words, "reorderWords" );
       
       
// embolden matched words
       
if ( $desc != "" )
        {
            for (
$i = 0; $i < sizeof( $words ); $i++ )
            {
               
$desc = highlight( $words[$i], $desc );
            }
        }
        return
$desc;
    }
}
?>