Revision | 345 (tree) |
---|---|
Time | 2021-09-06 18:36:13 |
Author | ![]() |
implementation of checkup actions
@@ -7,7 +7,7 @@ | ||
7 | 7 | === API === |
8 | 8 | ?a=pull&p=PACKAGE downloads the zip archive containing PACKAGE |
9 | 9 | ?a=search&p=PHRASE list packages that match PHRASE |
10 | - ?a=checkup&p=PACKAGE&v=ver check if package available in version > v | |
10 | + ?a=checkup list of packages+versions in $_POST | |
11 | 11 | */ |
12 | 12 | |
13 | 13 |
@@ -18,6 +18,44 @@ | ||
18 | 18 | } |
19 | 19 | |
20 | 20 | |
21 | +function csv_to_array($filename = '', $delimiter = "\t") { | |
22 | + //if (! file_exists($filename) || ! is_readable($filename)) return FALSE; | |
23 | + $handle = fopen($filename, 'r'); | |
24 | + if ($handle === false) return(false); | |
25 | + | |
26 | + $rez = array(); | |
27 | + | |
28 | + while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { | |
29 | + $rez[] = $row; | |
30 | + } | |
31 | + fclose($handle); | |
32 | + | |
33 | + return $rez; | |
34 | +} | |
35 | + | |
36 | + | |
37 | +function tabulprint($ar_data, $ar_width, $margin = true) { | |
38 | + $count = 0; | |
39 | + foreach ($ar_data as $item) { | |
40 | + if ($count == 0) { | |
41 | + echo '|'; | |
42 | + if ($margin) echo ' '; | |
43 | + } | |
44 | + echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]); | |
45 | + if ($margin) { | |
46 | + echo ' | '; | |
47 | + } else { | |
48 | + echo '|'; | |
49 | + } | |
50 | + $count++; | |
51 | + } | |
52 | + echo "\r\n"; | |
53 | +} | |
54 | + | |
55 | + | |
56 | +// *** MAIN START ************************************************************ | |
57 | + | |
58 | + | |
21 | 59 | if (empty($_GET['a'])) { |
22 | 60 | http_response_code(404); |
23 | 61 | echo "ERROR: no action specified\r\n"; |
@@ -24,15 +62,18 @@ | ||
24 | 62 | exit(0); |
25 | 63 | } |
26 | 64 | |
27 | -if (empty($_GET['p'])) { | |
28 | - http_response_code(404); | |
29 | - echo "ERROR: no package specified\r\n"; | |
30 | - exit(0); | |
65 | +$a = strtolower($_GET['a']); | |
66 | + | |
67 | +$p = ''; | |
68 | +if ($a != 'checkup') { | |
69 | + if (empty($_GET['p'])) { | |
70 | + http_response_code(404); | |
71 | + echo "ERROR: no package specified\r\n"; | |
72 | + exit(0); | |
73 | + } | |
74 | + $p = strtolower($_GET['p']); | |
31 | 75 | } |
32 | 76 | |
33 | -$a = strtolower($_GET['a']); | |
34 | -$p = strtolower($_GET['p']); | |
35 | - | |
36 | 77 | $v = ''; |
37 | 78 | if (!empty($_GET['v'])) $v = $_GET['v']; |
38 | 79 |
@@ -79,12 +120,29 @@ | ||
79 | 120 | } |
80 | 121 | |
81 | 122 | if ($a === 'checkup') { |
123 | + $found = 0; | |
124 | + $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version | |
82 | 125 | while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) { |
83 | - if (strcasecmp($pkg[0], $p)) { | |
84 | - echo "found package {$pkg[0]} ver {$pkg[1]} -> is it newer than '{$v}' ?\r\n"; | |
126 | + // is $pkg part of remote packages? | |
127 | + foreach ($remote_pkgs as $rpkg) { | |
128 | + if (strcasecmp($pkg[0], $rpkg[0]) != 0) continue; | |
129 | + if ($pkg[1] === $rpkg[1]) continue; // skip same version | |
130 | + if ($found == 0) { | |
131 | + echo str_pad('', 58, '-') . "\r\n"; | |
132 | + tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20)); | |
133 | + tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false); | |
134 | + } | |
135 | + $found++; | |
136 | + tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20)); | |
85 | 137 | break; |
86 | 138 | } |
87 | 139 | } |
140 | + if ($found == 0) { | |
141 | + echo "no available updates\r\n"; | |
142 | + } else { | |
143 | + echo str_pad('', 58, '-') . "\r\n"; | |
144 | + echo "found {$found} differing packages\r\n"; | |
145 | + } | |
88 | 146 | } |
89 | 147 | fclose($handle); |
90 | 148 |