-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-update-calculated-fields.php
102 lines (82 loc) · 3.35 KB
/
cli-update-calculated-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
$app_dir = dirname(__FILE__);
include("{$app_dir}/lib.php");
// this script can run only in CLI mode
if(php_sapi_name() != "cli") die('this script can run only in CLI mode');
$fn = basename(__FILE__);
$help = implode("\n", array(
'Updates calculated fields.',
'',
'Supported arguments:',
' -t: comma-separated list of tables to update. all tables will be updated if this argument is not specified',
' -s: comma-separated list of starting record numbers. Default is 0 (beginning of each table)',
' -l: comma-separated list of records count to update in each table. Default is records count - start',
' -x: comma-separated list of tables to exclude from updating, overrides -t',
' -u: username to use in queries that have %USERNAME% placeholder, default is admin user',
' -h: displays this help message',
'',
'Examples:',
"php {$fn}",
' Updates all records of all tables. Not recommended for large databases.',
"php {$fn} -s 2000 -l 1000",
' Updates 1000 records starting from rec# 2000 in all tables.',
"php {$fn} -t clients,orders -s 100,1000 -l 10,100",
' Updates records 100:110 of clients table and 1000:1100 of orders table.',
"php {$fn} -x clients",
' Updates all records of all tables excluding clients table.',
"php {$fn} -u bob",
' Updates all records of all tables as user bob.',
'',
));
// if -h argument is provided, show the help message and quit
if(in_array('-h', $argv)) die($help);
$allowed_args = array('-t', '-s', '-l', '-x');
$tables = getTableList(true);
// prepare args in an array ['switch' => 'value', ...]
array_shift($argv);
$args = array();
for($i = 0; $i < count($argv); $i += 2) {
if(!in_array($argv[$i], $allowed_args)) continue;
$args[$argv[$i]] = array_map(trim, explode(',', $argv[$i + 1]));
}
$calc = calculated_fields();
// if no tables provided, assume all tables
if(!isset($args['-t'])) $args['-t'] = array_keys($calc);
// default exclude tables, as specified with -x: none
if(!isset($args['-x'])) $args['-x'] = array();
if(!isset($args['-s'])) $args['-s'] = array();
if(!isset($args['-l'])) $args['-l'] = array();
$mi = getMemberInfo($args['-u']);
if(!$mi) {
$conf = config('adminConfig');
$mi = getMemberInfo($conf['adminUsername']);
}
$start = 0;
$length = pow(2, 45); // default length is a huge # to span the whole table (if your table really has more rows than 2^45, just increase this value!)
$eo = array('silentErrors' => true);
// start updating specified tables
for($i = 0; $i < count($args['-t']); $i++) {
$tn = $args['-t'][$i];
if(!isset($calc[$tn])) {
echo "`{$tn}` table not found.\n";
continue;
}
if(in_array($tn, $args['-x'])) {
echo "`{$tn}` table excluded.\n";
continue;
}
if(!count($calc[$tn])) {
echo "`{$tn}` table has no calculated fields.\n";
continue;
}
echo "Processing `{$tn}` table ... ";
if(isset($args['-s'][$i]) && intval($args['-s'][$i]) > 0) $start = intval($args['-s'][$i]);
if(isset($args['-l'][$i]) && intval($args['-l'][$i]) > 1) $length = intval($args['-l'][$i]);
// retrieve PKs of specified range in given table
$ids = array();
$pk = getPKFieldName($tn);
$res = sql("SELECT `{$pk}` FROM `{$tn}` LIMIT {$start}, {$length}", $eo);
while($row = db_fetch_row($res)) $ids[] = $row[0];
foreach($ids as $id) update_calc_fields($tn, $id, $calc[$tn], $mi);
echo count($ids) . " updated.\n";
}