Commit 4c8e983c authored by Zombienaute's avatar Zombienaute Committed by spleshka

Issue #1082860 by SqyD, mikeytown2, acbramley, Spleshka: Support for expiring...

Issue #1082860 by SqyD, mikeytown2, acbramley, Spleshka: Support for expiring managed files on update.
parent 37c86ef3
......@@ -300,6 +300,69 @@ function expire_admin_settings_form() {
);
// FILE SETTINGS.
if (module_exists('file')) {
$form['tabs']['file'] = array(
'#type' => 'fieldset',
'#title' => t('File expiration'),
'#group' => 'tabs',
'#weight' => 3,
);
$form['tabs']['file']['actions'] = array(
'#type' => 'fieldset',
'#title' => t('File actions'),
);
$form['tabs']['file']['actions']['expire_file_actions'] = array(
'#type' => 'checkboxes',
'#description' => t('Page cache for file will be expired after selected actions.'),
'#options' => array(
EXPIRE_FILE_UPDATE => t('File update'),
EXPIRE_FILE_DELETE => t('File delete'),
),
'#default_value' => variable_get('expire_file_actions', array()),
);
$form['tabs']['file']['expire'] = array(
'#type' => 'fieldset',
'#title' => t('What URLs should be expired when file action is triggered?'),
);
$form['tabs']['file']['expire']['expire_file_file'] = array(
'#type' => 'checkbox',
'#title' => t('File url'),
'#default_value' => variable_get('expire_file_file', EXPIRE_FILE_FILE),
);
$form['tabs']['file']['expire']['expire_file_front_page'] = array(
'#type' => 'checkbox',
'#title' => t('Front page'),
'#default_value' => variable_get('expire_file_front_page', EXPIRE_FILE_FRONT_PAGE),
);
$form['tabs']['file']['expire']['expire_file_custom'] = array(
'#type' => 'checkbox',
'#title' => t('Custom pages'),
'#default_value' => variable_get('expire_file_custom', EXPIRE_FILE_CUSTOM),
);
$form['tabs']['file']['expire']['expire_file_custom_pages'] = array(
'#type' => 'textarea',
'#title' => t('Enter custom urls'),
'#description' => t('Enter every new url on a single line. Example: !example.', array('!example' => '<strong>file/[file:fid]</strong>')) . '<br/>'
. t('If you want to use wildcard flush, add |wildcard to the end of the line. Example: !example', array('!example' => '<strong>file/[file:fid]|wildcard</strong>')) . '<br/>'
. t('You may use tokens here.'),
'#states' => array(
'visible' => array(
':input[name="expire_file_custom"]' => array('checked' => TRUE),
),
),
'#default_value' => variable_get('expire_file_custom_pages'),
);
}
// VOTINAPI SETTINGS.
if (module_exists('votingapi')) {
$form['tabs']['votingapi'] = array(
......@@ -376,6 +439,11 @@ function expire_admin_settings_form() {
$form['tabs']['user']['expire']['expire_user_custom_pages']['#description'] .= ' ' .
theme('token_tree_link', array('text' => t('View available tokens.'), 'token_types' => array('user')));
if (module_exists('file')) {
$form['tabs']['file']['expire']['expire_file_custom_pages']['#description'] .= ' ' .
theme('token_tree_link', array('text' => t('View available tokens.'), 'token_types' => array('file')));
}
if (module_exists('votingapi')) {
$form['tabs']['votingapi']['expire']['expire_votingapi_custom_pages']['#description'] .= ' ' .
theme('token_tree_link', array('text' => t('View available tokens.')));
......
......@@ -12,3 +12,4 @@ files[] = includes/expire.interface.inc
files[] = includes/expire.node.inc
files[] = includes/expire.user.inc
files[] = includes/expire.votingapi.inc
files[] = includes/expire.file.inc
......@@ -36,6 +36,10 @@ define('EXPIRE_USER_CANCEL', 4);
define('EXPIRE_VOTINGAPI_INSERT', 1);
define('EXPIRE_VOTINGAPI_DELETE', 2);
// File actions.
define('EXPIRE_FILE_UPDATE', 1);
define('EXPIRE_FILE_DELETE', 2);
// Debug levels.
define('EXPIRE_DEBUG_DISABLED', 0);
define('EXPIRE_DEBUG_WATCHDOG', 1);
......@@ -59,6 +63,9 @@ define('EXPIRE_USER_CUSTOM' , FALSE);
define('EXPIRE_VOTINGAPI_ENTITY' , FALSE);
define('EXPIRE_VOTINGAPI_ENTITY_PAGE' , FALSE);
define('EXPIRE_VOTINGAPI_CUSTOM' , FALSE);
define('EXPIRE_FILE_FILE' , FALSE);
define('EXPIRE_FILE_FRONT_PAGE' , FALSE);
define('EXPIRE_FILE_CUSTOM' , FALSE);
/**
* Implementation of hook_menu().
......@@ -190,6 +197,20 @@ function expire_votingapi_delete($votes) {
expire_execute_expiration('votingapi', $votes, EXPIRE_VOTINGAPI_DELETE);
}
/**
* Implements hook_file_update().
*/
function expire_file_update($file) {
expire_execute_expiration('file', $file, EXPIRE_FILE_UPDATE);
}
/**
* Implements hook_file_delete().
*/
function expire_file_delete($file) {
expire_execute_expiration('file', $file, EXPIRE_FILE_DELETE);
}
/**
* Execute expiration method for object.
*/
......
<?php
/**
* @file
* Provides class that expires files.
*/
class ExpireFile implements ExpireInterface {
/**
* Executes expiration actions for file.
*
* @param $file
* File object.
*
* @param $action
* Action that has been executed.
*
* @param $skip_action_check
* Shows whether should we check executed action or just expire node.
*/
function expire($file, $action, $skip_action_check = FALSE) {
global $base_path;
if (empty($file->fid)) {
return;
}
$enabled_actions = variable_get('expire_file_actions', array());
$enabled_actions = array_filter($enabled_actions);
// Stop further expiration if executed action is not selected by admin.
if (!in_array($action, $enabled_actions) && !$skip_action_check) {
return;
}
$expire_urls = array();
// Expire front page.
$expire_front_page = variable_get('expire_file_front_page', EXPIRE_FILE_FRONT_PAGE);
if ($expire_front_page) {
$expire_urls['front'] = '<front>';
}
// Expire file url.
$expire_file_page = variable_get('expire_file_file', EXPIRE_FILE_FILE);
if ($expire_file_page) {
$file_url = file_create_url($file->uri);
$parsed_url = parse_url($file_url);
// Remove the base path from our url.
$expire_urls['file-' . $file->fid] = substr($parsed_url['path'], strlen($base_path));
}
// Expire custom pages.
$expire_custom = variable_get('expire_file_custom', EXPIRE_FILE_CUSTOM);
if ($expire_custom) {
$pages = variable_get('expire_file_custom_pages');
$urls = ExpireAPI::expireCustomPages($pages, array('file' => $file));
$expire_urls = array_merge($expire_urls, $urls);
}
// Flush page cache for expired urls.
if (!empty($expire_urls)) {
ExpireAPI::executeExpiration($expire_urls, 'file', $file);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment