From 30c120604c86177c0f9e14420440de7911072c2a Mon Sep 17 00:00:00 2001 From: jaseclamp Date: Mon, 16 Feb 2015 12:45:43 +1000 Subject: [PATCH] cleanup! updates after some great feedback from kevincupp --- README.md | 2 +- third_party/purge/ext.purge.php | 27 ++- third_party/purge/helpers/varnish_helper.php | 13 +- third_party/purge/mcp.purge.php | 52 ++--- third_party/purge/upd.detour_pro.php | 188 ------------------- third_party/purge/upd.purge.php | 20 +- third_party/purge/views/rules.php | 1 - 7 files changed, 46 insertions(+), 257 deletions(-) delete mode 100644 third_party/purge/upd.detour_pro.php diff --git a/README.md b/README.md index a1f58a7..bc2ebe9 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ If you click into modules > Purge you can now set URL patterns to purge when ent ## Changelog -* **1.0.5 - February 13, 2015** +* **1.1 - February 13, 2015** * Changed addon to only purge via url patterns when entries are saved. All cache can still be purged using the accessory. * **1.0.4 - March 17, 2014** * Fixing issue #11 where there were bugs with purging multiple Varnish servers and purging by IP address. diff --git a/third_party/purge/ext.purge.php b/third_party/purge/ext.purge.php index 325166a..b558735 100755 --- a/third_party/purge/ext.purge.php +++ b/third_party/purge/ext.purge.php @@ -90,8 +90,6 @@ public function activate_extension() public function send_purge_request($id,$meta,$data) { - //var_dump($id,$meta,$data); die; - $this->EE->load->helper('varnish'); $urls = $this->site_url; @@ -101,26 +99,35 @@ public function send_purge_request($id,$meta,$data) $urls = array($urls); } - foreach ($urls as $url) + //get patterns for this channel + $this->EE->db->select('*'); + $this->EE->db->where('channel_id',(int) $meta['channel_id']); + $channelPatterns = $this->EE->db->get_where('purge_rules')->result_array(); + + //if no patterns this may mean patterns aren't configured therefore revert to old behaviour of clearing everything + if(count($channelPatterns)==0) { - //now loop through urls for this channel + //are there any patterns at all? $this->EE->db->select('*'); - $this->EE->db->where('channel_id',(int) $meta['channel_id']); $channelPatterns = $this->EE->db->get_where('purge_rules')->result_array(); - + if(count($channelPatterns)==0) $channelPatterns = array( array('pattern','/.*$') ); + else return false; //patterns are configured but not for this channel. + } + + //now loop through varnish urls + foreach ($urls as $url) + { + //and loop through patterns for each foreach($channelPatterns as $pattern) { - $_pattern = str_replace('{url_title}',$meta['url_title'],$pattern['pattern']); + $_pattern = str_replace('{url_title}',$meta['url_title'],$pattern['pattern']); //only str replacing url title at this point $_url = preg_replace('/\/$/','',$url).'/'.preg_replace('/^\//','',$_pattern); //handle trailing and beginning slashes - //echo $_url . "
"; send_purge_request($_url, $this->port); unset($_pattern); } } } - // ---------------------------------------------------------------------- - /** * Disable Extension * diff --git a/third_party/purge/helpers/varnish_helper.php b/third_party/purge/helpers/varnish_helper.php index 2a2b32d..8cd35ab 100644 --- a/third_party/purge/helpers/varnish_helper.php +++ b/third_party/purge/helpers/varnish_helper.php @@ -20,20 +20,13 @@ function send_purge_request($site_url = NULL, $site_port = NULL) { $purge_url = $site_url; $port = $site_port; - /*$parsed_url = parse_url($site_url); - $url_path = isset($parsed_url['path']) ? $parsed_url['path'] : '/'; - $url_scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : $protocol; - $purge_url = $url_scheme . $parsed_url['host'] . $url_path; - $port = ( ! isset($parsed_url['port']) || empty($parsed_url['port'])) ? 80 : $parsed_url['port'];*/ } if (empty($port)) { $port = 80; } - - echo $purge_url; - + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $purge_url); @@ -41,8 +34,8 @@ function send_purge_request($site_url = NULL, $site_port = NULL) curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Host: '.$_SERVER['SERVER_NAME'] ) ); curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'PURGE'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,1); - curl_setopt($ch, CURLOPT_TIMEOUT, 2); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,2); + curl_setopt($ch, CURLOPT_TIMEOUT, 4); if(curl_exec($ch) === false) die( curl_error ( $ch ) ); curl_close ($ch); diff --git a/third_party/purge/mcp.purge.php b/third_party/purge/mcp.purge.php index 203c23e..2552842 100644 --- a/third_party/purge/mcp.purge.php +++ b/third_party/purge/mcp.purge.php @@ -6,10 +6,8 @@ class Purge_mcp { public $return_data; public $return_array = array(); - private $_base_url; - private $_data = array(); - private $_module = 'purge'; + /** @@ -22,7 +20,7 @@ public function __construct() $this->_base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=purge'; $this->EE->cp->set_right_nav(array( - 'module_home' => $this->_cp_url() + 'module_home' => $this->_base_url )); $this->EE->view->cp_page_title = "Purge: Channel URL Patterns for Varnish"; @@ -34,19 +32,20 @@ public function __construct() public function index() { - $this->EE->load->library('table'); + //$this->EE->load->library('table'); - $this->_data['action_url'] = $this->_base_url . '&method=save'; - //$this->_data['images'] = $this->get_alttags(); + $_data = array(); + $_data['action_url'] = $this->_base_url . '&method=save'; + $this->EE->load->model('channel_model'); $channels_query = $this->EE->channel_model->get_channels()->result(); foreach ($channels_query as $channel) - $this->_data['channels'][] = array('channel_title' => $channel->channel_title, 'channel_name' => $channel->channel_name, 'channel_id' => $channel->channel_id ); + $_data['channels'][] = array('channel_title' => $channel->channel_title, 'channel_name' => $channel->channel_name, 'channel_id' => $channel->channel_id ); - $this->_data['rules'] = $this->get(); + $_data['rules'] = $this->get(); - return $this->EE->load->view('rules', $this->_data, TRUE); + return $this->EE->load->view('rules', $_data, TRUE); } @@ -69,40 +68,11 @@ public function save() { $this->EE->db->insert( 'purge_rules', array('channel_id' => $channel, 'pattern' => $patterns[$key]) ); } - - // Redirect back to Detour Pro landing page + $this->EE->functions->redirect($this->_base_url); } - function strposa($haystack, $needle, $offset=0) { - if(!is_array($needle)) $needle = array($needle); - foreach($needle as $query) { - if(strpos($haystack, $query, $offset) !== false) return true; // stop on first true result - } - return false; - } - - - - private function _cp_url ($method = 'index', $variables = array()) { - $url = BASE . AMP . 'C=addons_modules' . AMP . 'M=show_module_cp' . AMP . 'module=' . $this->_module . AMP . 'method=' . $method; - - foreach ($variables as $variable => $value) { - $url .= AMP . $variable . '=' . $value; - } - - return $url; - } - - private function _form_url ($method = 'index', $variables = array()) { - $url = 'C=addons_modules' . AMP . 'M=show_module_cp' . AMP . 'module=' . $this->_module . AMP . 'method=' . $method; - - foreach ($variables as $variable => $value) { - $url .= AMP . $variable . '=' . $value; - } - - return $url; - } + diff --git a/third_party/purge/upd.detour_pro.php b/third_party/purge/upd.detour_pro.php deleted file mode 100644 index 41fc4e6..0000000 --- a/third_party/purge/upd.detour_pro.php +++ /dev/null @@ -1,188 +0,0 @@ -EE =& get_instance(); - } - - // ---------------------------------------------------------------- - - /** - * Installation Method - * - * @return boolean TRUE - */ - public function install() - { - $mod_data = array( - 'module_name' => 'Detour_pro', - 'module_version' => $this->version, - 'has_cp_backend' => "y", - 'has_publish_fields' => 'n' - ); - - $this->EE->functions->clear_caching('db'); - $this->EE->db->insert('modules', $mod_data); - - $this->EE->load->dbforge(); - - /* Check to see if detour table exists */ - $sql = 'SHOW TABLES LIKE \'%detour%\''; - $result = $this->EE->db->query($sql); - - $prev_install = ($result->num_rows) ? TRUE : FALSE; - - if($prev_install) - { - // Detour ext installed, update table to include MSM - - $query = $this->EE->db->get('detours', 1)->row_array(); - - // Double check to see if site_id already exists - if(!array_key_exists('site_id', $query)) - { - $fields = array( - 'site_id' => array('type' => 'int', 'constraint' => '4', 'unsigned' => TRUE), - 'start_date' => array('type' => 'date', 'null' => TRUE), - 'end_date' => array('type' => 'date', 'null' => TRUE), - ); - $this->EE->dbforge->add_column('detours', $fields); - - // Apply site id of 1 to all existing detours - $this->EE->db->update('detours', array('site_id' => 1), 'detour_id > 0'); - } - } - else - { - // Create detour tables and keys - $fields = array - ( - 'detour_id' => array('type' => 'int', 'constraint' => '10', 'unsigned' => TRUE, 'auto_increment' => TRUE), - 'original_url' => array('type' => 'varchar', 'constraint' => '250'), - 'new_url' => array('type' => 'varchar', 'constraint' => '250', 'null' => TRUE, 'default' => NULL), - 'start_date' => array('type' => 'date', 'null' => TRUE), - 'end_date' => array('type' => 'date', 'null' => TRUE), - 'detour_method' => array('type' => 'int', 'constraint' => '3', 'unsigned' => TRUE, 'default' => '301'), - 'site_id' => array('type' => 'int', 'constraint' => '4', 'unsigned' => TRUE) - ); - - $this->EE->dbforge->add_field($fields); - $this->EE->dbforge->add_key('detour_id', TRUE); - $this->EE->dbforge->create_table('detours'); - } - - unset($fields); - - // Create hits table - - - $sql = 'SHOW TABLES LIKE \'%detours_hits%\''; - $result = $this->EE->db->query($sql); - - $prev_install = ($result->num_rows) ? TRUE : FALSE; - - if(!$prev_install) - { - $this->_create_table_hits(); - } - - // Enable the extension to prevent redirect erros while installing. - $this->EE->db->where('class', 'Detour_pro_ext'); - $this->EE->db->update('extensions', array('enabled'=>'y')); - - return TRUE; - } - - // ---------------------------------------------------------------- - - /** - * Uninstall - * - * @return boolean TRUE - */ - public function uninstall() - { - $mod_id = $this->EE->db->select('module_id') - ->get_where('modules', array( - 'module_name' => 'Detour_pro' - ))->row('module_id'); - - $this->EE->db->where('module_id', $mod_id) - ->delete('module_member_groups'); - - $this->EE->db->where('module_name', 'Detour_pro') - ->delete('modules'); - - $this->EE->load->dbforge(); - $this->EE->dbforge->drop_table('detours'); - $this->EE->dbforge->drop_table('detours_hits'); - - return TRUE; - } - - // ---------------------------------------------------------------- - - /** - * Module Updater - * - * @return boolean TRUE - */ - public function update($current = '') - { - // If you have updates, drop 'em in here. - return TRUE; - } - - - /* Private Functions */ - - function _create_table_hits() - { - $fields = array( - 'hit_id' => array('type' => 'int', 'constraint' => '10', 'unsigned' => TRUE, 'auto_increment' => TRUE), - 'detour_id' => array('type' => 'int', 'constraint' => '10', 'unsigned' => TRUE), - 'hit_date' => array('type' => 'datetime') - ); - - $this->EE->dbforge->add_field($fields); - $this->EE->dbforge->add_key('hit_id', TRUE); - - return ($this->EE->dbforge->create_table('detours_hits')) ? TRUE : FALSE; - } - -} -/* End of file upd.detour_pro.php */ -/* Location: /system/expressionengine/third_party/detour_pro/upd.detour_pro.php */ \ No newline at end of file diff --git a/third_party/purge/upd.purge.php b/third_party/purge/upd.purge.php index a6c6c9d..ea3e121 100644 --- a/third_party/purge/upd.purge.php +++ b/third_party/purge/upd.purge.php @@ -4,7 +4,7 @@ class Purge_upd { - public $version = '1.0'; + public $version = '1.1'; private $EE; @@ -36,11 +36,9 @@ public function install() $sql[] = " CREATE TABLE `{$this->EE->db->dbprefix}purge_rules` ( `id` int(11) unsigned NOT NULL auto_increment, - `site_id` int(4) unsigned NOT NULL default '1', - `channel_id` int(4) NOT NULL, + `channel_id` int(4) unsigned NOT NULL, `pattern` varchar(255) default NULL, - PRIMARY KEY (`id`), - KEY `site_id` (`site_id`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "; @@ -73,6 +71,16 @@ public function install() public function uninstall() { + $mod_id = $this->EE->db->select('module_id') + ->get_where('modules', array( + 'module_name' => 'Purge' + ))->row('module_id'); + + $this->EE->db->where('module_id', $mod_id) + ->delete('module_member_groups'); + + $this->EE->db->where('module_name', 'Purge') + ->delete('modules'); $this->EE->load->dbforge(); $this->EE->dbforge->drop_table('purge_rules'); @@ -90,7 +98,7 @@ public function uninstall() public function update($current = '') { // If you have updates, drop 'em in here. - return TRUE; + return FALSE; } diff --git a/third_party/purge/views/rules.php b/third_party/purge/views/rules.php index 2480b3d..27008f5 100644 --- a/third_party/purge/views/rules.php +++ b/third_party/purge/views/rules.php @@ -6,7 +6,6 @@ $( document ).ready(function() { var addtemplate = $('#add-template')[0].outerHTML; $('#add-template').remove(); - console.info('this is working'); $(document).on('click', '.icon-remove', function() { $(this).parents('tr.rulerow').remove();