PluginProbe
WP-Stateless – Google Cloud Storage / 3.1.1
WP-Stateless – Google Cloud Storage v3.1.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.1.1, at lib/cli/class-sm-cli-command.php

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