FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-bucket-pool.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 define("TITLE_admin_bucket_pool", _("Duplicate Bucketpool"));
14 
20 {
21  function __construct()
22  {
23  $this->Name = "admin_bucket_pool";
24  $this->Title = TITLE_admin_bucket_pool;
25  $this->MenuList = "Admin::Buckets::Duplicate Bucketpool";
26  $this->DBaccess = PLUGIN_DB_ADMIN;
27  parent::__construct();
28  }
29 
41  function CloneBucketpool($bucketpool_pk, $UpdateDefault, &$msg)
42  {
43  global $PG_CONN;
44 
45  /* select the old bucketpool record */
46  $sql = "select * from bucketpool where bucketpool_pk='$bucketpool_pk' ";
47  $result = pg_query($PG_CONN, $sql);
48  DBCheckResult($result, $sql, __FILE__, __LINE__);
49  $row = pg_fetch_assoc($result);
50  pg_free_result($result);
51 
52  /*
53  * Get the last version for this bucketpool name.
54  * There could be a race condition between getting the last version and
55  * inserting the new version, but this is an admin only function and it
56  * would be pretty odd if two admins were modifying the same bucketpool
57  * at the same instant. Besides if this does occur, the loser will just
58  * get a message about the dup record and no harm done.
59  */
60  $sql = "select max(version) as version from bucketpool where bucketpool_name='$row[bucketpool_name]'";
61  $result = pg_query($PG_CONN, $sql);
62  DBCheckResult($result, $sql, __FILE__, __LINE__);
63  $vrow = pg_fetch_assoc($result);
64  pg_free_result($result);
65  $newversion = $vrow['version'] + 1;
66 
67  /* Insert the new bucketpool record */
68  $sql = "insert into bucketpool (bucketpool_name, version, active, description) select bucketpool_name, '$newversion', active, description from bucketpool where bucketpool_pk=$bucketpool_pk";
69  $result = pg_query($PG_CONN, $sql);
70  DBCheckResult($result, $sql, __FILE__, __LINE__);
71  pg_free_result($result);
72 
73  /* Retrieve the new bucketpool_pk */
74  $sql = "select bucketpool_pk from bucketpool where bucketpool_name='$row[bucketpool_name]' and version='$newversion'";
75  $result = pg_query($PG_CONN, $sql);
76  DBCheckResult($result, $sql, __FILE__, __LINE__);
77  $row = pg_fetch_assoc($result);
78  pg_free_result($result);
79  $newbucketpool_pk = $row['bucketpool_pk'];
80 
81  /* duplicate all the bucketdef records for the new bucketpool_pk */
82  $sql = "insert into bucket_def (bucket_name, bucket_color, bucket_reportorder, bucket_evalorder, bucketpool_fk, bucket_type, bucket_regex, bucket_filename, stopon, applies_to)
83 select bucket_name, bucket_color, bucket_reportorder, bucket_evalorder, $newbucketpool_pk, bucket_type, bucket_regex, bucket_filename, stopon, applies_to from bucket_def where bucketpool_fk=$bucketpool_pk";
84  $insertresult = pg_query($PG_CONN, $sql);
85  DBCheckResult($insertresult, $sql, __FILE__, __LINE__);
86  pg_free_result($insertresult);
87 
88  /* Update default bucket pool in user table for this user only */
89  if ($UpdateDefault == 'on')
90  {
91  $sql = "update users set default_bucketpool_fk='$newbucketpool_pk' where user_pk='$_SESSION[UserId]'";
92  $result = pg_query($PG_CONN, $sql);
93  DBCheckResult($result, $sql, __FILE__, __LINE__);
94  pg_free_result($result);
95  }
96 
97  return $newbucketpool_pk;
98  }
99 
114  public function Output()
115  {
116  global $PROJECTSTATEDIR;
117 
118  /* get the bucketpool_pk to clone */
119  $bucketpool_pk = GetParm("default_bucketpool_fk",PARM_INTEGER);
120  $UpdateDefault = GetParm("updatedefault",PARM_RAW);
121 
122  if (!empty($bucketpool_pk))
123  {
124  $msg = "";
125  $newbucketpool_pk = $this->CloneBucketpool($bucketpool_pk, $UpdateDefault, $msg);
126  $text = _("Your new bucketpool_pk is");
127  $this->vars['message'] = "$text $newbucketpool_pk";
128  }
129 
130  $V = "<p>";
131  $V .= _("The purpose of this is to facilitate editing an existing bucketpool. Make sure you understand");
132  $V .= " <a href='https://github.com/fossology/fossology/wiki/Buckets'>";
133  $V .= _("Creating Bucket Pools");
134  $V .= "</a> ";
135  $V .= _("before continuing.");
136  $V .= _(" It will explain why you should create a new bucketpool rather than edit an old one that has already recorded results.");
137  $V .= "<p>";
138  $V .= _("Steps to modify a bucketpool:");
139  $V .= "<ol>";
140  $V .= "<li>";
141  $V .= _("Create a baseline with your current bucketpool. In other words, run a bucket scan on something. If you do this before creating a new modified bucketpool, you can compare the old results with the new to verify it is working as you expect.");
142  $V .= "<li>";
143  $V .= _("Duplicate the bucketpool (this will increment the bucketpool version and its bucketdef records). You should also check 'Update my default bucketpool' since new bucket jobs only use your default bucketpool.");
144  $V .= "<li>";
145  $V .= _("Duplicate any bucket scripts that you defined in $PROJECTSTATEDIR.");
146  $V .= "<li>";
147  $V .= _("Manually edit the new bucketpool record, if desired.");
148  $V .= "<li>";
149  $V .= _("Manually insert/update/delete the new bucketdef records.");
150  $V .= "<li>";
151  $V .= _("Manually insert a new buckets record in the agent table.");
152  $V .= "<li>";
153  $V .= _("Queue up the new bucket job in Jobs > Schedule Agents.");
154  $V .= "<li>";
155  $V .= _("Use Buckets > Compare to compare the new and old runs. Verify the results.");
156  $V .= "<li>";
157  $V .= _("If you still need to edit the buckets, use Buckets > Remove Bucket Results to remove the previous runs results and repeat starting with editing the bucketpool or def records.");
158  $V .= "<li>";
159  $V .= _("When the bucket results are what you want, then you can reset all the users of the old bucketpool to the new one (manual sql step).");
160  $V .= "</ol>";
161  $V .= "<hr>";
162 
163  $V .= "<form method='POST'>";
164  $text = _("Choose the bucketpool to duplicate");
165  $V .= "$text ";
166  $Val = "";
167  $V .= SelectBucketPool($Val);
168 
169  $V .= "<p>";
170  $text = _("Update my default bucketpool");
171  $V .= "<input type='checkbox' name='updatedefault' checked> $text.";
172  $V .= "<p>";
173  $text = _("Submit");
174  $V .= "<input type='submit' value='$text'>";
175  $V .= "</form>";
176  return $V;
177  }
178 }
179 
180 $NewPlugin = new admin_bucket_pool;
181 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
CloneBucketpool($bucketpool_pk, $UpdateDefault, &$msg)
Clone a bucketpool and its bucketdef records. Increment the bucketpool version.
__construct()
base constructor. Most plugins will just use this
Output()
User chooses a bucketpool to duplicate from a select list.
SelectBucketPool($selected, $active='Y')
Return a select list containing all the active bucketpool's.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_RAW
Definition: common-parm.php:22
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN