| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Implements example command. |
| 5 |
*/ |
| 6 |
class Redirection_Cli extends WP_CLI_Command { |
| 7 |
private function get_group( $group_id ) { |
| 8 |
if ( $group_id === 0 ) { |
| 9 |
$groups = Red_Group::get_filtered( array() ); |
| 10 |
|
| 11 |
if ( count( $groups['items'] ) > 0 ) { |
| 12 |
return $groups['items'][ 0 ]['id']; |
| 13 |
} |
| 14 |
} else { |
| 15 |
$groups = Red_Group::get( $group_id ); |
| 16 |
if ( $groups ) { |
| 17 |
return $group_id; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Import redirections from a JSON, CSV, or .htaccess file |
| 26 |
* |
| 27 |
* ## OPTIONS |
| 28 |
* |
| 29 |
* <file> |
| 30 |
* : The name of the file to import. |
| 31 |
* |
| 32 |
* [--group=<groupid>] |
| 33 |
* : The group ID to import into. Defaults to the first available group. JSON |
| 34 |
* contains it's own group |
| 35 |
* |
| 36 |
* [--format=<importformat>] |
| 37 |
* : The import format - csv, htaccess, or json. Defaults to json |
| 38 |
* |
| 39 |
* ## EXAMPLES |
| 40 |
* |
| 41 |
* wp redirection import .htaccess --format=htaccess |
| 42 |
*/ |
| 43 |
public function import( $args, $extra ) { |
| 44 |
$format = isset( $extra['format'] ) ? $extra['format'] : 'json'; |
| 45 |
$group = $this->get_group( isset( $extra['group'] ) ? intval( $extra['group'], 10 ) : 0 ); |
| 46 |
|
| 47 |
if ( ! $group ) { |
| 48 |
WP_CLI::error( 'Invalid group' ); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$importer = Red_FileIO::create( $format ); |
| 53 |
|
| 54 |
if ( ! $importer ) { |
| 55 |
WP_CLI::error( 'Invalid import format - csv, json, or htaccess supported' ); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( $format === 'csv' ) { |
| 60 |
$file = fopen( $args[0], 'r' ); |
| 61 |
|
| 62 |
if ( $file ) { |
| 63 |
$count = $importer->load( $group, $args[0], '' ); |
| 64 |
WP_CLI::success( 'Imported ' . $count . ' as ' . $format ); |
| 65 |
} else { |
| 66 |
WP_CLI::error( 'Invalid import file' ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
$data = @file_get_contents( $args[0] ); |
| 70 |
|
| 71 |
if ( $data ) { |
| 72 |
$count = $importer->load( $group, $args[0], $data ); |
| 73 |
WP_CLI::success( 'Imported ' . $count . ' redirects as ' . $format ); |
| 74 |
} else { |
| 75 |
WP_CLI::error( 'Invalid import file' ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Export redirections to a CSV, JSON, .htaccess, or rewrite.rules file |
| 82 |
* |
| 83 |
* ## OPTIONS |
| 84 |
* |
| 85 |
* <module> |
| 86 |
* : The module to export - wordpress, apache, nginx, or all |
| 87 |
* |
| 88 |
* <filename> |
| 89 |
* : The file to export to, or - for stdout |
| 90 |
* |
| 91 |
* [--format=<exportformat>] |
| 92 |
* : The export format. One of json, csv, apache, or nginx. Defaults to json |
| 93 |
* |
| 94 |
* ## EXAMPLES |
| 95 |
* |
| 96 |
* wp redirection export wordpress --format=apache |
| 97 |
*/ |
| 98 |
public function export( $args, $extra ) { |
| 99 |
$format = isset( $extra['format'] ) ? $extra['format'] : 'json'; |
| 100 |
$exporter = Red_FileIO::create( $format ); |
| 101 |
|
| 102 |
if ( ! $exporter ) { |
| 103 |
WP_CLI::error( 'Invalid export format - json, csv, htaccess, or nginx supported' ); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
$file = fopen( $args[1] === '-' ? 'php://stdout' : $args[1], 'w' ); |
| 108 |
if ( $file ) { |
| 109 |
$export = Red_FileIO::export( $args[0], $format ); |
| 110 |
|
| 111 |
if ( $export === false ) { |
| 112 |
WP_CLI::error( 'Invalid module - must be wordpress, apache, nginx, or all' ); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
fwrite( $file, $export['data'] ); |
| 117 |
fclose( $file ); |
| 118 |
|
| 119 |
WP_CLI::success( 'Exported ' . $export['total'] . ' to ' . $format ); |
| 120 |
} else { |
| 121 |
WP_CLI::error( 'Invalid output file' ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Perform Redirection database actions |
| 127 |
* |
| 128 |
* ## OPTIONS |
| 129 |
* |
| 130 |
* <action> |
| 131 |
* : The database action to perform: install, remove, upgrade |
| 132 |
* |
| 133 |
* ## EXAMPLES |
| 134 |
* |
| 135 |
* wp redirection database install |
| 136 |
*/ |
| 137 |
public function database( $args, $extra ) { |
| 138 |
$action = false; |
| 139 |
|
| 140 |
if ( count( $args ) === 0 || ! in_array( $args[0], array( 'install', 'remove', 'upgrade' ), true ) ) { |
| 141 |
WP_CLI::error( 'Invalid database action - please use install, remove, or upgrade' ); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if ( $args[0] === 'install' ) { |
| 146 |
Red_Database::apply_to_sites( function() { |
| 147 |
$latest = Red_Database::get_latest_database(); |
| 148 |
$latest->install(); |
| 149 |
|
| 150 |
WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is installed' ); |
| 151 |
} ); |
| 152 |
|
| 153 |
WP_CLI::success( 'Database install finished' ); |
| 154 |
} elseif ( $args[0] === 'upgrade' ) { |
| 155 |
Red_Database::apply_to_sites( function() { |
| 156 |
$database = new Red_Database(); |
| 157 |
$status = new Red_Database_Status(); |
| 158 |
|
| 159 |
if ( ! $status->needs_updating() ) { |
| 160 |
WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is already the latest version' ); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$loop = 0; |
| 165 |
|
| 166 |
while ( $loop < 50 ) { |
| 167 |
$result = $database->apply_upgrade( $status ); |
| 168 |
$info = $status->get_json(); |
| 169 |
|
| 170 |
if ( ! $info['inProgress'] ) { |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
if ( $info['status'] === 'error' ) { |
| 175 |
WP_CLI::error( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] ); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
$loop++; |
| 180 |
} |
| 181 |
|
| 182 |
WP_CLI::success( 'Site ' . get_current_blog_id() . ' database upgraded' ); |
| 183 |
} ); |
| 184 |
|
| 185 |
WP_CLI::success( 'Database upgrade finished' ); |
| 186 |
} elseif ( $args[0] === 'remove' ) { |
| 187 |
Red_Database::apply_to_sites( function() { |
| 188 |
$latest = Red_Database::get_latest_database(); |
| 189 |
$latest->remove(); |
| 190 |
} ); |
| 191 |
|
| 192 |
WP_CLI::success( 'Database removed' ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 198 |
WP_CLI::add_command( 'redirection import', array( 'Redirection_Cli', 'import' ) ); |
| 199 |
WP_CLI::add_command( 'redirection export', array( 'Redirection_Cli', 'export' ) ); |
| 200 |
WP_CLI::add_command( 'redirection database', array( 'Redirection_Cli', 'database' ) ); |
| 201 |
|
| 202 |
add_action( Red_Flusher::DELETE_HOOK, function() { |
| 203 |
$flusher = new Red_Flusher(); |
| 204 |
$flusher->flush(); |
| 205 |
} ); |
| 206 |
} |
| 207 |
|