forked from craftsmancoding/modx_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firehose.php
312 lines (282 loc) · 11.6 KB
/
firehose.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* Firehose
* Quickly create objects for modx Revolution via the CLI.
* Supported class names : modResource, modUser, modChunk, modSnippet, modPlugin
*
* INSTALLATION:
* Copy this file in its entirety somewhere into the webroot of your MODX Revo site.
* Alternatively, use wget to download the file directly from github:
*
* wget https://raw.githubusercontent.com/craftsmancoding/modx_utils/master/firehose.php
*
* USAGE:
* See --help for usage.
*
* AUTHOR:
* Daniel Edano (daniel@craftsmancoding.com)
*/
// This can only be run via cli
if (php_sapi_name() !== 'cli') {
error_log('Firehose CLI script can only be executed from the command line.');
die('CLI access only.');
}
$supported_classnames = array('modResource'=>'pagetitle','modUser'=>'username','modChunk'=>'name','modPlugin'=>'name','modSnippet'=>'name');
/**
* Add records to the database
* @param array $args
*/
function add_records($args) {
global $modx;
for ($i=1; $i <= $args['count'] ; $i++) {
$obj = $modx->newObject($args['classname']);
$obj->fromArray($args);
switch ($args['classname']) {
case 'modResource':
// Set the things that need to be unique
// and the things that must be set.
$pagetitle = "Firehose_".uniqid();
$obj->set('pagetitle', $pagetitle);
$obj->set('alias', $pagetitle);
$obj->set('content', ucfirst(generate_lorem(150)));
break;
case 'modChunk' :
$obj->set('name', "Firehose_".uniqid());
$obj->set('snippet', '<p>'.ucfirst(generate_lorem(50)).'</p>');
break;
case 'modSnippet':
$obj->set('name', "Firehose_".uniqid());
$obj->set('snippet', 'echo '."'".ucfirst(generate_lorem(20))."';");
break;
case 'modPlugin':
$obj->set('name', "Firehose_".uniqid());
$obj->set('plugincode', 'echo '."'".ucfirst(generate_lorem(20))."';");
break;
case 'modUser' :
$username = "Firehose_".uniqid();
$profile = $modx->newObject('modUserProfile');
$profile->set('email','test@test.com');
$profile->fromArray($args);
// force a value
$obj->set('username',$username);
$obj->set('password',$username );
$profile->set('internalKey',0);
$obj->addOne($profile,'Profile');
break;
}
if (!$obj->save()) {
print message("Failed to add a {$args['classname']} Record",'ERROR');
}
}
}
/**
* generate random length of words
*
* @param int count
* @return string random words
*/
function generate_lorem($count) {
$random_words = array();
$words = array('lorem','ipsum','dolor','sit','amet','consectetur','adipiscing','elit','curabitur','vel','hendrerit','libero','eleifend','blandit','nunc','ornare','odio','ut','orci','gravida','imperdiet','nullam','purus','lacinia','a','pretium','quis','congue','praesent','sagittis','laoreet','auctor','mauris','non','velit','eros','dictum','proin','accumsan','sapien','nec','massa','volutpat','venenatis','sed','eu','molestie','lacus','quisque','porttitor','ligula','dui','mollis','tempus','at','magna','vestibulum','turpis','ac','diam','tincidunt','id','condimentum','enim','sodales','in','hac','habitasse','platea','dictumst','aenean','neque','fusce','augue','leo','eget','semper','mattis','tortor','scelerisque','nulla','interdum','tellus','malesuada','rhoncus','porta','sem','aliquet','et','nam','suspendisse','potenti','vivamus','luctus','fringilla','erat','donec','justo','vehicula','ultricies','varius','ante','primis','faucibus','ultrices','posuere','cubilia','curae','etiam','cursus','aliquam','quam','dapibus',
'nisl','feugiat','egestas','class','aptent','taciti','sociosqu','ad','litora','torquent','per','conubia','nostra','inceptos','himenaeos','phasellus','nibh','pulvinar','vitae','urna','iaculis','lobortis','nisi','viverra','arcu','morbi','pellentesque','metus','commodo','ut','facilisis','felis','tristique','ullamcorper','placerat','aenean','convallis','sollicitudin','integer','rutrum','duis','est','etiam','bibendum','donec','pharetra','vulputate','maecenas','mi','fermentum','consequat','suscipit','aliquam','habitant','senectus','netus','fames','quisque','euismod','curabitur','lectus','elementum','tempor','risus','cras' );
$i = 0;
for($i; $i < $count; $i++) {
$index = array_rand($words);
$word = $words[$index];
//echo $index . '=>' . $word . '<br />';
if($i > 0 && $random_words[$i - 1] == $word)
$i--;
else
$random_words[$i] = $word;
}
return implode(' ', $random_words);
}
/**
* Colorize text for cleaner CLI UX.
* TODO: Windows compatible?
*
* Adapted from
* http://softkube.com/blog/generating-command-line-colors-with-php/
* http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/
*
* @param string $text
* @param string $status
* @return string message formatted for CLI
*/
function message($text, $status) {
$out = '';
switch($status) {
case 'SUCCESS':
$out = '[42m SUCCESS: '.chr(27).'[0;32m '; //Green background
break;
case 'ERROR':
$out = '[41m ERROR: '. chr(27).'[0;31m '; //Red
break;
case 'WARNING':
$out = '[43m WARNING: '; //Yellow background
break;
case 'INFO':
$out = '[46m NOTE: '. chr(27).'[0;34m '; //Blue
break;
case 'HEADER':
$out = '[46m '; //Blue
break;
case 'HELP':
$out = '[42m HELP: '. chr(27).'[0;32m '; //Green
break;
default:
throw new Exception('Invalid status: ' . $status);
}
return "\n".chr(27) . $out . $text .' '. chr(27) . '[0m'."\n\n";
}
/**
* Parse command line arguments and set defaults.
*
* @param array $args
* @return array
*/
function parse_args($args) {
$defaults = array(
'count' => 10,
'remove' => false,
'classname' => '',
'help' => false,
);
foreach($args as $a) {
if (substr($a,0,2) == '--') {
if ($equals_sign = strpos($a,'=',2)) {
$key = substr($a, 2, $equals_sign-2);
$val = substr($a, $equals_sign+1);
$defaults[$key] = $val;
}
else {
$flag = substr($a, 2);
$defaults[$flag] = true;
}
}
}
$defaults['count'] = (int) $defaults['count']; // enforce integer
return $defaults;
}
/**
* Remove records from the database
* @param string $classname
* @return integer count of the removed items
*/
function remove_records($classname) {
global $modx;
global $supported_classnames;
// Default is to delete all records that firehose added
// but if classname is set, then we only remove records from that table
if (!empty($classname) && array_key_exists($classname, $supported_classnames)) {
$supported_classnames = array($classname=>$supported_classnames[$classname]);
}
$cnt = 0;
foreach ($supported_classnames as $classname => $field) {
$c = $modx->newQuery($classname);
$c->where(array(
"$field:LIKE" => 'Firehose_%',
));
$collection = $modx->getIterator($classname,$c);
foreach ($collection as $obj) {
if ($obj->remove() == false) {
print message("Failed to delete a {$classname} Record with field ".$obj->get($field),'ERROR');
}
$cnt++;
}
}
return $cnt;
}
/**
* display help message
*/
function show_help() {
print message('Firehose','HELP');
print "Command line utility for quickly adding lots of sample records to MODX Revolution.
".message('PARAMETERS:','HEADER')."
--classname Identify the type of object to create or delete. Required for insert operations.
Supported class names: modResource, modUser, modChunk, modSnippet, modPlugin
--count how many objects should be created? (Default: 10)
--remove remove all records created with firehose. Specify --classname to restrict deletion.
--help displays this help page.
...more... When adding records, you may specify any non-reserved object attribute relevant to
the classname being created. E.g. --template for modResource or --category for modSnippet.
".message('USAGE EXAMPLES:','HEADER').
"Create 1000 pages:
php ".basename(__FILE__)." --modResource --count=1000
Create 10 users:
php ".basename(__FILE__)." --modUser --count=10
Specify extra parameters to create pages that are unpublished children of page 3
php ".basename(__FILE__)." --modResource --published=0 --parent=3
Cleanup all records created by firehose:
php ".basename(__FILE__)." --remove
Remove only modChunk records created by firehose:
php ".basename(__FILE__)." --remove --classname=modChunk
";
}
//------------------------------------------------------------------------------
//! MAIN BLOCK
//------------------------------------------------------------------------------
// Find MODX: as long this script is inside a MODX webroot, it will run.
$dir = '';
if (!defined('MODX_CORE_PATH') && !defined('MODX_CONFIG_KEY')) {
$max = 10;
$i = 0;
$dir = dirname(__FILE__);
while(true) {
if (file_exists($dir.'/config.core.php')) {
include $dir.'/config.core.php';
break;
}
$i++;
$dir = dirname($dir);
if ($i >= $max) {
print message("Could not find a valid MODX config.core.php file.\n"
."Make sure your repo is inside a MODX webroot and try again.",'ERROR');
die(1);
}
}
}
if (!defined('MODX_CORE_PATH') || !defined('MODX_CONFIG_KEY')) {
print message("Could not load MODX.\n"
."MODX_CORE_PATH or MODX_CONFIG_KEY undefined in\n"
."{$dir}/config.core.php",'ERROR');
die(2);
}
if (!file_exists(MODX_CORE_PATH.'model/modx/modx.class.php')) {
print message("modx.class.php not found at ".MODX_CORE_PATH,'ERROR');
die(3);
}
// fire up MODX
require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('mgr');
// get args from cli
$params = parse_args($argv);
if ($params['help']) {
show_help();
exit;
}
// Validate the args:
if($params['count'] <= 0) {
print message("--count must be greater than 0",'ERROR');
die();
}
if (!$params['remove'] && !$params['classname']) {
print message("--classname is required for insert operations.",'ERROR');
die();
}
if ($params['classname'] && !in_array( $params['classname'], array_keys($supported_classnames))) {
print message("Unsupported classname.",'ERROR');
die();
}
// do the action
if ($params['remove']) {
$cnt = remove_records($params['classname']);
print message("$cnt records removed.",'SUCCESS');
}
else {
add_records($params);
print message("{$params['count']} {$params['classname']} records created.",'SUCCESS');
}