-
Notifications
You must be signed in to change notification settings - Fork 18
/
mageploy.php
258 lines (220 loc) · 11.4 KB
/
mageploy.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
<?php
if (file_exists('abstract.php')) {
require_once 'abstract.php';
} else {
require_once 'shell/abstract.php';
}
class Mage_Shell_Mageploy extends Mage_Shell_Abstract {
const TERM_COLOR_RED = '31m';
const TERM_COLOR_GREEN = '32m';
const TERM_COLOR_YELLOW = '33m';
/** @var PugMoRe_Mageploy_Model_Io_File */
protected $_io;
/** @var PugMoRe_Mageploy_Helper_Data */
protected $_helper;
protected $_options = array(
'--h/help' => 'to show this help',
'--u/user [val]' => 'set current user name',
'--t/track [val]' => '0 to disable tracking, any other value or blank to enable it',
'--hi/history [n]' => 'Show the last n changes. Leave n blank to show all',
'--s/status' => 'Show if there are any changes to be imported',
'--r/run [id]' => 'Import changes for specified action (not recommended); leave id blank to import all',
);
protected function _construct() {
$this->_initSession('admin', 'p4ssw0rd');
$this->_helper = Mage::helper('pugmore_mageploy');
$this->_io = new PugMoRe_Mageploy_Model_Io_File();
return parent::_construct();
}
private function __getColoredString($str, $color = null) {
if (is_null($color))
return $str;
return sprintf("\033[0;%s%s\033[0m", $color, $str);
}
private function __getVersion() {
return $this->_helper->getVersion();
}
protected function _initSession() {
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
}
protected function _getControllerClassPath($controllerModule, $controllerName) {
$moduleParts = explode('_', $controllerModule);
$realModule = implode('_', array_splice($moduleParts, 0, 2));
$file = Mage::getModuleDir('controllers', $realModule);
if (count($moduleParts)) {
$file .= DS . implode(DS, $moduleParts);
}
$parts = explode('_', uc_words($controllerName));
if (count($parts)) {
$file .= DS . implode(DS, $parts);
}
$file .= 'Controller.php';
return $file;
}
public function getArgs() {
$val = null;
$args = func_get_args();
foreach ($args as $arg) {
$val = $this->getArg($arg);
if ($val !== false) break;
}
return $val;
}
public function _getControllerClassName($controllerModule, $controllerName) {
$class = $controllerModule . '_' . uc_words($controllerName) . 'Controller';
return $class;
}
public function run() {
// Set tracking
$track = $this->getArgs('t', 'track');
if ($track !== false) {
if (!strcmp('0', $track)) {
$doTracking = $this->_helper->disable();
} else {
$doTracking = $this->_helper->enable();
}
} else {
$doTracking = $this->_helper->isActive();
}
// Set username
$user = $this->getArgs('u', 'user');
if ($user !== false) {
$this->_helper->setUser($user);
}
if ($this->getArgs('s', 'status')) {
$this->_printHeader();
$pendingList = $this->_io->getPendingList();
if (count($pendingList)) {
printf("Pending Actions list:\r\n");
foreach ($pendingList as $i => $row) {
$actionDescr = sprintf("ID: %d\t - %s (%s on %s)", ($i + 1), $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_DESCR], $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_USER], strftime("%c", $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_TIMESTAMP]));
$spacer = str_repeat(" ", max(0, 40 - strlen($actionDescr)));
printf("%s\r\n", $actionDescr);
}
printf("\r\nTotal pending actions: %d\r\n", count($pendingList));
} else {
printf("There aren't any pending actions to execute.\r\n");
}
} else if ($limit = $this->getArgs('hi', 'history')) {
$this->_printHeader();
$historyList = $this->_io->getHistoryList($limit);
if (count($historyList)) {
printf("Global Actions list:\r\n");
foreach ($historyList as $i => $row) {
$actionDescr = sprintf("ID: %d\t - %s (%s on %s)", ($i + 1), $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_DESCR], $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_USER], strftime("%c", $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_TIMESTAMP]));
$spacer = str_repeat(" ", max(0, 40 - strlen($actionDescr)));
printf("%s\r\n", $actionDescr);
}
printf("\r\nTotal global actions listed: %d\r\n", count($historyList));
} else {
printf("There aren't any actions tracked.\r\n");
}
} else if ($id = $this->getArgs('r', 'run')) {
$this->_printHeader();
$pendingList = $this->_io->getPendingList();
if (count($pendingList)) {
$executed = 0;
$session = Mage::getSingleton('adminhtml/session');
foreach ($pendingList as $i => $row) {
if (($id > 0) && ($i + 1 != $id)) {
continue;
}
$actionExecutorClass = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_EXECUTOR_CLASS];
$actionExecutorVersion = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_VERSION];
$controllerModule = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_CONTROLLER_MODULE];
$controllerName = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_CONTROLLER_NAME];
$controllerClassName = $this->_getControllerClassName($controllerModule, $controllerName);
if (class_exists($actionExecutorClass)) {
$controllerFileName = $this->_getControllerClassPath($controllerModule, $controllerName);
if (file_exists($controllerFileName)) {
include_once $controllerFileName;
} else {
printf("Error: file '%s' not found!\r\n", $controllerFileName);
}
if (class_exists($controllerClassName)) {
$actionExecutor = new $actionExecutorClass();
$parameters = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_PARAMS];
try {
$request = $actionExecutor->decode($parameters, $actionExecutorVersion);
$controller = new $controllerClassName($request, new PugMoRe_Mageploy_Controller_Response_Http);
$action = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_NAME] . 'Action';
$controller->preDispatch();
$controller->$action();
$controller->postDispatch();
} catch (Zend_Controller_Response_Exception $e) {
# do nothing: avoid "Headers already sent" error message
} catch (Exception $e) {
$session->addError($e->getMessage());
}
// Add messages in body response in case of Ajax requests
$messages = $session->getMessages(true);
if ($request && $request->getParam('isAjax', false)) {
$body = $controller->getResponse()->getBody();
$msg = Mage::getSingleton('core/message')->notice($body);
$messages->add($msg);
}
foreach ($messages->getItems() as $message) {
$messageType = $message->getType();
switch ($messageType) {
case Mage_Core_Model_Message::ERROR:
$color = self::TERM_COLOR_RED;
break;
case Mage_Core_Model_Message::SUCCESS:
$color = self::TERM_COLOR_GREEN;
break;
default: #break intentionally omitted
case Mage_Core_Model_Message::WARNING: #break intentionally omitted
case Mage_Core_Model_Message::NOTICE:
$color = self::TERM_COLOR_YELLOW;
break;
}
printf("Action ID #%d - %s %s\r\n", ($i + 1), $this->__getColoredString($message->getType(), $color), $message->getText());
}
$executed++;
// register executed action
$this->_io->done($row);
} else {
printf("Error: class '%s' not found!\r\n", $controllerClassName);
}
} else {
printf("Error: class '%s' not found!\r\n", $actionExecutorClass);
}
// Yes, PHP is Object Oriented but don't forget the
// Superglobals! After all PHP is not Java :-)
$_GET = array();
$_POST = array();
$_REQUEST = array();
// And don't forget to reinitialize Magento to avoid
// problems with already populated objects (i.e. registry)
Mage::reset();
Mage::init($this->_appCode, $this->_appType);
}// end foreach
printf("\r\nExecuted actions: %d/%d\r\n", $executed, count($pendingList));
} else {
printf("There aren't any pending actions to execute.\r\n");
}
} else {
echo $this->usageHelp($doTracking);
}
printf("\r\n");
}
public function usageHelp() {
$this->_printHeader();
$help = "Usage:\tphp mageploy.php --[options]\r\n\r\n";
foreach ($this->_options as $option => $description) {
$help .= "$option" . str_repeat(" ", 20 - strlen($option)) . "$description\r\n";
}
return $help . "\r\n";
}
protected function _printHeader(/*$isActive*/) {
$isActive = $this->_helper->isActive() ? $this->__getColoredString("tracking is active", self::TERM_COLOR_GREEN) : $this->__getColoredString("tracking is not active", self::TERM_COLOR_RED);
$isAnonymous = $this->_helper->isAnonymousUser();
$user = $this->_helper->getUser();
$user = $this->__getColoredString("user is " . $user, $isAnonymous ? self::TERM_COLOR_YELLOW : self::TERM_COLOR_GREEN);
printf("\r\nMageploy v %s - %s - %s\r\n\r\n", $this->__getVersion(), $isActive, $user);
}
}
$shell = new Mage_Shell_Mageploy();
$shell->run();