9 namespace Fossology\Reuser;
28 include_once(__DIR__ .
"/version.php");
75 function __construct()
77 parent::__construct(REUSER_AGENT_NAME, AGENT_VERSION, AGENT_REV);
78 $this->aagentDao = $this->container->get(
'dao.agent');
79 $this->uploadDao = $this->container->get(
'dao.upload');
80 $this->clearingDao = $this->container->get(
'dao.clearing');
81 $this->copyrightDao = $this->container->get(
'dao.copyright');
82 $this->decisionTypes = $this->container->get(
'decision.types');
83 $this->clearingEventProcessor = $this->container->get(
'businessrules.clearing_event_processor');
84 $this->clearingDecisionFilter = $this->container->get(
'businessrules.clearing_decision_filter');
85 $this->clearingDecisionProcessor = $this->container->get(
'businessrules.clearing_decision_processor');
86 $this->agentLicenseEventProcessor = $this->container->get(
'businessrules.agent_license_event_processor');
96 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
97 foreach ($this->uploadDao->getReusedUpload($uploadId, $this->groupId) as $reuseTriple) {
99 $reusedUploadId = $reuseTriple[
'reused_upload_fk'];
101 $reusedGroupId = $reuseTriple[
'reused_group_fk'];
103 $reuseMode = $reuseTriple[
'reuse_mode'];
105 $itemTreeBoundsReused = $this->uploadDao->getParentItemBounds($reusedUploadId);
106 if (
false === $itemTreeBoundsReused) {
110 if ($reuseMode & UploadDao::REUSE_ENHANCED) {
113 $this->processUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId);
116 if ($reuseMode & UploadDao::REUSE_MAIN) {
117 $this->
reuseMainLicense($uploadId, $this->groupId, $reusedUploadId, $reusedGroupId);
120 if ($reuseMode & UploadDao::REUSE_CONF) {
121 $this->reuseConfSettings($uploadId, $reusedUploadId);
124 if ($reuseMode & UploadDao::REUSE_COPYRIGHT) {
130 protected function reuseConfSettings($uploadId, $reusedUploadId)
132 if (!$this->uploadDao->insertReportConfReuse($uploadId, $reusedUploadId)) {
133 echo
"INFO :: Report configuration for select upload doesn't exists. Unable to copy!!!";
151 $mainLicenseIds = $this->clearingDao->getMainLicenseIds($reusedUploadId, $reusedGroupId);
152 if (!empty($mainLicenseIds)) {
153 foreach ($mainLicenseIds as $mainLicenseId) {
154 if (in_array($mainLicenseId, $this->clearingDao->getMainLicenseIds($uploadId,
$groupId))) {
157 $this->clearingDao->makeMainLicense($uploadId,
$groupId, $mainLicenseId);
171 protected function getAgentId($uploadId)
176 $scanJobProxy =
new ScanJobProxy($this->aagentDao, $uploadId);
178 $scanJobProxy->createAgentStatus(array(
$agentName));
179 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
180 if (!array_key_exists(
$agentName, $selectedScanners)) {
197 $agentId = $this->getAgentId($uploadId);
198 $reusedAgentId = $this->getAgentId($reusedUploadId);
199 if (
$agentId ==
null || $reusedAgentId ==
null) {
202 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
203 $extrawhere =
' agent_fk='.$agentId;
204 $allCopyrights = $this->copyrightDao->getScannerEntries(
'copyright',
205 $uploadTreeTableName, $uploadId,
null, $extrawhere);
207 $reusedCopyrights = $this->copyrightDao->getAllEventEntriesForUpload(
208 $reusedUploadId, $reusedAgentId);
210 if (!empty($reusedCopyrights) && !empty($allCopyrights)) {
211 foreach ($reusedCopyrights as $reusedCopyright) {
212 foreach ($allCopyrights as $copyrightKey => $copyright) {
213 if (strcmp($copyright[
'hash'], $reusedCopyright[
'hash']) == 0) {
214 if ($this->
dbManager->booleanFromDb($reusedCopyright[
'is_enabled'])) {
216 $content = $reusedCopyright[
"contentedited"];
221 $hash = $copyright[
'hash'];
222 $item = $this->uploadDao->getItemTreeBounds(intval($copyright[
'uploadtree_pk']),
223 $uploadTreeTableName);
224 $this->copyrightDao->updateTable($item, $hash, $content,
225 $this->userId,
'copyright', $action);
226 unset($allCopyrights[$copyrightKey]);
243 protected function processUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId)
248 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
249 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
$groupId);
251 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
254 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
256 $clearingDecisionToImportByFileId = $this->
mapByFileId($clearingDecisionsToImport);
260 $containedItems = ArrayOperation::callChunked(
261 function ($fileIds) use ($itemTreeBounds,
$uploadDao)
265 "pfile_fk = ANY($1)",
266 array(
'{' . implode(
', ', $fileIds) .
'}')
268 }, array_keys($clearingDecisionToImportByFileId), 100);
270 foreach ($containedItems as $item) {
271 $fileId = $item->getFileId();
272 if (empty($fileId)) {
275 if (array_key_exists($fileId, $clearingDecisionToImportByFileId)) {
277 $clearingDecisionToImportByFileId[$fileId]);
279 throw new \Exception(
"bad internal state");
297 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
298 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $this->groupId);
300 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
303 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
305 $sql =
"SELECT ut.* FROM uploadtree ur, uploadtree ut WHERE ur.upload_fk=$2"
306 .
" AND ur.pfile_fk=$3 AND ut.upload_fk=$1 AND ut.ufile_name=ur.ufile_name";
307 $stmt = __METHOD__.
'.reuseByName';
309 $treeDao = $this->container->get(
'dao.tree');
311 foreach ($clearingDecisionsToImport as $clearingDecision) {
312 $reusedPath = $treeDao->getRepoPathOfPfile($clearingDecision->getPfileId());
313 if (empty($reusedPath)) {
318 $res = $this->
dbManager->execute($stmt,array($itemTreeBounds->getUploadId(),
319 $itemTreeBoundsReused->getUploadId(),$clearingDecision->getPfileId()));
320 while ($row = $this->
dbManager->fetchArray($res)) {
321 $newPath = $treeDao->getRepoPathOfPfile($row[
'pfile_fk']);
322 if (empty($newPath)) {
345 $diffLevel = exec(
"diff $reusedPath $newPath | wc -l");
346 if ($diffLevel ===
false) {
347 throw new \Exception(
'cannot use diff tool');
349 if ($diffLevel < 5) {
350 $this->createCopyOfClearingDecision($itemId, $this->userId, $this->groupId, $clearingDecision);
364 $clearingDecisionByFileId = array();
365 foreach ($clearingDecisions as $clearingDecision) {
366 $fileId = $clearingDecision->getPfileId();
367 if (!array_key_exists($fileId, $clearingDecisionByFileId)) {
368 $clearingDecisionByFileId[$fileId] = $clearingDecision;
371 return $clearingDecisionByFileId;
381 protected function createCopyOfClearingDecision($itemId,
$userId,
$groupId, $clearingDecisionToCopy)
383 $clearingEventIdsToCopy = array();
385 foreach ($clearingDecisionToCopy->getClearingEvents() as $clearingEvent) {
386 $licenseId = $clearingEvent->getLicenseId();
387 $uploadTreeId = $itemId;
388 $isRemoved = $clearingEvent->isRemoved();
389 $type = ClearingEventTypes::USER;
390 $reportInfo = $clearingEvent->getReportinfo();
391 $comment = $clearingEvent->getComment();
392 $acknowledgement = $clearingEvent->getAcknowledgement();
394 $clearingEventIdsToCopy[] = $this->clearingDao->insertClearingEvent(
396 $type, $reportInfo, $comment, $acknowledgement,
$jobId);
399 $this->clearingDao->createDecisionFromEvents(
403 $clearingDecisionToCopy->getType(),
404 $clearingDecisionToCopy->getScope(),
405 $clearingEventIdsToCopy
421 foreach ($clearingDecisions as $clearingDecision) {
422 $mapped[$clearingDecision->getClearingId()] = $clearingDecision;
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
Handle events related to license findings.
Various utility functions to filter ClearingDecision.
Utility functions to process ClearingDecision.
Functions to process clearing events.
processEnhancedUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId)
Get clearing decisions and use copyClearingDecisionIfDifferenceIsSmall()
processUploadId($uploadId)
Get the upload items and reuse based on reuse mode.
reuseMainLicense($uploadId, $groupId, $reusedUploadId, $reusedGroupId)
Reuse main license from previous upload.
mapByClearingId($clearingDecisions)
Map clearing decisions by clearing id.
copyClearingDecisionIfDifferenceIsSmall($reusedPath, $newPath, $clearingDecision, $itemId)
Use diff tool to compare files.
$agentLicenseEventProcessor
$clearingDecisionProcessor
mapByFileId($clearingDecisions)
Maps clearing decisions by file id.
reuseCopyrights($uploadId, $reusedUploadId)
Reuse deactivated Copyrights from previous upload.
fo_dbManager * dbManager
fo_dbManager object