PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.1
WP-Stateless – Google Cloud Storage v3.2.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / cli / class-sm-cli-command.php

class-sm-cli-command.php in WP-Stateless – Google Cloud Storage 3.2.1, at lib/cli/class-sm-cli-command.php

331 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WP CLI SM Commands
5 */
6 if (defined('WP_CLI') && WP_CLI && class_exists('WP_CLI_Command')) {
7
8 /**
9 * WP-CLI command
10 */
11 class SM_CLI_Command extends WP_CLI_Command {
12
13 public $url;
14
15 /**
16 * @param $args
17 * @param $assoc_args
18 */
19 public function __construct($args = array(), $assoc_args = array()) {
20 parent::__construct();
21
22 if (php_sapi_name() != 'cli') {
23 die('Must run from command line');
24 }
25
26 //** Setup some server settings */
27 set_time_limit(0);
28 ini_set('memory_limit', '2G');
29 //** Setup error handling */
30 ini_set('display_errors', 1);
31 ini_set('log_errors', 0);
32 ini_set('html_errors', 0);
33
34 if (!class_exists('SM_CLI_Process')) {
35 require_once(dirname(__FILE__) . '/class-sm-cli-process.php');
36 }
37
38 if (!class_exists('SM_CLI')) {
39 require_once(dirname(__FILE__) . '/class-sm-cli.php');
40 }
41
42 /** Be sure that we add url parameter to commands if we have MultiSite installation. */
43 $this->url = is_multisite() ? WP_CLI::get_runner()->config['url'] : false;
44 }
45
46 /**
47 * Sync Data
48 *
49 * ## OPTIONS
50 *
51 * <type>
52 * : Which data we want to sync. May be images or files.
53 *
54 * --url
55 * : Blog URL if multisite installation.
56 *
57 * --start
58 * : Indent (sql start). It's ignored on batches.
59 *
60 * --limit
61 * : Limit per query (sql limit)
62 *
63 * --end
64 * : Where ( on which row ) we should stop script. It's ignored on batches
65 *
66 * --batch
67 * : Number of Batch. Default is 1.
68 *
69 * --batches
70 * : General amount of batches.
71 *
72 * --b
73 * : Runs command using batches till it's done. Other parameters will be ignored. There are 10 batches by default. Batch is external command process
74 *
75 * --log
76 * : Show more information in command line
77 *
78 * --o
79 * : Process includes database optimization and transient removing.
80 *
81 * --order
82 * : Order. May be ASC or DESC
83 *
84 * ## EXAMPLES
85 *
86 * wp stateless sync images --url=example.com --b
87 * : Run process looping 10 batches. Every batch is external command 'wp stateless sync images --url=example.com --batch=<number> --batches=10'
88 *
89 * wp stateless sync images --url=example.com --b --batches=100
90 * : Run process looping 100 batches.
91 *
92 * wp stateless sync images --url=example.com --b --batches=10 --batch=2
93 * : Run second batch from 10 batches manually.
94 *
95 * wp stateless sync images --url=example.com --log
96 * : Run default process showing additional information in command line.
97 *
98 * wp stateless sync images --url=example.com --end=3000 --limit=50
99 * : Run process from 1 to 3000 row. Splits process by limiting queries to 50 rows. So, the current example does 60 queries ( 3000 / 50 = 60 )
100 *
101 * wp stateless sync images --url=example.com --start=777 --end=3000 --o
102 * : Run process from 777 to 3000 row. Also does database optimization and removes transient in the end.
103 *
104 * @synopsis <type> [--url=<val>] [--start=<val>] [--limit=<val>] [--end=<val>] [--batch=<val>] [--batches=<val>] [--b] [--log] [--o] [--order=<val>]
105 * @param $args
106 * @param $assoc_args
107 */
108 public function sync($args, $assoc_args) {
109
110 $sm_mode = ud_get_stateless_media()->get('sm.mode');
111 if ($sm_mode === 'stateless') {
112 WP_CLI::error('Sync is not supported in Stateless mode');
113 }
114 //** DB Optimization process */
115 if (isset($assoc_args['o'])) {
116 $this->_before_command_run();
117 }
118 //** Run batches */
119 if (isset($assoc_args['b'])) {
120 if (empty($args[0])) {
121 WP_CLI::error('Invalid type parameter');
122 }
123 $this->_run_batches('sync', $args[0], $assoc_args);
124 }
125 //** Or run command as is. */
126 else {
127 if (!class_exists('SM_CLI_Sync')) {
128 require_once(dirname(__FILE__) . '/class-sm-cli-sync.php');
129 }
130 if (class_exists('SM_CLI_Sync')) {
131 $object = new SM_CLI_Sync($args, $assoc_args);
132 $controller = !empty($args[0]) ? $args[0] : false;
133 if ($controller && is_callable(array($object, $controller))) {
134 call_user_func(array($object, $controller));
135 } else {
136 WP_CLI::error('Invalid type parameter');
137 }
138 } else {
139 WP_CLI::error('Class SM_CLI_Sync is undefined.');
140 }
141 }
142 //** Get rid of all transients and run DB optimization again */
143 if (isset($assoc_args['o'])) {
144 $this->_after_command_run();
145 }
146 }
147
148 /**
149 * Upgrade Data
150 *
151 * ## OPTIONS
152 *
153 * <type>
154 * : Which data we want to upgrade
155 *
156 * --start
157 * : Indent (sql start). It's ignored on batches.
158 *
159 * --limit
160 * : Limit per query (sql limit)
161 *
162 * --end
163 * : Where ( on which row ) we should stop script. It's ignored on batches
164 *
165 * --batch
166 * : Number of Batch. Default is 1.
167 *
168 * --batches
169 * : General amount of batches.
170 *
171 * --b
172 * : Runs command using batches till it's done. Other parameters will be ignored. There are 10 batches by default. Batch is external command process
173 *
174 * --log
175 * : Show more information in command line
176 *
177 * --o
178 * : Process includes database optimization and transient removing.
179 *
180 * --url
181 * : Blog URL if multisite installation.
182 *
183 * ## EXAMPLES
184 *
185 * wp stateless upgrade meta --url=example.com --b
186 * : Run process looping 10 batches. Every batch is external command 'wp stateless upgrade meta --url=example.com --batch=<number> --batches=10'
187 *
188 * wp stateless upgrade meta --url=example.com --b --batches=100
189 * : Run process looping 100 batches.
190 *
191 * wp stateless upgrade meta --url=example.com --b --batches=10 --batch=2
192 * : Run second batch from 10 batches manually.
193 *
194 * wp stateless upgrade meta --url=example.com --log
195 * : Run default process showing additional information in command line.
196 *
197 * wp stateless upgrade meta --url=example.com --end=3000 --limit=50
198 * : Run process from 1 to 3000 row. Splits process by limiting queries to 50 rows. So, the current example does 60 queries ( 3000 / 50 = 60 )
199 *
200 * wp stateless upgrade meta --url=example.com --start=777 --end=3000 --o
201 * : Run process from 777 to 3000 row. Also does database optimization and removes transient in the end.
202 *
203 * @synopsis <type> [--url=<val>] [--start=<val>] [--limit=<val>] [--end=<val>] [--batch=<val>] [--batches=<val>] [--b] [--log] [--o]
204 * @param $args
205 * @param $assoc_args
206 */
207 public function upgrade($args, $assoc_args) {
208 //** DB Optimization process */
209 if (isset($assoc_args['o'])) {
210 $this->_before_command_run();
211 }
212 //** Run batches */
213 if (isset($assoc_args['b'])) {
214 if (empty($args[0])) {
215 WP_CLI::error('Invalid type parameter');
216 }
217 $this->_run_batches('upgrade', $args[0], $assoc_args);
218 }
219 //** Or run command as is. */
220 else {
221 if (!class_exists('SM_CLI_Upgrade')) {
222 require_once(dirname(__FILE__) . '/class-sm-cli-upgrade.php');
223 }
224 if (class_exists('SM_CLI_Upgrade')) {
225 $object = new SM_CLI_Upgrade($args, $assoc_args);
226 $controller = !empty($args[0]) ? $args[0] : false;
227 if ($controller && is_callable(array($object, $controller))) {
228 call_user_func(array($object, $controller));
229 } else {
230 WP_CLI::error('Invalid type parameter');
231 }
232 } else {
233 WP_CLI::error('Class SM_CLI_Upgrade is undefined.');
234 }
235 }
236 //** Get rid of all transients and run DB optimization again */
237 if (isset($assoc_args['o'])) {
238 $this->_after_command_run();
239 }
240 }
241
242 /**
243 * Runs batches
244 */
245 private function _run_batches($method, $type, $assoc_args) {
246 $batches = isset($assoc_args['batches']) ? $assoc_args['batches'] : 10;
247 if (!is_numeric($batches) || $batches <= 0) {
248 WP_CLI::error('Parameter --batches must have numeric value.');
249 }
250 $limit = isset($assoc_args['limit']) ? $assoc_args['limit'] : 100;
251 if (!is_numeric($limit) || $limit <= 0) {
252 WP_CLI::error('Parameter --limit must have numeric value.');
253 }
254 $force = isset($assoc_args['force']) ? '--force' : '';
255
256 for ($i = 1; $i <= $batches; $i++) {
257
258 if (!empty($this->url)) {
259 $command = "wp stateless {$method} {$type} {$force} --batch={$i} --batches={$batches} --limit={$limit} --url={$this->url}";
260 } else {
261 $command = "wp stateless {$method} {$type} {$force} --batch={$i} --batches={$batches} --limit={$limit}";
262 }
263
264 WP_CLI::line('...');
265 WP_CLI::line("Launching external command '{$command}'");
266 WP_CLI::line('Waiting...');
267
268 @ob_flush();
269 flush();
270
271 $r = SM_CLI::launch($command, false, true);
272
273 if ($r->return_code) {
274 WP_CLI::error("Something went wrong. External command process failed.");
275 } else {
276 echo $r->stdout;
277 }
278 }
279 }
280
281 /**
282 * Optimization process
283 * Runs before command's process
284 */
285 private function _before_command_run() {
286 WP_CLI::line("Starting Database optimization process. Waiting...");
287 @ob_flush();
288 flush();
289 $command = !empty($this->url) ? "wp db optimize --url={$this->url}" : "wp db optimize";
290 $r = SM_CLI::launch($command, false, true);
291 if ($r->return_code) {
292 WP_CLI::error("Something went wrong. Database optimization process failed.");
293 } else {
294 WP_CLI::success("Database is optimized");
295 }
296 }
297
298 /**
299 * Optimization process
300 * Runs after command's process
301 */
302 private function _after_command_run() {
303 //** Run transient flushing */
304 WP_CLI::line("Starting remove transient. Waiting...");
305 @ob_flush();
306 flush();
307 $command = !empty($this->url) ? "wp transient delete-all --url={$this->url}" : "wp transient delete-all";
308 $r = SM_CLI::launch($command, false, true);
309 if ($r->return_code) {
310 WP_CLI::error("Something went wrong. Transient process failed.");
311 } else {
312 WP_CLI::success("Transient is removed");
313 }
314 //** Run MySQL optimization */
315 WP_CLI::line("Starting Database optimization process. Waiting...");
316 @ob_flush();
317 flush();
318 $command = !empty($this->url) ? "wp db optimize --url={$this->url}" : "wp db optimize";
319 $r = SM_CLI::launch($command, false, true);
320 if ($r->return_code) {
321 WP_CLI::error("Something went wrong. Database optimization process failed.");
322 } else {
323 WP_CLI::success("Database is optimized");
324 }
325 }
326 }
327
328 /** Add the commands from above */
329 WP_CLI::add_command('stateless', 'SM_CLI_Command');
330 }
331