Commit 110860bc authored by Mathieu Lutfy's avatar Mathieu Lutfy Committed by Aegir user

Remove redirect module, moved to platform to make cloning easier.

parent 7040e6b6
This diff is collapsed.
This is the new module home for a unified redirection API (also replaces
path_redirect and globalredirect).
This diff is collapsed.
(function ($) {
Drupal.behaviors.redirectAdmin = {
attach: function (context) {
$('table.redirect-list-tableselect tbody input:checkbox').bind('change', function(context) {
var checked = $('table.redirect-list-tableselect input:checkbox:checked').length;
if (checked) {
$('fieldset.redirect-list-operations').slideDown();
}
else {
$('fieldset.redirect-list-operations').slideUp();
}
});
$('table.redirect-list-tableselect th.select-all input:checkbox').bind('change', function(context) {
var checked = $(this, context).attr('checked');
if (checked) {
$('fieldset.redirect-list-operations').slideDown();
}
else {
$('fieldset.redirect-list-operations').slideUp();
}
});
$('fieldset.redirect-list-operations').hide();
}
};
})(jQuery);
<?php
/**
* @file
* Hooks provided by the Redirect module.
*/
/**
* @defgroup redirect_api_hooks Redirect API Hooks
* @{
* During redirect operations (create, update, view, delete, etc.), there are
* several sets of hooks that get invoked to allow modules to modify the
* redirect operation:
* - All-module hooks: Generic hooks for "redirect" operations. These are
* always invoked on all modules.
* - Entity hooks: Generic hooks for "entity" operations. These are always
* invoked on all modules.
*
* Here is a list of the redirect and entity hooks that are invoked, and other
* steps that take place during redirect operations:
* - Creating a new redirect (calling redirect_save() on a new redirect):
* - hook_redirect_presave() (all)
* - Redirect written to the database
* - hook_redirect_insert() (all)
* - hook_entity_insert() (all)
* - Updating an existing redirect (calling redirect_save() on an existing redirect):
* - hook_redirect_presave() (all)
* - Redirect written to the database
* - hook_redirect_update() (all)
* - hook_entity_update() (all)
* - Loading a redirect (calling redirect_load(), redirect_load_multiple(), or
* entity_load() with $entity_type of 'redirect'):
* - Redirect information is read from database.
* - hook_entity_load() (all)
* - hook_redirect_load() (all)
* - Deleting a redirect (calling redirect_delete() or redirect_delete_multiple()):
* - Redirect is loaded (see Loading section above)
* - Redirect information is deleted from database
* - hook_redirect_delete() (all)
* - hook_entity_delete() (all)
* - Preparing a redirect for editing (note that if it's
* an existing redirect, it will already be loaded; see the Loading section
* above):
* - hook_redirect_prepare() (all)
* - Validating a redirect during editing form submit (calling
* redirect_form_validate()):
* - hook_redirect_validate() (all)
* @}
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Act on redirects being loaded from the database.
*
* This hook is invoked during redirect loading, which is handled by
* entity_load(), via classes RedirectController and
* DrupalDefaultEntityController. After the redirect information is read from
* the database or the entity cache, hook_entity_load() is invoked on all
* implementing modules, and then hook_redirect_load() is invoked on all
* implementing modules.
*
* This hook should only be used to add information that is not in the redirect
* table, not to replace information that is in that table (which could
* interfere with the entity cache). For performance reasons, information for
* all available redirects should be loaded in a single query where possible.
*
* The $types parameter allows for your module to have an early return (for
* efficiency) if your module only supports certain redirect types.
*
* @param $redirects
* An array of the redirects being loaded, keyed by rid.
* @param $types
* An array containing the types of the redirects.
*
* @ingroup redirect_api_hooks
*/
function hook_redirect_load(array &$redirects, $types) {
}
/**
* Alter the list of redirects matching a certain source.
*
* @param $redirects
* An array of redirect objects.
* @param $source
* The source request path.
* @param $context
* An array with the following key/value pairs:
* - language: The language code of the source request.
* - query: An array of the source request query string.
*
* @see redirect_load_by_source()
* @ingroup redirect_api_hooks
*/
function hook_redirect_load_by_source_alter(array &$redirects, $source, array $context) {
foreach ($redirects as $rid => $redirect) {
if ($redirect->source !== $source) {
// If the redirects to do not exactly match $source (e.g. case
// insensitive matches), then remove them from the results.
unset($redirects[$rid]);
}
}
}
/**
* Control access to a redirect.
*
* Modules may implement this hook if they want to have a say in whether or not
* a given user has access to perform a given operation on a redirect.
*
* The administrative account (user ID #1) always passes any access check,
* so this hook is not called in that case. Users with the "administer redirects"
* permission may always update and delete redirects through the administrative
* interface.
*
* Note that not all modules will want to influence access on all
* redirect types. If your module does not want to actively grant or
* block access, return REDIRECT_ACCESS_IGNORE or simply return nothing.
* Blindly returning FALSE will break other redirect access modules.
*
* @param $redirect
* The redirect object on which the operation is to be performed, or, if it
* does not yet exist, the type of redirect to be created.
* @param $op
* The operation to be performed. Possible values:
* - "create"
* - "delete"
* - "update"
* @param $account
* A user object representing the user for whom the operation is to be
* performed.
*
* @return
* REDIRECT_ACCESS_ALLOW if the operation is to be allowed;
* REDIRECT_ACCESS_DENY if the operation is to be denied;
* REDIRECT_ACCESSS_IGNORE to not affect this operation at all.
*
* @see redirect_access()
* @ingroup redirect_api_hooks
*/
function hook_redirect_access($op, $redirect, $account) {
$type = is_string($redirect) ? $redirect : $redirect->type;
if (in_array($type, array('normal', 'special'))) {
if ($op == 'create' && user_access('create ' . $type . ' redirects', $account)) {
return REDIRECT_ACCESS_ALLOW;
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
return REDIRECT_ACCESS_ALLOW;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
return REDIRECT_ACCESS_ALLOW;
}
}
}
// Returning nothing from this function would have the same effect.
return REDIRECT_ACCESS_IGNORE;
}
/**
* Act on a redirect object about to be shown on the add/edit form.
*
* This hook is invoked from redirect_object_prepare().
*
* @param $redirect
* The redirect that is about to be shown on the add/edit form.
*
* @ingroup redirect_api_hooks
*/
function hook_redirect_prepare($redirect) {
}
/**
* Perform redirect validation before a redirect is created or updated.
*
* This hook is invoked from redirect_validate(), after a user has has finished
* editing the redirect and is submitting it. It is invoked at the end of all
* the standard validation steps.
*
* To indicate a validation error, use form_set_error().
*
* Note: Changes made to the $redirect object within your hook implementation
* will have no effect. The preferred method to change a redirect's content is
* to use hook_redirect_presave() instead. If it is really necessary to change
* the redirect at the validate stage, you can use form_set_value().
*
* @param $redirect
* The redirect being validated.
* @param $form
* The form being used to edit the redirect.
* @param $form_state
* The form state array.
*
* @see redirect_validate()
* @ingroup redirect_api_hooks
*/
function hook_redirect_validate($redirect, $form, $form_state) {
}
/**
* Act on a redirect being inserted or updated.
*
* This hook is invoked from redirect_save() before the redirect is saved to
* the database.
*
* @param $redirect
* The redirect that is being inserted or updated.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_presave($redirect) {
}
/**
* Respond to creation of a new redirect.
*
* This hook is invoked from redirect_save() after the redirect is inserted
* into the redirect table in the database.
*
* @param $redirect
* The redirect that is being created.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_insert($redirect) {
}
/**
* Respond to updates to a redirect.
*
* This hook is invoked from redirect_save() after the redirect is updated in
* the redirect table in the database.
*
* @param $redirect
* The redirect that is being updated.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_update($redirect) {
}
/**
* Respond to redirect deletion.
*
* This hook is invoked from redirect_delete_multiple() after the redirect has
* been removed from the redirect table in the database.
*
* @param $redirect
* The redirect that is being deleted.
*
* @see redirect_delete_multiple()
* @ingroup redirect_api_hooks
*/
function hook_redirect_delete($redirect) {
}
/**
* Act on a redirect being redirected.
*
* This hook is invoked from redirect_redirect() before the redirect callback
* is invoked.
*
* @param $redirect
* The redirect that is being used for the redirect.
*
* @see redirect_redirect()
* @see drupal_page_is_cacheable()
* @ingroup redirect_api_hooks
*/
function hook_redirect_alter($redirect) {
}
/**
* @} End of "addtogroup hooks".
*/
<?php
/**
* Controller class for redirects.
*
* This extends the DrupalDefaultEntityController class, adding required
* special handling for redirect objects.
*/
class RedirectController extends DrupalDefaultEntityController {
protected function attachLoad(&$redirects, $revision_id = FALSE) {
// Unserialize the URL option fields.
foreach ($redirects as $key => $redirect) {
$redirects[$key]->source_options = unserialize($redirect->source_options);
$redirects[$key]->redirect_options = unserialize($redirect->redirect_options);
}
parent::attachLoad($redirects, $revision_id);
}
}
<?php
/**
* @file
* Drush integration for the redirect module.
*/
/**
* Implementation of hook_drush_command().
*/
function redirect_drush_command() {
$items['generate-redirects'] = array(
'description' => 'Create redirects.',
'drupal dependencies' => array('devel_generate'),
'arguments' => array(
'count' => 'Number of redirects to generate.',
),
'options' => array(
'delete' => 'Delete all redirects before generating new ones.',
),
);
return $items;
}
/**
* Command callback. Generate a number of redirects.
*/
function drush_redirect_generate_redirects($count = NULL) {
if (drush_generate_is_number($count) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of redirects.'));
}
module_load_include('inc', 'redirect', 'redirect.generate');
drush_generate_include_devel();
redirect_run_unprogressive_batch('redirect_generate_redirects_batch_info', $count, drush_get_option('delete'));
}
/**
* Perform an unprogressive batch process for CLI.
*/
function redirect_run_unprogressive_batch() {
$batch = batch_get();
if (!empty($batch)) {
// If there is already something in the batch, don't run.
return FALSE;
}
$args = func_get_args();
$batch_callback = array_shift($args);
if (!lock_acquire($batch_callback)) {
return FALSE;
}
// Attempt to increase the execution time.
drupal_set_time_limit(240);
// Build the batch array.
$batch = call_user_func_array($batch_callback, $args);
batch_set($batch);
// We need to manually set the progressive variable again.
// @todo Remove when http://drupal.org/node/638712 is fixed.
$batch =& batch_get();
$batch['progressive'] = FALSE;
// Run the batch process.
batch_process();
lock_release($batch_callback);
return TRUE;
}
<?php
/**
* @file
* Devel generate integration for the redirect module.
*/
function redirect_generate_form() {
$form['count'] = array(
'#type' => 'textfield',
'#title' => t('How many URL redirects would you like to generate?'),
'#default_value' => 50,
'#size' => 4,
);
$form['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete all URL redirects before generating new URL redirects.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
return $form;
}
function redirect_generate_form_submit(&$form, &$form_state) {
// Run the batch.
$batch = redirect_generate_redirects_batch_info($form_state['values']['count'], $form_state['values']['delete']);
batch_set($batch);
}
function redirect_generate_redirects_batch_info($count, $delete = FALSE) {
if ($delete) {
$operations[] = array('redirect_generate_batch_delete', array());
}
$operations[] = array('redirect_generate_batch_generate', array($count));
return array(
'operations' => $operations,
'finished' => 'redirect_generate_batch_finished',
'file' => drupal_get_path('module', 'redirect') . '/redirect.generate.inc',
);
}
function redirect_generate_batch_delete(array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_rid'] = 0;
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT rid) FROM {redirect}')->fetchField();
}
$limit = 20;
$rids = db_query_range("SELECT rid FROM {redirect} WHERE rid > :rid ORDER BY rid", 0, $limit, array(':rid' => $context['sandbox']['current_rid']))->fetchCol();
redirect_delete_multiple($rids);
// Update our progress information.
$context['sandbox']['progress'] += count($rids);
$context['sandbox']['current_rid'] = end($rids);
$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
}
}
function redirect_generate_batch_generate($num, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $num;
$query = db_select('node', 'n');
$query->addField('n', 'nid');
$query->condition('n.status', NODE_PUBLISHED);
$query->addTag('node_access');
$context['sandbox']['nids'] = $query->execute()->fetchAllKeyed(0, 0);
}
module_load_include('inc', 'devel_generate');
$limit = 20;
$types = array_keys(redirect_status_code_options());
$languages = module_exists('locale') ? array_keys(locale_language_list('name')) : array();
for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
$rand = mt_rand(0, 100);
$redirect = new stdClass();
redirect_object_prepare($redirect);
$redirect->source = _redirect_generate_url();
$redirect->devel_generate = TRUE;
if ($context['sandbox']['nids'] && $rand >= 40) {
$redirect->redirect = 'node/'. array_rand($context['sandbox']['nids']);
}
else {
$redirect->redirect = _redirect_generate_url(TRUE);
if ($rand <= 20) {
$redirect->redirect_options['query'] = _redirect_generate_querystring();
}
if ($rand <= 5) {
$redirect->redirect_options['fragment'] = devel_generate_word(mt_rand(4, 8));
}
}
if ($rand <= 20) {
$redirect->status_code = $types[array_rand($types)];
}
if ($languages && $rand <= 20) {
$redirect->language = $languages[array_rand($languages)];
}
if ($rand <= 30) {
$redirect->source_options['query'] = _redirect_generate_querystring();
}
redirect_save($redirect);
if (mt_rand(0, 1)) {
db_update('redirect')
->fields(array(
'count' => mt_rand(1, 500),
'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
))
->condition('rid', $redirect->rid)
->execute();
}
$context['results'][] = $redirect->rid;
}
// Update our progress information.
$context['sandbox']['progress'] += $limit;
//$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
}
}
function redirect_generate_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(format_plural(count($results), 'One URL redirect created.', '@count URL redirects created.'));
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
}
}
function _redirect_generate_url($external = FALSE, $max_levels = 2) {
module_load_include('inc', 'devel_generate');
$url = array();
if ($external) {
$tlds = array('com', 'net', 'org');
$url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
}
$max_levels = mt_rand($external ? 0 : 1, $max_levels);
for ($i = 1; $i <= $max_levels; $i++) {
$url[] = devel_generate_word(mt_rand(6 / $i, 8));
}
return implode('/', $url);
}
function _redirect_generate_querystring() {
module_load_include('inc', 'devel_generate');
$query = array(devel_generate_word(mt_rand(1, 3)) => devel_generate_word(mt_rand(2, 4)));
return $query;
}
name = Redirect
description = Allows users to redirect from old URLs to new URLs.
core = 7.x
files[] = redirect.controller.inc
files[] = redirect.test
files[] = views/redirect.views.inc
;files[] = views/redirect_handler_field_redirect_type.inc
files[] = views/redirect_handler_filter_redirect_type.inc
files[] = views/redirect_handler_field_redirect_source.inc
files[] = views/redirect_handler_field_redirect_redirect.inc
files[] = views/redirect_handler_field_redirect_operations.inc
files[] = views/redirect_handler_field_redirect_link_edit.inc
files[] = views/redirect_handler_field_redirect_link_delete.inc
configure = admin/config/search/redirect/settings
; Information added by Drupal.org packaging script on 2015-07-08
version = "7.x-1.0-rc3"
core = "7.x"
project = "redirect"
datestamp = "1436393342"
This diff is collapsed.
(function ($) {
Drupal.behaviors.redirectFieldsetSummaries = {
attach: function (context) {
$('fieldset.redirect-list', context).drupalSetSummary(function (context) {
if ($('table.redirect-list tbody td.empty', context).length) {
return Drupal.t('No redirects');
}
else {
var enabled_redirects = $('table.redirect-list tbody tr.redirect-enabled', context).length;
var disabled_redirects = $('table.redirect-list tbody tr.redirect-disabled', context).length;
var text = '';
if (enabled_redirects > 0) {
var text = Drupal.formatPlural(enabled_redirects, '1 enabled redirect', '@count enabled redirects');
}
if (disabled_redirects > 0) {
if (text.length > 0) {
text = text + '<br />';
}
text = text + Drupal.formatPlural(disabled_redirects, '1 disabled redirect', '@count disabled redirects');
}
return text;
}
});
}
};
})(jQuery);
This diff is collapsed.
This diff is collapsed.
<?php
/**
* @file
* Views integration and data for the redirect module.
*/
/**
* Implements hook_views_data().
*/
function redirect_views_data() {
// Basic table information.
$data['redirect']['table']['group'] = t('Redirect');
// Advertise this table as a possible base table
$data['redirect']['table']['base'] = array(
'field' => 'rid',
'title' => t('URL redirects'),
'help' => t('Listings of URL redirects.'),
'weight' => 10,
);
// {redirect}.rid
$data['redirect']['rid'] = array(
'title' => t('Redirect ID'),
'help' => t('The internal ID of the redirect.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
'allow empty' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'argument' => array(
'handler' => 'views_handler_argument_numeric',
),
);
// {redirect}.type
$data['redirect']['type'] = array(
'title' => t('Type'),
'help' => t('The type of redirect.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(