71 use Symfony\Component\Process\Process;
73 include_once(__DIR__ .
"/version.php");
81 const RULES_NOMOS_IN_MONK = 0x1;
82 const RULES_NOMOS_MONK_NINKA = 0x2;
83 const RULES_BULK_REUSE = 0x4;
84 const RULES_WIP_SCANNER_UPDATES = 0x8;
85 const RULES_OJO_NO_CONTRADICTION = 0x10;
86 const RULES_COPYRIGHT_FALSE_POSITIVE = 0x20;
87 const RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER = 0x40;
88 const RULES_LICENSE_TYPE_CONCLUSION = 0x80;
89 const RULES_KOTOBA_NO_CONTRADICTION = 0x100;
90 const RULES_RESO_NO_CONTRADICTION = 0x200;
91 const RULES_ALL = self::RULES_NOMOS_IN_MONK | self::RULES_NOMOS_MONK_NINKA |
92 self::RULES_BULK_REUSE | self::RULES_WIP_SCANNER_UPDATES |
93 self::RULES_OJO_NO_CONTRADICTION | self::RULES_RESO_NO_CONTRADICTION |
94 self::RULES_LICENSE_TYPE_CONCLUSION | self::RULES_KOTOBA_NO_CONTRADICTION;
158 parent::__construct(AGENT_DECIDER_NAME, AGENT_DECIDER_VERSION, AGENT_DECIDER_REV);
160 $this->uploadDao = $this->container->get(
'dao.upload');
161 $this->clearingDao = $this->container->get(
'dao.clearing');
162 $this->highlightDao = $this->container->get(
'dao.highlight');
163 $this->showJobsDao = $this->container->get(
'dao.show_jobs');
164 $this->decisionTypes = $this->container->get(
'decision.types');
165 $this->clearingDecisionProcessor = $this->container->get(
'businessrules.clearing_decision_processor');
166 $this->agentLicenseEventProcessor = $this->container->get(
'businessrules.agent_license_event_processor');
167 $this->copyrightDao = $this->container->get(
'dao.copyright');
168 $this->compatibilityDao = $this->container->get(
'dao.compatibility');
169 $this->licenseDao = $this->container->get(
'dao.license');
171 $this->agentSpecifOptions =
"r:t:";
181 $this->activeRules = array_key_exists(
'r',
$args) ? intval(
$args[
'r']) : self::RULES_ALL;
182 $this->licenseType = array_key_exists(
't',
$args) ?
184 $this->licenseMap =
new LicenseMap($this->
dbManager, $this->groupId, $this->licenseMapUsage);
186 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_COPYRIGHT_FALSE_POSITIVE)== self::RULES_COPYRIGHT_FALSE_POSITIVE)) {
189 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER)== self::RULES_COPYRIGHT_FALSE_POSITIVE_CLUTTER)) {
192 if (array_key_exists(
"r",
$args) && (($this->activeRules&self::RULES_BULK_REUSE)== self::RULES_BULK_REUSE)) {
194 $bulkIds = $this->clearingDao->getPreviousBulkIds($uploadId, $this->groupId, $this->userId);
195 if (count($bulkIds) == 0) {
201 foreach ($bulkIds as $bulkId) {
202 $jqId = $bulkReuser->rerunBulkAndDeciderOnUpload($uploadId, $this->groupId, $this->userId, $bulkId, $jqId);
205 $jqIdRow = $this->showJobsDao->getDataForASingleJob($jqId);
206 while ($this->showJobsDao->getJobStatus($jqId)) {
208 $timeInSec = $this->showJobsDao->getEstimatedTime($jqIdRow[
'jq_job_fk'],
'',0,0,1);
209 if ($timeInSec > $maxTime) {
211 }
else if ($timeInSec < $minTime) {
220 $parentBounds = $this->uploadDao->getParentItemBounds($uploadId);
221 foreach ($this->uploadDao->getContainedItems($parentBounds) as $item) {
239 $itemTreeBounds = $item->getItemTreeBounds();
241 $unMappedMatches = $this->agentLicenseEventProcessor->getLatestScannerDetectedMatches($itemTreeBounds);
244 $lastDecision = $this->clearingDao->getRelevantClearingDecision($itemTreeBounds, $this->groupId);
246 if (
null!==$lastDecision && $lastDecision->getType()==DecisionTypes::IRRELEVANT) {
250 $currentEvents = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $this->groupId);
253 if (
null !== $lastDecision && $projectedScannerMatches
254 && ($this->activeRules & self::RULES_WIP_SCANNER_UPDATES) == self::RULES_WIP_SCANNER_UPDATES) {
255 $licensesFromDecision = array();
256 foreach ($lastDecision->getClearingLicenses() as $clearingLicense) {
257 $licenseIdFromEvent = $this->licenseMap->getProjectedId($clearingLicense->getLicenseId());
258 $licensesFromDecision[$licenseIdFromEvent] = $licenseIdFromEvent;
263 if (
null !== $lastDecision && $markAsWip) {
264 $this->clearingDao->markDecisionAsWip($item->getId(), $this->userId, $this->groupId);
271 if (
null === $lastDecision && count($currentEvents) > 0
272 && ($this->activeRules & self::RULES_KOTOBA_NO_CONTRADICTION) == self::RULES_KOTOBA_NO_CONTRADICTION) {
278 if (
null!==$lastDecision || 0<count($currentEvents)) {
282 $haveDecided =
false;
284 if (!$haveDecided && ($this->activeRules&self::RULES_OJO_NO_CONTRADICTION) == self::RULES_OJO_NO_CONTRADICTION) {
288 if (!$haveDecided && ($this->activeRules&self::RULES_RESO_NO_CONTRADICTION) == self::RULES_RESO_NO_CONTRADICTION) {
292 if (!$haveDecided && ($this->activeRules&self::RULES_NOMOS_IN_MONK) == self::RULES_NOMOS_IN_MONK) {
296 if (!$haveDecided && ($this->activeRules&self::RULES_NOMOS_MONK_NINKA)== self::RULES_NOMOS_MONK_NINKA) {
300 if (!$haveDecided && ($this->activeRules &
301 self::RULES_LICENSE_TYPE_CONCLUSION) ==
302 self::RULES_LICENSE_TYPE_CONCLUSION) {
304 $projectedScannerMatches);
307 if (!$haveDecided && $markAsWip) {
308 $this->clearingDao->markDecisionAsWip($item->getId(), $this->userId, $this->groupId);
311 return ($haveDecided||$markAsWip ? 1 : 0);
322 foreach (array_keys($projectedScannerMatches) as $projectedLicenseId) {
323 if (!array_key_exists($projectedLicenseId, $licensesFromDecision)) {
340 $licenseMatchExists = count($matches) > 0;
341 foreach ($matches as $licenseMatches) {
345 if ($licenseMatchExists) {
347 $this->clearingDecisionProcessor->makeDecisionFromLastEvents(
348 $itemTreeBounds, $this->userId, $this->groupId,
349 DecisionTypes::IDENTIFIED,
false);
350 }
catch (\Exception $e) {
351 echo
"Can not auto decide as file '" .
352 $itemTreeBounds->
getItemId() .
"' contains candidate license.\n";
355 return $licenseMatchExists;
370 $accountedLicenses = array();
371 foreach ($currentEvents as $event) {
372 if ($event->getEventType() != ClearingEventTypes::KOTOBA) {
375 $projectedId = $this->licenseMap->getProjectedId($event->getLicenseId());
376 $accountedLicenses[$projectedId] =
true;
379 foreach (array_keys($projectedScannerMatches) as $projectedLicenseId) {
380 if (!array_key_exists($projectedLicenseId, $accountedLicenses)) {
386 $this->clearingDecisionProcessor->makeDecisionFromLastEvents(
387 $itemTreeBounds, $this->userId, $this->groupId,
388 DecisionTypes::IDENTIFIED,
false);
389 }
catch (\Exception $e) {
390 echo
"Can not auto decide as file '" .
391 $itemTreeBounds->
getItemId() .
"' contains candidate license.\n";
407 $licenseMatchExists = count($matches) > 0;
408 foreach ($matches as $licenseMatches) {
412 if ($licenseMatchExists) {
414 $this->clearingDecisionProcessor->makeDecisionFromLastEvents(
415 $itemTreeBounds, $this->userId, $this->groupId,
416 DecisionTypes::IDENTIFIED,
false);
417 }
catch (\Exception $e) {
418 echo
"Can not auto decide as file '" .
419 $itemTreeBounds->
getItemId() .
"' contains candidate license.\n";
422 return $licenseMatchExists;
435 $canDecide = (count($matches)>0);
437 foreach ($matches as $licenseMatches) {
445 $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $global=
true);
460 $canDecide = (count($matches)>0);
462 foreach ($matches as $licenseMatches) {
470 $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $global=
true);
493 $this->clearingDecisionProcessor
494 ->makeDecisionFromLastEvents($itemTreeBounds, $this->userId,
495 $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::ITEM, [],
true);
509 foreach ($matches as $licenseId => $licenseMatches) {
510 $projectedId = $this->licenseMap->getProjectedId($licenseId);
512 foreach ($licenseMatches as $agent => $agentMatches) {
513 $haveId = array_key_exists($projectedId, $remapped);
514 $haveAgent = $haveId && array_key_exists($agent, $remapped[$projectedId]);
516 $remapped[$projectedId][$agent] = array_merge($remapped[$projectedId][$agent], $agentMatches);
518 $remapped[$projectedId][$agent] = $agentMatches;
533 return ($big[0] >= 0) && ($small[0] >= $big[0]) && ($small[1] <= $big[1]);
543 if (!array_key_exists(
"nomos", $licenseMatches)) {
546 if (!array_key_exists(
"monk", $licenseMatches)) {
550 foreach ($licenseMatches[
"nomos"] as $licenseMatch) {
551 $matchId = $licenseMatch->getLicenseFileId();
552 $nomosRegion = $this->highlightDao->getHighlightRegion($matchId);
555 foreach ($licenseMatches[
"monk"] as $monkLicenseMatch) {
556 $monkRegion = $this->highlightDao->getHighlightRegion($monkLicenseMatch->getLicenseFileId());
577 $scanners = array(
'nomos',
'monk',
'ninka');
579 foreach ($scanners as $scanner) {
580 if (!array_key_exists($scanner, $licenseMatches)) {
583 foreach ($licenseMatches[$scanner] as $licenseMatch) {
584 $licId = $licenseMatch->getLicenseId();
585 $vote[$licId][$scanner] =
true;
589 foreach ($vote as $licId=>$voters) {
590 if (count($voters) != 3) {
605 if (array_key_exists($scanner, $licenseMatches) ===
true) {
608 return $match->getLicenseId();
609 }, $licenseMatches[$scanner]);
622 if (count($findingsByOjo) == 0) {
628 if (count($findingsByOtherScanner) == 0) {
632 foreach ($findingsByOtherScanner as $findingsByScanner) {
633 if (in_array($findingsByScanner, $findingsByOjo) ===
false) {
649 if (count($findingsByReso) == 0) {
655 if (count($findingsByOtherScanner) == 0) {
659 foreach ($findingsByOtherScanner as $findingsByScanner) {
660 if (in_array($findingsByScanner, $findingsByReso) ===
false) {
677 if (empty($uploadId)) {
681 $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
683 $scanJobProxy->createAgentStatus(array(
$agentName));
684 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
685 if (!array_key_exists(
$agentName, $selectedScanners)) {
688 $latestXpAgentId = $selectedScanners[
$agentName];
689 $extrawhere =
' agent_fk='.$latestXpAgentId;
690 $allCopyrights = $this->copyrightDao->getScannerEntries(
'copyright',
691 $uploadTreeTableName, $uploadId,
null, $extrawhere);
693 if (empty($allCopyrights)) {
694 echo
"No copyrights found for upload $uploadId. Skipping false positive deactivation.\n";
698 $copyrightJSON = json_encode($allCopyrights);
699 $tmpFile = tmpfile();
700 $tmpFilePath = stream_get_meta_data($tmpFile)[
'uri'];
701 fwrite($tmpFile, $copyrightJSON);
703 if (empty($deactivatedCopyrightData)) {
707 $deactivatedCopyrights = json_decode($deactivatedCopyrightData,
true);
708 foreach ($deactivatedCopyrights as $deactivatedCopyright) {
709 $item = $deactivatedCopyright[
'uploadtree_pk'];
710 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $uploadTreeTableName);
711 $hash = $deactivatedCopyright[
'hash'];
712 $content = $deactivatedCopyright[
'content'];
713 $cpTable =
'copyright';
714 if ($deactivatedCopyright[
'is_copyright'] ==
"t") {
716 if (array_key_exists(
'decluttered_content', $deactivatedCopyright) &&
717 !empty($deactivatedCopyright[
'decluttered_content'])) {
718 $content = $deactivatedCopyright[
'decluttered_content'];
727 $this->copyrightDao->updateTable($itemTreeBounds, $hash, $content, $this->userId, $cpTable, $action);
740 $clutter_flag): string
742 $script =
"copyrightDeactivationClutterRemovalScript.py";
743 $args = [
"python3", __DIR__ .
"/$script",
"--file", $tmpFilePath];
745 $args[] =
"--clutter";
751 $process =
new Process(
$args);
752 $process->setTimeout(
null);
759 if ($sleepTime < $maxSleepTime) {
762 }
while ($process->isRunning());
764 echo $process->getErrorOutput();
766 return $process->getOutput();
777 $licenseTypes = array_map(
'trim', explode(
',',
778 $SysConf[
'SYSCONFIG'][
'LicenseTypes']));
795 foreach ($licenseMatches as $agentMatch) {
796 foreach ($agentMatch as $agentLicenseMatches) {
797 foreach ($agentLicenseMatches as $licenseMatch) {
798 $shortnames[$licenseMatch->getLicenseId()] = $licenseMatch
799 ->getLicenseRef()->getShortName();
803 foreach ($shortnames as $shortname) {
805 if (! $this->compatibilityDao->getCompatibilityForFile($itemTreeBounds,
824 foreach ($licenseMatches as $agentMatch) {
825 foreach ($agentMatch as $agentLicenseMatches) {
826 foreach ($agentLicenseMatches as $licenseMatch) {
827 if (!array_key_exists($licenseMatch->getLicenseId(), $licenseTypes)) {
828 $licenseTypes[$licenseMatch->getLicenseId()] = $this->licenseDao
829 ->getLicenseType($licenseMatch->getLicenseId());
834 if (! in_array($this->licenseType, $licenseTypes)) {
837 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.
autodecideIfKotobaMatchesNoContradiction(ItemTreeBounds $itemTreeBounds, $projectedScannerMatches, $currentEvents)
Auto-conclude when kotoba events cover all scanner findings.
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']