FOSSology  4.7.1
Open Source License Compliance by Open Source Software
agent-kotoba.php
1 <?php
2 /*
3 SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5 SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
11 {
13  private $kotobaDesc = "Custom phrase analysis agent";
14 
15  public function __construct()
16  {
17  $this->Name = "agent_kotoba";
18  $this->Title = _("Kotoba Analysis <img src=\"images/info_16.png\" data-toggle=\"tooltip\" title=\"".$this->kotobaDesc."\" class=\"info-bullet\"/>");
19  $this->AgentName = "kotoba";
20 
21  parent::__construct();
22  }
23 
28  function AgentHasResults($uploadId = 0)
29  {
30  return CheckARS($uploadId, $this->AgentName, "kotoba scanner", "kotoba_ars");
31  }
32 
33  function AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=[],
34  $arguments=null, $request=null, $unpackArgs=null)
35  {
36  // Handle SCM flag if needed
37  if ($request != null && !is_array($request)) {
38  $unpackArgs = intval($request->get('scm', 0)) == 1 ? '-I' : '';
39  } else {
40  $unpackArgs = intval(@$_POST['scm']) == 1 ? '-I' : '';
41  }
42 
43  // Check if agent already has results for this upload
44  if ($this->AgentHasResults($uploadId) == 1) {
45  return 0;
46  }
47 
48  // Check if already scheduled
49  $jobQueueId = \IsAlreadyScheduled($jobId, $this->AgentName, $uploadId);
50  if ($jobQueueId != 0) {
51  return $jobQueueId;
52  }
53 
54  // Check if there are any active custom phrases
55  if (!$this->hasActiveCustomPhrases()) {
56  $errorMsg = _("No active custom phrases found. Please add and activate custom phrases before running the kotoba bulk agent.");
57  return -1;
58  }
59 
60  // Build dependencies list - start with base dependencies
61  $baseDependencies = array();
62  if (!empty($unpackArgs)) {
63  $baseDependencies[] = "agent_mimetype";
64  } else {
65  $baseDependencies[] = "agent_adj2nest";
66  }
67 
68  // Add license scanner agents as dependencies if they are scheduled for this upload
69  $licenseScannerAgents = array("agent_monk", "agent_nomos", "agent_ojo", "agent_scancode");
70  foreach ($licenseScannerAgents as $agentName) {
71  // Check if agent is scheduled for this upload (in any job)
72  if ($this->isAgentScheduledForUpload($uploadId, $agentName)) {
73  $baseDependencies[] = $agentName;
74  }
75  }
76 
77  // Set dependencies and arguments - pass uploadId directly to agent
78  $args = $unpackArgs;
79  if (!empty($unpackArgs)) {
80  $kotobaJqId = $this->doAgentAdd($jobId, $uploadId, $errorMsg,
81  $baseDependencies, $uploadId, $args, $request);
82  } else {
83  $kotobaJqId = $this->doAgentAdd($jobId, $uploadId, $errorMsg,
84  $baseDependencies, $uploadId, null, $request);
85  }
86 
87  // If kotoba was successfully scheduled, schedule deciderjob agent
88  if ($kotobaJqId > 0) {
89  // Schedule deciderjob agent with kotoba as dependency
90  $deciderJobPlugin = \plugin_find("agent_deciderjob");
91  if ($deciderJobPlugin !== null) {
92  $deciderJobDependencies = array(array('name' => 'agent_kotoba', 'args' => $uploadId));
93  $deciderJobErrorMsg = '';
94  $deciderJobJqId = $deciderJobPlugin->AgentAdd($jobId, $uploadId, $deciderJobErrorMsg, $deciderJobDependencies, null, $request);
95 
96  if (!empty($deciderJobErrorMsg)) {
97  $errorMsg .= " DeciderJob scheduling: " . $deciderJobErrorMsg;
98  }
99 
100  // Return deciderjob queue ID as the final job in the chain
101  if ($deciderJobJqId > 0) {
102  return $deciderJobJqId;
103  }
104  }
105  }
106 
107  return $kotobaJqId;
108  }
109 
114  private function hasActiveCustomPhrases()
115  {
116  global $container;
118  $dbManager = $container->get('db.manager');
119 
120  $sql = "SELECT COUNT(*) as count FROM custom_phrase WHERE is_active = true";
121  $result = $dbManager->getSingleRow($sql);
122 
123  return ($result['count'] > 0);
124  }
125 
132  protected function isAgentIncluded($dependencies, $agentName)
133  {
134  foreach ($dependencies as $dependency) {
135  if ($dependency == $agentName) {
136  return true;
137  }
138  if (is_array($dependency) && $agentName == $dependency['name']) {
139  return true;
140  }
141  }
142  return false;
143  }
144 
151  private function isAgentScheduledForUpload($uploadId, $pluginName)
152  {
153  global $container;
155  $dbManager = $container->get('db.manager');
156 
157  // Get the agent name from the plugin
158  $plugin = plugin_find($pluginName);
159  if ($plugin === null) {
160  return false;
161  }
162  $agentName = $plugin->AgentName;
163 
164  // Check if agent is scheduled for this upload in any job
165  // Similar to IsAlreadyScheduled but checks across all jobs for the upload
166  $sql = "SELECT jq_pk FROM jobqueue, job WHERE job_pk=jq_job_fk " .
167  "AND jq_type=$1 AND job_upload_fk = $2 " .
168  "AND jq_endtime IS NULL";
169  $result = $dbManager->getSingleRow($sql, array($agentName, $uploadId));
170 
171  return !empty($result);
172  }
173 }
174 
175 register_plugin(new KotobaAgentPlugin());
176 
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null, $request=null)
isAgentIncluded($dependencies, $agentName)
AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=[], $arguments=null, $request=null, $unpackArgs=null)
AgentHasResults($uploadId=0)
CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
Check the ARS table to see if an agent has successfully scanned an upload.
IsAlreadyScheduled($job_pk, $AgentName, $upload_pk)
Check if an agent is already scheduled in a job.
Definition: common-job.php:378
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.