69 use Symfony\Component\Process\Process;
71 include_once(__DIR__ .
"/version.php");
79 const RULES_NOMOS_IN_MONK = 0x1;
80 const RULES_NOMOS_MONK_NINKA = 0x2;
81 const RULES_BULK_REUSE = 0x4;
82 const RULES_WIP_SCANNER_UPDATES = 0x8;
83 const RULES_OJO_NO_CONTRADICTION = 0x10;
84 const RULES_COPYRIGHT_FALSE_POSITIVE = 0x20;
85 const RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER = 0x40;
86 const RULES_LICENSE_TYPE_CONCLUSION = 0x80;
87 const RULES_ALL = self::RULES_NOMOS_IN_MONK | self::RULES_NOMOS_MONK_NINKA |
88 self::RULES_BULK_REUSE | self::RULES_WIP_SCANNER_UPDATES |
89 self::RULES_OJO_NO_CONTRADICTION | self::RULES_LICENSE_TYPE_CONCLUSION;
153 parent::__construct(AGENT_DECIDER_NAME, AGENT_DECIDER_VERSION, AGENT_DECIDER_REV);
155 $this->uploadDao = $this->container->get(
'dao.upload');
156 $this->clearingDao = $this->container->get(
'dao.clearing');
157 $this->highlightDao = $this->container->get(
'dao.highlight');
158 $this->showJobsDao = $this->container->get(
'dao.show_jobs');
159 $this->decisionTypes = $this->container->get(
'decision.types');
160 $this->clearingDecisionProcessor = $this->container->get(
'businessrules.clearing_decision_processor');
161 $this->agentLicenseEventProcessor = $this->container->get(
'businessrules.agent_license_event_processor');
162 $this->copyrightDao = $this->container->get(
'dao.copyright');
163 $this->compatibilityDao = $this->container->get(
'dao.compatibility');
164 $this->licenseDao = $this->container->get(
'dao.license');
166 $this->agentSpecifOptions =
"r:t:";
176 $this->activeRules = array_key_exists(
'r',
$args) ? intval(
$args[
'r']) : self::RULES_ALL;
177 $this->licenseType = array_key_exists(
't',
$args) ?
179 $this->licenseMap =
new LicenseMap($this->
dbManager, $this->groupId, $this->licenseMapUsage);
181 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_COPYRIGHT_FALSE_POSITIVE)== self::RULES_COPYRIGHT_FALSE_POSITIVE)) {
184 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER)== self::RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER)) {
187 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_BULK_REUSE)== self::RULES_BULK_REUSE)) {
189 $bulkIds = $this->clearingDao->getPreviousBulkIds($uploadId, $this->groupId, $this->userId);
190 if (count($bulkIds) == 0) {
196 foreach ($bulkIds as $bulkId) {
197 $jqId = $bulkReuser->rerunBulkAndDeciderOnUpload($uploadId, $this->groupId, $this->userId, $bulkId, $jqId);
200 $jqIdRow = $this->showJobsDao->getDataForASingleJob($jqId);
201 while ($this->showJobsDao->getJobStatus($jqId)) {
203 $timeInSec = $this->showJobsDao->getEstimatedTime($jqIdRow[
'jq_job_fk'],
'',0,0,1);
204 if ($timeInSec > $maxTime) {
206 }
else if ($timeInSec < $minTime) {
215 $parentBounds = $this->uploadDao->getParentItemBounds($uploadId);
216 foreach ($this->uploadDao->getContainedItems($parentBounds) as $item) {
234 $itemTreeBounds = $item->getItemTreeBounds();
236 $unMappedMatches = $this->agentLicenseEventProcessor->getLatestScannerDetectedMatches($itemTreeBounds);
239 $lastDecision = $this->clearingDao->getRelevantClearingDecision($itemTreeBounds, $this->groupId);
241 if (
null!==$lastDecision && $lastDecision->getType()==DecisionTypes::IRRELEVANT) {
245 $currentEvents = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $this->groupId);
248 if (
null !== $lastDecision && $projectedScannerMatches
249 && ($this->activeRules & self::RULES_WIP_SCANNER_UPDATES) == self::RULES_WIP_SCANNER_UPDATES) {
250 $licensesFromDecision = array();
251 foreach ($lastDecision->getClearingLicenses() as $clearingLicense) {
252 $licenseIdFromEvent = $this->licenseMap->getProjectedId($clearingLicense->getLicenseId());
253 $licensesFromDecision[$licenseIdFromEvent] = $licenseIdFromEvent;
258 if (
null !== $lastDecision && $markAsWip) {
259 $this->clearingDao->markDecisionAsWip($item->getId(), $this->userId, $this->groupId);
263 if (
null!==$lastDecision || 0<count($currentEvents)) {
267 $haveDecided =
false;
269 if (($this->activeRules&self::RULES_OJO_NO_CONTRADICTION) == self::RULES_OJO_NO_CONTRADICTION) {
273 if (!$haveDecided && ($this->activeRules&self::RULES_OJO_NO_CONTRADICTION) == self::RULES_OJO_NO_CONTRADICTION) {
277 if (!$haveDecided && ($this->activeRules&self::RULES_NOMOS_IN_MONK) == self::RULES_NOMOS_IN_MONK) {
281 if (!$haveDecided && ($this->activeRules&self::RULES_NOMOS_MONK_NINKA)== self::RULES_NOMOS_MONK_NINKA) {
285 if (!$haveDecided && ($this->activeRules &
286 self::RULES_LICENSE_TYPE_CONCLUSION) ==
287 self::RULES_LICENSE_TYPE_CONCLUSION) {
289 $projectedScannerMatches);
292 if (!$haveDecided && $markAsWip) {
293 $this->clearingDao->markDecisionAsWip($item->getId(), $this->userId, $this->groupId);
296 return ($haveDecided||$markAsWip ? 1 : 0);
307 foreach (array_keys($projectedScannerMatches) as $projectedLicenseId) {
308 if (!array_key_exists($projectedLicenseId, $licensesFromDecision)) {
325 $licenseMatchExists = count($matches) > 0;
326 foreach ($matches as $licenseMatches) {
330 if ($licenseMatchExists) {
332 $this->clearingDecisionProcessor->makeDecisionFromLastEvents(
333 $itemTreeBounds, $this->userId, $this->groupId,
334 DecisionTypes::IDENTIFIED,
false);
335 }
catch (\Exception $e) {
336 echo
"Can not auto decide as file '" .
337 $itemTreeBounds->
getItemId() .
"' contains candidate license.\n";
340 return $licenseMatchExists;
353 $licenseMatchExists = count($matches) > 0;
354 foreach ($matches as $licenseMatches) {
358 if ($licenseMatchExists) {
360 $this->clearingDecisionProcessor->makeDecisionFromLastEvents(
361 $itemTreeBounds, $this->userId, $this->groupId,
362 DecisionTypes::IDENTIFIED,
false);
363 }
catch (\Exception $e) {
364 echo
"Can not auto decide as file '" .
365 $itemTreeBounds->
getItemId() .
"' contains candidate license.\n";
368 return $licenseMatchExists;
381 $canDecide = (count($matches)>0);
383 foreach ($matches as $licenseMatches) {
391 $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $global=
true);
406 $canDecide = (count($matches)>0);
408 foreach ($matches as $licenseMatches) {
416 $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $global=
true);
439 $this->clearingDecisionProcessor
440 ->makeDecisionFromLastEvents($itemTreeBounds, $this->userId,
441 $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::ITEM, [],
true);
455 foreach ($matches as $licenseId => $licenseMatches) {
456 $projectedId = $this->licenseMap->getProjectedId($licenseId);
458 foreach ($licenseMatches as $agent => $agentMatches) {
459 $haveId = array_key_exists($projectedId, $remapped);
460 $haveAgent = $haveId && array_key_exists($agent, $remapped[$projectedId]);
462 $remapped[$projectedId][$agent] = array_merge($remapped[$projectedId][$agent], $agentMatches);
464 $remapped[$projectedId][$agent] = $agentMatches;
479 return ($big[0] >= 0) && ($small[0] >= $big[0]) && ($small[1] <= $big[1]);
489 if (!array_key_exists(
"nomos", $licenseMatches)) {
492 if (!array_key_exists(
"monk", $licenseMatches)) {
496 foreach ($licenseMatches[
"nomos"] as $licenseMatch) {
497 $matchId = $licenseMatch->getLicenseFileId();
498 $nomosRegion = $this->highlightDao->getHighlightRegion($matchId);
501 foreach ($licenseMatches[
"monk"] as $monkLicenseMatch) {
502 $monkRegion = $this->highlightDao->getHighlightRegion($monkLicenseMatch->getLicenseFileId());
523 $scanners = array(
'nomos',
'monk',
'ninka');
525 foreach ($scanners as $scanner) {
526 if (!array_key_exists($scanner, $licenseMatches)) {
529 foreach ($licenseMatches[$scanner] as $licenseMatch) {
530 $licId = $licenseMatch->getLicenseId();
531 $vote[$licId][$scanner] =
true;
535 foreach ($vote as $licId=>$voters) {
536 if (count($voters) != 3) {
551 if (array_key_exists($scanner, $licenseMatches) ===
true) {
554 return $match->getLicenseId();
555 }, $licenseMatches[$scanner]);
568 if (count($findingsByOjo) == 0) {
574 if (count($findingsByOtherScanner) == 0) {
578 foreach ($findingsByOtherScanner as $findingsByScanner) {
579 if (in_array($findingsByScanner, $findingsByOjo) ===
false) {
595 if (count($findingsByReso) == 0) {
601 if (count($findingsByOtherScanner) == 0) {
605 foreach ($findingsByOtherScanner as $findingsByScanner) {
606 if (in_array($findingsByScanner, $findingsByReso) ===
false) {
623 if (empty($uploadId)) {
627 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
629 $scanJobProxy->createAgentStatus(array(
$agentName));
630 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
631 if (!array_key_exists(
$agentName, $selectedScanners)) {
634 $latestXpAgentId = $selectedScanners[
$agentName];
635 $extrawhere =
' agent_fk='.$latestXpAgentId;
636 $allCopyrights = $this->copyrightDao->getScannerEntries(
'copyright',
637 $uploadTreeTableName, $uploadId,
null, $extrawhere);
639 $copyrightJSON = json_encode($allCopyrights);
640 $tmpFile = tmpfile();
641 $tmpFilePath = stream_get_meta_data($tmpFile)[
'uri'];
642 fwrite($tmpFile, $copyrightJSON);
644 if (empty($deactivatedCopyrightData)) {
648 $deactivatedCopyrights = json_decode($deactivatedCopyrightData,
true);
649 foreach ($deactivatedCopyrights as $deactivatedCopyright) {
650 $item = $deactivatedCopyright[
'uploadtree_pk'];
651 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $uploadTreeTableName);
652 $hash = $deactivatedCopyright[
'hash'];
653 $content = $deactivatedCopyright[
'content'];
654 $cpTable =
'copyright';
655 if ($deactivatedCopyright[
'is_copyright'] ==
"t") {
657 if (array_key_exists(
'decluttered_content', $deactivatedCopyright) &&
658 !empty($deactivatedCopyright[
'decluttered_content'])) {
659 $content = $deactivatedCopyright[
'decluttered_content'];
668 $this->copyrightDao->updateTable($itemTreeBounds, $hash, $content, $this->userId, $cpTable, $action);
681 $clutter_flag): string
683 $script =
"copyrightDeactivationClutterRemovalScript.py";
684 $args = [
"python3", __DIR__ .
"/$script",
"--file", $tmpFilePath];
686 $args[] =
"--clutter";
692 $process =
new Process(
$args);
693 $process->setTimeout(
null);
700 if ($sleepTime < $maxSleepTime) {
703 }
while ($process->isRunning());
705 echo $process->getErrorOutput();
707 return $process->getOutput();
718 $licenseTypes = array_map(
'trim', explode(
',',
719 $SysConf[
'SYSCONFIG'][
'LicenseTypes']));
736 foreach ($licenseMatches as $agentMatch) {
737 foreach ($agentMatch as $agentLicenseMatches) {
738 foreach ($agentLicenseMatches as $licenseMatch) {
739 $shortnames[$licenseMatch->getLicenseId()] = $licenseMatch
740 ->getLicenseRef()->getShortName();
744 foreach ($shortnames as $shortname) {
746 if (! $this->compatibilityDao->getCompatibilityForFile($itemTreeBounds,
765 foreach ($licenseMatches as $agentMatch) {
766 foreach ($agentMatch as $agentLicenseMatches) {
767 foreach ($agentLicenseMatches as $licenseMatch) {
768 if (!array_key_exists($licenseMatch->getLicenseId(), $licenseTypes)) {
769 $licenseTypes[$licenseMatch->getLicenseId()] = $this->licenseDao
770 ->getLicenseType($licenseMatch->getLicenseId());
775 if (! in_array($this->licenseType, $licenseTypes)) {
778 if (! empty(array_diff($licenseTypes, [$this->licenseType]))) {
Prepares bulk licenses for an upload and run DeciderJob on it.
Agent to decide license findings in an upload.
$agentLicenseEventProcessor
existsUnhandledMatch($projectedScannerMatches, $licensesFromDecision)
Check if matches contains unhandled match.
areOtherScannerFindingsAndRESOAgreed($licenseMatches)
Check if the finding by only contains one single license and that no other scanner (nomos) has produc...
$clearingDecisionProcessor
autodecideIfResoMatchesNoContradiction(ItemTreeBounds $itemTreeBounds, $matches)
Auto decide matches which are in nomos, monk, OJO and Reso findings.
processItem(Item $item)
Given an item, check with the $activeRules and apply rules to it.
areOtherScannerFindingsAndOJOAgreed($licenseMatches)
Check if the finding by only contains one single license and that no other scanner (nomos) has produc...
callCopyrightDeactivationClutterRemovalScript($tmpFilePath, $clutter_flag)
autodecideNomosMatchesInsideMonk(ItemTreeBounds $itemTreeBounds, $matches)
Auto decide matches by nomos which are in monk findings.
noLicenseConflict($itemTreeBounds, $licenseMatches)
Check if findings by all agents are same or not.
autodecideNomosMonkNinka(ItemTreeBounds $itemTreeBounds, $matches)
Auto decide matches which are in nomos, monk and ninka findings.
processUploadId($uploadId)
Given an upload ID, process the items in it.
getLicenseType($licenseType)
allLicenseInType($licenseMatches)
Check if findings by all agents are same or not.
autodecideLicenseType(ItemTreeBounds $itemTreeBounds, $matches)
Auto decide matches where there is no license conflict.
getLicenseIdsOfMatchesForScanner($scanner, $licenseMatches)
extracts the matches corresponding to a scanner from a $licenseMatches structure
isRegionIncluded($small, $big)
Check if the small highlight region is inside big one.
autodecideIfOjoMatchesNoContradiction(ItemTreeBounds $itemTreeBounds, $matches)
Auto decide matches which are in nomos, monk and OJO findings.
areNomosMatchesInsideAMonkMatch($licenseMatches)
Check if matches by nomos are inside monk findings.
getCopyrightsToDisableFalsePositivesClutter($uploadId, $clutter_flag)
remapByProjectedId($matches)
Given a set of matches, remap according to project id instead of license id.
areNomosMonkNinkaAgreed($licenseMatches)
Check if findings by all agents are same or not.
Structure of an Agent with all required parameters.
heartbeat($newProcessed)
Send hear beat to the scheduler.
Handle events related to license findings.
Utility functions to process ClearingDecision.
Wrapper class for license map.
Exception when an agent's stage is invalid.
fo_dbManager * dbManager
fo_dbManager object
Namespace for decider agent.
$GLOBALS['xyyzzzDeciderJob']