Rev 3 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 6 | ||
---|---|---|---|
1 | #!/usr/bin/php |
1 | #!/usr/bin/php |
2 | <?php |
2 | <?php |
3 | 3 | ||
4 | require_once __DIR__ . '/includes/db.inc.php'; |
4 | require_once __DIR__ . '/includes/db.inc.php'; |
5 | require_once __DIR__ . '/includes/src.inc.php'; |
5 | require_once __DIR__ . '/includes/src.inc.php'; |
6 | 6 | ||
- | 7 | $stam_cfg = array(); |
|
- | 8 | $stam_cfg['simulation'] = false; |
|
- | 9 | require_once __DIR__ . '/includes/config.inc.php'; |
|
- | 10 | ||
7 | if ($argc <> 3) { |
11 | if ($argc <> 3) { |
8 | echo "Syntax: $argv[0] <newCategory> <id>\n"; |
12 | echo "Syntax: $argv[0] <newCategory> <id>\n"; |
9 | #db_close(); |
13 | #db_close(); |
10 | exit(2); |
14 | exit(2); |
11 | } |
15 | } |
12 | 16 | ||
13 | $newcat = trim($argv[1]); |
17 | $newcat = trim($argv[1]); |
14 | $id = trim($argv[2]); |
18 | $id = trim($argv[2]); |
15 | 19 | ||
16 | # Range support |
20 | # Range support |
17 | if (strpos($id, '-') !== false) { |
21 | if (strpos($id, '-') !== false) { |
18 | $ary = explode('-', $id, 2); |
22 | $ary = explode('-', $id, 2); |
19 | $id_min = $ary[0]; |
23 | $id_min = $ary[0]; |
20 | $id_max = $ary[1]; |
24 | $id_max = $ary[1]; |
21 | } else { |
25 | } else { |
22 | $id_min = $id; |
26 | $id_min = $id; |
23 | $id_max = $id; |
27 | $id_max = $id; |
24 | } |
28 | } |
25 | if (($id_min == 0) || ($id_max == 0)) { |
29 | if (($id_min == 0) || ($id_max == 0)) { |
26 | fwrite(STDERR, "0 not a valid ID\n"); |
30 | fwrite(STDERR, "0 not a valid ID\n"); |
27 | #db_close(); |
31 | #db_close(); |
28 | exit(3); |
32 | exit(3); |
29 | } |
33 | } |
- | 34 | $id_min = (int)$id_min; |
|
- | 35 | $id_max = (int)$id_max; |
|
30 | if ($id_min>$id_max) { |
36 | if ($id_min>$id_max) { |
31 | fwrite(STDERR, "RANGE ERROR $id_min>$id_max\n"); |
37 | fwrite(STDERR, "RANGE ERROR $id_min>$id_max\n"); |
32 | #db_close(); |
38 | #db_close(); |
33 | exit(3); |
39 | exit(3); |
34 | } |
40 | } |
35 | $id_cnt = $id_max-$id_min + 1; |
41 | $id_cnt = $id_max - $id_min + 1; |
36 | 42 | ||
37 | # Simulation mode |
43 | # Simulation mode |
38 | if ($stam_cfg['simulation']) { |
44 | if ($stam_cfg['simulation']) { |
39 | fwrite(STDERR, "============ ID-MOVETO SIMULATION: $id_min..$id_max :: $newcat\n"); |
45 | fwrite(STDERR, "============ ID-MOVETO SIMULATION: $id_min..$id_max :: $newcat\n"); |
40 | #db_close(); |
46 | #db_close(); |
41 | exit(0); |
47 | exit(0); |
42 | } |
48 | } |
43 | 49 | ||
44 | $res = db_query("SELECT id, cat, txt FROM stam_entries WHERE |
50 | $res = db_query("SELECT id, cat, txt FROM stam_entries WHERE |
45 | id >= '".db_real_escape_string($id_min)."' |
51 | id >= '".db_real_escape_string($id_min)."' |
46 | AND id <= '".db_real_escape_string($id_max)."' |
52 | AND id <= '".db_real_escape_string($id_max)."' |
47 | "); |
53 | "); |
48 | if (!$res) { |
54 | if (!$res) { |
49 | fwrite(STDERR, db_error()."\n"); |
55 | fwrite(STDERR, db_error()."\n"); |
50 | #db_close(); |
56 | #db_close(); |
51 | exit(1); |
57 | exit(1); |
52 | } |
58 | } |
53 | while ($row = db_fetch_array($res)) { |
59 | while ($row = db_fetch_array($res)) { |
54 | $id = $row['id']; |
60 | $id = $row['id']; |
55 | $oldcat = $row['cat']; |
61 | $oldcat = $row['cat']; |
56 | $txt = $row['txt']; |
62 | $txt = $row['txt']; |
57 | 63 | ||
58 | if ($oldcat == $newcat) { |
64 | if ($oldcat == $newcat) { |
59 | echo "ID $id is already in <$oldcat>: $txt\n"; |
65 | echo "ID $id is already in <$oldcat>: $txt\n"; |
60 | continue; // not affected |
66 | continue; // not affected |
61 | } |
67 | } |
62 | 68 | ||
63 | $x = db_query("UPDATE stam_entries SET |
69 | $x = db_query("UPDATE stam_entries SET |
64 | cat = '".db_real_escape_string($newcat)."' |
70 | cat = '".db_real_escape_string($newcat)."' |
65 | WHERE |
71 | WHERE |
66 | id = '".db_real_escape_string($id)."' |
72 | id = '".db_real_escape_string($id)."' |
67 | "); |
73 | "); |
68 | if (!$x) { |
74 | if (!$x) { |
69 | fwrite(STDERR, db_error()."\n"); |
75 | fwrite(STDERR, db_error()."\n"); |
70 | #db_close(); |
76 | #db_close(); |
71 | exit(1); |
77 | exit(1); |
72 | } |
78 | } |
73 | 79 | ||
74 | $afr = db_affected_rows(); |
80 | $afr = db_affected_rows(); |
75 | if ($afr != 1) { |
81 | if ($afr != 1) { |
76 | fwrite(STDERR, "Error: Could not update $id <$oldcat> '$txt'\n"); |
82 | fwrite(STDERR, "Error: Could not update $id <$oldcat> '$txt'\n"); |
77 | #db_close(); |
83 | #db_close(); |
78 | exit(1); |
84 | exit(1); |
79 | } else { |
85 | } else { |
80 | echo green("OK! Moved $id from '$oldcat' to '$newcat': $txt\n"); |
86 | echo green("OK! Moved $id from '$oldcat' to '$newcat': $txt\n"); |
81 | } |
87 | } |
82 | } |
88 | } |
83 | 89 | ||
84 | # --- |
90 | # --- |
85 | 91 | ||
86 | function green($txt) { |
92 | function green($txt) { |
87 | return "\033[1;32;40m".$txt."\033[0m"; |
93 | return "\033[1;32;40m".$txt."\033[0m"; |
88 | } |
94 | } |
89 | 95 |