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 $existingMainLicenseIds = $this->clearingDao->getMainLicenseIds($uploadId,
$groupId);
154 foreach ($mainLicenseIds as $mainLicenseId) {
155 if (in_array($mainLicenseId, $existingMainLicenseIds)) {
158 $this->clearingDao->makeMainLicense($uploadId,
$groupId, $mainLicenseId);
172 protected function getAgentId($uploadId)
177 $scanJobProxy =
new ScanJobProxy($this->aagentDao, $uploadId);
179 $scanJobProxy->createAgentStatus(array(
$agentName));
180 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
181 if (!array_key_exists(
$agentName, $selectedScanners)) {
198 $agentId = $this->getAgentId($uploadId);
199 $reusedAgentId = $this->getAgentId($reusedUploadId);
200 if (
$agentId ==
null || $reusedAgentId ==
null) {
203 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
204 $extrawhere =
' agent_fk='.$agentId;
205 $allCopyrights = $this->copyrightDao->getScannerEntries(
'copyright',
206 $uploadTreeTableName, $uploadId,
null, $extrawhere);
208 $reusedCopyrights = $this->copyrightDao->getAllEventEntriesForUpload(
209 $reusedUploadId, $reusedAgentId);
211 if (!empty($reusedCopyrights) && !empty($allCopyrights)) {
213 $copyrightsByHash = [];
214 foreach ($allCopyrights as $copyrightKey => $copyright) {
215 $hash = $copyright[
'hash'];
216 if (!isset($copyrightsByHash[$hash])) {
217 $copyrightsByHash[$hash] = [];
219 $copyrightsByHash[$hash][] = $copyrightKey;
222 foreach ($reusedCopyrights as $reusedCopyright) {
223 $reusedHash = $reusedCopyright[
'hash'];
224 if (isset($copyrightsByHash[$reusedHash]) && !empty($copyrightsByHash[$reusedHash])) {
226 $copyrightKey = array_shift($copyrightsByHash[$reusedHash]);
227 $copyright = $allCopyrights[$copyrightKey];
229 if ($this->
dbManager->booleanFromDb($reusedCopyright[
'is_enabled'])) {
231 $content = $reusedCopyright[
"contentedited"];
236 $hash = $copyright[
'hash'];
238 intval($copyright[
'uploadtree_pk']), $uploadTreeTableName,
239 $copyright[
'upload_fk'], $copyright[
'lft'], $copyright[
'rgt']);
240 $this->copyrightDao->updateTable($item, $hash, $content,
241 $this->userId,
'copyright', $action);
257 protected function processUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId)
262 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
263 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
$groupId);
265 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
268 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
270 $clearingDecisionToImportByFileId = $this->
mapByFileId($clearingDecisionsToImport);
274 $containedItems = ArrayOperation::callChunked(
275 function ($fileIds) use ($itemTreeBounds,
$uploadDao)
279 "pfile_fk = ANY($1)",
280 array(
'{' . implode(
', ', $fileIds) .
'}')
282 }, array_keys($clearingDecisionToImportByFileId), 100);
284 foreach ($containedItems as $item) {
285 $fileId = $item->getFileId();
286 if (empty($fileId)) {
289 if (array_key_exists($fileId, $clearingDecisionToImportByFileId)) {
291 $clearingDecisionToImportByFileId[$fileId]);
293 throw new \Exception(
"bad internal state");
311 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
312 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $this->groupId);
314 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
317 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
319 $sql =
"SELECT ut.* FROM uploadtree ur, uploadtree ut WHERE ur.upload_fk=$2"
320 .
" AND ur.pfile_fk=$3 AND ut.upload_fk=$1 AND ut.ufile_name=ur.ufile_name";
321 $stmt = __METHOD__.
'.reuseByName';
323 $treeDao = $this->container->get(
'dao.tree');
325 foreach ($clearingDecisionsToImport as $clearingDecision) {
326 $reusedPath = $treeDao->getRepoPathOfPfile($clearingDecision->getPfileId());
327 if (empty($reusedPath)) {
332 $res = $this->
dbManager->execute($stmt,array($itemTreeBounds->getUploadId(),
333 $itemTreeBoundsReused->getUploadId(),$clearingDecision->getPfileId()));
334 while ($row = $this->
dbManager->fetchArray($res)) {
335 $newPath = $treeDao->getRepoPathOfPfile($row[
'pfile_fk']);
336 if (empty($newPath)) {
359 $diffLevel = exec(
"diff $reusedPath $newPath | wc -l");
360 if ($diffLevel ===
false) {
361 throw new \Exception(
'cannot use diff tool');
363 if ($diffLevel < 5) {
364 $this->createCopyOfClearingDecision($itemId, $this->userId, $this->groupId, $clearingDecision);
378 $clearingDecisionByFileId = array();
379 foreach ($clearingDecisions as $clearingDecision) {
380 $fileId = $clearingDecision->getPfileId();
381 if (!array_key_exists($fileId, $clearingDecisionByFileId)) {
382 $clearingDecisionByFileId[$fileId] = $clearingDecision;
385 return $clearingDecisionByFileId;
395 protected function createCopyOfClearingDecision($itemId,
$userId,
$groupId, $clearingDecisionToCopy)
397 $clearingEventIdsToCopy = array();
399 foreach ($clearingDecisionToCopy->getClearingEvents() as $clearingEvent) {
400 $licenseId = $clearingEvent->getLicenseId();
401 $uploadTreeId = $itemId;
402 $isRemoved = $clearingEvent->isRemoved();
403 $type = ClearingEventTypes::USER;
404 $reportInfo = $clearingEvent->getReportinfo();
405 $comment = $clearingEvent->getComment();
406 $acknowledgement = $clearingEvent->getAcknowledgement();
408 $clearingEventIdsToCopy[] = $this->clearingDao->insertClearingEvent(
410 $type, $reportInfo, $comment, $acknowledgement,
$jobId);
413 $this->clearingDao->createDecisionFromEvents(
417 $clearingDecisionToCopy->getType(),
418 $clearingDecisionToCopy->getScope(),
419 $clearingEventIdsToCopy
435 foreach ($clearingDecisions as $clearingDecision) {
436 $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