CWIS Developer Documentation
GlobalSearchEngine.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: SPT--GlobalSearchEngine.php
5 #
6 # METHODS PROVIDED:
7 # GlobalSearchEngine()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR: Edward Almasy
13 #
14 # Part of CWIS and the Scout Portal Toolkit
15 # Copyright 2005 Internet Scout
16 # http://scout.wisc.edu
17 #
18 
19 /*
20 OUTSTANDING ISSUES:
21 - search string(s) must be escaped (~XX)
22 - search scores must be normalized
23 */
24 
25 
27 
28  # ---- PUBLIC INTERFACE --------------------------------------------------
29 
30  # object constructor
31  function GlobalSearchEngine()
32  {
33  }
34 
35  # perform keyword search
36  function Search($SearchString, $StartingResult = 0, $NumberOfResults = 10)
37  {
38  # save start time to use in calculating search time
39  $StartTime = $this->GetMicrotime();
40 
41  # create OAI-SQ set specification from search string
42  $SetSpec = "OAI-SQ!".$SearchString;
43 
44  # perform global search
45  $SearchResults = $this->PerformSearch(
46  $SetSpec, $StartingResult, $NumberOfResults);
47 
48  # record search time
49  $this->LastSearchTime = $this->GetMicrotime() - $StartTime;
50 
51  # return results to caller
52  return $SearchResults;
53  }
54 
55  # perform search across multiple fields and return trimmed results to caller
56  function FieldedSearch($SearchStrings, $StartingResult = 0, $NumberOfResults = 10)
57  {
58  }
59 
60  # report number of results found during last search
62 
63  # report time taken to perform last search
64  function SearchTime()
65  {
66  return $this->LastSearchTime;
67  }
68 
69 
70  # ---- PRIVATE INTERFACE -------------------------------------------------
71 
74 
75  # perform OAI-SQ search
76  function PerformSearch($SetSpec, $StartingResult, $NumberOfResults)
77  {
78  # for each global search site
79  $DB = new Database();
80  $DB->Query("SELECT * FROM GlobalSearchSites");
81  $SearchResults = array();
82  while ($SiteInfo = $DB->FetchRow())
83  {
84  # retrieve results from site
85  $SiteSearchResults = $this->SearchSite($SiteInfo, $SetSpec);
86 
87  # add results to result list
88  $SearchResults = array_merge($SearchResults, $SiteSearchResults);
89  }
90 
91  # sort the results in descending order by search score
92  function SearchScoreCmp($ResultA, $ResultB)
93  {
94  return ($ResultA["Search Score"] == $ResultB["Search Score"]) ? 0
95  : (($ResultA["Search Score"] < $ResultB["Search Score"]) ? 1 : -1);
96  }
97  usort($SearchResults, "SearchScoreCmp");
98 
99  # save number of results found
100  $this->NumberOfResultsAvailable = count($SearchResults);
101 
102  # trim result list to match range requested by caller
103  $SearchResults = array_slice($SearchResults, $StartingResult, $NumberOfResults);
104 
105  # return search results to caller
106  return $SearchResults;
107  }
108 
109  # search one site
110  function SearchSite($SiteInfo, $SetSpec)
111  {
112  # create OAI client and perform query
113  $Client = new OAIClient($SiteInfo["OaiUrl"]);
114  $Client->SetSpec($SetSpec);
115  $QueryResults = $Client->GetRecords();
116 
117  # for each result returned from query
118  foreach ($QueryResults as $Result)
119  {
120  # extract and save result data where available
121  unset($ResultData);
122  $ResultData["Title"] =
123  isset($Result["metadata"]["DC:TITLE"][0])
124  ? $Result["metadata"]["DC:TITLE"][0] : NULL;
125  $ResultData["Description"] =
126  isset($Result["metadata"]["DC:DESCRIPTION"][0])
127  ? $Result["metadata"]["DC:DESCRIPTION"][0] : NULL;
128  $ResultData["Url"] =
129  isset($Result["metadata"]["DC:IDENTIFIER"][0])
130  ? $Result["metadata"]["DC:IDENTIFIER"][0] : NULL;
131  $ResultData["Full Record Link"] =
132  isset($Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0])
133  ? $Result["about"]["SEARCHINFO"]["FULLRECORDLINK"][0] : NULL;
134  $ResultData["Search Score"] =
135  isset($Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0])
136  ? $Result["about"]["SEARCHINFO"]["SEARCHSCORE"][0] : NULL;
137  $ResultData["Cumulative Rating"] =
138  isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0])
139  ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATING"][0] : NULL;
140  $ResultData["Cumulative Rating Scale"] =
141  isset($Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0])
142  ? $Result["about"]["SEARCHINFO"]["CUMULATIVERATINGSCALE"][0] : NULL;
143 
144  # save site info for result
145  $ResultData["Site ID"] = $SiteInfo["SiteId"];
146  $ResultData["Site Name"] = $SiteInfo["SiteName"];
147  $ResultData["Site URL"] = $SiteInfo["SiteUrl"];
148 
149  # add result data to search results
150  $SearchResults[] = $ResultData;
151  }
152 
153  # return search results to caller
154  return $SearchResults;
155  }
156 
157  # convenience function for getting time in microseconds
158  function GetMicrotime()
159  {
160  list($usec, $sec) = explode(" ", microtime());
161  return ((float)$usec + (float)$sec);
162  }
163 }
164 
165 
166 ?>