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/ 18 # set up recommender configuration values for SPT 19 $ItemTableName =
"Resources";
20 $ItemIdFieldName =
"ResourceId";
21 $RatingTableName =
"ResourceRatings";
22 $UserIdFieldName =
"UserId";
23 $RatingFieldName =
"Rating";
25 # build field info from SPT metadata schema 27 $Fields = $this->Schema->GetFields();
28 foreach ($Fields as $Field)
30 if ($Field->Enabled() && $Field->IncludeInKeywordSearch())
32 $FieldName = $Field->Name();
33 $FieldInfo[$FieldName][
"DBFieldName"] = $Field->DBFieldName();
34 $FieldInfo[$FieldName][
"Weight"] = $Field->SearchWeight();
35 switch ($Field->Type())
41 $FieldInfo[$FieldName][
"FieldType"] =
48 $FieldInfo[$FieldName][
"FieldType"] =
54 $FieldInfo[$FieldName][
"FieldType"] =
59 $FieldInfo[$FieldName][
"FieldType"] =
60 Recommender::CONTENTFIELDTYPE_DATERANGE;
64 $FieldInfo[$FieldName][
"FieldType"] =
69 # (for images we use their alt text) 70 $FieldInfo[$FieldName][
"FieldType"] =
75 # (for files we use the file name) 76 $FieldInfo[$FieldName][
"FieldType"] =
83 # create our own schema object and tell it to cache values 85 $this->Schema->CacheData(TRUE);
87 # create a database connection for recommender to use 90 # pass configuration info to real recommender object 91 parent::__construct($DB, $ItemTableName, $RatingTableName,
92 $ItemIdFieldName, $UserIdFieldName, $RatingFieldName,
106 # if resource not already loaded 107 if (!isset($Resources[$ItemId]))
109 # get resource object 110 $Resources[$ItemId] =
new Resource($ItemId);
112 # if cached resource limit exceeded 113 if (count($Resources) > 100)
115 # dump oldest resource 117 list($DumpedItemId, $DumpedResources) = each($Resources);
118 unset($Resources[$DumpedItemId]);
122 # retrieve field value from resource object and return to caller 123 $FieldValue = $Resources[$ItemId]->Get($FieldName);
135 if (is_numeric($ItemOrItemId))
137 $ItemId = $ItemOrItemId;
142 $Item = $ItemOrItemId;
143 $ItemId = $Item->Id();
146 # if no proirity was provided, use the default 147 if ($TaskPriority === NULL)
149 $TaskPriority = self::$TaskPriority;
152 $TaskDescription =
"Update recommender data for" 153 .
" <a href=\"r".$ItemId.
"\"><i>" 154 .$Item->GetMapped(
"Title").
"</i></a>";
155 $GLOBALS[
"AF"]->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
156 array(intval($ItemId), 0), $TaskPriority);
166 # check that resource still exists 168 if (!$RFactory->ItemExists($SourceItemId)) {
return; }
170 # load recommender engine 172 if (!isset($Recommender)) { $Recommender =
new SPTRecommender(); }
174 # if starting update for source item 175 if ($StartingIndex == 0)
177 # clear data for item 178 $Recommender->DropItem($SourceItemId);
181 # load array of item IDs and pare down to those in same schema as source item 182 $TargetItemIds = $Recommender->GetItemIds();
183 $SourceSchemaIds = $RFactory->GetItemIds();
184 $TargetItemIds = array_values(array_intersect(
185 $TargetItemIds, $SourceSchemaIds));
186 $TargetCount = count($TargetItemIds);
188 # while not last item ID and not out of time 189 for ($Index = $StartingIndex; $Index < $TargetCount; $Index++)
191 # if target ID points to non-temporary entry 192 if ($TargetItemIds[$Index] >= 0)
194 # update correlation for source item and current item 195 $StartTime = microtime(TRUE);
196 $Recommender->UpdateContentCorrelation(
197 $SourceItemId, $TargetItemIds[$Index]);
198 $ExecutionTime = microtime(TRUE) - $StartTime;
200 # clear all caches if memory has run low 201 if ($GLOBALS[
"AF"]->GetFreeMemory() < 8000000)
206 if (function_exists(
"gc_collect_cycles"))
212 # bail out if out of memory or not enough time for another update 213 if (($GLOBALS[
"AF"]->GetSecondsBeforeTimeout() < ($ExecutionTime * 2))
214 || ($GLOBALS[
"AF"]->GetFreeMemory() < 8000000))
221 # if all correlations completed for source item 222 if ($Index >= $TargetCount)
224 # periodically prune correlations if enough time remaining 225 if (($GLOBALS[
"AF"]->GetSecondsBeforeTimeout() > 20)
226 && (rand(1, 10) == 1))
228 $Recommender->PruneCorrelations();
233 # requeue updates for remaining items 234 $Item =
new Resource($SourceItemId);
235 $TaskDescription =
"Update recommender data for" 236 .
" <a href=\"r".$SourceItemId.
"\"><i>" 237 .$Item->GetMapped(
"Title").
"</i></a>";
238 $GLOBALS[
"AF"]->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
239 array((
int)$SourceItemId, $Index),
251 self::$TaskPriority = $NewPriority;
254 # ---- PRIVATE INTERFACE -------------------------------------------------
static Caching($NewSetting=NULL)
Get or set whether query result caching is currently enabled.
const PRIORITY_LOW
Lower priority.
const CONTENTFIELDTYPE_DATE
SQL database abstraction object with smart query caching.
__construct()
SPTRecommender object constructor.
GetFieldValue($ItemId, $FieldName)
Get value for a given field.
const CONTENTFIELDTYPE_NUMERIC
static RunUpdateForItem($SourceItemId, $StartingIndex)
Perform recommender db updates for a specified item (usually in the background)
QueueUpdateForItem($ItemOrItemId, $TaskPriority=NULL)
Queue a background update for a specified item.
const CONTENTFIELDTYPE_TEXT
Represents a "resource" in CWIS.
static SetUpdatePriority($NewPriority)
Set the default priority for background tasks.
Factory for Resource objects.
const PRIORITY_BACKGROUND
Lowest priority.