You are here

function drush_redirect_create_redirect in Redirect 7.2

Same name and namespace in other branches
  1. 7 redirect.drush.inc \drush_redirect_create_redirect()

Command callback. Validates and adds a redirect.

Parameters

string $source: The path from which to redirect.

string $destination: The path to which to redirect.

File

./redirect.drush.inc, line 50
Drush integration for the redirect module.

Code

function drush_redirect_create_redirect($source, $destination) {

  // Create redirect object and set initial values.
  $redirect = new stdClass();
  redirect_object_prepare($redirect, array(
    'source' => $source,
    'language' => drush_get_option('language', LANGUAGE_NONE),
    'redirect' => $destination,
    'status_code' => drush_get_option('code', 0),
  ));

  // Do some sanity checks.
  // Check that there there are no redirect loops.
  if (url($redirect->source) == url($redirect->redirect)) {
    return drush_set_error('redirect', t('You are attempting to redirect the page to itself. This would result in an infinite loop.'));
  }

  // Check if this redirect already exists.
  redirect_hash($redirect);
  if ($existing = redirect_load_by_hash($redirect->hash)) {
    if ($redirect->rid != $existing->rid) {
      return drush_set_error('redirect', t('The source path %source is already being redirected to %redirect.', array(
        '%source' => redirect_url($redirect->source, $redirect->source_options),
        '%redirect' => redirect_url($existing->redirect),
      )));
    }
  }

  // Save the redirect if there were no errors.
  redirect_save($redirect);
  drush_log(dt('Redirect from !source to !destination saved.', array(
    '!source' => $redirect->source,
    '!destination' => $redirect->redirect,
  )), 'success');
}