3 # FILE: SPTOAIServer.php
5 # Copyright 2002-2010 Internet Scout
6 # http://scout.wisc.edu
11 # ---- PUBLIC INTERFACE --------------------------------------------------
17 # grab our own database handle
21 # set up repository description
22 $DB->Query(
"SELECT * FROM SystemConfiguration");
23 $Record =
$DB->FetchRow();
24 $RepDescr[
"Name"] = $SysConfig->PortalName();
25 $Protocol = isset($_SERVER[
"HTTPS"]) ?
"https://" :
"http://";
26 $ServerName = ($_SERVER[
"SERVER_NAME"] !=
"127.0.0.1")
27 ? $_SERVER[
"SERVER_NAME"]
28 : $_SERVER[
"HTTP_HOST"];
29 $RepDescr[
"BaseURL"] = $Protocol.$ServerName.$_SERVER[
"SCRIPT_NAME"];
31 $RepDescr[
"EarliestDate"] = strlen(trim($Record[
"OaiEarliestDate"]))
32 ? $Record[
"OaiEarliestDate"] :
"1990-01-01";
33 $RepDescr[
"AdminEmail"][] = $SysConfig->AdminEmail();
34 $RepDescr[
"IDDomain"] = strlen(trim($Record[
"OaiIdDomain"]))
35 ? $Record[
"OaiIdDomain"] : $ServerName;
36 $RepDescr[
"IDPrefix"] = strlen(trim($Record[
"OaiIdPrefix"]))
37 ? $Record[
"OaiIdPrefix"] : $ServerName;
39 # create item factory object for retrieving items from DB
42 # call parent's constructor
45 # set up description of nsdl_dc format
46 $NsdldcNamespaceList = array(
47 "nsdl_dc" =>
"http://ns.nsdl.org/nsdl_dc_v1.01",
48 "dc" =>
"http://purl.org/dc/elements/1.1/",
49 "dct" =>
"http://purl.org/dc/terms/",
50 "ieee" =>
"http://www.ieee.org/xsd/LOMv1p0",
52 $NsdldcElements = array(
70 "dct:tableOfContents",
96 "dct:dateCopyrighted",
100 "dct:bibliographicCitation",
101 "ieee:interactivityType",
102 "ieee:interactivityLevel",
103 "ieee:typicalLearningTime",
105 $NsdldcQualifiers = array(
124 $this->
AddFormat(
"nsdl_dc",
"nsdl_dc:nsdl_dc",
125 "http://ns.nsdl.org/nsdl_dc_v1.01"
126 .
" http://ns.nsdl.org/schemas/nsdl_dc/nsdl_dc_v1.01.xsd",
128 $NsdldcNamespaceList, $NsdldcElements, $NsdldcQualifiers);
130 # load field mappings from database and set in parent
132 $DB->Query(
"SELECT * FROM OAIFieldMappings");
133 while ($Record =
$DB->FetchRow())
135 if ($Record[
"OAIFieldName"] !=
"Unmapped")
137 parent::SetFieldMapping($Record[
"FormatName"],
138 $Record[
"SPTFieldId"],
139 $Record[
"OAIFieldName"]);
143 # load qualifier mappings from database and set in parent
144 $DB->Query(
"SELECT * FROM OAIQualifierMappings");
145 while ($Record =
$DB->FetchRow())
147 if ($Record[
"OAIQualifierName"] !=
"Unmapped")
149 $LocalQualifier =
new Qualifier($Record[
"SPTQualifierId"]);
150 $LocalQualifierName = $LocalQualifier->Name();
151 parent::SetQualifierMapping($Record[
"FormatName"],
153 $Record[
"OAIQualifierName"]);
158 # add SQL conditional for selecting resources
161 # pass conditional on to item factory
162 $this->SPTItemFactory->AddSQLConditionalForResources($Conditional);
165 # get/set mapping of local field to OAI field (overloads parent method)
168 # retrieve ID for local field
170 $LocalField = $Schema->GetFieldByName($LocalFieldName);
171 $LocalFieldId = $LocalField->Id();
173 # return stored value
174 return parent::GetFieldMapping($FormatName, $LocalFieldId);
178 # retrieve ID for local field
180 $LocalField = $Schema->GetFieldByName($LocalFieldName);
181 $LocalFieldId = $LocalField->Id();
183 # check whether mapping is already in database
185 $MapCount =
$DB->Query(
"SELECT COUNT(*) AS MapCount FROM OAIFieldMappings"
186 .
" WHERE FormatName = '".$FormatName.
"'"
187 .
" AND SPTFieldId = '".$LocalFieldId.
"'",
190 # if mapping is already in database
193 # change mapping in database
194 $DB->Query(
"UPDATE OAIFieldMappings"
195 .
" SET OAIFieldName = '".addslashes($OAIFieldName).
"'"
196 .
" WHERE FormatName = '".addslashes($FormatName).
"'"
197 .
" AND SPTFieldId = '".$LocalFieldId.
"'");
201 # add new mapping to database
202 $DB->Query(
"INSERT INTO OAIFieldMappings"
203 .
" (FormatName, SPTFieldId, OAIFieldName) VALUES"
204 .
" ('".addslashes($FormatName).
"', '".$LocalFieldId
205 .
"', '".addslashes($OAIFieldName).
"')");
209 parent::SetFieldMapping($FormatName, $LocalFieldId, $OAIFieldName);
212 # set mapping of local qualifier to OAI qualifier (overloads parent method)
215 # retrieve ID for local qualifier
217 $LocalQualifier = $QFactory->GetQualifierByName($LocalQualifierName);
218 $LocalQualifierId = $LocalQualifier->Id();
220 # check whether mapping is already in database
222 $MapCount =
$DB->Query(
"SELECT COUNT(*) AS MapCount FROM OAIQualifierMappings"
223 .
" WHERE FormatName = '".addslashes($FormatName).
"'"
224 .
" AND SPTQualifierId = '".$LocalQualifierId.
"'",
227 # if mapping is already in database
230 # change mapping in database
231 $DB->Query(
"UPDATE OAIQualifierMappings"
232 .
" SET OAIQualifierName = '".addslashes($OAIQualifierName).
"'"
233 .
" WHERE FormatName = '".addslashes($FormatName).
"'"
234 .
" AND SPTQualifierId = '".$LocalQualifierId.
"'");
238 # add new mapping to database
239 $DB->Query(
"INSERT INTO OAIQualifierMappings"
240 .
" (FormatName, SPTQualifierId, OAIQualifierName) VALUES"
241 .
" ('".addslashes($FormatName).
"', '".$LocalQualifierId
242 .
"', '".addslashes($OAIQualifierName).
"')");
246 parent::SetQualifierMapping($FormatName, $LocalQualifierName, $OAIQualifierName);
250 # ---- PRIVATE INTERFACE -------------------------------------------------
259 # ---- PUBLIC INTERFACE --------------------------------------------------
264 # save any supplied retrieval parameters
270 # add link to full record page for item
271 $ServerName = ($_SERVER[
"SERVER_NAME"] !=
"127.0.0.1")
272 ? $_SERVER[
"SERVER_NAME"]
273 : $_SERVER[
"HTTP_HOST"];
274 $Protocol = isset($_SERVER[
"HTTPS"]) ?
"https://" :
"http://";
275 $SearchInfo[
"fullRecordLink"] = $Protocol.$ServerName.dirname($_SERVER[
"SCRIPT_NAME"]).
"/index.php?P=FullRecord&ID=".$ItemId;
277 # if a search score is available for the item
278 if (isset($this->SearchScores) && isset($this->SearchScores[$ItemId]))
280 # add search info for item
281 $SearchInfo[
"searchScore"] = $this->SearchScores[$ItemId];
285 # attempt to create item
288 # if item creation failed
289 if ($Item->Status() == -1)
291 # return NULL to indicate that no item was found with that ID
296 # return item to caller
301 function GetItems($StartingDate = NULL, $EndingDate = NULL)
303 return $this->
GetItemsInSet(NULL, $StartingDate, $EndingDate);
308 # initialize search parameters with release flag requirement
309 $SearchStrings[
"Release Flag"] =
"=1";
311 # if both begin and end date supplied
312 if (($StartingDate != NULL) && ($EndingDate != NULL))
314 # select resources created between starting and ending dates
315 $SearchStrings[
"Date Of Record Creation"] =
316 array(
">=".$StartingDate,
"<=".$EndingDate);
318 # else if begin date specified
319 elseif ($StartingDate != NULL)
321 # select resources created after begin date
322 $SearchStrings[
"Date Of Record Creation"] =
">=".$StartingDate;
324 # else if end date specified
325 elseif ($EndingDate != NULL)
327 # select resources created after begin date
328 $SearchStrings[
"Date Of Record Creation"] =
"<=".$EndingDate;
338 if (isset($this->SetFields[$Set]))
340 # add field spec to search strings
341 $SearchStrings[$this->SetFields[$Set]] =
"= ".$this->SetValues[$Set];
345 # set will not match anything so return empty array to caller
350 # perform search for desired items
352 if ($this->RetrievalSearchParameters)
354 $SearchStrings = array_merge($SearchStrings,
355 $this->RetrievalSearchParameters);
357 $SearchResults = $Engine->FieldedSearch($SearchStrings, 0, 1000000);
359 # extract resource IDs from search results
360 $ItemIds = array_keys($SearchResults);
362 # return array of resource IDs to caller
366 # return array containing all set specs (with human-readable set names as keys)
369 # make sure set name info is loaded
372 # return list of sets to caller
376 # retrieve IDs of items that match search parameters (only needed if OAI-SQ supported)
379 # translate field IDs into field names for search parameters
381 foreach ($SearchParams as $FieldId => $Value)
383 if ($FieldId ==
"X-KEYWORD-X")
385 $SearchStrings[
"XXXKeywordXXX"] = $Value;
389 $Field = $Schema->
GetField($FieldId);
390 $SearchStrings[$Field->Name()] = $Value;
394 # add release flag requirement to search parameters
395 $SearchStrings[
"Release Flag"] =
"=1";
397 # if both begin and end date supplied
398 if (($StartingDate != NULL) && ($EndingDate != NULL))
400 # select resources created between starting and ending dates
401 $SearchStrings[
"Date Of Record Creation"] =
402 array(
">=".$StartingDate,
"<=".$EndingDate);
404 # else if begin date specified
405 elseif ($StartingDate != NULL)
407 # select resources created after begin date
408 $SearchStrings[
"Date Of Record Creation"] =
">=".$StartingDate;
410 # else if end date specified
411 elseif ($EndingDate != NULL)
413 # select resources created after begin date
414 $SearchStrings[
"Date Of Record Creation"] =
"<=".$EndingDate;
417 # perform search for desired items
419 if ($this->RetrievalSearchParameters)
421 $SearchStrings = array_merge($SearchStrings,
422 $this->RetrievalSearchParameters);
424 $SearchResults = $Engine->FieldedSearch($SearchStrings, 0, 1000000);
427 $this->SearchScores = $SearchResults;
428 $this->SearchScoreScale = $Engine->FieldedSearchWeightScale($SearchStrings);
430 # extract resource IDs from search results
431 $ItemIds = array_keys($SearchResults);
433 # return array of resource IDs to caller
438 # ---- PRIVATE INTERFACE -------------------------------------------------
447 # normalize value for use as an OAI set spec
450 return preg_replace(
"/[^a-zA-Z0-9\-_.!~*'()]/",
"", $Name);
453 # load normalized set names and name mappings
456 # if set names have not already been loaded
457 if (!isset($this->SetSpecs))
459 # start with empty list of sets
460 $this->SetSpecs = array();
461 $this->SetFields = array();
462 $this->SetValues = array();
464 # for each metadata field that is a type that can be used for sets
467 foreach ($Fields as $Field)
469 # if field is flagged as being used for OAI sets
470 if ($Field->UseForOaiSets())
472 # retrieve all possible values for field
473 $FieldValues = $Field->GetPossibleValues();
475 # prepend field name to each value and add to list of sets
476 $FieldName = $Field->Name();
478 foreach ($FieldValues as $Value)
480 $SetSpec = $NormalizedFieldName.
":"
481 .$this->NormalizeForSetSpec($Value);
482 $this->SetSpecs[$FieldName.
": ".$Value] = $SetSpec;
483 $this->SetFields[$SetSpec] = $FieldName;
484 $this->SetValues[$SetSpec] = $Value;
494 # ---- PUBLIC INTERFACE --------------------------------------------------
499 # save ID for later use
502 # save any search info supplied
505 # attempt to create resource object
508 # if resource object creation failed
511 # set status to -1 to indicate constructor failure
512 $this->LastStatus = -1;
516 # set status to 1 to indicate constructor success
517 $this->LastStatus = 1;
519 # if cumulative rating data is available for this resource
521 if ($SysConfig->ResourceRatingsEnabled()
524 # add cumulative rating data to search info
525 $this->SearchInfo[
"cumulativeRating"] =
527 $this->SearchInfo[
"cumulativeRatingScale"] = 100;
536 $DateString = $this->
Resource->
Get(
"Date Of Record Creation");
537 if ($DateString ==
"0000-00-00 00:00:00") { $DateString = date(
"Y-m-d"); }
538 $Date =
new Date($DateString);
539 return $Date->FormattedISO8601();
547 # strip out any HTML tags if text value
548 if (is_string($ReturnValue))
550 $ReturnValue = strip_tags($ReturnValue);
553 # format correctly if standardized date
556 $Timestamp = strtotime($ReturnValue);
557 $ReturnValue = date(
'Y-m-d\TH:i:s', $Timestamp)
558 .substr_replace(date(
'O', $Timestamp),
':', 3, 0);
561 # return value to caller
569 if (is_array($Qualifier))
571 foreach ($Qualifier as $ItemId => $QualObj)
573 if (is_object($QualObj))
575 $ReturnValue[$ItemId] = $QualObj->Name();
581 if (isset($Qualifier) && is_object($Qualifier))
583 $ReturnValue = $Qualifier->Name();
591 # start out with empty list
594 # for each possible metadata field
597 foreach ($Fields as $Field)
599 # if field is flagged for use for OAI sets
600 if ($Field->UseForOaiSets())
602 # retrieve values for resource for this field and add to set list
603 $FieldName = $Field->Name();
605 if (isset($Values) && ($Values != NULL))
608 if (is_array($Values) && count($Values))
610 foreach ($Values as $Value)
612 $Sets[] = $NormalizedFieldName.
":"
613 .$this->NormalizeForSetSpec($Value);
618 $Sets[] = $NormalizedFieldName.
":"
619 .$this->NormalizeForSetSpec($Values);
625 # return list of sets to caller
640 # ---- PRIVATE INTERFACE -------------------------------------------------
647 # normalize value for use as an OAI set spec
650 return preg_replace(
"/[^a-zA-Z0-9\-_.!~*'()]/",
"", $Name);