3 # FILE: SPTRecommender.php
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011 Edward Almasy and Internet Scout Project
7 # http://scout.wisc.edu/
14 # set up recommender configuration values for SPT
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
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);
122 $AF->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
123 array(intval($ItemId), 0), $Priority);
128 # check that resource still exists
130 if (!$RFactory->ItemExists($SourceItemId)) {
return; }
132 # load recommender engine
134 if (!isset($Recommender)) { $Recommender =
new SPTRecommender(); }
136 # if starting update for source item
137 if ($StartingIndex == 0)
139 # clear data for item
140 $Recommender->DropItem($SourceItemId);
143 # load array of item IDs
144 $TargetItemIds = $Recommender->GetItemIds();
145 $TargetCount = count($TargetItemIds);
147 # while not last item ID and not out of time
149 for ($Index = $StartingIndex; ($Index < $TargetCount)
150 && ($AF->GetSecondsBeforeTimeout() > 5); $Index++)
152 # if target ID points to non-temporary entry
153 if ($TargetItemIds[$Index] >= 0)
155 # update correlation for source item and current item
156 $Recommender->UpdateContentCorrelation(
157 $SourceItemId, $TargetItemIds[$Index]);
161 # if all correlations completed for source item
162 if ($Index >= $TargetCount)
164 # periodically prune correlations if enough time remaining
165 if (($AF->GetSecondsBeforeTimeout() > 10) && (rand(1, 10) == 1))
167 $Recommender->PruneCorrelations();
172 # requeue updates for remaining items
173 $AF->QueueUniqueTask(array(__CLASS__,
"RunUpdateForItem"),
179 # ---- PRIVATE INTERFACE -------------------------------------------------