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