FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-buckets.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
34 function SelectBucketDataset($upload_pk, &$ars_pk, $id="selectbucketdataset", $extra="")
35 {
36  global $PG_CONN;
37 
38  /* schedule bucket */
39  $NoData = ActiveHTTPscript("Schedule");
40  $NoData .= "<script language='javascript'>\n";
41  $NoData .= "function Schedule_Reply()\n";
42  $NoData .= " {\n";
43  $NoData .= " if ((Schedule.readyState==4) && (Schedule.status==200))\n";
44  $NoData .= " document.getElementById('msgdiv').innerHTML = Schedule.responseText;\n";
45  $NoData .= " }\n";
46  $NoData .= "</script>\n";
47 
48  $NoData .= "<form name='formy' method='post'>\n";
49  $NoData .= "<div id='msgdiv'>\n";
50  $NoData .= _("No data available.");
51  $NoData .= "<input type='button' name='scheduleAgent' value='Schedule Agent'";
52  $NoData .= "onClick='Schedule_Get(\"" . Traceback_uri() . "?mod=schedule_agent&upload=$upload_pk&agent=agent_bucket \")'>\n";
53  $NoData .= "</input>";
54  $NoData .= "</div> \n";
55  $NoData .= "</form>\n";
56 
57  $name = $id;
58  $select = "<select name='$name' id='$id' $extra>";
59 
60  /* get the bucketpool recs */
61  $sql = "select ars_pk, bucketpool_pk, bucketpool_name, version from bucketpool, bucket_ars where active='Y' and bucketpool_fk=bucketpool_pk and ars_success=True and upload_fk='$upload_pk' order by ars_starttime desc";
62  $result = pg_query($PG_CONN, $sql);
63  DBCheckResult($result, $sql, __FILE__, __LINE__);
64  $NumRows = pg_num_rows($result);
65  if ($NumRows == 0) {
66  return $NoData;
67  }
68  $rows = pg_fetch_all($result);
69  pg_free_result($result);
70  if ($NumRows == 1) {
71  $ars_pk = $rows[0]['ars_pk'];
72  return ""; /* only one row */
73  }
74 
75  /* Find the users default_bucketpool_fk */
76  $sql = "select default_bucketpool_fk from users where user_pk='$_SESSION[UserId]'";
77  $result = pg_query($PG_CONN, $sql);
78  DBCheckResult($result, $sql, __FILE__, __LINE__);
79  $row = pg_fetch_assoc($result);
80  $DefaultBucketpool_pk = $row['default_bucketpool_fk'];
81  pg_free_result($result);
82 
83  /* Find the default selected row if ars_pk wasn't passed in */
84  if (empty($ars_pk)) {
85  foreach ($rows as $row) {
86  if ($row['bucketpool_pk'] == $DefaultBucketpool_pk) {
87  $ars_pk = $row['ars_pk'];
88  break;
89  }
90  }
91  reset($rows);
92  }
93 
94  // $select .= "<option value=''";
95  foreach ($rows as $row) {
96  $select .= "<option value='$row[ars_pk]'";
97 
98  if (empty($ars_pk)) {
99  $select .= " SELECTED ";
100  $ars_pk = $row["ars_pk"];
101  } else if ($ars_pk == $row['ars_pk']) {
102  $select .= " SELECTED ";
103  }
104 
105  $select .= ">$row[bucketpool_name], v $row[version]\n";
106  }
107  $select .= "</select>";
108  return $select;
109 }
110 
121 function SelectBucketPool($selected, $active='Y')
122 {
123  global $PG_CONN;
124 
125  $id = "default_bucketpool_fk";
126  $name = $id;
127  $select = "<select name='$name' id='$id' class='ui-render-select2'>";
128 
129  /* get the bucketpool recs */
130  if ($active == 'Y') {
131  $Where = "where active='Y'";
132  } else {
133  $Where = "";
134  }
135  $sql = "select * from bucketpool $Where order by bucketpool_name asc,version desc";
136  $result = pg_query($PG_CONN, $sql);
137  DBCheckResult($result, $sql, __FILE__, __LINE__);
138 
139  while ($row = pg_fetch_assoc($result)) {
140  $select .= "<option value='$row[bucketpool_pk]'";
141  if ($row['bucketpool_pk'] == $selected) {
142  $select .= " SELECTED ";
143  }
144  $select .= ">$row[bucketpool_name], v $row[version]</option>\n";
145  }
146  $select .= "</select>";
147  return $select;
148 }
149 
162 function GetFileBuckets($nomosagent_pk, $bucketagent_pk, $uploadtree_pk, $bucketpool_pk)
163 {
164  global $PG_CONN;
165  $BuckArray = array();
166 
167  if (empty($nomosagent_pk) || empty($bucketagent_pk) || empty($uploadtree_pk)) {
168  Fatal(
169  "Missing parameter: nomosagent_pk $nomosagent_pk, bucketagent_pk: $bucketagent_pk, uploadtree_pk: $uploadtree_pk<br>",
170  __FILE__, __LINE__);
171  }
172 
173  /* Find lft and rgt bounds for this $uploadtree_pk */
174  $sql = "SELECT lft,rgt,upload_fk FROM uploadtree
175  WHERE uploadtree_pk = $uploadtree_pk";
176  $result = pg_query($PG_CONN, $sql);
177  DBCheckResult($result, $sql, __FILE__, __LINE__);
178  if (pg_num_rows($result) < 1) {
179  pg_free_result($result);
180  return $BuckArray;
181  }
182  $row = pg_fetch_assoc($result);
183  $lft = $row["lft"];
184  $rgt = $row["rgt"];
185  $upload_pk = $row["upload_fk"];
186  pg_free_result($result);
187 
188  /*select all the buckets for this tree */
189  $sql = "SELECT distinct(bucket_fk) as bucket_pk
190  from bucket_file, bucket_def,
191  (SELECT distinct(pfile_fk) as PF from uploadtree
192  where upload_fk=$upload_pk
193  and ((ufile_mode & (1<<28))=0)
194  and uploadtree.lft BETWEEN $lft and $rgt) as SS
195  where PF=pfile_fk and agent_fk=$bucketagent_pk
196  and bucket_file.nomosagent_fk=$nomosagent_pk
197  and bucket_pk=bucket_fk
198  and bucketpool_fk=$bucketpool_pk";
199 
200  $result = pg_query($PG_CONN, $sql);
201  DBCheckResult($result, $sql, __FILE__, __LINE__);
202  while ($row = pg_fetch_assoc($result)) {
203  $BuckArray[] = $row['bucket_pk'];
204  }
205  pg_free_result($result);
206 
207  return $BuckArray;
208 }
209 
210 
226 function GetFileBuckets_string($nomosagent_pk, $bucketagent_pk, $uploadtree_pk,
227  $bucketDefArray, $delimiter, $color)
228 {
229  $outstr = "";
230  $defrec = current($bucketDefArray);
231  $bucketpool_pk = $defrec['bucketpool_fk'];
232  $BuckArray = GetFileBuckets($nomosagent_pk, $bucketagent_pk, $uploadtree_pk, $bucketpool_pk);
233  if (empty($BuckArray)) {
234  return "";
235  }
236 
237  /* convert array of bucket_pk's to array of bucket names */
238  $BuckNames = array();
239  foreach ($BuckArray as $bucket_pk) {
240  $BuckNames[$bucket_pk] = $bucketDefArray[$bucket_pk]['bucket_name'];
241  }
242 
243  /* sort $BuckArray */
244  natcasesort($BuckNames);
245 
246  $first = true;
247  foreach ($BuckNames as $bucket_name) {
248  if ($first) {
249  $first = false;
250  } else {
251  $outstr .= $delimiter . " ";
252  }
253 
254  if ($color) {
255  $bucket_pk = array_search($bucket_name, $BuckNames);
256  $bucket_color = $bucketDefArray[$bucket_pk]['bucket_color'];
257  $outstr .= "<span style='background-color:$bucket_color'>";
258  $outstr .= $bucket_name;
259  $outstr .= "</span>";
260  } else {
261  $outstr .= $bucket_name;
262  }
263  }
264 
265  return $outstr;
266 }
267 
268 
279 function BucketInTree($bucket_pk, $uploadtree_pk)
280 {
281  global $PG_CONN;
282  $BuckArray = array();
283 
284  if (empty($bucket_pk) || empty($uploadtree_pk)) {
285  Fatal(
286  "Missing parameter: bucket_pk: $bucket_pk, uploadtree_pk: $uploadtree_pk<br>",
287  __FILE__, __LINE__);
288  }
289 
290  /* Find lft and rgt bounds for this $uploadtree_pk */
291  $sql = "SELECT lft,rgt, upload_fk FROM uploadtree WHERE uploadtree_pk = $uploadtree_pk";
292  $result = pg_query($PG_CONN, $sql);
293  DBCheckResult($result, $sql, __FILE__, __LINE__);
294  if (pg_num_rows($result) < 1) {
295  pg_free_result($result);
296  return false;
297  }
298  $row = pg_fetch_assoc($result);
299  $lft = $row["lft"];
300  $rgt = $row["rgt"];
301  $upload_fk = $row["upload_fk"];
302  pg_free_result($result);
303 
304  /* search for bucket in tree */
305  $sql = "SELECT bucket_fk from bucket_file,
306  (SELECT distinct(pfile_fk) as PF from uploadtree
307  where uploadtree.lft BETWEEN $lft and $rgt and upload_fk='$upload_fk') as SS
308  where PF=pfile_fk and bucket_fk='$bucket_pk' limit 1";
309 
310  $result = pg_query($PG_CONN, $sql);
311  DBCheckResult($result, $sql, __FILE__, __LINE__);
312  if (pg_num_rows($result) == 0) {
313  $rv = false;
314  } else {
315  $rv = true;
316  }
317  pg_free_result($result);
318 
319  return $rv;
320 }
321 
322 
330 function initBucketDefArray($bucketpool_pk)
331 {
332  global $PG_CONN;
333 
334  $sql = "select * from bucket_def where bucketpool_fk=$bucketpool_pk";
335  $result_name = pg_query($PG_CONN, $sql);
336  DBCheckResult($result_name, $sql, __FILE__, __LINE__);
337  $bucketDefArray = array();
338  while ($name_row = pg_fetch_assoc($result_name)) {
339  $bucketDefArray[$name_row['bucket_pk']] = $name_row;
340  }
341  pg_free_result($result_name);
342  return $bucketDefArray;
343 }
344 
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
SelectBucketPool($selected, $active='Y')
Return a select list containing all the active bucketpool's.
initBucketDefArray($bucketpool_pk)
Initializes array of bucket_def records.
GetFileBuckets_string($nomosagent_pk, $bucketagent_pk, $uploadtree_pk, $bucketDefArray, $delimiter, $color)
Get string of $delimiter delimited bucket names for the given inputs. Args are same as GetFileBuckets...
SelectBucketDataset($upload_pk, &$ars_pk, $id="selectbucketdataset", $extra="")
Return a select list showing all the successful bucket runs on a particular $upload_pk.
GetFileBuckets($nomosagent_pk, $bucketagent_pk, $uploadtree_pk, $bucketpool_pk)
Get all the unique bucket_pk's for a given uploadtree_pk and for a given nomos and bucket agent.
BucketInTree($bucket_pk, $uploadtree_pk)
Check if a bucket_pk is found in a tree for a given nomos and bucket agent.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:66
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN