CWIS Developer Documentation
GlobalSearchEngine.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: SPT--GlobalSearchEngine.php
5 #
6 # Part of the Collection Workflow Integration System (CWIS)
7 # Copyright 2005-2013 Edward Almasy and Internet Scout Research Group
8 # http://scout.wisc.edu/cwis/
9 #
10 
11 /*
12 OUTSTANDING ISSUES:
13 - search string(s) must be escaped (~XX)
14 - search scores must be normalized
15 */
16 
17 
19 {
20 
21  # ---- PUBLIC INTERFACE --------------------------------------------------
22 
26  public function __construct()
27  {
28  }
29 
40  public function Search($SearchString, $StartingResult = 0, $NumberOfResults = 10)
41  {
42  # save start time to use in calculating search time
43  $StartTime = $this->GetMicrotime();
44 
45  # create OAI-SQ set specification from search string
46  $SetSpec = "OAI-SQ!".$SearchString;
47 
48  # perform global search
49  $SearchResults = $this->PerformSearch(
50  $SetSpec, $StartingResult, $NumberOfResults);
51 
52  # record search time
53  $this->LastSearchTime = $this->GetMicrotime() - $StartTime;
54 
55  # return results to caller
56  return $SearchResults;
57  }
58 
63  public function NumberOfResults()
64  {
65  return $this->NumberOfResultsAvailable;
66  }
67 
72  public function SearchTime()
73  {
74  return $this->LastSearchTime;
75  }
76 
77 
78  # ---- PRIVATE INTERFACE -------------------------------------------------
79 
80  private $NumberOfResultsAvailable;
81  private $LastSearchTime;
82 
90  private function PerformSearch($SetSpec, $StartingResult, $NumberOfResults)
91  {
92  # for each global search site
93  $DB = new Database();
94  $DB->Query("SELECT * FROM GlobalSearchSites");
95  $SearchResults = array();
96  while ($SiteInfo = $DB->FetchRow())
97  {
98  # retrieve results from site
99  $SiteSearchResults = $this->SearchSite($SiteInfo, $SetSpec);
100 
101  # add results to result list
102  $SearchResults = array_merge($SearchResults, $SiteSearchResults);
103  }
104 
105  usort($SearchResults, function ($A, $B) {
106  return StdLib::SortCompare($A["Search Score"], $B["Search Score"]);
107  });
108 
109  # save number of results found
110  $this->NumberOfResultsAvailable = count($SearchResults);
111 
112  # trim result list to match range requested by caller
113  $SearchResults = array_slice($SearchResults, $StartingResult, $NumberOfResults);
114 
115  # return search results to caller
116  return $SearchResults;
117  }
118 
125  private function SearchSite($SiteInfo, $SetSpec)
126  {
127  # create OAI client and perform query
128  $Client = new OAIClient($SiteInfo["OaiUrl"]);
129  $Client->SetSpec($SetSpec);
130  $QueryResults = $Client->GetRecords();
131 
132  # for each result returned from query
133  foreach ($QueryResults as $Result)
134  {
135  # extract and save result data where available
136  unset($ResultData);
137  $ResultData["Title"] =
138  isset($Result["metadata"]["DC:TITLE"][0])
139  ? $Result["metadata"]["DC:TITLE"][0] : NULL;
140  $ResultData["Description"] =
141  isset($Result["metadata"]["DC:DESCRIPTION"][0])
142  ? $Result["metadata"]["DC:DESCRIPTION"][0] : NULL;
143  $ResultData["Url"] =
144  isset($Result["metadata"]["DC:IDENTIFIER"][0])
145  ? $Result["metadata"]["DC:IDENTIFIER"][0] : NULL;
146  $ResultData["Full Record Link"] =
147  isset($Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0])
148  ? $Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0] : NULL;
149  $ResultData["Search Score"] =
150  isset($Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0])
151  ? $Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0] : NULL;
152  $ResultData["Cumulative Rating"] =
153  isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0])
154  ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0] : NULL;
155  $ResultData["Cumulative Rating Scale"] =
156  isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0])
157  ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0] : NULL;
158 
159  # save site info for result
160  $ResultData["Site ID"] = $SiteInfo["SiteId"];
161  $ResultData["Site Name"] = $SiteInfo["SiteName"];
162  $ResultData["Site URL"] = $SiteInfo["SiteUrl"];
163 
164  # add result data to search results
165  $SearchResults[] = $ResultData;
166  }
167 
168  # return search results to caller
169  return $SearchResults;
170  }
171 
176  private function GetMicrotime()
177  {
178  list($usec, $sec) = explode(" ", microtime());
179  return ((float)$usec + (float)$sec);
180  }
181 }
SQL database abstraction object with smart query caching.
Definition: Database.php:22
static SortCompare($A, $B)
Perform compare and return value appropriate for sort function callbacks.
Definition: StdLib.php:572
NumberOfResults()
Gets the number of results returned in the last search.
SearchTime()
Gets the time taken to perform the previous search, in microseconds.
__construct()
Constructs a GlobalSearchEngine object.
Search($SearchString, $StartingResult=0, $NumberOfResults=10)
Performs a keyword search using a specified search string and returns the results, starting with the one numbered with the starting result (default 0) and continuing until reaching the desired number of results (default 10).