FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ui-reunpack.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
24 define("TITLE_ui_reunpack", _("Schedule an Reunpack"));
25 
30 class ui_reunpack extends FO_Plugin
31 {
32  public $Name = "ui_reunpack";
33  public $Title = TITLE_ui_reunpack;
34  //public $MenuList = "Jobs::Agents::Reunpack";
35  public $Version = "1.2";
36  public $Dependency = array();
37  public $DBaccess = PLUGIN_DB_WRITE;
38 
43  function Output()
44  {
45  if ($this->State != PLUGIN_STATE_READY) { return; }
46  global $Plugins;
47  $V="";
48  switch($this->OutputType)
49  {
50  case "XML":
51  break;
52  case "HTML":
53  /* If this is a POST, then process the request. */
54  $uploadpk = GetParm('uploadunpack',PARM_INTEGER);
55  if (!empty($uploadpk))
56  {
57  $P = &$Plugins[plugin_find_id("agent_unpack")];
58  $rc = $P->AgentAdd($uploadpk);
59  if (empty($rc))
60  {
61  /* Need to refresh the screen */
62  $text = _("Unpack added to job queue");
63  $V .= displayMessage($text);
64  }
65  else
66  {
67  $text = _("Unpack of Upload failed");
68  $V .= displayMessage("$text: $rc");
69  }
70  }
71 
72  /* Set default values */
73  if (empty($GetURL)) { $GetURL='http://'; }
74 
75  //$V .= $this->ShowReunpackView($uploadtree_pk);
76 
77  break;
78  case "Text":
79  break;
80  default:
81  break;
82  }
83  if (!$this->OutputToStdout) { return($V); }
84  print("$V");
85  return;
86  }
87 
100  function CheckStatus ($uploadpk, $job_name, $jobqueue_type)
101  {
102  global $PG_CONN;
103  $SQLcheck = "SELECT jq_pk,jq_starttime,jq_endtime,jq_end_bits FROM jobqueue
104  LEFT OUTER JOIN job ON jobqueue.jq_job_fk = job.job_pk
105  WHERE job.job_upload_fk = '$uploadpk'
106  AND job.job_name = '$job_name'
107  AND jobqueue.jq_type = '$jobqueue_type' ORDER BY jq_pk DESC;";
108  $result = pg_query($PG_CONN, $SQLcheck);
109  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
110  $i = 0;
111  $State = 0;
112  while ($Row = pg_fetch_assoc($result)) {
113  if ($Row['jq_end_bits'] == 2) {
114  $State = 1;
115  break;
116  }
117  if (!empty($Row['jq_starttime'])) {
118  if (!empty($Row['jq_endtime'])) {
119  $State = 2;
120  } else {
121  $State = 3;
122  break;
123  }
124  } else {
125  $State = 4;
126  break;
127  }
128  $i++;
129  }
130  return ($State);
131 
132  pg_free_result($result);
133  }
134 
146  function AgentAdd ($uploadpk, $Depends=NULL, $priority=0)
147  {
148  global $PG_CONN;
149  $Job_name = str_replace("'", "''", "reunpack");
150 
151  //get userpk from uploadpk
152  $UploadRec = GetSingleRec("upload", "where upload_pk='$uploadpk'");
153  //if upload record didn't have user pk, use current user
154  $user_fk = $UploadRec['user_fk'];
155  if (empty($user_fk))
156  $user_fk = $_SESSION[UserId];
157  //updated ununpack_ars table to let reunpack run
158  $SQLARS = "UPDATE ununpack_ars SET ars_success = FALSE WHERE upload_fk = '$uploadpk';";
159  $result = pg_query($PG_CONN, $SQLARS);
160  DBCheckResult($result, $SQLARS, __FILE__, __LINE__);
161  pg_free_result($result);
162 
163  if (empty($uploadpk)) {
164  $SQLInsert = "INSERT INTO job
165  (job_queued,job_priority,job_name,job_user_fk) VALUES
166  (now(),'$priority','$Job_name','$user_fk');";
167  }
168  else {
169  $SQLInsert = "INSERT INTO job
170  (job_queued,job_priority,job_name,job_upload_fk,job_user_fk) VALUES
171  (now(),'$priority','$Job_name','$uploadpk','$user_fk');";
172  }
173 
174  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk'"
175  . " AND job_name = '$Job_name' AND job_user_fk = '$user_fk' ORDER BY job_pk DESC LIMIT 1;";
176  $result = pg_query($PG_CONN, $SQLcheck);
177  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
178  $row = pg_fetch_assoc($result);
179  pg_free_result($result);
180 
181  if (!empty($row)){
182  $jobpk = $row['job_pk'];
183  } else {
184  $result = pg_query($PG_CONN, $SQLInsert);
185  DBCheckResult($result, $SQLInsert, __FILE__, __LINE__);
186  $row = pg_fetch_assoc($result);
187  pg_free_result($result);
188  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk'"
189  . " AND job_name = '$Job_name' AND job_user_fk = '$user_fk';";
190  $result = pg_query($PG_CONN, $SQLcheck);
191  DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
192  $row = pg_fetch_assoc($result);
193  pg_free_result($result);
194  $jobpk = $row['job_pk'];
195  }
196 
197  if (empty($jobpk) || ($jobpk < 0)) { return("Failed to insert job record! $SQLInsert"); }
198  if (!empty($Depends) && !is_array($Depends)) { $Depends = array($Depends); }
199 
200  /* job "unpack" has jobqueue item "unpack" */
201  $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,
202  upload_pk, pfile_fk
203  FROM upload
204  INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk
205  WHERE upload.upload_pk = '$uploadpk';";
206  echo "JobQueueAdd used to do a reschedule here<br>";
207  $jobqueuepk = JobQueueAdd($jobpk,"ununpack",$uploadpk,NULL,$Depends);
208  if (empty($jobqueuepk)) { return("Failed to insert item into job queue"); }
209 
210  /* Tell the scheduler to check the queue. */
211  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
212  if (!$success)
213  {
214  $ErrorMsg = $error_msg . "\n" . $output;
215  }
216  return(NULL);
217  } // AgentAdd()
218 
225  function ShowReunpackView($Item, $Reunpack=0)
226  {
227  global $PG_CONN;
228  $V = "";
229 
230  $sql = "SELECT upload_fk FROM uploadtree WHERE uploadtree_pk = $Item;";
231  $result = pg_query($PG_CONN, $sql);
232  DBCheckResult($result, $sql, __FILE__, __LINE__);
233  $row = pg_fetch_assoc($result);
234  pg_free_result($result);
235  if (empty($row['upload_fk'])) { return; }
236  $Upload_pk = $row['upload_fk'];
237  $sql = "SELECT pfile_fk,ufile_name from uploadtree where upload_fk=$Upload_pk and parent is NULL;";
238  $result = pg_query($PG_CONN, $sql);
239  DBCheckResult($result, $sql, __FILE__, __LINE__);
240  $row = pg_fetch_assoc($result);
241  pg_free_result($result);
242  if (empty($row['pfile_fk'])) { return; }
243  $Pfile_fk = $row['pfile_fk'];
244  $Ufile_name = $row['ufile_name'];
245 
246  $Fin_gold = @fopen( RepPath($Pfile_fk,"gold") ,"rb");
247  if (empty($Fin_gold))
248  {
249  $text = _("The File's Gold file is not available in the repository.");
250  $V = "<p/>$text\n";
251  return $V;
252  }
253 
254  $V = "<p/>";
255  $text = _("This file is unpacked from");
256  $V.= "$text <font color='blue'>[".$Ufile_name."]</font>\n";
257 
258  /* Display the form */
259  $V .= "<form method='post'>\n"; // no url = this url
260 
261  $text = _("Reunpack");
262  $V .= "<p />\n$text: " . $Ufile_name . "<input name='uploadunpack' type='hidden' value='$Upload_pk'/>\n";
263  $V .= "<input type='submit' value='$text!' ";
264  if ($Reunpack) {$V .= "disabled";}
265  $V .= " >\n";
266  $V .= "</form>\n";
267 
268  return $V;
269  }
270 }
271 $NewPlugin = new ui_reunpack;
272 
273 ?>
displayMessage($Message, $keep=null)
Display a message.
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:159
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:102
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
Definition: common-repo.php:69
CheckStatus($uploadpk, $job_name, $jobqueue_type)
Given an uploadpk and job_name to check if an reunpack/rewget job is running.
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
ShowReunpackView($Item, $Reunpack=0)
Generate the reunpack view page. Give the unploadtree_pk, return page view output.
Scheduler an reunpack.
Definition: ui-reunpack.php:30
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
const PARM_INTEGER
Definition: common-parm.php:25
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
AgentAdd($uploadpk, $Depends=NULL, $priority=0)
Given an uploadpk, add a job.
Output()
This function is called when user output is requested. This function is responsible for content...
Definition: ui-reunpack.php:43
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198