/**
* Bulk import redirects from a CSV file matching the following structure:
*
* redirect_from_path,(redirect_to_post_id|redirect_to_path|redirect_to_url)
*
* @subcommand import-from-csv
* @synopsis --csv=<path-to-csv>
*/
function import_from_csv($args, $assoc_args)
{
define('WP_IMPORTING', true);
if (empty($assoc_args['csv']) || !file_exists($assoc_args['csv'])) {
WP_CLI::error("Invalid 'csv' file");
}
global $wpdb;
if (($handle = fopen($assoc_args['csv'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
$row++;
$redirect_from = $data[0];
$redirect_to = $data[1];
WP_CLI::line("Adding (CSV) redirect for {$redirect_from} to {$redirect_to}");
WP_CLI::line("-- at {$row}");
WPCOM_Legacy_Redirector::insert_legacy_redirect($redirect_from, $redirect_to);
if (0 == $row % 100) {
if (function_exists('stop_the_insanity')) {
stop_the_insanity();
}
sleep(1);
}
}
fclose($handle);
}
}