Skip to content

Instantly share code, notes, and snippets.

@gandbox
Created September 5, 2012 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gandbox/3634261 to your computer and use it in GitHub Desktop.
Save gandbox/3634261 to your computer and use it in GitHub Desktop.
bin/php/ script for template-block files expiry
#!/usr/bin/env php
<?php
require 'autoload.php';
if ( !function_exists( 'readline' ) )
{
function readline( $prompt = '' )
{
echo $prompt . ' ';
return trim( fgets( STDIN ) );
}
}
define("DEFAULT_LIMIT", 500);
$cli = eZCLI::instance();
$script = eZScript::instance(
array(
'description' =>
"Manage cache blocks\n\n",
'use-session' => true,
'use-modules' => true,
'use-extensions' => true
)
);
$ezCacheBlock = new ezgCacheBlock( $script, $cli );
$ezCacheBlock->run();
$script->shutdown( 0 );
/**
*
*/
class ezgCacheBlock
{
/**
* Constructor
*
* @param eZScript $script
* @param eZCLI $cli
*/
function ezgCacheBlock( eZScript $script, eZCLI $cli )
{
$this->Script = $script;
$this->CLI = $cli;
$this->Options = null;
$this->Limit=DEFAULT_LIMIT;
$this->Verbose=false;
$this->VarDir = eZINI::instance()->variable( 'FileSettings', 'VarDir' );
$this->VarTemplateCacheDir = $this->VarDir . '/cache/template-block/';
}
/**
* Startup and run script.
*/
public function run()
{
$this->Script->startup();
$this->Options = $this->Script->getOptions(
"[verbose][highlight][limit:][match:]",
"",
array(
'match' => "Value to match inside the cache-blocks to delete",
'verbose' => "Verbose mode",
'highlight' => "Highlight the matching part of files",
'limit' => "Limit number of file to delete"
)
);
$this->Limit=$this->Options['limit'] ? $this->Options['limit'] : DEFAULT_LIMIT;
$this->Match=trim($this->Options['match']);
$this->Verbose=$this->Options['verbose'] ? true : false;
$this->Highlight=$this->Options['highlight'] ? true : false;
$this->Script->initialize();
$fileHandler = eZClusterFileHandler::instance();
if ( $fileHandler->requiresClusterizing() )
{
$this->CLI->error( 'This script only work with FS FileHandler, and not DB FileHandler as ' . get_class( $fileHandler ) );
$this->Script->shutdown( 0 );
}
if ( strlen( $this->Match ) == 0 ) {
$this->CLI->error( 'You need to set a value to match. If you want to clear the all template-block cache, use "php bin/php/ezcache.php --clear-id=template-block" instead' );
$this->Script->shutdown( 0 );
exit();
}
if ( strlen( $this->Match ) < 2 ) {
$this->CLI->error( 'You need to set a value to match >= 3 characters' );
$this->Script->shutdown( 0 );
exit();
}
if (!is_dir( $this->VarDir ))
{
$this->CLI->error( 'Folder ' . $this->VarDir . 'does not exist !' );
$this->Script->shutdown( 0 );
exit();
}
$dir = $this->VarDir."/cache/template-block/";
$search = "\"". $this->Match ."\"";
$cmd = 'find '.$dir.' -name "*.cache" -exec grep -iHl '.$search.' {} \;';
$CachefilesNumber = 0;
if ( $this->Verbose ) {
$input = readline( 'Do you want to display the complet list of matching files with : "'. $this->Match .'" ? ([y] or [n] to skip )' );
if ( $input === 'y' )
{
if ( $this->Highlight ) {
$list = $this->getFilesList( 'iH' );
$CachefilesNumber = $this->getCachefilesNumber();
} else {
$list = $this->getFilesList( 'iHl' );
if ( $list !== "") {
$CachefilesNumber = count( explode("\n", $list ) );
}
}
$this->CLI->output( $list );
}
}
else {
$CachefilesNumber = $this->getCachefilesNumber();
}
if ( $CachefilesNumber > $this->Limit ) {
$this->CLI->error( 'Script aborted. The maximum number limit of ' . $this->Limit . ' files was exceeded (' . $CachefilesNumber . ' files to delete !) ');
$this->Script->shutdown( 0 );
exit();
}
if ( $CachefilesNumber == 0 ) {
$this->CLI->warning( 'No matching with ' . $this->Match );
$this->Script->shutdown( 0 );
echo "toto";
exit();
}
$input = readline( 'Are you really sure to delete ' . $CachefilesNumber . ' template-block cache files ? ([y] or [q] to quit )' );
if ( $input === 'q' )
{
$this->Script->shutdown( 0 );
}
$this->deleteFilesList();
}
/**
* Get the number of files to delete
*/
protected function getCachefilesNumber()
{
$cmd = 'find '.$this->VarTemplateCacheDir.' -name "*.cache" -exec grep -iHl ' . stripslashes($this->Match) . ' {} \; | wc -l';
return $this->getSHELL( $cmd );
}
/**
* Get the file list to delete (output depending of grep $params)
*/
protected function getFilesList( $params = 'iHl' )
{
$cmd = 'find '.$this->VarTemplateCacheDir.' -name "*.cache" -exec grep -' . $params . ' "' . stripslashes($this->Match) . '" {} \;';
return $this->getSHELL( $cmd );
}
/**
* Delete the file list
*/
protected function deleteFilesList( )
{
$cmd = 'find '.$this->VarTemplateCacheDir.' -name "*.cache" -exec grep -iHl "' . stripslashes($this->Match) . '" {} \; -delete';
return $this->getSHELL( $cmd );
}
/**
* Generic shell exec
*/
protected function getSHELL( $cmd )
{
try {
if ( $this->Verbose ) {
$this->CLI->warning( 'SHELL COMMAND: ' . $cmd );
}
$cmd_result = shell_exec( $cmd );
$cmd_result = trim( $cmd_result, " \n" );
return $cmd_result;
} catch (Exception $e) {
$this->CLI->error( 'ERROR: ' . $e->getMessage() );
}
}
}
?>
@Alexnder
Copy link

This: $this->VarDir = eZINI::instance()->variable( 'FileSettings', 'VarDir' );
Need go after $this->Script->initialize();
Because if it's go before, it get data from default siteaccess.
// Maybe it need only if you use multiple siteaccess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment