You are here

function advagg_admin_operations_form in Advanced CSS/JS Aggregation 7.2

Form builder; Do advagg operations.

Related topics

1 string reference to 'advagg_admin_operations_form'
advagg_menu in ./advagg.module
Implements hook_menu().

File

./advagg.admin.inc, line 717
Admin page callbacks for the advanced CSS/JS aggregation module.

Code

function advagg_admin_operations_form($form, $form_state) {
  drupal_set_title(t('AdvAgg: Operations'));
  advagg_display_message_if_requirements_not_met();

  // Explain what can be done on this page.
  $form['tip'] = array(
    '#markup' => '<p>' . t('This is a collection of commands to control the cache and to manage testing of this module. In general this page is useful when troubleshooting some aggregation issues. For normal operations, you do not need to do anything on this page below the Smart Cache Flush. There are no configuration options here.') . '</p>',
  );

  // Buttons to do stuff.
  // AdvAgg smart cache flushing.
  $form['smart_flush'] = array(
    '#type' => 'fieldset',
    '#title' => t('Smart Cache Flush'),
    '#description' => t('Scan all files referenced in aggregated files. If any of them have changed, clear that cache so the changes will go out.'),
  );
  $form['smart_flush']['advagg_flush'] = array(
    '#type' => 'submit',
    '#value' => t('Flush AdvAgg Cache'),
    '#submit' => array(
      'advagg_admin_flush_cache_button',
    ),
  );

  // Set/Remove Bypass Cookie.
  $form['bypass'] = array(
    '#type' => 'fieldset',
    '#title' => t('Aggregation Bypass Cookie'),
    '#description' => t('This will set or remove a cookie that disables aggregation for a set period of time.'),
  );
  $bypass_length = drupal_map_assoc(array(
    60 * 60 * 6,
    60 * 60 * 12,
    60 * 60 * 24,
    60 * 60 * 24 * 2,
    60 * 60 * 24 * 7,
    60 * 60 * 24 * 30,
    60 * 60 * 24 * 365,
  ), 'format_interval');
  $form['bypass']['timespan'] = array(
    '#type' => 'select',
    '#title' => 'Bypass length',
    '#options' => $bypass_length,
  );
  $form['bypass']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Toggle The "aggregation bypass cookie" For This Browser'),
    '#attributes' => array(
      'onclick' => 'javascript:return advagg_toggle_cookie()',
    ),
    '#submit' => array(
      'advagg_admin_toggle_bypass_cookie',
    ),
  );

  // Add in aggregation bypass cookie javascript.
  $form['#attached']['js'][] = array(
    'data' => array(
      'advagg' => array(
        'key' => drupal_hmac_base64('advagg_cookie', drupal_get_private_key() . drupal_get_hash_salt() . variable_get('cron_key', 'drupal')),
      ),
    ),
    'type' => 'setting',
  );
  $form['#attached']['js'][] = drupal_get_path('module', 'advagg') . '/advagg.admin.js';

  // Regenerate .htaccess files.
  if (variable_get('advagg_htaccess_check_generate', ADVAGG_HTACCESS_CHECK_GENERATE)) {
    list($css_path, $js_path) = advagg_get_root_files_dir();
    $form['htaccess'] = array(
      '#type' => 'fieldset',
      '#title' => t('Regenerate .htaccess files'),
      '#description' => t('<code>@advagg_css_htaccess</code><br><code>@advagg_js_htaccess</code>', array(
        '@advagg_css_htaccess' => $css_path[1] . '/.htaccess',
        '@advagg_js_htaccess' => $js_path[1] . '/.htaccess',
      )),
    );
    $form['htaccess']['advagg_regenerate_htaccess'] = array(
      '#type' => 'submit',
      '#value' => t('Recreate htaccess files'),
      '#submit' => array(
        'advagg_admin_regenerate_htaccess_button',
      ),
    );
  }

  // Tasks run by cron.
  $form['cron'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cron Maintenance Tasks'),
    '#description' => t('The following 7 operations are ran on cron but you can run them manually here.'),
  );
  $form['cron']['smart_file_flush'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Clear All Stale Files'),
    '#description' => t('Remove all stale files. Scan all files in the advagg_css/js directories and remove the ones that have not been accessed in the last 30 days.'),
  );
  $form['cron']['smart_file_flush']['advagg_flush_stale_files'] = array(
    '#type' => 'submit',
    '#value' => t('Remove All Stale Files'),
    '#submit' => array(
      'advagg_admin_flush_stale_files_button',
    ),
  );
  $form['cron']['delete_empty_aggregates'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete empty aggregates'),
    '#description' => t('Delete all aggregates that have no content so that they can be regenerated.'),
  );
  $form['cron']['delete_empty_aggregates']['advagg_delete_empty_aggregates'] = array(
    '#type' => 'submit',
    '#value' => t('Delete empty aggregates'),
    '#submit' => array(
      'advagg_delete_empty_aggregates_button',
    ),
  );
  $form['cron']['delete_orphaned_aggregates'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete orphaned aggregates'),
    '#description' => t('Scan CSS/JS advagg dir and remove file if there is no associated db record.'),
  );
  $form['cron']['delete_orphaned_aggregates']['advagg_delete_orphaned_aggregates'] = array(
    '#type' => 'submit',
    '#value' => t('Delete orphaned aggregates'),
    '#submit' => array(
      'advagg_admin_delete_orphaned_aggregates_button',
    ),
  );
  $form['cron']['remove_missing_files'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Clear Missing Files From Database'),
    '#description' => t('Scan for missing files and remove the associated entries in the database.'),
  );
  $form['cron']['remove_missing_files']['advagg_remove_missing_files_from_db'] = array(
    '#type' => 'submit',
    '#value' => t('Clear Missing Files From Database'),
    '#submit' => array(
      'advagg_admin_remove_missing_files_from_db_button',
    ),
  );
  $form['cron']['remove_old_aggregates'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete Unused Aggregates From Database'),
    '#description' => t('Delete aggregates that have not been accessed in the last 6 weeks.'),
  );
  $form['cron']['remove_old_aggregates']['advagg_remove_old_unused_aggregates'] = array(
    '#type' => 'submit',
    '#value' => t('Delete Unused Aggregates From Database'),
    '#submit' => array(
      'advagg_admin_remove_old_unused_aggregates_button',
    ),
  );
  $form['cron']['cleanup_semaphore_table'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete Orphaned Semaphore Locks'),
    '#description' => t('Delete orphaned/expired advagg locks from the semaphore database table.'),
  );
  $form['cron']['cleanup_semaphore_table']['advagg_cleanup_semaphore_table'] = array(
    '#type' => 'submit',
    '#value' => t('Delete Orphaned Semaphore Locks'),
    '#submit' => array(
      'advagg_admin_cleanup_semaphore_table_button',
    ),
  );
  $form['cron']['cleanup_temp_files'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete leftover temporary files'),
    '#description' => t('Delete old temporary files from the filesystem.'),
  );
  $form['cron']['cleanup_temp_files']['advagg_remove_temp_files'] = array(
    '#type' => 'submit',
    '#value' => t('Delete leftover temporary files'),
    '#submit' => array(
      'advagg_admin_cleanup_temp_files_button',
    ),
  );
  if (module_exists('locale')) {
    $form['cron']['refresh_locale_files'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Refresh all locale files'),
      '#description' => t('Run all js files in the advagg_files table through locale_js_alter; loop for each enabled language.'),
    );
    $form['cron']['refresh_locale_files']['advagg_refresh_all_locale_files'] = array(
      '#type' => 'submit',
      '#value' => t('Refresh all locale files'),
      '#submit' => array(
        'advagg_admin_refresh_locale_files_button',
      ),
    );
  }

  // Hide drastic measures as they should not be done unless you really need it.
  $form['drastic_measures'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Drastic Measures'),
    '#description' => t('The options below should normally never need to be done.'),
  );
  $form['drastic_measures']['dumb_cache_flush'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Clear All Caches'),
    '#description' => t('Remove all entries from the advagg cache bins. Useful if you suspect a cache is not getting cleared.'),
  );
  $form['drastic_measures']['dumb_cache_flush']['advagg_flush_all_caches'] = array(
    '#type' => 'submit',
    '#value' => t('Clear All Caches'),
    '#submit' => array(
      'advagg_admin_clear_all_caches_button',
    ),
  );
  $form['drastic_measures']['dumb_file_flush'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Clear All Files'),
    '#description' => t('Remove all generated files. Useful if you think some of the generated files got corrupted and thus need to be deleted.'),
  );
  $form['drastic_measures']['dumb_file_flush']['advagg_flush_all_files'] = array(
    '#type' => 'submit',
    '#value' => t('Remove All Generated Files'),
    '#submit' => array(
      'advagg_admin_clear_all_files_button',
    ),
  );
  $form['drastic_measures']['force_change'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Force new aggregates'),
    '#description' => t('Force the creation of all new aggregates by incrementing a global counter. Current value of counter: %value. This is useful if a CDN has cached an aggregate incorrectly as it will force new ones to be used even if nothing else has changed.', array(
      '%value' => advagg_get_global_counter(),
    )),
  );
  $form['drastic_measures']['force_change']['increment_global_counter'] = array(
    '#type' => 'submit',
    '#value' => t('Increment Global Counter'),
    '#submit' => array(
      'advagg_admin_increment_global_counter',
    ),
  );
  $form['drastic_measures']['rescan_files'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Rescan all files'),
    '#description' => t('Force rescan all files and clear the cache. Useful if a css/js change from a deployment did not work.'),
  );
  $form['drastic_measures']['rescan_files']['reset_mtime'] = array(
    '#type' => 'submit',
    '#value' => t('Reset All mtime Values'),
    '#submit' => array(
      'advagg_admin_reset_mtime',
    ),
  );
  $form['drastic_measures']['remove_empty_advagg_files'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Remove deleted files in the advagg_files table'),
    '#description' => t('Remove entries in the advagg_files table that have a filesize of 0 and delete the javascript_parsed variable. This gets around the grace period that the cron cleanup does.'),
  );
  $form['drastic_measures']['remove_empty_advagg_files']['advagg_admin_remove_empty_advagg_files'] = array(
    '#type' => 'submit',
    '#value' => t('Remove deleted files from advagg_files'),
    '#submit' => array(
      'advagg_admin_remove_empty_advagg_files',
    ),
  );
  $form['drastic_measures']['reset_advagg_files'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Reset the AdvAgg Files table'),
    '#description' => t('Truncate the advagg_files table and delete the javascript_parsed variable. This may cause some 404s for CSS/JS assets for a short amount of time (seconds). Useful if you really want to reset some stuff. Might be best to put the site into maintenance mode before doing this.'),
  );
  $form['drastic_measures']['reset_advagg_files']['truncate_advagg_files'] = array(
    '#type' => 'submit',
    '#value' => t('Truncate advagg_files'),
    '#submit' => array(
      'advagg_admin_truncate_advagg_files',
    ),
  );
  $form['drastic_measures']['clear_base_file'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Delete all aggregates containing this base file.'),
    '#description' => t('Pinpoint clearing of all aggregates that contain a file.'),
  );
  $form['drastic_measures']['clear_base_file']['filename'] = array(
    '#type' => 'textfield',
    '#size' => 170,
    '#maxlength' => 256,
    '#default_value' => '',
    '#title' => t('Filename'),
  );
  $form['drastic_measures']['clear_base_file']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Delete Select Aggregates'),
    '#validate' => array(
      'advagg_admin_clear_file_aggregate_validate',
    ),
    '#submit' => array(
      'advagg_admin_clear_file_aggregate_submit',
    ),
    '#ajax' => array(
      'callback' => 'advagg_admin_clear_file_aggregate_callback',
      'wrapper' => 'advagg-clear-file-aggregate-ajax',
      'effect' => 'fade',
    ),
  );
  $types = array(
    'css',
    'js',
  );
  $css_file = '';
  $js_file = '';
  foreach ($types as $type) {

    // Get valid filename.
    $results = db_select('advagg_files', 'af')
      ->fields('af', array(
      'filename',
    ))
      ->condition('filetype', $type)
      ->orderBy('filename', 'ASC')
      ->execute();
    while ($row = $results
      ->fetchAssoc()) {
      if (empty($css_file) && $type === 'css') {
        $css_file = $row['filename'];
        break;
      }
      if (empty($js_file) && $type === 'js') {
        $js_file = $row['filename'];
        break;
      }
    }
  }
  $form['drastic_measures']['clear_base_file']['tip'] = array(
    '#markup' => '<p>' . t('Takes input like "@css_file" or "@advagg_js"', array(
      '@css_file' => $css_file,
      '@advagg_js' => $js_file,
    )) . '</p>',
  );
  $form['drastic_measures']['clear_base_file']['wrapper'] = array(
    '#markup' => "<div id='advagg-clear-file-aggregate-ajax'></div>",
  );
  return $form;
}