FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo-restore.php
1 #!/usr/bin/php
2 <?php
3 /***********************************************************
4  Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 /**************************************************************
20  fo-restore
21 
22  Implement restore process.
23 
24  @return 0 for success, 1 for failure.
25  *************************************************************/
26 /* Have to set this or else plugins will not load. */
27 $GlobalReady = 1;
28 /* Load all code */
29 require_once (dirname(__FILE__) . '/../php/pathinclude.php');
30 global $WEBDIR;
31 $UI_CLI = 1; /* this is a command-line program */
32 require_once ("$WEBDIR/common/common.php");
33 cli_Init();
34 error_reporting(E_NOTICE & E_STRICT);
35 
36 
37 /*********************************************************
38  AddReunpackjob(): Given an uploadpk, add a reunpack job.
39  Return $jobpk if Success, or return fail string
40 *********************************************************/
41 
42 function AddReunpackjob ($uploadpk,$Depends=NULL,$priority=0)
43 {
44  global $DB;
45  if (empty($DB)) {
46  return;
47  }
48  $Job_name = str_replace("'", "''", "unpack");
49 
50  $SQLInsert = "INSERT INTO job
51  (job_queued,job_priority,job_name,job_upload_fk) VALUES
52  (now(),'$priority','$Job_name','$uploadpk');";
53 
54  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk' AND job_name = '$Job_name' AND job_user_fk is NULL;";
55  $Results = $DB->Action($SQLcheck);
56  if (!empty($Results)){
57  $jobpk = $Results[0]['job_pk'];
58  } else {
59  $DB->Action($SQLInsert);
60  $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '$uploadpk' AND job_name = '$Job_name' AND job_user_fk is NULL;";
61  $Results = $DB->Action($SQLcheck);
62  $jobpk = $Results[0]['job_pk'];
63  }
64 
65  if (empty($jobpk) || ($jobpk < 0)) { return("Failed to insert job record! $SQLInsert"); }
66  if (!empty($Depends) && !is_array($Depends)) { $Depends = array($Depends); }
67 
68  /* job "unpack" has jobqueue item "unpack" */
69  $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,
70  upload_pk, pfile_fk
71  FROM upload
72  INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk
73  WHERE upload.upload_pk = '$uploadpk';";
74  $jobqueuepk = JobQueueAdd($jobpk,"unpack",$jqargs,"no","pfile",$Depends,1);
75  if (empty($jobqueuepk)) { return("Failed to insert item into job queue"); }
76 
77  return ($jobqueuepk);
78 }/* AddReunpackjob() */
79 
80 
81 global $DB;
82 if (empty($DB)) {
83  return;
84 }
85 $SQL = "SELECT job_pk,jq_pk,job_upload_fk FROM jobqueue
86  INNER JOIN job ON jq_job_fk = job_pk
87  WHERE jq_end_bits = 0 AND jq_starttime IS NOT NULL AND jq_endtime IS NULL AND job_name NOT IN('unpack','wget','fo_notify')
88 ORDER BY job_Pk;";
89 $Results = $DB->Action($SQL);
90 $i = 0;
91 while(!empty($Results[$i]['job_pk'])) {
92  $jq_parent = AddReunpackjob($Results[$i]['job_upload_fk']);
93  print $jq_parent;
94  $jq_child = $Results[$i]['jq_pk'];
95  JobQueueAddDependency($jq_child,$jq_parent);
96  $i++;
97 }
98 return (0);
99 ?>
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
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:36