FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_tagfile.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
22 $GlobalReady = 1;
23 /* Load all code */
24 require_once "/usr/share/fossology/php/pathinclude.php";
25 global $WEBDIR;
26 $UI_CLI = 1; /* this is a command-line program */
27 require_once ("$WEBDIR/common/common.php");
28 
29 function Usage($argc, $argv)
30 {
31  echo "$argv[0] -m -f {file of pathnames} -u {original upload_pk} -t {tag_pk}\n";
32  echo " -m means to only print out missing files. Do not update the db.\n";
33 }
34 
41 function Strip2Path($RawFilePath)
42 {
43  if (empty($RawFilePath)) {
44  return "";
45  }
46 
47  /* strip to first slash */
48  $FirstSlashPos = strpos($RawFilePath, "/");
49  $StartStr = substr($RawFilePath, $FirstSlashPos);
50 
51  $FParts = explode(" ", $StartStr, 3);
52 
53  /* strip trailing text after path */
54  $fPath = strtok($FParts[0], " \t");
55  return $fPath;
56 }
57 
68 function Path2Uploadtree($upload_pk, $FilePath)
69 {
70  global $PG_CONN;
71 
72  $FileName = basename($FilePath);
73 
74  /* create the cleaned up file path string
75  * note delimiters are not put in the string since they are not needed for
76  * the string compare.
77  */
78  $FilePathArray = explode('/', $FilePath);
79  $FilePathStr = "";
80  foreach ($FilePathArray as $name) {
81  if (empty($name)) {
82  continue;
83  }
84  $FilePathStr .= $name;
85  }
86 
87  $sql = "SELECT * from uploadtree where upload_fk='$upload_pk' and ufile_name='$FileName'";
88  $result = pg_query($PG_CONN, $sql);
89  DBCheckResult($result, $sql, __FILE__, __LINE__);
90  if (pg_num_rows($result) == 0) {
91  return false;
92  }
93 
94  /* Get the uploadtree recs for this file name */
95  while ($row = pg_fetch_assoc($result)) {
96  $sql = "select ufile_name from uploadtree2path('$row[uploadtree_pk]')";
97  $PathResult = pg_query($PG_CONN, $sql);
98  DBCheckResult($PathResult, $sql, __FILE__, __LINE__);
99 
100  /* Check each uploadtree rec to see if the path matches */
101  $SelectedPathStr = "";
102  while ($PathRow = pg_fetch_assoc($PathResult)) {
103  $SelectedPathStr = $PathRow['ufile_name'] . $SelectedPathStr;
104  }
105  pg_free_result($PathResult);
106 
107  /* do the paths match? */
108  if ($FilePathStr == $SelectedPathStr) {
109  pg_free_result($result);
110  return $row;
111  }
112  }
113  pg_free_result($result);
114  return false;
115 }
116 
117 
122 function TagPath($UploadtreeRow, $tag_pk)
123 {
124  global $PG_CONN;
125 
126  if (empty($UploadtreeRow['pfile_fk'])) {
127  /* this is not a pfile, update table tag_uploadtree */
128  /* There is no constraint preventing duplicate tags so do a precheck */
129  $sql = "SELECT * from tag_uploadtree where uploadtree_fk='$UploadtreeRow[uploadtree_pk]' and tag_fk='$tag_pk'";
130  $result = pg_query($PG_CONN, $sql);
131  DBCheckResult($result, $sql, __FILE__, __LINE__);
132  if (pg_num_rows($result) == 0) {
133  $sql = "insert into tag_uploadtree (tag_fk, uploadtree_fk, tag_uploadtree_date, tag_uploadtree_text) values ($tag_pk, '$UploadtreeRow[uploadtree_pk]', now(), NULL)";
134  $InsResult = pg_query($PG_CONN, $sql);
135  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
136  }
137  } else {
138  /* this is a pfile, update table tag_file */
139  /* There is no constraint preventing duplicate tags so do a precheck */
140  $sql = "SELECT * from tag_file where pfile_fk='$UploadtreeRow[pfile_fk]' and tag_fk='$tag_pk'";
141  $result = pg_query($PG_CONN, $sql);
142  DBCheckResult($result, $sql, __FILE__, __LINE__);
143  if (pg_num_rows($result) == 0) {
144  $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$UploadtreeRow[pfile_fk]', now(), NULL)";
145  $InsResult = pg_query($PG_CONN, $sql);
146  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
147  }
148  }
149  pg_free_result($result);
150  return;
151 }
152 
153 
154 /* note cli_Init(), and db_init() should go away in 2.0
155  * but keep it here for now to ease backporting to 1.4
156  */
157 cli_Init();
158 global $Plugins;
159 error_reporting(E_NOTICE & E_STRICT);
160 
161 global $DB;
162 global $PG_CONN;
163 $dbok = $DB->db_init();
164 if (! $dbok) {
165  echo "FATAL: NO DB connection";
166  exit -1;
167 }
168 
169 /* -f {file of pathnames} -u {original upload_pk} -t {tag_pk} */
170 $Options = getopt("mf:t:u:");
171 if (array_key_exists('f', $Options)
172  && array_key_exists('t', $Options)
173  && array_key_exists('u', $Options)
174  ) {
175  $Missing = array_key_exists('m', $Options) ? true : false;
176  $PathFile = $Options['f'];
177  $tag_pk = $Options['t'];
178  $upload_pk = $Options['u'];
179 } else {
180  echo "Fatal: Missing parameter\n";
181  Usage($argc, $argv);
182  exit -1;
183 }
184 
185 if ($Missing) {
186  "Missing: $Missing\n";
187 }
188 
189 /* read $PathFile a line at a time */
190 $fhandle = @fopen($PathFile, "r");
191 $FileCount = 0;
192 $MissCount = 0;
193 if ($fhandle) {
194  while (($fpathRaw = fgets($fhandle, 4096)) !== false) {
195  $fpath = Strip2Path($fpathRaw);
196  $UploadtreeRow = Path2Uploadtree($upload_pk, $fpath);
197  if ($UploadtreeRow === false) {
198  echo "Missing $fpath\n";
199  $MissCount ++;
200  } else {
201  if ($Missing === false) {
202  TagPath($UploadtreeRow, $tag_pk);
203  }
204  $FileCount ++;
205  }
206  }
207  if (! feof($fhandle)) {
208  echo "Error: unexpected fgets() fail\n";
209  exit(- 1);
210  }
211  fclose($fhandle);
212 }
213 
214 echo "$FileCount files tagged\n";
215 echo "$MissCount file paths not found\n";
216 
217 return (0);
218 
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:25
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN