3 # FILE: SPTRecommender.php
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
14 # set up recommender configuration values for SPT
15 $ItemTableName =
"Resources";
16 $ItemIdFieldName =
"ResourceId";
17 $RatingTableName =
"ResourceRatings";
18 $UserIdFieldName =
"UserId";
19 $RatingFieldName =
"Rating";
21 # build field info from SPT metadata schema
23 $Fields = $this->Schema->GetFields();
24 foreach ($Fields as $Field)
26 if ($Field->Enabled() && $Field->IncludeInKeywordSearch())
28 $FieldName = $Field->Name();
29 $FieldInfo[$FieldName][
"DBFieldName"] = $Field->DBFieldName();
30 $FieldInfo[$FieldName][
"Weight"] = $Field->SearchWeight();
31 switch ($Field->Type())
37 $FieldInfo[$FieldName][
"FieldType"] =
44 $FieldInfo[$FieldName][
"FieldType"] =
50 $FieldInfo[$FieldName][
"FieldType"] =
55 $FieldInfo[$FieldName][
"FieldType"] =
56 Recommender::CONTENTFIELDTYPE_DATERANGE;
60 $FieldInfo[$FieldName][
"FieldType"] =
65 # (for images we use their alt text)
66 $FieldInfo[$FieldName][
"FieldType"] =
71 # (for files we use the file name)
72 $FieldInfo[$FieldName][
"FieldType"] =
79 # create our own schema object and tell it to cache values
81 $this->Schema->CacheData(TRUE);
83 # create a database connection for recommender to use
86 # pass configuration info to real recommender object
87 parent::__construct($DB, $ItemTableName, $RatingTableName,
88 $ItemIdFieldName, $UserIdFieldName, $RatingFieldName,
92 # overloaded version of method to retrieve field values from DB
97 # if resource not already loaded
98 if (!isset($Resources[$ItemId]))
100 # get resource object
101 $Resources[$ItemId] =
new Resource($ItemId);
103 # if cached resource limit exceeded
104 if (count($Resources) > 100)
106 # dump oldest resource
108 list($DumpedItemId, $DumpedResources) = each($Resources);
109 unset($Resources[$DumpedItemId]);
113 # retrieve field value from resource object and return to caller
114 $FieldValue = $Resources[$ItemId]->Get($FieldName);
119 $TaskPriority = ApplicationFramework::PRIORITY_BACKGROUND)
122 $TaskDescription =
"Update recommender data for"
123 .
" <a href=\"r".$ItemId.
"\"><i>"
124 .$Item->GetMapped(
"Title").
"</i></a>";
125 $GLOBALS[
"AF"]->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
126 array(intval($ItemId), 0), $TaskPriority);
131 # check that resource still exists
133 if (!$RFactory->ItemExists($SourceItemId)) {
return; }
135 # load recommender engine
137 if (!isset($Recommender)) { $Recommender =
new SPTRecommender(); }
139 # if starting update for source item
140 if ($StartingIndex == 0)
142 # clear data for item
143 $Recommender->DropItem($SourceItemId);
146 # load array of item IDs and pare down to those in same schema as source item
147 $TargetItemIds = $Recommender->GetItemIds();
148 $SourceSchemaIds = $RFactory->GetItemIds();
149 $TargetItemIds = array_values(array_intersect(
150 $TargetItemIds, $SourceSchemaIds));
151 $TargetCount = count($TargetItemIds);
153 # while not last item ID and not out of time
154 for ($Index = $StartingIndex; $Index < $TargetCount; $Index++)
156 # if target ID points to non-temporary entry
157 if ($TargetItemIds[$Index] >= 0)
159 # update correlation for source item and current item
160 $StartTime = microtime(TRUE);
161 $Recommender->UpdateContentCorrelation(
162 $SourceItemId, $TargetItemIds[$Index]);
163 $ExecutionTime = microtime(TRUE) - $StartTime;
165 # clear all caches if memory has run low
166 if ($GLOBALS[
"AF"]->GetFreeMemory() < 8000000)
171 if (function_exists(
"gc_collect_cycles"))
177 # bail out if out of memory or not enough time for another update
178 if (($GLOBALS[
"AF"]->GetSecondsBeforeTimeout() < ($ExecutionTime * 2))
179 || ($GLOBALS[
"AF"]->GetFreeMemory() < 8000000))
186 # if all correlations completed for source item
187 if ($Index >= $TargetCount)
189 # periodically prune correlations if enough time remaining
190 if (($GLOBALS[
"AF"]->GetSecondsBeforeTimeout() > 20)
191 && (rand(1, 10) == 1))
193 $Recommender->PruneCorrelations();
198 # requeue updates for remaining items
199 $Item =
new Resource($SourceItemId);
200 $TaskDescription =
"Update recommender data for"
201 .
" <a href=\"r".$SourceItemId.
"\"><i>"
202 .$Item->GetMapped(
"Title").
"</i></a>";
203 $GLOBALS[
"AF"]->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
204 array((
int)$SourceItemId, $Index),
205 ApplicationFramework::PRIORITY_LOW);
210 # ---- PRIVATE INTERFACE -------------------------------------------------
static Caching($NewSetting=NULL)
Get or set whether query result caching is currently enabled.
const CONTENTFIELDTYPE_DATE
SQL database abstraction object with smart query caching.
QueueUpdateForItem($ItemId, $TaskPriority=ApplicationFramework::PRIORITY_BACKGROUND)
GetFieldValue($ItemId, $FieldName)
const CONTENTFIELDTYPE_NUMERIC
static RunUpdateForItem($SourceItemId, $StartingIndex)
const CONTENTFIELDTYPE_TEXT
Represents a "resource" in CWIS.
Factory for Resource objects.