[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/imagecache/ -> imagecache.drush.inc (source)

   1  <?php
   2  
   3  /**
   4   * Implementation of hook_drush_command().
   5   */
   6  function imagecache_drush_command() {
   7    $items = array();
   8  
   9    $items['imagecache-flush'] = array(
  10      'callback' => 'imagecache_drush_preset_flush',
  11      'description' => dt('Flush an imagecache preset.'),
  12      'examples' => array(
  13        'drush imagecache-flush foobar' => dt('Flush the ImageCache preset "foobar".'),
  14      ),
  15      'aliases' => array('icf'),
  16    );
  17  
  18    $items['imagecache-build'] = array(
  19      'callback' => 'imagecache_drush_preset_build',
  20      'description' => dt('Build imagecache derivates for all images for a given preset.'),
  21      'examples' => array(
  22        'drush imagecache-build foobar' => dt('Build all images for preset "foobar".'),
  23      ),
  24      'aliases' => array('icb'),
  25    );
  26  
  27    return $items;
  28  }
  29  
  30  /**
  31   * Implementation of hook_drush_help().
  32   */
  33  function imagecache_drush_help($section) {
  34    switch ($section) {
  35      case 'drush:imagecache-flush':
  36        return dt('Flush a given preset.');
  37      case 'drush:imagecache-build':
  38        return dt('Build derivative images for a given preset.');
  39    }
  40  }
  41  
  42  /**
  43   * Drush callback to perform actual imagecache preset flush.
  44   */
  45  function imagecache_drush_preset_flush() {
  46    $args = func_get_args();
  47  
  48    // Rebuild imagecache presets.
  49    foreach (imagecache_presets(TRUE) as $preset) {
  50      $preset_names[] = $preset['presetname'];
  51    }
  52  
  53    if (empty($args)) {
  54      $choice = drush_choice($preset_names, 'Enter a number to choose which preset to flush.');
  55      if ($choice !== FALSE) {
  56        $args[] = $preset_names[$choice];
  57      }
  58    }
  59    else {
  60      // Implement 'all'
  61      if (count($args) == 1 && $args[0] == 'all') {
  62        $args = $preset_names;
  63      }
  64    }
  65  
  66    // Remove any invalid preset names and report them as errors.
  67    $not_found = array_diff($args, $preset_names);
  68    $args = array_intersect($args, $preset_names);
  69    if ($not_found) {
  70      drush_log(dt('Preset(s) not found: @presets', array('@presets' => implode($not_found, ' '))), 'error');
  71    }
  72  
  73    if (empty($args)) {
  74      return FALSE;
  75    }
  76  
  77    $path = drush_get_context('DRUSH_DRUPAL_ROOT') .'/'. file_directory_path() .'/imagecache/';
  78    foreach ($args as $arg) {
  79      // Load preset.
  80      if ($preset = imagecache_preset_by_name($arg)) {
  81        // This mimics the logic inside of the function
  82        // imagecache_preset_flush(), but without the access check.
  83        $presetdir = $path . $preset['presetname'];
  84        if (is_dir($presetdir)) {
  85          _imagecache_recursive_delete($presetdir);
  86          drush_log(dt('Flushed "@preset" preset.', array('@preset' => $arg)), 'ok');
  87        }
  88        else {
  89          drush_log(dt('Cache for preset "@preset" was already empty.', array('@preset' => $arg)), 'ok');
  90        }
  91      }
  92    }
  93    return TRUE;
  94  }
  95  
  96  /**
  97   * Drush callback to perform actual imagecache preset build.
  98   */
  99  function imagecache_drush_preset_build() {
 100    $args = func_get_args();
 101  
 102    // Rebuild imagecache presets.
 103    foreach (imagecache_presets(TRUE) as $preset) {
 104      $preset_names[] = $preset['presetname'];
 105    }
 106  
 107    if (empty($args)) {
 108      $choice = drush_choice($preset_names, 'Enter a number to choose which preset to flush.');
 109      if ($choice !== FALSE) {
 110        $args[] = $preset_names[$choice];
 111      }
 112    }
 113    elseif ($args[0] == 'all') {
 114      // Implement 'all'
 115      $args = $preset_names;
 116    }
 117  
 118    // Remove any invalid preset names and report them as errors.
 119    $not_found = array_diff($args, $preset_names);
 120    $args = array_intersect($args, $preset_names);
 121    if ($not_found) {
 122      drush_log(dt('Preset(s) not found: @presets', array('@presets' => implode($not_found, ' '))), 'error');
 123    }
 124  
 125    if (empty($args)) {
 126      return FALSE;
 127    }
 128  
 129    // Get a list of files to processes.
 130    $file_query = db_query("SELECT filepath FROM {files} where filemime LIKE 'image%' ORDER BY fid DESC");
 131    $files = array();
 132    drush_log(dt('Generating file list...', array()), 'ok');
 133    while ($filepath = db_result($file_query)) {
 134      if (file_exists($filepath)) {
 135        $files[] = $filepath;
 136      }
 137    }
 138    if (empty($files)) {
 139      drush_log(dt('No images found in the files table.', array()), 'error');
 140      return FALSE;
 141    }
 142    $count = count($files);
 143    drush_log(dt('Done. @count files to process using these presets: @presets', array('@count' => $count, '@presets' => implode(' ', $args))), 'ok');
 144  
 145    // Generate the images.
 146    $counter = 0;
 147    $mod = round($count / 200);
 148    foreach ($files as $filepath) {
 149      foreach ($args as $arg) {
 150        $path = imagecache_create_path($arg, $filepath);
 151        if (!file_exists($path)) {
 152          imagecache_generate_image($arg, $filepath);
 153          if (file_exists($path)) {
 154            drush_log(dt('File "@file" created.', array('@file' => $path)), 'ok');
 155          }
 156          else {
 157            drush_log(dt('File "@file" not created.', array('@file' => $path)), 'error');
 158          }
 159        }
 160      }
 161      // Output progress.
 162      $counter++;
 163      if ($counter % $mod == 0) {
 164        drush_log(dt('@percent% done.', array('@percent' => round($counter / $count * 100, 2))), 'ok');
 165      }
 166    }
 167  
 168    return TRUE;
 169  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7