Subversion Repositories stackman

Rev

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