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);
440 $this->clearingDecisionProcessor
441 ->makeDecisionFromLastEvents($itemTreeBounds, $this->userId,
442 $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::ITEM);
456 foreach ($matches as $licenseId => $licenseMatches) {
457 $projectedId = $this->licenseMap->getProjectedId($licenseId);
459 foreach ($licenseMatches as $agent => $agentMatches) {
460 $haveId = array_key_exists($projectedId, $remapped);
461 $haveAgent = $haveId && array_key_exists($agent, $remapped[$projectedId]);
463 $remapped[$projectedId][$agent] = array_merge($remapped[$projectedId][$agent], $agentMatches);
465 $remapped[$projectedId][$agent] = $agentMatches;
480 return ($big[0] >= 0) && ($small[0] >= $big[0]) && ($small[1] <= $big[1]);
490 if (!array_key_exists(
"nomos", $licenseMatches)) {
493 if (!array_key_exists(
"monk", $licenseMatches)) {
497 foreach ($licenseMatches[
"nomos"] as $licenseMatch) {
498 $matchId = $licenseMatch->getLicenseFileId();
499 $nomosRegion = $this->highlightDao->getHighlightRegion($matchId);
502 foreach ($licenseMatches[
"monk"] as $monkLicenseMatch) {
503 $monkRegion = $this->highlightDao->getHighlightRegion($monkLicenseMatch->getLicenseFileId());
524 $scanners = array(
'nomos',
'monk',
'ninka');
526 foreach ($scanners as $scanner) {
527 if (!array_key_exists($scanner, $licenseMatches)) {
530 foreach ($licenseMatches[$scanner] as $licenseMatch) {
531 $licId = $licenseMatch->getLicenseId();
532 $vote[$licId][$scanner] =
true;
536 foreach ($vote as $licId=>$voters) {
537 if (count($voters) != 3) {
552 if (array_key_exists($scanner, $licenseMatches) ===
true) {
555 return $match->getLicenseId();
556 }, $licenseMatches[$scanner]);
569 if (count($findingsByOjo) == 0) {
575 if (count($findingsByOtherScanner) == 0) {
579 foreach ($findingsByOtherScanner as $findingsByScanner) {
580 if (in_array($findingsByScanner, $findingsByOjo) ===
false) {
596 if (count($findingsByReso) == 0) {
602 if (count($findingsByOtherScanner) == 0) {
606 foreach ($findingsByOtherScanner as $findingsByScanner) {
607 if (in_array($findingsByScanner, $findingsByReso) ===
false) {
624 if (empty($uploadId)) {
628 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
630 $scanJobProxy->createAgentStatus(array(
$agentName));
631 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
632 if (!array_key_exists(
$agentName, $selectedScanners)) {
635 $latestXpAgentId = $selectedScanners[
$agentName];
636 $extrawhere =
' agent_fk='.$latestXpAgentId;
637 $allCopyrights = $this->copyrightDao->getScannerEntries(
'copyright',
638 $uploadTreeTableName, $uploadId,
null, $extrawhere);
640 $copyrightJSON = json_encode($allCopyrights);
641 $tmpFile = tmpfile();
642 $tmpFilePath = stream_get_meta_data($tmpFile)[
'uri'];
643 fwrite($tmpFile, $copyrightJSON);
645 if (empty($deactivatedCopyrightData)) {
649 $deactivatedCopyrights = json_decode($deactivatedCopyrightData,
true);
650 foreach ($deactivatedCopyrights as $deactivatedCopyright) {
651 $item = $deactivatedCopyright[
'uploadtree_pk'];
652 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $uploadTreeTableName);
653 $hash = $deactivatedCopyright[
'hash'];
654 $content = $deactivatedCopyright[
'content'];
655 $cpTable =
'copyright';
656 if ($deactivatedCopyright[
'is_copyright'] ==
"t") {
658 if (array_key_exists(
'decluttered_content', $deactivatedCopyright) &&
659 !empty($deactivatedCopyright[
'decluttered_content'])) {
660 $content = $deactivatedCopyright[
'decluttered_content'];
669 $this->copyrightDao->updateTable($itemTreeBounds, $hash, $content, $this->userId, $cpTable, $action);
682 $clutter_flag): string
684 $script =
"copyrightDeactivationClutterRemovalScript.py";
685 $args = [
"python3", __DIR__ .
"/$script",
"--file", $tmpFilePath];
687 $args[] =
"--clutter";
693 $process =
new Process(
$args);
694 $process->setTimeout(
null);
701 if ($sleepTime < $maxSleepTime) {
704 }
while ($process->isRunning());
706 echo $process->getErrorOutput();
708 return $process->getOutput();
719 $licenseTypes = array_map(
'trim', explode(
',',
720 $SysConf[
'SYSCONFIG'][
'LicenseTypes']));
737 foreach ($licenseMatches as $agentMatch) {
738 foreach ($agentMatch as $agentLicenseMatches) {
739 foreach ($agentLicenseMatches as $licenseMatch) {
740 $shortnames[$licenseMatch->getLicenseId()] = $licenseMatch
741 ->getLicenseRef()->getShortName();
745 foreach ($shortnames as $shortname) {
747 if (! $this->compatibilityDao->getCompatibilityForFile($itemTreeBounds,
766 foreach ($licenseMatches as $agentMatch) {
767 foreach ($agentMatch as $agentLicenseMatches) {
768 foreach ($agentLicenseMatches as $licenseMatch) {
769 if (!array_key_exists($licenseMatch->getLicenseId(), $licenseTypes)) {
770 $licenseTypes[$licenseMatch->getLicenseId()] = $this->licenseDao
771 ->getLicenseType($licenseMatch->getLicenseId());
776 if (! in_array($this->licenseType, $licenseTypes)) {
779 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.
char * trim(char *ptext)
Trimming whitespace.
fo_dbManager * dbManager
fo_dbManager object
Namespace for decider agent.
$GLOBALS['xyyzzzDeciderJob']