PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.2
WP-Stateless – Google Cloud Storage v2.2.2
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 2.2.2, at lib/cli/class-sm-cli-command.php

231 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP CLI SM Commands
4 *
5 */
6
7 if( defined( 'WP_CLI' ) && WP_CLI ) {
8
9 /**
10 * WP-CLI command
11 *
12 */
13 class SM_CLI_Command extends WP_CLI_Command {
14
15 public $url;
16
17 /**
18 * @param $args
19 * @param $assoc_args
20 */
21 public function __construct( $args = array(), $assoc_args = array() ) {
22 parent::__construct();
23
24 if ( php_sapi_name() != 'cli' ) {
25 die('Must run from command line');
26 }
27
28 //** Setup some server settings */
29 set_time_limit( 0 );
30 ini_set( 'memory_limit', '2G' );
31 //** Setup error handling */
32 error_reporting( E_ALL );
33 ini_set( 'display_errors', 1 );
34 ini_set( 'log_errors', 0 );
35 ini_set( 'html_errors', 0 );
36
37 if( !class_exists( 'SM_CLI_Process' ) ) {
38 require_once( dirname( __FILE__ ) . '/class-sm-cli-process.php' );
39 }
40
41 if( !class_exists( 'SM_CLI' ) ) {
42 require_once( dirname( __FILE__ ) . '/class-sm-cli.php' );
43 }
44
45 /** Be sure that we add url parameter to commands if we have MultiSite installation. */
46 $this->url = is_multisite() ? WP_CLI::get_runner()->config[ 'url' ] : false;
47
48 }
49
50 /**
51 * Upgrade Data
52 *
53 * ## OPTIONS
54 *
55 * <type>
56 * : Which data we want to upgrade
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 * ## EXAMPLES
83 *
84 * wp sm upgrade meta --url=example.com --b
85 * : Run process looping 10 batches. Every batch is external command 'wp sm upgrade meta --url=example.com --batch=<number> --batches=10'
86 *
87 * wp sm upgrade meta --url=example.com --b --batches=100
88 * : Run process looping 100 batches.
89 *
90 * wp sm upgrade meta --url=example.com --b --batches=10 --batch=2
91 * : Run second batch from 10 batches manually.
92 *
93 * wp sm upgrade meta --url=example.com --log
94 * : Run default process showing additional information in command line.
95 *
96 * wp sm upgrade meta --url=example.com --end=3000 --limit=50
97 * : 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 )
98 *
99 * wp sm upgrade meta --url=example.com --start=777 --end=3000 --o
100 * : Run process from 777 to 3000 row. Also does database optimization and removes transient in the end.
101 *
102 * @synopsis [<type>] [--start=<val>] [--limit=<val>] [--end=<val>] [--batch=<val>] [--batches=<val>] [--b] [--log] [--o]
103 * @param $args
104 * @param $assoc_args
105 */
106 public function upgrade( $args, $assoc_args ) {
107 //** DB Optimization process */
108 if( isset( $assoc_args[ 'o' ] ) ) {
109 $this->_before_command_run();
110 }
111 //** Run batches */
112 if( isset( $assoc_args[ 'b' ] ) ) {
113 if( empty( $args[0] ) ) {
114 WP_CLI::error( 'Invalid type parameter' );
115 }
116 $this->_run_batches( 'upgrade', $args[0], $assoc_args );
117 }
118 //** Or run command as is. */
119 else {
120 if( !class_exists( 'SM_CLI_Upgrade' ) ) {
121 require_once( dirname( __FILE__ ) . '/class-sm-cli-upgrade.php' );
122 }
123 if( class_exists( 'SM_CLI_Upgrade' ) ) {
124 $object = new SM_CLI_Upgrade( $args, $assoc_args );
125 $controller = !empty( $args[0] ) ? $args[0] : false;
126 if( $controller && is_callable( array( $object, $controller ) ) ) {
127 call_user_func( array( $object, $controller ) );
128 } else {
129 WP_CLI::error( 'Invalid type parameter' );
130 }
131 } else {
132 WP_CLI::error( 'Class SM_CLI_Upgrade is undefined.' );
133 }
134 }
135 //** Get rid of all transients and run DB optimization again */
136 if( isset( $assoc_args[ 'o' ] ) ) {
137 $this->_after_command_run();
138 }
139 }
140
141 /**
142 * Runs batches
143 */
144 private function _run_batches( $method, $type, $assoc_args ) {
145 $batches = isset( $assoc_args[ 'batches' ] ) ? $assoc_args[ 'batches' ] : 10;
146 if( !is_numeric( $batches ) || $batches <= 0 ) {
147 WP_CLI::error( 'Parameter --batches must have numeric value.' );
148 }
149 $limit = isset( $assoc_args[ 'limit' ] ) ? $assoc_args[ 'limit' ] : 100;
150 if( !is_numeric( $limit ) || $limit <= 0 ) {
151 WP_CLI::error( 'Parameter --limit must have numeric value.' );
152 }
153
154 for( $i=1; $i<=$batches; $i++ ) {
155
156 if( !empty( $this->url ) ) {
157 $command = "wp sm {$method} {$type} --batch={$i} --batches={$batches} --limit={$limit} --url={$this->url}";
158 } else {
159 $command = "wp sm {$method} {$type} --batch={$i} --batches={$batches} --limit={$limit}";
160 }
161
162 WP_CLI::line( '...' );
163 WP_CLI::line( "Launching external command '{$command}'" );
164 WP_CLI::line( 'Waiting...' );
165
166 @ob_flush();
167 flush();
168
169 $r = SM_CLI::launch( $command, false, true );
170
171 if( $r->return_code ) {
172 WP_CLI::error( "Something went wrong. External command process failed." );
173 } else {
174 echo $r->stdout;
175 }
176
177 }
178 }
179
180 /**
181 * Optimization process
182 * Runs before command's process
183 */
184 private function _before_command_run(){
185 WP_CLI::line( "Starting Database optimization process. Waiting..." );
186 @ob_flush();
187 flush();
188 $command = !empty( $this->url ) ? "wp db optimize --url={$this->url}" : "wp db optimize";
189 $r = SM_CLI::launch( $command, false, true );
190 if( $r->return_code ) {
191 WP_CLI::error( "Something went wrong. Database optimization process failed." );
192 } else {
193 WP_CLI::success( "Database is optimized" );
194 }
195 }
196
197 /**
198 * Optimization process
199 * Runs after command's process
200 */
201 private function _after_command_run(){
202 //** Run transient flushing */
203 WP_CLI::line( "Starting remove transient. Waiting..." );
204 @ob_flush();
205 flush();
206 $command = !empty( $this->url ) ? "wp transient delete-all --url={$this->url}" : "wp transient delete-all";
207 $r = SM_CLI::launch( $command, false, true );
208 if( $r->return_code ) {
209 WP_CLI::error( "Something went wrong. Transient process failed." );
210 } else {
211 WP_CLI::success( "Transient is removed" );
212 }
213 //** Run MySQL optimization */
214 WP_CLI::line( "Starting Database optimization process. Waiting..." );
215 @ob_flush();
216 flush();
217 $command = !empty( $this->url ) ? "wp db optimize --url={$this->url}" : "wp db optimize";
218 $r = SM_CLI::launch( $command, false, true );
219 if( $r->return_code ) {
220 WP_CLI::error( "Something went wrong. Database optimization process failed." );
221 } else {
222 WP_CLI::success( "Database is optimized" );
223 }
224 }
225
226 }
227
228 /** Add the commands from above */
229 WP_CLI::add_command( 'sm', 'SM_CLI_Command' );
230
231 }