expire.rules.inc 3.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?php

/**
 * @file
 * Integration with the rules module
 */

/**
 * Implementation of hook_rules_action_info().
 *
 * @ingroup rules
 */
function expire_rules_action_info() {
  return array(
    'expire_rules_action_flush_url' => array(
      'label' => t('Clear URL(s) from the page cache.'),
      'arguments' => array(
        'urls' => array('type' => 'string', 'label' => t('URL of page to clear')),
      ),
      'module' => 'Expire',
    ),
  );
}

/**
 * Expire a URL from the page cache.
 */
function expire_rules_action_flush_url($urls, $settings) {
  global $base_root, $base_path;
  $urls = preg_replace("/\r\n/", "\n", $urls);
  $urls = explode("\n", $urls);
  $urls = array_map('trim', $urls);
  $urls = array_filter($urls);
  $urls_parsed = array_map('parse_url', $urls);

  // Figure out what kind of URL's these are and act accordingly.
  $full_urls = array();
  $bad_paths = array();
  $internal = array();
  foreach ($urls_parsed as $key => $parts) {
    if (!empty($parts['host'])) {
      $full_urls[] = $urls[$key];
    }
    elseif (!empty($parts['path'])) {
      // Strip base path from path.
      $parts['path'] = preg_replace('/^' . preg_quote($base_path, '/') .'/i', '', $parts['path']);
      if (empty($parts['path']) || $parts['path'] == '<front>') {
        $internal[$key] = variable_get('site_frontpage', 'node');
      }
      else {
        $normal = expire_normal_path_check($parts['path']);
        if (is_array($normal)) {
          $internal[$key] = $urls[$key];
        }
        else {
          $normal = drupal_get_normal_path($parts['path']);
          if ($normal == $parts['path']) {
            // Bad path given
            $bad_paths[$key] = $urls[$key];
          }
          else {
            $internal[$key] = $normal;
          }
        }
      }
    }
  }

  if (!empty($bad_paths)){
    watchdog('expire', 'Bad URL(s) given. !url do not match any given paths or aliases', array('!url' => expire_print_r($bad_paths)));
  }

  // Process internal paths
  foreach ($internal as $key => $path) {
    $arg = arg(NULL, $path);
    if ($arg[0] == 'node' && !empty($arg[1]) && is_numeric($arg[1])) {
      $node = node_load($arg[1]);
      expire_node($node);
      unset($internal[$key]);
    }
  }
  if (!empty($internal)) {
    $flushed = expire_cache_derivative($internal);
  }

  // Process Full URLs
  // hook_expire_cache
  $modules = module_implements('expire_cache');
  foreach ($modules as $module) {
    module_invoke($module, 'expire_cache', $full_urls);
  }
  watchdog('expire', 'Output: !urls <br /> Modules Using hook_expire_cache(): !modules', array(
    '!urls' => expire_print_r($full_urls),
    '!modules' => expire_print_r($modules),
  ));

}

/**
 * Action clear page from cache configuration form.
 */
function expire_rules_action_flush_url_form($settings, &$form) {
  $settings += array('urls' => '');

  $form['settings']['urls'] = array(
    '#type' => 'textarea',
    '#title' => t('URL(s)'),
    '#default_value' => $settings['urls'],
    '#description' => t('Full URL (including http://); node/8; taxonomy/term/3; user/5; path alias; & <front> will work. One Per line.'),
  );

}