Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | require_once __DIR__ . '/includes/db.inc.php'; |
||
5 | require_once __DIR__ . '/includes/src.inc.php'; |
||
6 | require_once __DIR__ . '/includes/wildcard.inc.php'; |
||
7 | |||
8 | if ($argc < 3) { |
||
9 | echo "Syntax: $argv[0] <newCategory> <categoryWildcard>\n"; |
||
10 | #db_close(); |
||
11 | exit(2); |
||
12 | } |
||
13 | |||
14 | array_shift($argv); |
||
15 | $newcat = trim($argv[0]); |
||
16 | |||
17 | array_shift($argv); |
||
18 | if (!isset($argv[0]) || (trim($argv[0])=='')) $argv[0] = '*'; |
||
19 | |||
20 | $arg = trim(implode(' ', $argv)); |
||
21 | # while ($arg = array_shift($argv)) { |
||
22 | $wildcard = trim(mywc($arg)); |
||
23 | |||
24 | # Simulation mode |
||
25 | if ($stam_cfg['simulation']) { |
||
26 | fwrite(STDERR, "============ CAT-MOVETO SIMULATION: $wildcard :: $newcat\n"); |
||
27 | #db_close(); |
||
28 | exit(0); |
||
29 | } |
||
30 | |||
31 | $res = db_query("SELECT id, cat, txt FROM stam_entries WHERE |
||
32 | cat LIKE '".db_real_escape_string($wildcard)."' |
||
33 | "); |
||
34 | if (!$res) { |
||
35 | fwrite(STDERR, db_error()."\n"); |
||
36 | #db_close(); |
||
37 | exit(1); |
||
38 | } |
||
39 | while ($row = db_fetch_array($res)) { |
||
40 | $id = $row['id']; |
||
41 | $oldcat = $row['cat']; |
||
42 | $txt = $row['txt']; |
||
43 | |||
44 | if ($oldcat == $newcat) { |
||
45 | echo "ID $id is already in <$oldcat>: $txt\n"; |
||
46 | continue; // not affected |
||
47 | } |
||
48 | |||
49 | $x = db_query("UPDATE stam_entries SET |
||
50 | cat = '".db_real_escape_string($newcat)."' |
||
51 | WHERE |
||
52 | id = '".db_real_escape_string($id)."' |
||
53 | "); |
||
54 | if (!$x) { |
||
55 | fwrite(STDERR, db_error()."\n"); |
||
56 | #db_close(); |
||
57 | exit(1); |
||
58 | } |
||
59 | |||
60 | $afr = db_affected_rows(); |
||
61 | if ($afr != 1) { |
||
62 | fwrite(STDERR, "Error: Could not update $id <$oldcat> '$txt'\n"); |
||
63 | #db_close(); |
||
64 | exit(1); |
||
65 | } else { |
||
66 | echo green("OK! Moved $id from '$oldcat' to '$newcat': $txt\n"); |
||
67 | } |
||
68 | } |
||
69 | # } |
||
70 | |||
71 | # --- |
||
72 | |||
73 | function green($txt) { |
||
74 | return "\033[1;32;40m".$txt."\033[0m"; |
||
75 | } |
||
76 |