logo

Member
Member
Offline Offline
Posts: 46
Modification Release: Clear all mysql cache data from admin panel

Clear Cache

This will add ability to clear the cache to the admin panel. I don't know about others, but sometimes, I'm tweaking things in phpMyAdmin. It's annoying how the cache doesn't realize things have changed, and there's no button to reset the cache, so I wrote this.

As per a little more about it:
It requires a new table, which I named 'cache'
You need to change the cache file creation function to add the file to the cache table
You need to add the cache clearing function
You need to add the button to call the function

Below, is -step by step- exactly how to do it.

(Since it involves modification of core files, can't exactly make this a plugin.)

Step 1
The cache-clearing toy, thanks confuser for showing me the basics of how to read the directory. Your code wasn't right, but close enough to modify easily.

Create and Open:
Code:
site_root/functions/clearcache.php


Paste Contents:
Code:

<?php
/* eoCMS is a content management system written in php
    Copyright (C) 2007 - 2009  James Mortemore, Ryan Matthews
    http://www.eocms.com
   This work is licensed under the Creative Commons
   Attribution-Share Alike 3.0 United States License.
   To view a copy of this license, visit
   http://creativecommons.org/licenses/by-sa/3.0/us/
   or send a letter to Creative Commons, 171 Second Street,
   Suite 300, San Francisco, California, 94105, USA.
   Additional license terms at http://eocms.com/license.html

   Clear Cache written by FWishbringer and confuser
*/

function clearcache()
{
   global $user, $FUNCTIONS_LANG, $error, $error_die;
   if(!$user['admin_panel'])  {
      $error_die[] = $FUNCTIONS_LANG["e_permissions"];
      return false;
   }

   // Destroy cached tables.
   $file = './'.CACHE.'/tables.php';
   $OUTPUT = "<?php die(); ?>n";
   file_put_contents($file, $OUTPUT);

   $dir = opendir('cache/');
      while (($file=readdir($dir))!==false) {
         if(strlen($file) == 36 && $file != 'index.php' && $file != 'tables.php')
            unlink('./'.CACHE.'/' . $file);
      }
    closedir($dir);
}


Step 2
Next, time to give it a home. I think Site Settings is probably best, based on what's there.

Open:
Code:
site_root/Layouts/Admin/SiteSettings.php

..if using a custom SiteSettings.php, modify your template as well.

First part of step 2... add ability to click a button to call a php function. This method is used on other admin pages.

Find:
Code:

if($check==true && !errors()) {
   $_SESSION['update'] = 'The site settings have been updated';
   session_write_close();
   header("Location: index.php?act=admin&opt=sitesettings&".$authid);
}


After, Add:
Code:

if(isset($_GET['action']) && $_GET['action'] == 'clearcache') {
   call('clearcache');
}


Second part of step 2, actually putting the button in.
Find:
Code:

   <td><input type="text" name="username" value="'; if(errors()) { $body.= $_POST['username']; } else { $body.= $settings['smtp_username']; }$body .='" /></td></tr>
     <tr>
  <td>Password:</td>
   <td><input type="password" name="password" /></td>
</tr>


After, Add:
Code:

    <tr><td colspan="2" class="admin-subtitlebg">Cache</td></tr>
    <tr><td colspan="2" align="center"><div class="imagebutton"><a href="'.$settings['site_url'].'/index.php?act=admin&amp;opt=sitesettings&amp;type=clearcache&amp;'.$authid.'">Clear Cache</a></div></td></tr>
    <tr><td colspan="2" >&nbsp;</td></tr>
   

Done.

Now, there's a new button under Site Settings in the Admin Panel,

When clicked, it will delete the contents of tables.php (but leave the Die!), as well as the md5 named cache files.

A note about this, it also exposes the bug I was getting after running the installer... [[Warning: array_key_exists() expects parameter 2 to be array, boolean given in htdocsfunctionssql_query.php on line 23]]
Last Edit: 19th September, 2009, 05:15:58 PM by FWishbringer
eoCMS Developer
eoCMS Developer
Offline Offline
avatar
Posts: 1528
Re: Modification Release: Clear all mysql cache data from admin panel

You could just eliminate the need for another table by going through the directory and unlink() any .php files that do not have the name tables.php or index.php

Please do not PM me requesting support or anything, use the forums, thats what they are here for
Member
Member
Offline Offline
Posts: 46
Re: Modification Release: Clear all mysql cache data from admin panel

Therein lies my problem, I know a healthy bit about php + mysql, but I've never had to set up directly polling.. this unlink is the first time I've every written any web based file deletion... and I've tried reading a directory, lol.

If you can do it better, by all means.

The way I did it, I know it works, and can see what files would be there from the database without needing an ftp client open, it uses 1 extra insert per file creation, and the only other time the table is used, is when it's cleared, which is 1 select, a delete for every line, and 1 alter.
eoCMS Developer
eoCMS Developer
Offline Offline
avatar
Posts: 1528
Re: Modification Release: Clear all mysql cache data from admin panel

Try this
Code:
$dir = opendir('cache/');
   while (($file=readdir($dir))!==false) {
      if(strlen($file) == 35 && $file != 'index.php' && $file != 'tables.php')
         unlink($file);
   }
   closedir($dir);
Not tested it

Please do not PM me requesting support or anything, use the forums, thats what they are here for
Member
Member
Offline Offline
Posts: 46
Mod Updated

Thanks for the heads up on the 'basically what I needed to do' to do it without a database table.

You were close, but no cigar. Not bad for not-tested.

When counting characters, you must have forgot that the . counts too, so its
md5 + . + php = 36

Also, had to point it to the cache folder path.

Only took 10 seconds to figure out what was wrong and change it.

Updated original post with this method, since its much easier.

Of course, you're welcome to roll it into 0.9.0, I'm trying to help this place grow. Wink
Last Edit: 19th September, 2009, 05:19:36 PM by FWishbringer
eoCMS Developer
eoCMS Developer
Offline Offline
avatar
Posts: 1528
Re: Modification Release: Clear all mysql cache data from admin panel

Ok I have added it into 0.9.0. SVN is down, have to wait for it to come back up. Just a few minor changes though.

clearcache.php function file
Code:
<?php
/* eoCMS is a content management system written in php
    Copyright (C) 2007 - 2009  James Mortemore, Ryan Matthews
    http://www.eocms.com
   This work is licensed under the Creative Commons
   Attribution-Share Alike 3.0 United States License.
   To view a copy of this license, visit
   http://creativecommons.org/licenses/by-sa/3.0/us/
   or send a letter to Creative Commons, 171 Second Street,
   Suite 300, San Francisco, California, 94105, USA.
   Additional license terms at http://eocms.com/license.html

   Clear Cache written by FWishbringer and confuser
*/

function clearcache()
{
   global $user, $FUNCTIONS_LANG, $error, $error_die;
   if(!$user['admin_panel'])  {
      $error_die[] = $FUNCTIONS_LANG["e_permissions"];
      return false;
   }

   // Destroy cached tables.
   $file = './'.CACHE.'/tables.php';
   $OUTPUT = "<?php die(); ?>n";
   file_put_contents($file, $OUTPUT);

   $dir = opendir(CACHE.'/');
      while (($file=readdir($dir))!==false) {
         if(strlen($file) == 36 && $file != 'index.php' && $file != 'tables.php')
            unlink('./'.CACHE.'/' . $file);
      }
    closedir($dir);
}
?>

Changed the n to n in the $OUTPUT and changed the directory in opendir to CACHE.'/'

I also fixed a little typo you made with the if() statement
Code:
if(isset($_GET['type']) && $_GET['type'] == 'clearcache') {
   call('clearcache');
}
should be that, you have 'action' which is not what is set Wink

I also added
Code:
<tr><td colspan="2" >&nbsp;</td></tr>
before the button to space it out, it was too close to the "Cache" title...
Last Edit: 20th September, 2009, 02:51:51 AM by confuser

Please do not PM me requesting support or anything, use the forums, thats what they are here for
eoCMS Designer
eoCMS Designer
Offline Offline
avatar
Posts: 1290
tbarkass_willamson@hotmail.com
Re: Modification Release: Clear all mysql cache data from admin panel

Sorry to go off subject but I just realised that the breadcrumb on this is so long is has gone off the page... :S

I'll take a look at it.

Seen a bug? Report it!

Jump to:


0.06 seconds Queries: 13