FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-reunpack.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
12 define("TITLE_ui_reunpack", _("Schedule an Reunpack"));
13 
18 class ui_reunpack extends FO_Plugin
19 {
20  public $Name = "ui_reunpack";
21  public $Title = TITLE_ui_reunpack;
22  //public $MenuList = "Jobs::Agents::Reunpack";
23  public $Version = "1.2";
24  public $Dependency = array();
25  public $DBaccess = PLUGIN_DB_WRITE;
26 
31  function Output()
32  {
33  if ($this->State != PLUGIN_STATE_READY) { return; }
34  global $Plugins;
35  $V="";
36  switch($this->OutputType)
37  {
38  case "XML":
39  break;
40  case "HTML":
41  /* If this is a POST, then process the request. */
42  $uploadpk = GetParm('uploadunpack',PARM_INTEGER);
43  if (!empty($uploadpk))
44  {
45  $P = &$Plugins[plugin_find_id("agent_unpack")];
46  $rc = $P->AgentAdd($uploadpk);
47  if (empty($rc))
48  {
49  /* Need to refresh the screen */
50  $text = _("Unpack added to job queue");
51  $V .= displayMessage($text);
52  }
53  else
54  {
55  $text = _("Unpack of Upload failed");
56  $V .= displayMessage("$text: $rc");
57  }
58  }
59 
60  /* Set default values */
61  if (empty($GetURL)) { $GetURL='http://'; }
62 
63  //$V .= $this->ShowReunpackView($uploadtree_pk);
64 
65  break;
66  case "Text":
67  break;
68  default:
69  break;
70  }
71  if (!$this->OutputToStdout) { return($V); }
72  print("$V");
73  return;
74  }
75 
89  function CheckStatus ($uploadpk, $job_name, $jobqueue_type)
90  {
91  global $PG_CONN;
92  $SQLcheck = "SELECT jq_pk,jq_starttime,jq_endtime,jq_end_bits FROM jobqueue
93  LEFT OUTER JOIN job ON jobqueue.jq_job_fk = job.job_pk
94  WHERE job.job_upload_fk = '$uploadpk'
95  AND job.job_name = '$job_name'
96  AND jobqueue.jq_type = '$jobqueue_type' ORDER BY jq_pk DESC;";
97  $result = pg_query($PG_CONN, $SQLcheck);
98  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
99  $i = 0;
100  $State = 0;
101  while ($Row = pg_fetch_assoc($result)) {
102  if ($Row['jq_end_bits'] == 2) {
103  $State = 1;
104  break;
105  }
106  if (!empty($Row['jq_starttime'])) {
107  if (!empty($Row['jq_endtime'])) {
108  $State = 2;
109  } else {
110  $State = 3;
111  break;
112  }
113  } else {
114  $State = 4;
115  break;
116  }
117  $i++;
118  }
119  return ($State);
120 
121  pg_free_result($result);
122  }
123 
135  function AgentAdd ($uploadpk, $Depends=NULL, $priority=0)
136  {
137  global $PG_CONN;
138  $Job_name = str_replace("'", "''", "reunpack");
139 
140  //get userpk from uploadpk
141  $UploadRec = GetSingleRec("upload", "where upload_pk='$uploadpk'");
142  //if upload record didn't have user pk, use current user
143  $user_fk = $UploadRec['user_fk'];
144  if (empty($user_fk))
145  $user_fk = $_SESSION[UserId];
146  //updated ununpack_ars table to let reunpack run
147  $SQLARS = "UPDATE ununpack_ars SET ars_success = FALSE WHERE upload_fk = '$uploadpk';";
148  $result = pg_query($PG_CONN, $SQLARS);
149  DBCheckResult($result, $SQLARS, __FILE__, __LINE__);
150  pg_free_result($result);
151 
152  if (empty($uploadpk)) {
153  $SQLInsert = "INSERT INTO job
154  (job_queued,job_priority,job_name,job_user_fk) VALUES
155  (now(),'$priority','$Job_name','$user_fk');";
156  }
157  else {
158  $SQLInsert = "INSERT INTO job
159  (job_queued,job_priority,job_name,job_upload_fk,job_user_fk) VALUES
160  (now(),'$priority','$Job_name','$uploadpk','$user_fk');";
161  }
162 
163  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk'"
164  . " AND job_name = '$Job_name' AND job_user_fk = '$user_fk' ORDER BY job_pk DESC LIMIT 1;";
165  $result = pg_query($PG_CONN, $SQLcheck);
166  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
167  $row = pg_fetch_assoc($result);
168  pg_free_result($result);
169 
170  if (empty($row)) {
171  $result = pg_query($PG_CONN, $SQLInsert);
172  DBCheckResult($result, $SQLInsert, __FILE__, __LINE__);
173  $row = pg_fetch_assoc($result);
174  pg_free_result($result);
175  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk'"
176  . " AND job_name = '$Job_name' AND job_user_fk = '$user_fk';";
177  $result = pg_query($PG_CONN, $SQLcheck);
178  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
179  $row = pg_fetch_assoc($result);
180  pg_free_result($result);
181  }
182  $jobpk = $row['job_pk'];
183 
184  if (empty($jobpk) || ($jobpk < 0)) { return("Failed to insert job record! $SQLInsert"); }
185  if (!empty($Depends) && !is_array($Depends)) { $Depends = array($Depends); }
186 
187  /* job "unpack" has jobqueue item "unpack" */
188  $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,
189  upload_pk, pfile_fk
190  FROM upload
191  INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk
192  WHERE upload.upload_pk = '$uploadpk';";
193  echo "JobQueueAdd used to do a reschedule here<br>";
194  $jobqueuepk = JobQueueAdd($jobpk,"ununpack",$uploadpk,NULL,$Depends);
195  if (empty($jobqueuepk)) { return("Failed to insert item into job queue"); }
196 
197  /* Tell the scheduler to check the queue. */
198  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
199  if (!$success)
200  {
201  $ErrorMsg = $error_msg . "\n" . $output;
202  }
203  return(NULL);
204  } // AgentAdd()
205 
212  function ShowReunpackView($Item, $Reunpack=0)
213  {
214  global $PG_CONN;
215  $V = "";
216 
217  $sql = "SELECT upload_fk FROM uploadtree WHERE uploadtree_pk = $Item;";
218  $result = pg_query($PG_CONN, $sql);
219  DBCheckResult($result, $sql, __FILE__, __LINE__);
220  $row = pg_fetch_assoc($result);
221  pg_free_result($result);
222  if (empty($row['upload_fk'])) { return; }
223  $Upload_pk = $row['upload_fk'];
224  $sql = "SELECT pfile_fk,ufile_name from uploadtree where upload_fk=$Upload_pk and parent is NULL;";
225  $result = pg_query($PG_CONN, $sql);
226  DBCheckResult($result, $sql, __FILE__, __LINE__);
227  $row = pg_fetch_assoc($result);
228  pg_free_result($result);
229  if (empty($row['pfile_fk'])) { return; }
230  $Pfile_fk = $row['pfile_fk'];
231  $Ufile_name = $row['ufile_name'];
232 
233  $Fin_gold = @fopen( RepPath($Pfile_fk,"gold") ,"rb");
234  if (empty($Fin_gold))
235  {
236  $text = _("The File's Gold file is not available in the repository.");
237  return "<p/>$text\n";
238  }
239 
240  $V = "<p/>";
241  $text = _("This file is unpacked from");
242  $V.= "$text <font color='blue'>[".$Ufile_name."]</font>\n";
243 
244  /* Display the form */
245  $V .= "<form method='post'>\n"; // no url = this url
246 
247  $text = _("Reunpack");
248  $V .= "<p />\n$text: " . $Ufile_name . "<input name='uploadunpack' type='hidden' value='$Upload_pk'/>\n";
249  $V .= "<input type='submit' value='$text!' ";
250  if ($Reunpack) {$V .= "disabled";}
251  $V .= " >\n";
252  $V .= "</form>\n";
253 
254  return $V;
255  }
256 }
257 $NewPlugin = new ui_reunpack;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Definition: state.hpp:16
Scheduler an reunpack.
Definition: ui-reunpack.php:19
CheckStatus($uploadpk, $job_name, $jobqueue_type)
Given an uploadpk and job_name to check if an reunpack/rewget job is running.
Definition: ui-reunpack.php:89
AgentAdd($uploadpk, $Depends=NULL, $priority=0)
Given an uploadpk, add a job.
ShowReunpackView($Item, $Reunpack=0)
Generate the reunpack view page. Give the unploadtree_pk, return page view output.
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: ui-reunpack.php:31
displayMessage($Message, $keep=null)
Display a message.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
Definition: common-repo.php:58
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN