Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- File List
- File Members

# GlobalSearchEngine.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 #
    00004 #   FILE:  SPT--GlobalSearchEngine.php
    00005 #
    00006 #   METHODS PROVIDED:
    00007 #       GlobalSearchEngine()
    00008 #           - constructor
    00009 #       SomeMethod($SomeParameter, $AnotherParameter)
    00010 #           - short description of method
    00011 #
    00012 #   AUTHOR:  Edward Almasy
    00013 #
    00014 #   Part of CWIS and the Scout Portal Toolkit
    00015 #   Copyright 2005 Internet Scout
    00016 #   http://scout.wisc.edu
    00017 #
    00018
    00019 /*
    00020 OUTSTANDING ISSUES:
    00021 - search string(s) must be escaped (~XX)
    00022 - search scores must be normalized
    00023 */
    00024
    00025
    00026 class GlobalSearchEngine {
    00027
    00028     # ---- PUBLIC INTERFACE --------------------------------------------------
    00029
    00030     # object constructor
    00031     function GlobalSearchEngine()
    00032     {
    00033     }
    00034
    00035     # perform keyword search
    00036     function Search($SearchString, $StartingResult = 0, $NumberOfResults = 10)
    00037     {
    00038         # save start time to use in calculating search time
    00039         $StartTime = $this->GetMicrotime();
    00040
    00041         # create OAI-SQ set specification from search string
    00042         $SetSpec = "OAI-SQ!".$SearchString;
    00043
    00044         # perform global search
    00045         $SearchResults = $this->PerformSearch(
    00046                 $SetSpec, $StartingResult, $NumberOfResults);
    00047
    00048         # record search time
    00049         $this->LastSearchTime = $this->GetMicrotime() - $StartTime;
    00050
    00051         # return results to caller
    00052         return $SearchResults;
    00053     }
    00054
    00055     # perform search across multiple fields and return trimmed results to caller
    00056     function FieldedSearch($SearchStrings, $StartingResult = 0, $NumberOfResults = 10)
    00057     {
    00058     }
    00059
    00060     # report number of results found during last search
    00061     function NumberOfResults() {  return $this->NumberOfResultsAvailable;  }
    00062
    00063     # report time taken to perform last search
    00064     function SearchTime()
    00065     {
    00066         return $this->LastSearchTime;
    00067     }
    00068
    00069
    00070     # ---- PRIVATE INTERFACE -------------------------------------------------
    00071
    00072     var $NumberOfResultsAvailable;
    00073     var $LastSearchTime;
    00074
    00075     # perform OAI-SQ search
    00076     function PerformSearch($SetSpec, $StartingResult, $NumberOfResults)
    00077     {
    00078         # for each global search site
    00079         $DB = new SPTDatabase();
    00080         $DB->Query("SELECT * FROM GlobalSearchSites");
    00081         $SearchResults = array();
    00082         while ($SiteInfo = $DB->FetchRow())
    00083         {
    00084             # retrieve results from site
    00085             $SiteSearchResults = $this->SearchSite($SiteInfo, $SetSpec);
    00086
    00087             # add results to result list
    00088             $SearchResults = array_merge($SearchResults, $SiteSearchResults);
    00089         }
    00090
    00091         # sort the results in descending order by search score
    00092         function SearchScoreCmp($ResultA, $ResultB)
    00093         {
    00094             return ($ResultA["Search Score"] == $ResultB["Search Score"]) ? 0
    00095                     : (($ResultA["Search Score"] < $ResultB["Search Score"]) ? 1 : -1);
    00096         }
    00097         usort($SearchResults, "SearchScoreCmp");
    00098
    00099         # save number of results found
    00100         $this->NumberOfResultsAvailable = count($SearchResults);
    00101
    00102         # trim result list to match range requested by caller
    00103         $SearchResults = array_slice($SearchResults, $StartingResult, $NumberOfResults);
    00104
    00105         # return search results to caller
    00106         return $SearchResults;
    00107     }
    00108
    00109     # search one site
    00110     function SearchSite($SiteInfo, $SetSpec)
    00111     {
    00112         # create OAI client and perform query
    00113         $Client = new OAIClient($SiteInfo["OaiUrl"]);
    00114         $Client->SetSpec($SetSpec);
    00115         $QueryResults = $Client->GetRecords();
    00116
    00117         # for each result returned from query
    00118         foreach ($QueryResults as $Result)
    00119         {
    00120             # extract and save result data where available
    00121             unset($ResultData);
    00122             $ResultData["Title"] =
    00123                     isset($Result["metadata"]["DC:TITLE"][0])
    00124                     ? $Result["metadata"]["DC:TITLE"][0] : NULL;
    00125             $ResultData["Description"] =
    00126                     isset($Result["metadata"]["DC:DESCRIPTION"][0])
    00127                     ? $Result["metadata"]["DC:DESCRIPTION"][0] : NULL;
    00128             $ResultData["Url"] =
    00129                     isset($Result["metadata"]["DC:IDENTIFIER"][0])
    00130                     ? $Result["metadata"]["DC:IDENTIFIER"][0] : NULL;
    00131             $ResultData["Full Record Link"] =
    00132                     isset($Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0])
    00133                     ? $Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0] : NULL;
    00134             $ResultData["Search Score"] =
    00135                     isset($Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0])
    00136                     ? $Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0] : NULL;
    00137             $ResultData["Cumulative Rating"] =
    00138                     isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0])
    00139                     ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0] : NULL;
    00140             $ResultData["Cumulative Rating Scale"] =
    00141                     isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0])
    00142                     ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0] : NULL;
    00143
    00144             # save site info for result
    00145             $ResultData["Site ID"] = $SiteInfo["SiteId"];
    00146             $ResultData["Site Name"] = $SiteInfo["SiteName"];
    00147             $ResultData["Site URL"] = $SiteInfo["SiteUrl"];
    00148
    00149             # add result data to search results
    00150             $SearchResults[] = $ResultData;
    00151         }
    00152
    00153         # return search results to caller
    00154         return $SearchResults;
    00155     }
    00156
    00157     # convenience function for getting time in microseconds
    00158     function GetMicrotime()
    00159     {
    00160         list($usec, $sec) = explode(" ", microtime());
    00161         return ((float)$usec + (float)$sec);
    00162     }
    00163 }
    00164
    00165
    00166 ?>