3 # FILE: ResourceFactory.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/
15 # ---- PUBLIC INTERFACE --------------------------------------------------
25 $this->SchemaId = $SchemaId;
27 # set up item factory base class
28 $this->
ItemFactory(
"Resource",
"Resources",
"ResourceId", NULL, FALSE,
29 "SchemaId = ".intval($this->SchemaId));
39 # create new target resource
42 # load up resource to duplicate
43 $SrcResource =
new Resource($ResourceId);
45 # if resource to duplicate was found
46 if ($SrcResource->Status() > 0)
48 # for each metadata field
50 $Fields = $Schema->GetFields();
51 foreach ($Fields as $Field)
53 # skip the Cumulative Rating and Date Last Modified fields
54 if ($Field->Name() !=
"Cumulative Rating" &&
55 $Field->Name() !=
"Date Last Modified")
57 $NewValue = $SrcResource->GetByField($Field, TRUE);
59 # clear default value from destination resource that is
60 # set when creating a new resource
61 $DstResource->ClearByField($Field);
63 # copy value from source resource to destination resource
64 $DstResource->SetByField($Field, $NewValue);
69 # return new resource to caller
81 # sanitize qualifier ID or retrieve from object
82 $QualifierId = is_object($ObjectOrId)
83 ? $ObjectOrId->Id() : intval($ObjectOrId);
85 # if new qualifier passed in
86 if ($NewObjectOrId !== NULL)
88 # sanitize qualifier ID to change to or retrieve it from object
89 $NewQualifierIdVal = is_object($NewObjectOrId)
90 ? $NewObjectOrId->Id() : intval($NewObjectOrId);
94 # qualifier should be cleared
95 $NewQualifierIdVal =
"NULL";
98 # for each metadata field
100 $Fields = $Schema->GetFields();
101 foreach ($Fields as $Field)
103 # if field uses qualifiers and uses item-level qualifiers
104 $QualColName = $Field->DBFieldName().
"Qualifier";
105 if ($Field->UsesQualifiers()
106 && $Field->HasItemLevelQualifiers()
107 && $this->DB->FieldExists(
"Resources", $QualColName))
109 # set all occurrences to new qualifier value
110 $this->DB->Query(
"UPDATE Resources"
111 .
" SET ".$QualColName.
" = ".$NewQualifierIdVal.
""
112 .
" WHERE ".$QualColName.
" = '".$QualifierId.
"'"
113 .
" AND SchemaId = ".intval($this->SchemaId));
117 # clear or change qualifier association with controlled names
118 # (NOTE: this should probably be done in a controlled name factory object)
119 $this->DB->Query(
"UPDATE ControlledNames"
120 .
" SET QualifierId = ".$NewQualifierIdVal
121 .
" WHERE QualifierId = '".$QualifierId.
"'");
123 # clear or change qualifier association with classifications
124 # (NOTE: this should probably be done in a classification factory object)
125 $this->DB->Query(
"UPDATE Classifications"
126 .
" SET QualifierId = ".$NewQualifierIdVal
127 .
" WHERE QualifierId = '".$QualifierId.
"'");
136 return $this->DB->Query(
137 "SELECT COUNT(DISTINCT ResourceId) AS ResourceCount"
138 .
" FROM ResourceRatings",
148 return $this->DB->Query(
149 "SELECT COUNT(DISTINCT UserId) AS UserCount"
150 .
" FROM ResourceRatings",
165 # assume that no resources will be found
166 $Resources = array();
168 # calculate cutoff date for resources
169 $CutoffDate = date(
"Y-m-d H:i:s", strtotime($MaxDaysToGoBack.
" days ago"));
171 # query for resource IDs
172 $this->DB->Query(
"SELECT ResourceId FROM Resources WHERE"
173 .
" DateOfRecordRelease > '".$CutoffDate.
"'"
174 .
" AND ReleaseFlag = 1"
175 .
" AND ResourceId >= 0"
176 .
" AND SchemaId = ".intval($this->SchemaId)
177 .
" ORDER BY DateOfRecordRelease DESC, DateOfRecordCreation DESC"
178 .
" LIMIT ".intval($Offset).
", ".intval($Count));
179 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
181 # for each resource ID found
182 foreach ($ResourceIds as $ResourceId)
184 # load resource and add to list of found resources
185 $Resources[$ResourceId] =
new Resource($ResourceId);
188 # return found resources to caller
202 # assume no resources will be found
203 $ResourceIds = array();
207 $Field = $Schema->GetFieldByName($FieldName);
212 # construct query based on field type
213 switch ($Field->Type())
218 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
219 .
" FROM Resources WHERE "
220 .$Field->DBFieldName().
" IS NOT NULL"
221 .
" AND LENGTH(LTRIM(RTRIM(".$Field->DBFieldName().
"))) > 0"
222 .
" AND SchemaId = ".intval($this->SchemaId),
226 $Query =
"SELECT ResourceId FROM Resources"
227 .
" WHERE SchemaId = ".intval($this->SchemaId)
228 .
" ORDER BY ".$Field->DBFieldName()
229 .($Ascending ?
" ASC" :
" DESC");
235 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
236 .
" FROM Resources WHERE "
237 .$Field->DBFieldName().
" IS NOT NULL"
238 .
" AND SchemaId = ".intval($this->SchemaId),
242 $Query =
"SELECT ResourceId FROM Resources"
243 .
" WHERE SchemaId = ".intval($this->SchemaId)
244 .
" ORDER BY ".$Field->DBFieldName()
245 .($Ascending ?
" ASC" :
" DESC");
250 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
251 .
" FROM Resources WHERE "
252 .$Field->DBFieldName().
"Begin IS NOT NULL"
253 .
" AND SchemaId = ".intval($this->SchemaId),
257 $Query =
"SELECT ResourceId FROM Resources"
258 .
" WHERE SchemaId = ".intval($this->SchemaId)
259 .
" ORDER BY ".$Field->DBFieldName().
"Begin"
260 .($Ascending ?
" ASC" :
" DESC");
265 # if appropriate query was found
268 # if limited number of results were requested
272 $Query .=
" LIMIT ".intval($Limit);
275 # perform query and retrieve resource IDs
276 $this->DB->Query($Query);
277 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
281 # return resource IDs to caller
295 # compute this user's class
296 $UserClass = $this->ComputeUserClass($User);
298 # generate an array where the keys are ResourceIds affected by
299 # user comparions for the current user
300 $UserComparisonsRIDs = array_flip($this->InUserComparisons($User));
302 # grab all the ResourceIds for this user class
303 $DB->Query(
"SELECT ResourceId, CanView FROM UserPermsCache WHERE"
304 .
" UserClass='".$UserClass.
"'");
306 # filter out those not requested
307 $Cache = array_intersect_key(
308 $DB->FetchColumn(
"CanView",
"ResourceId"),
309 array_flip($ResourceIds) );
311 # figure out which resources we didn't have cached values for
312 # and iterate over those
313 $MissingIds = array_diff( $ResourceIds, array_keys($Cache) );
314 foreach ($MissingIds as $Id)
316 # evaluate perms for this resource
318 $CanView = $Resource->UserCanView($User, FALSE);
320 # if this is a result we can cache, do so
321 if ( !isset($UserComparisonRIDs[ $Id ]) )
323 $this->DB->Query(
"INSERT INTO UserPermsCache (ResourceId, UserClass, CanView) "
324 .
"VALUES (".$Id.
",'".$UserClass.
"',".($CanView?
"1":
"0").
")");
327 $Cache[$Id] = $CanView;
330 # apply schema permissions hooks to all our values
331 foreach (array_keys($Cache) as $Id)
333 $SignalResult = $GLOBALS[
"AF"]->SignalEvent(
334 "EVENT_RESOURCE_VIEW_PERMISSION_CHECK",
338 "CanView" => $Cache[$Id]));
339 $Cache[$Id] = $SignalResult[
"CanView"];
342 # return the viewable ResourceIds
343 return array_keys( array_filter($Cache) );
353 $DB->Query(
"DELETE FROM UserPermsCache");
364 $LastChangeDate = $this->DB->Query(
365 "SELECT MAX(DateLastModified) AS LastChangeDate"
367 .
" WHERE SchemaId = ".intval($this->SchemaId)
368 .($OnlyReleasedResources ?
" AND ReleaseFlag = 1" :
""),
370 return ($LastChangeDate ? strtotime($LastChangeDate) : NULL);
380 # retrieve field names from schema
381 $FieldNames = array();
383 $Fields = $Schema->GetFields();
384 foreach ($Fields as $Field)
386 $FieldNames[$Field->Id()] = $Field->Name();
389 # return field names to caller
402 # start out assuming we won't find any resources
403 $Resources = array();
407 $Fields = $Schema->GetFields(
417 foreach ($ValuesToMatch as $FieldId => $Value)
419 # if field can be used for comparison
420 if (isset($Fields[$FieldId]))
422 # add comparison to condition
423 $Condition .= $LinkingTerm.$Fields[$FieldId]->DBFieldName()
424 .
" = '".addslashes($Value).
"'";
425 $LinkingTerm =
" AND ";
429 # if there were valid conditions
430 if (strlen($Condition))
432 # build query statment
433 $Query =
"SELECT ResourceId FROM Resources WHERE ".$Condition
434 .
" AND SchemaId = ".intval($this->SchemaId);
436 # execute query to retrieve matching resource IDs
437 $this->DB->Query($Query);
438 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
440 # retrieve resource objects
441 foreach ($ResourceIds as $Id)
443 $Resources[$Id] =
new Resource($Id);
447 # return any resources found to caller
451 # Functions for keeping per-field resource counts updated:
469 $Field = $Schema->GetField($FieldId);
473 if ($this->ResourceCount === NULL)
476 "SELECT FieldId, ClassName, CountType, Count FROM ResourceCounts");
478 while ($Row = $this->DB->FetchRow())
480 $R_FieldId = $Row[
"FieldId"];
481 $R_ClassName = $Row[
"ClassName"];
482 $R_CountType = $Row[
"CountType"];
483 $R_Count = $Row[
"Count"];
485 $this->ResourceCount[$R_FieldId][$R_ClassName][$R_CountType] = $R_Count;
492 return isset($this->ResourceCount[$FieldId][$Value][$CountType]) ?
493 $this->ResourceCount[$FieldId][$Value][$CountType] :
508 return $this->DB->Query(
"
509 SELECT COUNT(*) AS ResourceTotal
513 AND SchemaId = ".intval($this->SchemaId),
524 return $this->DB->Query(
"
525 SELECT COUNT(*) AS ResourceTotal
528 AND SchemaId = ".intval($this->SchemaId),
543 # be sure that we're not a gigantic object when the task is queued
544 $TmpResourceCount = $this->ResourceCount;
545 $this->ResourceCount = NULL;
547 $AF->QueueUniqueTask(
548 array($this,
"UpdateResourceCountCallback"), array());
549 $this->ResourceCount = $TmpResourceCount;
563 "CREATE TABLE ResourceCountsNew (FieldId INT, ClassName TEXT, CountType TEXT, Count INT);");
565 $Start = microtime(TRUE);
567 foreach ($this->ResourceCountConditions as $CountType => $CountCondition)
570 "INSERT INTO ResourceCountsNew "
571 .
"SELECT FieldId, ControlledName AS ClassName,"
572 .
"'".$CountType.
"' AS CountType, Count(ResourceId) AS Count "
573 .
"FROM (SELECT * FROM ResourceNameInts WHERE ResourceId IN "
574 .
"(SELECT ResourceId FROM Resources "
575 .
" WHERE SchemaId = ".intval($this->SchemaId)
576 .(($CountCondition!==NULL)
577 ?
" AND ".$CountCondition:
"").
")) AS T0 "
578 .
"JOIN ControlledNames USING(ControlledNameId) GROUP BY ControlledNameId;" );
581 $Stop = microtime(TRUE);
584 "INSERT INTO ResourceCountsNew VALUES (-1, '__LAST_UPDATED__', '', UNIX_TIMESTAMP()); ");
586 "INSERT INTO ResourceCountsNew VALUES (-2, '__UPDATE_RUNTIME__','',".($Stop-$Start).
");");
588 "RENAME TABLE ResourceCounts TO ResourceCountsOld, ResourceCountsNew TO ResourceCounts; ");
590 "DROP TABLE ResourceCountsOld; ");
593 # ---- PRIVATE INTERFACE -------------------------------------------------
595 private $ResourceCount = NULL;
596 private $ResourceCountConditions = array(
"All" => NULL,
"Released" =>
"ReleaseFlag=1");
604 private function ComputeUserClass( $User )
608 if (!isset($ClassCache))
610 $ClassCache = array();
613 $UserId = $User->IsLoggedIn() ? $User->Id() :
"XX-ANON-XX";
615 if (!isset($ClassCache[$UserId]))
617 $RelevantPerms = array();
620 foreach ($Schema->GetFields() as $Field)
622 $RelevantPerms = array_merge(
624 $Field->ViewingPrivileges()->PrivilegeFlagsChecked() );
626 $RelevantPerms = array_unique($RelevantPerms);
628 $PermsInvolved = array();
629 foreach ($RelevantPerms as $Perm)
631 if ($User->HasPriv($Perm))
632 $PermsInvolved []= $Perm;
634 $ClassCache[$UserId] = md5( implode(
"-", $PermsInvolved ) );
637 return $ClassCache[$UserId];
647 private function InUserComparisons( $User )
651 $this->CheckUserComparisons( $User,
"=="),
652 $this->CheckUserComparisons( $User,
"!=") ) ) ;
663 private function CheckUserComparisons( $User, $ComparisonType )
665 # assume no resources match
668 # if we're checking the anonymous user, presume that
670 if ( !$User->IsLoggedIn() )
673 # iterate through all the fields in the schema,
674 # constructing a list of the User fields implicated
675 # in "User is the value of" comparisions
677 $UserComparisonFields = array();
678 foreach ($Schema->GetFields() as $Field)
680 $UserComparisonFields = array_merge(
681 $UserComparisonFields,
682 $Field->ViewingPrivileges()->FieldsWithUserComparisons($ComparisonType) );
684 $UserComparisonFields = array_unique($UserComparisonFields);
686 # from the list of fields, construct an array of SQL conditions to
687 # count how many resources are implicated
688 $SqlConditions = array();
689 foreach ($UserComparisonFields as $FieldId)
692 $SqlConditions []= $Field->DBFieldName().
693 ($ComparisonType ==
"==" ?
" = " :
" != ")
697 # use the list of SQL conditions to see if any resources
698 # will actually match this predicate
699 if (count($SqlConditions)>0)
702 $Query =
"SELECT ResourceId FROM Resources WHERE "
703 .
"SchemaId=".$this->SchemaId.
" AND ("
704 .implode(
" OR ", $SqlConditions).
")";
706 $Result =
$DB->FetchColumn(
"ResourceId");
GetRatedResourceUserCount()
Return number of users who have rated resources.
SQL database abstraction object with smart query caching.
GetTimestampOfLastResourceModification($OnlyReleasedResources=TRUE)
Get date/time of when last a resource was modified.
UpdateResourceCountCallback()
Update the stored counts of resources per controlled name, looking at the private var $ResourceCountC...
GetResourceCount($FieldId, $Value, $CountType="All")
Return the number of resources having a given value set for a specified ControlledName field...
ClearViewingPermsCache()
Clear the cache of viewable resources.
GetResourceIdsSortedBy($FieldName, $Ascending=TRUE, $Limit=NULL)
Get resource IDs sorted by specified field.
ResourceFactory($SchemaId=MetadataSchema::SCHEMAID_DEFAULT)
Class constructor.
GetRecentlyReleasedResources($Count=10, $Offset=0, $MaxDaysToGoBack=90)
Get resources sorted by descending Date of Record Release, with Date of Record Creation as the second...
QueueResourceCountUpdate()
Add a task to the queue which will update the resource counts for ControlledNames.
GetMatchingResources($ValuesToMatch)
Find resources with values that match those specified.
FilterNonViewableResources($ResourceIds, $User)
Filter a list of resources leaving only those viewable by a specified user.
ClearQualifier($ObjectOrId, $NewObjectOrId=NULL)
Clear or change specific qualifier for all resources.
Represents a "resource" in CWIS.
GetPossibleFieldNames()
Get possible field names for resources.
GetReleasedResourceTotal()
Get the total number of released resources in the collection.
static Create($SchemaId)
Create a new resource.
Common factory class for item manipulation.
GetRatedResourceCount()
Return number of resources that have ratings.
Factory for Resource objects.
ItemFactory($ItemClassName, $ItemTableName, $ItemIdFieldName, $ItemNameFieldName=NULL, $OrderOpsAllowed=FALSE, $SqlCondition=NULL)
Class constructor.
GetResourceTotal()
Get the total number of resources in the collection, even if they are not released.
DuplicateResource($ResourceId)
Duplicate the specified resource and return to caller.