-
Notifications
You must be signed in to change notification settings - Fork 3
/
mate
executable file
·806 lines (784 loc) · 42.7 KB
/
mate
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#!/usr/bin/php
<?php
$DS = "/";
// Define present working dir & thus root
$pwd = $_SERVER['PWD'];
$root = false;
$pathParts = explode($DS,$pwd);
if(stripos($pwd,$DS.'app')!==false) {
$root = $DS;
foreach($pathParts as $part) { if($part=="app") { break; } $root .= $DS.$part; }
} else {
for($i=0;$i<count($pathParts);$i++) {
$path = implode($pathParts,$DS);
foreach(explode("\n",`ls $path`) as $listing) {
if($listing=="app") { $root = $path; }
}
array_pop($pathParts);
}
}
// $root = "/home/app/magento/current";
$root = ".";
if(!$root AND ((!isset($argv[1]) || (isset($argv[1]) AND $argv[1]!="minstall") && (isset($argv[1]) AND $argv[1]!="help")))) {
print "\nMagento installation not recognized; are you in the correct directory?\n\nRun 'mate help' to show all available commands.\n\n";
exit;
}
if($root AND ((!isset($argv[1]) || (isset($argv[1]) AND $argv[1]!="minstall")))) {
require_once $root . $DS . 'app' . $DS . 'Mage.php';
}
if(!isset($argv[1])) { $argv[1] = null; }
switch($argv[1]) {
case 'modules':
case 'mod':
case 'module':
echo "\nFound modules;\n\n";
echo moduleOverview($root);
break;
case 'enable':
if(empty($argv[2])) {
echo "\nPlease pass along modules name. Here is the list;\n\n";
echo moduleOverview($root);
exit;
}
if(stripos($argv[2],"_")!==false) {
$filename = $argv[2].".xml";
} else {
if(empty($argv[3])) { echo "Missing arguments"; exit; }
$filename = $argv[2]."_".$argv[3].".xml";
}
$moduleXml = $root. DS . "app" . DS . "etc" . DS . "modules" . DS . $filename;
if(isModuleEnabled($moduleXml)) {
echo "\nModule ".str_replace(".xml","",$filename)." is already enabled\n\n";
} else {
shell_exec("sed -i 's/false/true/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now enabled\n\n";
}
break;
case 'skeleton':
if(count($argv)<4) {
echo "Use 'mate skeleton Package Module Version'\n\n";
exit;
}
$package = $argv[2];
$module = $argv[3];
$modulelow = strtolower($module);
if(!isset($argv[4])) {
$version = '0.1.0';
} else {
$version = $argv[4];
}
echo "Creating module skeleton for ".$package."_".$module.", version ".$version."\n";
shell_exec('mkdir -p '.$root. DS . 'app' . DS . 'code' . DS . 'local' . DS . $package. DS . $module . DS . 'etc
echo "<?xml version=\"1.0\"?>\n<config>\n\t<modules>\n\t\t<'.$package.'_'.$module.'>\n\t\t\t<version>'.$version.'</version>\n\t\t</'.$package.'_'.$module.'>\n\t</modules>\n\t<adminhtml>\n\t\t<translate>\n\t\t\t<modules>\n\t\t\t\t<'.$package.'_'.$module.'>\n\t\t\t\t\t<files>\n\t\t\t\t\t\t<default>'.$package.'_'.$module.'.csv</default>\n\t\t\t\t\t</files>\n\t\t\t\t</'.$package.'_'.$module.'>\n\t\t\t</modules>\n\t\t</translate>\n\t\t<layout>\n\t\t\t<updates>\n\t\t\t\t<'.$modulelow.'>\n\t\t\t\t\t<file>'.$modulelow.'.xml</file>\n\t\t\t\t</'.$modulelow.'>\n\t\t\t</updates>\n\t\t</layout>\n\t\t\n\t\t<acl>\n\t\t\t<resources>\n\t\t\t\t<admin>\n\t\t\t\t\t<children>\n\t\t\t\t\t\t<system>\n\t\t\t\t\t\t\t<children>\n\t\t\t\t\t\t\t\t<config>\n\t\t\t\t\t\t\t\t\t<children>\n\t\t\t\t\t\t\t\t\t\t<'.$modulelow.' translate=\"title\" module=\"'.$modulelow.'\">\n\t\t\t\t\t\t\t\t\t\t\t<title>'.$module.'</title>\n\t\t\t\t\t\t\t\t\t\t</'.$modulelow.'>\n\t\t\t\t\t\t\t\t\t</children>\n\t\t\t\t\t\t\t\t</config>\n\t\t\t\t\t\t\t</children>\n\t\t\t\t\t\t</system>\n\t\t\t\t\t</children>\n\t\t\t\t</admin>\n\t\t\t</resources>\n\t\t</acl>\n\t</adminhtml>\n\t<global>\n\t\t<models>\n\t\t\t<'.$modulelow.'>\n\t\t\t\t<class>'.$package.'_'.$module.'_Model</class>\n\t\t\t</'.$modulelow.'>\n\t\t</models>\n\t\t<resources>\n\t\t\t<'.$modulelow.'_setup>\n\t\t\t\t<setup>\n\t\t\t\t\t<module>'.$package.'_'.$module.'</module>\n\t\t\t\t\t<class>'.$package.'_'.$module.'_Model_Mysql4_Setup</class>\n\t\t\t\t</setup>\n\t\t\t</'.$modulelow.'_setup>\n\t\t</resources>\n\n\t\t<helpers>\n\t\t\t<'.$modulelow.'>\n\t\t\t\t<class>'.$package.'_'.$module.'_Helper</class>\n\t\t\t</'.$modulelow.'>\n\t\t</helpers>\n\t</global>\n</config>" > '.$root.'/app/code/local/'.$package.'/'.$module.'/etc/config.xml
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\t<config>\n\t\t<modules>\n\t\t\t<'.$package.'_'.$module.'>\n\t\t\t\t<active>false</active>\n\t\t\t\t<codePool>local</codePool>\n\t\t\t</'.$package.'_'.$module.'>\n\t\t</modules>\n\t</config>" > '.$root.'/app/etc/modules/'.$package.'_'.$module.'.xml
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<config>\n\t<acl>\n\t\t<resources>\n\t\t\t<admin>\n\t\t\t\t<children>\n\t\t\t\t\t<system>\n\t\t\t\t\t\t<children>\n\t\t\t\t\t\t\t<config>\n\t\t\t\t\t\t\t\t<children>\n\t\t\t\t\t\t\t\t\t<'.$modulelow.' translate=\"title\" module=\"'.$modulelow.'\">\n\t\t\t\t\t\t\t\t\t\t<title>'.$module.'</title>\n\t\t\t\t\t\t\t\t\t</'.$modulelow.'>\n\t\t\t\t\t\t\t\t</children>\n\t\t\t\t\t\t\t</config>\n\t\t\t\t\t\t</children>\n\t\t\t\t\t</system>\n\t\t\t\t</children>\n\t\t\t</admin>\n\t\t</resources>\n\t</acl>\n</config>" > '.$root.'/app/code/local/'.$package.'/'.$module.'/etc/adminhtml.xml
echo "<?xml version=\"1.0\"?>\n<config>\n\t\n\t<tabs>\n\t\t\t<'.strtolower($package).'>\n\t\t\t\t<label>'.$package.'</label>\n\t\t\t\t<sort_order>195</sort_order>\n\t\t\t</'.strtolower($package).'>\n\t\t</tabs>\n\t<sections>\n\t\n\t\t<'.$modulelow.' translate=\"label\" module=\"'.$modulelow.'\">\n\t\t\t<label>'.$module.'</label>\n\t\t\t<tab>'.strtolower($package).'</tab>\n\t\t\t<frontend_type>text</frontend_type>\n\t\t\t<sort_order>71</sort_order>\n\t\t\t<show_in_default>1</show_in_default>\n\t\t\t<show_in_website>1</show_in_website>\n\t\t\t<show_in_store>1</show_in_store>\n\t\t\t<groups>\n\t\t\t\t<general translate="label">\n\t\t\t\t\t<label>General</label>\n\t\t\t\t\t<expanded>1</expanded>\n\t\t\t\t\t<sort_order>600</sort_order>\n\t\t\t\t\t<show_in_default>1</show_in_default>\n\t\t\t\t\t<show_in_website>1</show_in_website>\n\t\t\t\t\t<show_in_store>1</show_in_store>\n\t\t\t\t\t<fields>\n\t\t\t\t\t\t<disabled translate="label">\n\t\t\t\t\t\t<label>Disable module</label>\n\t\t\t\t\t\t<frontend_type>select</frontend_type>\n\t\t\t\t\t\t<source_model>adminhtml/system_config_source_yesno</source_model>\n\t\t\t\t\t\t<sort_order>10</sort_order>\n\t\t\t\t\t\t<show_in_default>1</show_in_default>\n\t\t\t\t\t\t<show_in_website>1</show_in_website>\n\t\t\t\t\t\t<show_in_store>1</show_in_store>\n\t\t\t\t\t</disabled>\n\t\t\t\t</fields>\n\t\t\t</general>\n\t\t</groups>\n\t\t</'.$modulelow.'>\n\t</sections>\n</config>" > '.$root.'/app/code/local/'.$package.'/'.$module.'/etc/system.xml
mkdir -p '.$root . DS . 'app' . DS . 'code' . DS . 'local' . DS .$package. DS . $module . DS . 'Helper
echo "<?php\n\nclass '.$package.'_'.$module.'_Helper_Data extends Mage_Core_Helper_Abstract {\n\n}" > '.$root.'/app/code/local/'.$package.'/'.$module.'/Helper/Data.php
mkdir -p '.$root.'/app/code/local/'.$package.'/'.$module.'/Model/Mysql4
echo "<?php\n\nclass '.$package.'_'.$module.'_Model_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup\n{\n\n\n}" > '.$root.'/app/code/local/'.$package.'/'.$module.'/Model/Mysql4/Setup.php
mkdir -p '.$root.'/app/code/local/'.$package.'/'.$module.'/controllers
echo "<?php\n\nclass '.$package.'_'.$module.'_IndexController extends Mage_Core_Controller_Front_Action\n{\n\tpublic function indexAction() {\n\n\t}\n}" > '.$root.'/app/code/local/'.$package.'/'.$module.'/controllers/IndexController.php
mkdir -p '.$root.'/app/code/local/'.$package.'/'.$module.'/sql/"'.$modulelow.'"_setup
echo "<?php\n\n\$installer = \$this;\n\$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup(\'core_setup\');\n\$installer->startSetup();\n\n\$installer->endSetup();" > '.$root.'/app/code/local/'.$package.'/'.$module.'/sql/"'.$modulelow.'"_setup/mysql4-install-'.$version.'.php');
break;
case 'disable':
if(empty($argv[2])) {
echo "\nPlease pass along modules name. Here is the list;\n\n";
echo moduleOverview($root);
exit;
}
if(stripos($argv[2],"_")!==false) {
$filename = $argv[2].".xml";
} else {
if(empty($argv[3])) { echo "Missing arguments"; exit; }
$filename = $argv[2]."_".$argv[3].".xml";
}
$moduleXml = $root."/app/etc/modules/".$filename;
if(!isModuleEnabled($moduleXml)) {
echo "\nModule ".str_replace(".xml","",$filename)." is already disabled\n\n";
} else {
shell_exec("sed -i 's/true/false/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now disabled\n\n";
}
break;
case 'toggle':
if(empty($argv[2])) {
echo "\nPlease pass along modules name. Here is the list;\n\n";
foreach(explode("\n",shell_exec('ls '.$root."/app/etc/modules")) as $xml) {
if(!empty($xml)) {
echo str_replace(".xml","",$xml);
if(isModuleEnabled($root."/app/etc/modules/".$xml)) { echo " - enabled"; } else { echo " - disabled"; }
}
echo "\n";
}
exit;
}
if(stripos($argv[2],"*")!==false) {
foreach(glob($root.'/app/etc/modules/'.$argv[2]) as $moduleXml) {
$filename = array_pop(explode("/",$moduleXml));
if(isModuleEnabled($moduleXml)) {
shell_exec("sed -i 's/true/false/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now disabled\n\n";
} else {
shell_exec("sed -i 's/false/true/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now enabled\n\n";
}
}
} else {
if(stripos($argv[2],"_")!==false) {
$filename = $argv[2].".xml";
} else {
if(empty($argv[3])) { echo "Missing arguments"; exit; }
$filename = $argv[2]."_".$argv[3].".xml";
}
$moduleXml = $root."/app/etc/modules/".$filename;
if(isModuleEnabled($moduleXml)) {
shell_exec("sed -i 's/true/false/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now disabled\n\n";
} else {
shell_exec("sed -i 's/false/true/' ".$moduleXml);
echo "\nModule ".str_replace(".xml","",$filename)." is now enabled\n\n";
}
}
break;
case 'resetmods':
shell_exec("find ".$root." -type d -exec chmod 775 {} \;");
shell_exec("find ".$root." -type f -exec chmod 664 {} \;");
shell_exec("chmod 660 ".$root."/app/etc/local.xml");
echo "\nPermissions are reset.\n\n";
break;
case 'log':
if(isset($argv[2])) {
$filename = $argv[2];
} else {
$filename = 'system.log';
}
if(isset($argv[3])) {
$lines = $argv[3];
} else {
$lines = 50;
}
echo shell_exec("tail -n ".$lines." ".$root."/var/log/".$filename);
break;
case 'backup':
$skipExtendedInsert = false;
$outputFile = 'database-' . date('Ymdhis') . '.sql';
if(isset($argv[2])) { $outputFile = $argv[2]; }
if(isset($argv[3])) { $skipExtendedInsert = true; }
$path = explode("/",$outputFile);
$path = implode("/",array_slice($path,0,-1));
if(strlen($path)>2) { $path .= '/'; }
$local = simplexml_load_string(file_get_contents($root."/app/etc/local.xml"), 'SimpleXMLElement', LIBXML_NOCDATA);
$info = (array)$local->global->resources->default_setup->connection;
$ignoreTables = array('log_customer','log_visitor','log_visitor_info','log_visitor_online','log_url','log_url_info','log_quote','report_viewed_product_index','report_compared_product_index','report_event','catalog_compare_item');
$dumpCommand = "mysqldump -u ".$info['username']." -p".$info['password'];
if($skipExtendedInsert) {
$dumpCommand .= " --skip-extended-insert ";
}
foreach($ignoreTables as $ignoreTable) {
$dumpCommand .= " --ignore-table=".$info['dbname'].".".$ignoreTable;
}
$dumpCommand .= " ".$info['dbname'] . " > ".$outputFile;
shell_exec($dumpCommand);
$dumpCommand = "mysqldump -u ".$info['username']." -p".$info['password']." --no-data=true --skip-extended-insert ".$info['dbname'] . " ";
foreach($ignoreTables as $ignoreTable) {
$dumpCommand .= $ignoreTable." ";
}
$dumpCommand .= " > " . $path . "ignoredStructure.sql";
shell_exec($dumpCommand);
shell_exec('cat '.$path.'ignoredStructure.sql >> '.$outputFile);
shell_exec('rm '.$path.'ignoredStructure.sql');
print "\nBackup made at ".date("d-m-Y H:i:s")."; filename is ".$outputFile."\n\n";
break;
case 'import':
$local = simplexml_load_string(file_get_contents($root."/app/etc/local.xml"), 'SimpleXMLElement', LIBXML_NOCDATA);
$info = (array)$local->global->resources->default_setup->connection;
if(isset($argv[2])) {
$filename = $argv[2];
} else {
$filename = 'database.sql';
}
if(file_exists($filename)) {
shell_exec("mysql -u ".$info['username']." -p".$info['password']." ".$info['dbname']." < ".$filename);
print "\nBackup imported at ".date("d-m-Y H:i:s")."\n\n";
} else {
print "\nFile ".$filename." not found\n\n";
}
break;
case 'reindexall':
print shell_exec('php '.$root.'/shell/indexer.php reindexall');
break;
case 'hints':
connectDb($root);
if($argv[2]=="admin") {
$storeId = 0;
$switch = $argv[3];
} else {
if(is_numeric($argv[2])) {
$storeId = $argv[2];
$switch = $argv[3];
} else { // default to '1'
$storeId = 1;
$switch = $argv[2];
}
}
$enable = "INSERT INTO core_config_data (scope, scope_id, path, value) VALUES ('default', $storeId, 'dev/debug/template_hints', 1), ('default', $storeId, 'dev/debug/template_hints_blocks', 1) ON DUPLICATE KEY UPDATE value=1;";
$disable = "DELETE FROM core_config_data WHERE path LIKE 'dev/debug/template_hints%' AND scope_id = '".$storeId."'";
if($switch=="on") {
mysql_query($enable) or die(mysql_error());
clearCache($root);
echo "\n";
if($storeId==0) { echo "Admin "; }
echo "Template hints are now enabled.\n\n";
} elseif($switch=="off") {
mysql_query($disable) or die(mysql_error());
clearCache($root);
echo "\n";
if($storeId==0) { echo "Admin "; }
echo "Template hints are now disabled.\n\n";
}
break;
case 'baseurl':
connectDb($root);
if(empty($argv[2])) {
//die("No base URL defined. Please give new base URL as second parameter.\n");
Mage::app();
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_write');
$stores = $db->fetchAll('SELECT * FROM core_store');
foreach($stores as $store) {
if($store['store_id'] == 1) continue;
if($store['store_id'] == 0) {
$baseurls[$store['store_id']] = prompt("What is the default base URL?");
} else {
$baseurls[$store['store_id']] = prompt("What is the base URL for store view '".$store['code']."' (".$store['store_id'].")? [leave empty for default]");
}
}
foreach($baseurls as $store_id=>$baseurl) {
if(isset($argv[3])) {
if(stripos($argv[3],"un")!==false) {
$type = 'unsecure';
} else {
$type = 'secure';
}
} else {
$type = 'secure & unsecure';
}
if(!empty($baseurl)) {
if (filter_var($baseurl, FILTER_VALIDATE_URL) === FALSE) {
die("Not a valid URL (" . $baseurl .")\n");
}
if(substr($baseurl,-1)!='/') {
die("The base URL has no trailing slash! (" . $baseurl .")\n");
}
if(isset($type) && $type == 'secure') {
$result = $db->query("INSERT INTO core_config_data SET value = ?, path = 'web/secure/base_url', scope_id = ? ON DUPLICATE KEY UPDATE value = ?", array($baseurl,$store_id,$baseurl));
} elseif(isset($type) && $type == 'unsecure') {
$result = $db->query("INSERT INTO core_config_data SET value = ?, path = 'web/unsecure/base_url', scope_id = ? ON DUPLICATE KEY UPDATE value = ?", array($baseurl,$store_id,$baseurl));
} else {
$result = $db->query("INSERT INTO core_config_data SET value = ?, path = 'web/secure/base_url', scope_id = ? ON DUPLICATE KEY UPDATE value = ?", array($baseurl,$store_id,$baseurl));
$result = $db->query("INSERT INTO core_config_data SET value = ?, path = 'web/unsecure/base_url', scope_id = ? ON DUPLICATE KEY UPDATE value = ?", array($baseurl,$store_id,$baseurl));
}
} else {
$result = $db->query("DELETE FROM core_config_data WHERE (path = 'web/unsecure/base_url' OR path = 'web/secure/base_url') AND scope_id = ?",array($store_id));
}
}
$db->query("UPDATE core_config_data SET value = '{{secure_base_url}}' WHERE path = 'web/secure/base_link_url'");
$db->query("UPDATE core_config_data SET value = '{{secure_base_url}}skin/' WHERE path = 'web/secure/base_skin_url'");
$db->query("UPDATE core_config_data SET value = '{{secure_base_url}}media/' WHERE path = 'web/secure/base_media_url'");
$db->query("UPDATE core_config_data SET value = '{{secure_base_url}}js/' WHERE path = 'web/secure/base_js_url'");
$db->query("UPDATE core_config_data SET value = '{{unsecure_base_url}}' WHERE path = 'web/unsecure/base_link_url'");
$db->query("UPDATE core_config_data SET value = '{{unsecure_base_url}}skin/' WHERE path = 'web/unsecure/base_skin_url'");
$db->query("UPDATE core_config_data SET value = '{{unsecure_base_url}}media/' WHERE path = 'web/unsecure/base_media_url'");
$db->query("UPDATE core_config_data SET value = '{{unsecure_base_url}}js/' WHERE path = 'web/unsecure/base_js_url'");
} else {
if (filter_var($argv[2], FILTER_VALIDATE_URL) === FALSE) {
die("Not a valid URL\n");
} else {
$baseurl = $argv[2];
}
if(substr($baseurl,-1)!='/') {
die("The base URL has no trailing slash!\n");
}
if(isset($argv[3])) {
if(stripos($argv[3],"un")!==false) {
$type = 'unsecure';
} else {
$type = 'secure';
}
} else {
$type = 'secure & unsecure';
}
if(isset($type) && $type == 'secure') {
$result = mysql_query("UPDATE core_config_data SET value = '".$baseurl."' WHERE path = 'web/secure/base_url'");
} elseif(isset($type) && $type == 'unsecure') {
$result = mysql_query("UPDATE core_config_data SET value = '".$baseurl."' WHERE path = 'web/unsecure/base_url'");
} else {
$result = mysql_query("UPDATE core_config_data SET value = '".$baseurl."' WHERE path = 'web/unsecure/base_url' OR path = 'web/secure/base_url'");
}
mysql_query("UPDATE core_config_data SET value = '{{secure_base_url}}' WHERE path = 'web/secure/base_link_url'");
mysql_query("UPDATE core_config_data SET value = '{{secure_base_url}}skin/' WHERE path = 'web/secure/base_skin_url'");
mysql_query("UPDATE core_config_data SET value = '{{secure_base_url}}media/' WHERE path = 'web/secure/base_media_url'");
mysql_query("UPDATE core_config_data SET value = '{{secure_base_url}}js/' WHERE path = 'web/secure/base_js_url'");
mysql_query("UPDATE core_config_data SET value = '{{unsecure_base_url}}' WHERE path = 'web/unsecure/base_link_url'");
mysql_query("UPDATE core_config_data SET value = '{{unsecure_base_url}}skin/' WHERE path = 'web/unsecure/base_skin_url'");
mysql_query("UPDATE core_config_data SET value = '{{unsecure_base_url}}media/' WHERE path = 'web/unsecure/base_media_url'");
mysql_query("UPDATE core_config_data SET value = '{{unsecure_base_url}}js/' WHERE path = 'web/unsecure/base_js_url'");
if($result) {
echo 'Base URL ('.$type.') is updated to '.$baseurl."\n";
} else {
echo "Could not update base URL.";
}
}
break;
case 'events':
echo shell_exec("grep -r 'Mage::dispatchEvent(' . | sed -e 's/.*Mage::dispatchEvent(//; s/,.*//; s/).*//;' | sed -e \"s/'//g;\" | grep -v '[^a-z_]' | grep -vE '^$' | sort | uniq -c");
break;
case 'cc': // fallthrough
case 'cache':
clearCache($root);
print "\nCache is cleared.\n\n";
break;
case 'cs': // fallthrough
case 'sessions': // fallthrough
case 'session':
shell_exec("rm -rf ".$root."/var/session/*");
print "\nSessions are cleared.\n\n";
break;
case 'index': // fallthrough
case 'indexer':
($argument = isset($argv[2]) ? $argv[2] : null);
if($argument=='options') {
print "Magento Indexer options\n";
print " --status <indexer> Show Indexer(s) Status\n";
print " --mode <indexer> Show Indexer(s) Index Mode\n";
print " --mode-realtime <indexer> Set index mode type \"Update on Save\"\n";
print " --mode-manual <indexer> Set index mode type \"Manual Update\"\n";
print " --reindex <indexer> Reindex Data\n";
print " info Show allowed indexers\n";
print " reindexall Reindex Data by all indexers\n";
print " Indexes:\n";
print " catalog_product_attribute Product Attributes\n";
print " catalog_product_price Product Prices\n";
print " catalog_url Catalog URL Rewrites\n";
print " catalog_product_flat Product Flat Data\n";
print " catalog_category_flat Category Flat Data\n";
print " catalog_category_product Category Products\n";
print " catalogsearch_fulltext Catalog Search Index\n";
print " cataloginventory_stock Stock Status\n";
print " tag_summary Tag Aggregation Data\n";
exit;
}
print shell_exec('php '.$root.'/shell/indexer.php '.$argument);
break;
case 'compiler':
($argument = isset($argv[2]) ? $argv[2] : null);
echo shell_exec('php '.$root.'/shell/compiler.php '.$argument);
break;
case 'cron':
shell_exec('php '.$root.'/cron.php');
echo "\nCron run at ".date("d-m-Y H:i:s")."\n\n";
break;
case 'install':
($argument = isset($argv[2]) ? $argv[2] : null);
print shell_exec('sh '.$root.'/mage install '.$argument);
break;
case 'connect':
shell_exec("chmod +x ".$root."/mage");
$argument = null;
foreach($argv as $key=>$value) {
if($key>1) {
$argument .= $value." ";
}
}
$argument = trim($argument);
if(stripos($argument,"upgrade")!==false) {
$result = prompt("Are you sure you want to run '".$argument."' ? [Y/N]");
if(strtolower($result)=="y") {
print shell_exec('sh '.$root.'/mage '.$argument);
print "\nUpgraded!\n\n";
}
} else {
print shell_exec('sh '.$root.'/mage '.$argument);
}
break;
case 'minstall':
$version = $argv[2];
if(strlen($version)==4) {
$newVersion = null;
for($i=0;$i<strlen($version);$i++) {
$newVersion .= $version[$i];
if($i!=strlen($version)-1) {
$newVersion .= ".";
}
}
$version = $newVersion;
}
$ls = glob("*");
if(count($ls)) {
echo "The current directory is not empty; are you sure you want to install Magento in this directory?\n";
exit;
}
shell_exec("wget http://www.magentocommerce.com/downloads/assets/".$version."/magento-".$version.".tar.gz");
shell_exec("tar -zxvf magento-".$version.".tar.gz");
shell_exec("mv magento/* magento/.htaccess .");
shell_exec("chmod -R o+w media var");
shell_exec("chmod o+w app/etc");
shell_exec("rm -rf magento/ magento-" . $newVersion . ".tar.gz");
shell_exec("chmod 775 -R app/etc var media");
print "\nMagento ".$version." downloaded.\n\n";
if(isset($argv[5])) {
$sampleVersion = $argv[5];
} else {
$sampleVersion = '1.6.1.0';
}
if(isset($argv[3])) {
shell_exec('mysql -e "create database ' . $argv[3] . ';"');
echo "Database " . $argv[3] . " is created.\n\n";
}
if(isset($argv[4]) AND $argv[4]=='sample') {
shell_exec("wget http://www.magentocommerce.com/downloads/assets/" . $sampleVersion . "/magento-sample-data-" . $sampleVersion . ".tar.gz");
shell_exec("tar -zxvf magento-sample-data-" . $sampleVersion . ".tar.gz");
shell_exec("cp -R magento-sample-data-" . $sampleVersion . "/* . ; rm -rf magento-sample-data-" . $sampleVersion);
shell_exec("chmod -R 777 media/catalog");
if(!isset($argv[3]) AND $argv[3]!="null") {
echo "Import the magento-sample-data-" . $sampleVersion .".sql file into an empty database.\n";
} else {
shell_exec("mysql " . $argv[3] . " < magento_sample_data_for_" . $sampleVersion . ".sql");
echo "Magento sample data file imported into " . $argv[3] . "\n";
}
}
break;
case 'devurl':
Mage::app();
$resource = Mage::getResourceModel('core/config');
$resource->saveConfig('web/unsecure/base_url', '{{base_url}}', 'default', 0);
$resource->saveConfig('web/secure/base_url', '{{base_url}}', 'default', 0);
echo "Secure & unsecure base URLs have been set to {{base_url}} (not recommended in production!)\n";
break;
case 'logs':
$status = $argv[2];
Mage::app();
$resource = Mage::getResourceModel('core/config');
if($status=='enable' || $status=='on' || $status=='1' || $status=='true') {
$status = 1;
} else {
$status = 0;
}
$resource->saveConfig('dev/log/active', $status, 'default', 0);
if($status) {
echo "Logging is now enabled.\n";
} else {
echo "Logging is now disabled.\n";
}
break;
case 'user':
Mage::app();
$username = prompt("What is the desired username?");
$password = prompt("What is the desired password?");
$email = prompt("What is users' email address?");
$firstname = prompt("What is users' first name?");
$lastname = prompt("What is users' last name?");
try {
//create new user
$user = Mage::getModel('admin/user')
->setData(array(
'username' => $username,
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'password' => $password,
'is_active' => 1
))->save();
$user->setRoleIds(array(1))
->setRoleUserId($user->getUserId())
->saveRelations();
echo "User '".$username."' has been succesfully created.\n\n";
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
break;
case 'passwd':
Mage::app();
$username = prompt("For which user do you want to change the password?");
$password = prompt("What is the desired password?");
try {
//create new user
Mage::getModel('admin/user')
->loadByUsername($username)
->setPassword($password)
->save();
echo $username."'s password has been succesfully changed.\n\n";
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
break;
case 'conflicts':
$modules = array_slice(scandir($root . '/app/etc/modules'),2);
$potentialConflicts = array();
foreach($modules as $module) {
if(substr($module,0,4)=='Mage') continue; // skip core modules
$xml = file_get_contents('app/etc/modules/' . $module);
if(!$xml) continue;
$data = new SimpleXMLElement($xml);
$moduleName = array_pop(array_keys(get_object_vars($data->modules)));
if(stripos($moduleName,'_')!==false) {
list($vendor,$package) = explode('_',$moduleName);
} else {
$vendor = $moduleName;
$package = null;
}
$codePool = $data->modules->{$moduleName}->codePool[0];
$active = $data->modules->{$moduleName}->active[0];
if($active!='true' AND $active!='1') continue; // skip disabled modules
$configFilePath = $root . '/app/code/' . $codePool . '/' . $vendor . '/' . $package . '/etc/config.xml';
if(!file_exists($configFilePath)) {
echo "Cannot open {$configFilePath}; it does not exist while we expected it to be there!\n";
continue;
}
$configFile = file_get_contents($configFilePath);
if(!$configFile) continue;
$config = new SimpleXMLElement($configFile);
$areas = array('global','frontend','adminhtml');
$elements = array('blocks','models');
foreach($areas as $area) {
foreach($elements as $element) {
if(!count($config->{$area}->{$element})) continue;
$result = $config->xpath($area.'/'.$element);
foreach($result as $part) {
foreach($part as $sub) {
foreach($sub as $rewrite) {
if($rewrite->getName()!='rewrite') continue;
foreach($rewrite as $module=>$class) {
echo 'Rewrite found in ' . $moduleName . ' / ' . $area . ' / ' . $element . ' / ' . $sub->getName() . ' for ' . $module . ' to; ' . $class . "\n";
$potentialConflicts[$area . '/' . $element . '/' . $sub->getName() . '/' . $module][] = (string)$class;
}
}
}
}
}
}
}
echo "\n";
$conflicts = 0;
foreach($potentialConflicts as $rewritePath=>$potentialConflict) {
if(count($potentialConflict)>1) {
echo 'Conflict found for ' . $rewritePath . '; '.implode(', ',$potentialConflict)."\n";
$conflicts++;
}
}
if(!$conflicts) echo 'No conflicts were found. Beware; this does not mean there aren\'t any conflicts, just that they haven\'t been found! Use the force, read the source.' . "\n";
break;
case 'translations':
if(count($argv)<3) {
echo "Use 'mate translate Package Module'\n\n";
exit;
}
Mage::app();
$package = $argv[2];
$module = $argv[3];
if(isset($argv[4])) {
$language = $argv[4];
} else {
$language = 'nl_NL';
}
$modInfo = modInfo($root,$package,$module);
if($modInfo) {
echo "\nAll found translation strings in the module ".$package."_".$module.":\n\n";
$dir = $root."/app/code/".$modInfo['codepool']."/".$package."/".$module."/";
$finds = shell_exec("find ".$dir." | xargs grep '__'");
$finds = explode("\n",$finds);
foreach($finds as $find) {
@list($filename,$foundtext) = explode(":",$find,2);
if($filename && $foundtext) {
$filename = str_replace($dir,'',$filename);
$filenameLengths[] = strlen($filename);
}
}
$name = $package.'_'.$module;
$configXml = $root . '/app/code/'.$modInfo['codepool'].'/'.$package.'/'.$module.'/etc/config.xml';
$translateFiles = array();
if(file_exists($configXml)) {
$configXml = simplexml_load_string(file_get_contents($configXml));
if(is_object($configXml) AND isset($configXml->adminhtml) AND isset($configXml->adminhtml->translate->modules->{$name}->files)) {
$translateFiles[] = $configXml->adminhtml->translate->modules->{$name}->files->default;
}
if(is_object($configXml) AND isset($configXml->adminhtml) AND isset($configXml->frontend->translate->modules->{$name}->files)) {
$translateFiles[] = $configXml->frontend->translate->modules->{$name}->files->default;
}
}
foreach($translateFiles as $transFile) {
if (($handle = fopen($root.'/app/locale/'.$language.'/'.$transFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(count($data)==2) {
$translations[$data[0]] = $data[1];
}
}
}
}
$maxLength = max($filenameLengths);
$notfounds = array();
foreach($finds as $find) {
@list($filename,$foundtext) = explode(":",$find,2);
if($filename && $foundtext) {
$filename = str_replace($dir,'',$filename);
$foundtext = trim($foundtext);
$beginTrans = stripos($foundtext,'__(\'');
if($beginTrans) {
echo str_pad($filename.": ",($maxLength+10),' ',STR_PAD_RIGHT);
$foundtext = stripslashes($foundtext);
$foundtext = substr($foundtext,$beginTrans+4);
$endTrans = stripos($foundtext,'\'');
$foundtext = substr($foundtext,0,$endTrans);
echo $foundtext;
echo "\n";
$translation = $translations[$foundtext];
if($translation==$foundtext || !$translation) {
$translation = "NOT FOUND";
$notfounds[] = $foundtext;
}
echo str_pad(" ",$maxLength+10,' ',STR_PAD_RIGHT)."Translation ".$language.": " . $translation;
echo "\n";
echo str_pad("=",$maxLength+50,'=',STR_PAD_RIGHT);
echo "\n";
}
}
}
echo "\nCopy/paste this into ".implode(" or ",$translateFiles)." and add the translations;\n\n";
foreach($notfounds as $nf) {
echo '"'.$nf.'",""';
echo "\n";
}
}
break;
case 'modinfo':
if(count($argv)<3) {
echo "Use 'mate modinfo Package Module'\n\n";
exit;
}
$package = $argv[2];
$module = $argv[3];
$modInfo = modInfo($root,$package,$module);
if($modInfo) {
echo "Module information ".$package."_".$module."\n";
foreach($modInfo as $key=>$value) {
echo $key.": ".$value."\n";
}
} else {
echo "Module ".$package."_".$module." is not found in this installation.\n";
}
break;
default;
print "\nMate (command line Magento toolkit) - by Elgentos <[email protected]>\n\n";
print "mate index/indexer <default indexer options> (run 'mate indexer options' to see all options)\n";
print "mate reindexall - shortcut for mate index reindexall\n";
print "mate compiler <default compiler options> (run 'mate compiler' to see all options')\n";
print "mate connect <default mage options> (run 'mate connect' to see all options)\n";
print "mate modules / mate mod - overview the module list\n";
print "mate enable Package_Module - enable a module\n";
print "mate disable Package_Module - disable a module\n";
print "mate toggle Package_Module - toggle the status of a module\n";
print "mate skeleton Package Module Version - create a skeleton module for development\n";
print "mate backup - make a database dump to database.sql\n";
print "mate import - import a backupped database\n";
print "mate events - list all events (for observers) found in the codebase/\n";
print "mate cc/cache - clear cache\n";
print "mate cs/session(s) - clear sessions\n";
print "mate cron - run the cron\n";
print "mate connect {arguments} - use default 'mage' exec to interface Magento Connect\n";
print "mate install channelName packageName - install packages using default 'mage' exec\n";
print "mate user - create an admin account, will prompt for username, pass, email and name\n";
print "mate passwd - change password for a user, will prompt for username & pass\n";
print "mate minstall {version} {db_name} {sample} {sampleversion}- install Magento (use '1600' or '1.6.2.0' for {version}). Give up {db_name} to automatically create database. Use 'sample' for {sample} to download sample data file.\n";
print "mate resetmods - reset all permissions of dirs to 775 and files to 664\n";
print "mate devurl - set the secure & unsecure base url to {{base_url}} for during development\n";
print "mate hints (admin/scope id/leave empty) on/off - turn the template hints on and off for store or admin\n";
print "mate baseurl {baseURL} [secure/unsecure] - set a new base url (both secure and unsecure or one of each) for the current installation\n";
print "mate conflicts - looks through all third party modules and checks for possible conflicts\n";
print "mate events - look for all events that are available within this installation\n";
print "mate logs [enable/disable] / [on/off] - enable/disable logging to var/log/system.log\n";
print "mate backup - create a backup of the current database (saves to database.sql)\n";
print "mate import {filename} - import a backup into the current database (caution; will drop all tables). When filename is omitted, 'database.sql' is assumed\n";
print "mate translations {package} {module} {language} - find untranslated translation strings in a module for any given language. Language defaults to 'nl_NL'\n";
if(!empty($root)) { print "\nCurrent root dir is ".$root."\n"; }
print "\n";
break;
}
function modInfo($root,$package,$module) {
$modinfo = moduleOverview($root,true);
$modinfo = explode("\n",$modinfo);
$data = array();
foreach($modinfo as $mod) {
@list($data['name'],$data['stats'],$data['codepool'],$data['version']) = explode("\t",$mod);
$data['name'] = trim($data['name']);
$data['stats'] = trim($data['stats']);
$data['codepool'] = trim($data['codepool']);
$data['version'] = trim($data['version']);
if($data['name']==$package."_".$module) {
return $data;
}
}
return false;
}
function isModuleEnabled($filename) {
return !stripos(file_get_contents($filename),">false<");
}
function moduleOverview($root,$tabs=false) {
$return = null;
foreach(explode("\n",shell_exec('ls '.$root."/app/etc/modules")) as $xml) {
if(!empty($xml)) {
$name = str_replace(".xml","",$xml);
$return .= str_pad($name,50," ",STR_PAD_RIGHT);
if($tabs) { $return .= "\t"; }
$filename = $root."/app/etc/modules/".$xml;
if(isModuleEnabled($filename)) { $status = "enabled"; } else { $status = "disabled"; }
$return .= str_pad($status,15," ",STR_PAD_RIGHT);
if($tabs) { $return .= "\t"; }
if(!stripos(file_get_contents($filename),">local<")) {
$codepool = 'community';
} else {
$codepool = 'local';
}
$return .= str_pad($codepool,15," ",STR_PAD_RIGHT);
if($tabs) { $return .= "\t"; }
$configXml = $root . '/app/code/'.$codepool.'/'.str_replace("_","/",$name).'/etc/config.xml';
if(file_exists($configXml)) {
$configXml = simplexml_load_string(file_get_contents($configXml));
if(is_object($configXml) AND isset($configXml->modules) AND isset($configXml->modules->{$name})) {
$return .= $configXml->modules->{$name}->version;
} else {
$return .= "N/A";
}
} else {
$return .= "N/A";
}
}
$return .= "\n";
}
return $return;
}
function connectDb($root) {
$local = simplexml_load_string(file_get_contents($root."/app/etc/local.xml"), 'SimpleXMLElement', LIBXML_NOCDATA);
$info = (array)$local->global->resources->default_setup->connection;
$m = mysql_connect((string)$info['host'],(string)$info['username'],(string)$info['password']);
mysql_select_db((string)$info['dbname']);
}
function clearCache($root) {
shell_exec("rm -rf ".$root."/var/cache/*");
}
function prompt($prompt,$silent=false) {
echo $prompt." ";
if($silent) {
system('stty -echo');
} else {
system('stty echo');
}
$return = trim(fgets(STDIN));
system('stty echo');
// add a new line since the users CR didn't echo
echo "\n";
return $return;
}