FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_tagfile.php
1 #!/usr/bin/php
2 <?php
3 /***********************************************************
4  Copyright (C) 2011 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 
33 $GlobalReady = 1;
34 /* Load all code */
35 require_once "/usr/share/fossology/php/pathinclude.php";
36 global $WEBDIR;
37 $UI_CLI = 1; /* this is a command-line program */
38 require_once ("$WEBDIR/common/common.php");
39 
40 function Usage($argc, $argv)
41 {
42  echo "$argv[0] -m -f {file of pathnames} -u {original upload_pk} -t {tag_pk}\n";
43  echo " -m means to only print out missing files. Do not update the db.\n";
44 }
45 
52 function Strip2Path($RawFilePath)
53 {
54  if (empty($RawFilePath)) {
55  return "";
56  }
57 
58  /* strip to first slash */
59  $FirstSlashPos = strpos($RawFilePath, "/");
60  $StartStr = substr($RawFilePath, $FirstSlashPos);
61 
62  $FParts = explode(" ", $StartStr, 3);
63 
64  /* strip trailing text after path */
65  $fPath = strtok($FParts[0], " \t");
66  return $fPath;
67 }
68 
78 function Path2Uploadtree($upload_pk, $FilePath)
79 {
80  global $PG_CONN;
81 
82  $FileName = basename($FilePath);
83 
84  /* create the cleaned up file path string
85  * note delimiters are not put in the string since they are not needed for
86  * the string compare.
87  */
88  $FilePathArray = explode('/', $FilePath);
89  $FilePathStr = "";
90  foreach ($FilePathArray as $name) {
91  if (empty($name)) {
92  continue;
93  }
94  $FilePathStr .= $name;
95  }
96 
97  $sql = "SELECT * from uploadtree where upload_fk='$upload_pk' and ufile_name='$FileName'";
98  $result = pg_query($PG_CONN, $sql);
99  DBCheckResult($result, $sql, __FILE__, __LINE__);
100  if (pg_num_rows($result) == 0) {
101  return false;
102  }
103 
104  /* Get the uploadtree recs for this file name */
105  while ($row = pg_fetch_assoc($result)) {
106  $sql = "select ufile_name from uploadtree2path('$row[uploadtree_pk]')";
107  $PathResult = pg_query($PG_CONN, $sql);
108  DBCheckResult($PathResult, $sql, __FILE__, __LINE__);
109 
110  /* Check each uploadtree rec to see if the path matches */
111  $SelectedPathStr = "";
112  while ($PathRow = pg_fetch_assoc($PathResult)) {
113  $SelectedPathStr = $PathRow['ufile_name'] . $SelectedPathStr;
114  }
115  pg_free_result($PathResult);
116 
117  /* do the paths match? */
118  if ($FilePathStr == $SelectedPathStr) {
119  pg_free_result($result);
120  return $row;
121  }
122  }
123  pg_free_result($result);
124  return false;
125 }
126 
127 
132 function TagPath($UploadtreeRow, $tag_pk)
133 {
134  global $PG_CONN;
135 
136  if (empty($UploadtreeRow['pfile_fk'])) {
137  /* this is not a pfile, update table tag_uploadtree */
138  /* There is no constraint preventing duplicate tags so do a precheck */
139  $sql = "SELECT * from tag_uploadtree where uploadtree_fk='$UploadtreeRow[uploadtree_pk]' and tag_fk='$tag_pk'";
140  $result = pg_query($PG_CONN, $sql);
141  DBCheckResult($result, $sql, __FILE__, __LINE__);
142  if (pg_num_rows($result) == 0) {
143  $sql = "insert into tag_uploadtree (tag_fk, uploadtree_fk, tag_uploadtree_date, tag_uploadtree_text) values ($tag_pk, '$UploadtreeRow[uploadtree_pk]', now(), NULL)";
144  $InsResult = pg_query($PG_CONN, $sql);
145  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
146  }
147  } else {
148  /* this is a pfile, update table tag_file */
149  /* There is no constraint preventing duplicate tags so do a precheck */
150  $sql = "SELECT * from tag_file where pfile_fk='$UploadtreeRow[pfile_fk]' and tag_fk='$tag_pk'";
151  $result = pg_query($PG_CONN, $sql);
152  DBCheckResult($result, $sql, __FILE__, __LINE__);
153  if (pg_num_rows($result) == 0) {
154  $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$UploadtreeRow[pfile_fk]', now(), NULL)";
155  $InsResult = pg_query($PG_CONN, $sql);
156  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
157  }
158  }
159  pg_free_result($result);
160  return;
161 }
162 
163 
164 /* note cli_Init(), and db_init() should go away in 2.0
165  * but keep it here for now to ease backporting to 1.4
166  */
167 cli_Init();
168 global $Plugins;
169 error_reporting(E_NOTICE & E_STRICT);
170 
171 global $DB;
172 global $PG_CONN;
173 $dbok = $DB->db_init();
174 if (! $dbok) {
175  echo "FATAL: NO DB connection";
176  exit -1;
177 }
178 
179 /* -f {file of pathnames} -u {original upload_pk} -t {tag_pk} */
180 $Options = getopt("mf:t:u:");
181 if (array_key_exists('f', $Options)
182  && array_key_exists('t', $Options)
183  && array_key_exists('u', $Options)
184  ) {
185  $Missing = array_key_exists('m', $Options) ? true : false;
186  $PathFile = $Options['f'];
187  $tag_pk = $Options['t'];
188  $upload_pk = $Options['u'];
189 } else {
190  echo "Fatal: Missing parameter\n";
191  Usage($argc, $argv);
192  exit -1;
193 }
194 
195 if ($Missing) {
196  "Missing: $Missing\n";
197 }
198 
199 /* read $PathFile a line at a time */
200 $fhandle = @fopen($PathFile, "r");
201 $FileCount = 0;
202 $MissCount = 0;
203 if ($fhandle) {
204  while (($fpathRaw = fgets($fhandle, 4096)) !== false) {
205  $fpath = Strip2Path($fpathRaw);
206  $UploadtreeRow = Path2Uploadtree($upload_pk, $fpath);
207  if ($UploadtreeRow === false) {
208  echo "Missing $fpath\n";
209  $MissCount ++;
210  } else {
211  if ($Missing === false) {
212  TagPath($UploadtreeRow, $tag_pk);
213  }
214  $FileCount ++;
215  }
216  }
217  if (! feof($fhandle)) {
218  echo "Error: unexpected fgets() fail\n";
219  exit(- 1);
220  }
221  fclose($fhandle);
222 }
223 
224 echo "$FileCount files tagged\n";
225 echo "$MissCount file paths not found\n";
226 
227 return (0);
228 
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
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:75
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