We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47243
    • 30 Posts
    hi!
    been using refreshcache! so handy!

    just wanted to share some options I added to the snippet.
    Let you choose the context, how many of the last resource to refresh, restrict to parents and include refresh of parents :
    <?php
    
    /* setup default properties */
    
    $start = $modx->getOption('start',$scriptProperties,0);
    $limit = $modx->getOption('limit',$scriptProperties,1);
    $sort = $modx->getOption('sort',$scriptProperties,'editedon');
    $dir = $modx->getOption('dir',$scriptProperties,'DESC');
    $dateFormat = $modx->getOption('dateFormat',$scriptProperties,'%b %d, %Y %I:%M %p');
    
    
    /** @var $modx modX */
    $modx->lexicon->load('refreshcache:default');
    if (! $modx->user->isMember('Administrator') ) {
        $msg = $modx->lexicon('rc_admin_only');
        return $msg;
    }
    
    // $modx->regClientStartupScript("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js");
    
    //include class
    $path = $modx->getOption('refresh_cache_core_path', null, $modx->getOption('core_path') . 'components/refreshcache/') . 'model/refreshcache/';
    require_once($path . 'install.class.php');
    
    //initialize class
    $install = new Installer($modx);
    $buttonMsg = $modx->lexicon('rc_button_message');
    
    
    $contextKeys = array();
    $query = $modx->newQuery('modContext', array('key:NOT IN' => array('mgr')));
    $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
    $query->sortby('rank','ASC');
    if ($query->prepare() && $query->stmt->execute()) {
        $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
    }
    
    
    
    echo "\n" . '
    <!-- Remember to add form id="apiform" and target="progressFrame" to make script work -->
    <center>
    <form id="apiform" target="progressFrame" method="post">
    	<br><br>
    	<label style="font-size:20px; ">Select context</label><br>';
    	echo '<div style="margin:7px 0 20px;">';
    
    	foreach ($contextKeys as $contextKey) {
    		$check = '';
    		if($contextKey == 'web') { $check = 'checked';}
    		echo '<label>  <input type="radio" name="contextKey" value="'.$contextKey.'" '.$check.'> '.$contextKey.'</label>';
    
    	}
    	echo '</div>';
    	echo '
    	<label style="font-size:20px; ">Restrict in parents (type in ids separated by comma)</label><br>
    	<div style="margin:7px 0 20px;">
    		<input type="text" size="25" value="" name="parentsIds">
    	</div>
    	<label style="font-size:20px; ">Number of last resources to refresh</label><br><br>
    	<label>  <input type="radio" name="nbLast" value="0">All</label>
    	<label>  <input type="radio" name="nbLast" value="1" checked> 1</label>
    	<label>  <input type="radio" name="nbLast" value="2"> 2</label>
    	<label>  <input type="radio" name="nbLast" value="3"> 3</label>
    	<label>  <input type="radio" name="nbLast" value="4"> 4</label>
    	<label>  <input type="radio" name="nbLast" value="5"> 5</label>
    	<label>  <input type="radio" name="nbLast" value="10"> 10</label>
    	<label>  <input type="radio" name="nbLast" value="15"> 15</label>
    	<label>  <input type="radio" name="nbLast" value="20"> 20</label>
    	<label>  <input type="radio" name="nbLast" value="30"> 30</label>
    	<label>  <input type="radio" name="nbLast" value="50"> 50</label>
    	<label>  <input type="radio" name="nbLast" value="75"> 75</label>
    	<label>  <input type="radio" name="nbLast" value="100"> 100</label><br>
    	<br>
    	<label style="font-size:20px; ">Refresh resources parents</label><br>
    	<div style="margin:7px 0 20px;">
    		<input type="checkbox" name="refreshParents" value="1" checked> 
    	</div>
    
        <input style="position : inherit;" id="apisubmit" type="submit" name="submit" value="' . $buttonMsg . '">
    </form>
    </center>';
    
    $limit= $_POST['nbLast'];
    $arrayParents = '';
    if($_POST['parentsIds']!='') { $arrayParents = explode(',',$_POST['parentsIds']);};
    
    
    
    
    
    
    
    
    //load form, define progress bar colours
    $install->placeholder();
    
    /* Get curl delay from System Setting */
    $delay = $modx->getOption('refreshcache_curl_delay', null, 0);
    
    $mtime = microtime();
    $mtime = explode(" ", $mtime);
    $mtime = $mtime[1] + $mtime[0];
    $tstart = $mtime;
    
    
    
    if (isset($_POST['submit'])) {
    
        /* Ignore resources that are uncached, deleted,
            or unpublished */
        set_time_limit(0);
        $query = $modx->newQuery('modResource');
        $query->where(array(
               array(
                   'class_key:=' => 'modDocument',
                   'OR:class_key:=' => 'Article'
               ),
               array(
                   'AND:published:=' => '1',
                   'AND:deleted:=' => '0',
                   'AND:cacheable:=' => '1',
    			   'AND:parent:IN' => $arrayParents
               )
        ));
    
    	// custom arteractive
    	$query->sortby($sort,$dir);
    	if ($isLimit) $query->limit($limit,$start);
    
    
    	// test of other queries
    	//$query2 = $modx->newQuery('modResource', array('parent' => 204, 'deleted' => false, 'published' => true));
    	//$query2->limit(3,$start);
    
    
    	// getCollection
        $resources = $modx->getCollection('modResource', $query);
        $count = count($resources);  //set number of process steps
    
    
    	$install->setSteps($count+2);
        $install->defineBar();
    
    	//$install->save(implode(",", $parentsID)." ".$count_parents."_");
    
    
    
        if (empty($resources)) {
            $output = $modx->lexicon('rc_no_resources');
            $install->save($output);
            $install->delay(3);
        }
    
        $ch = curl_init(); // Initialize Curl
        if ($ch === false) {
            $output = $modx->lexicon("rc_no_curl");
            $install->save($output);
            $install->delay(3);
        }
    
        @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        @curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
    
        ignore_user_abort(true); // keep on going even if user pulls the plug*
    
        $i = 1;
        $refreshingMsg = $modx->lexicon('rc_refreshing');
        $resourceMsg = $modx->lexicon('resources');
        $output = "<p>" . $refreshingMsg . " " . $count . " " . $resourceMsg . "</p><p> </p>";
        $install->save($output);
        $install->delay(1);
        sleep(1);
    
    
    
        foreach ($resources as $resource) {
            // @var $resource modResource 
            $pageId = $resource->get('id');
            $pagetitle = $resource->get('pagetitle');
            $url = $modx->makeUrl($pageId, '', '', 'full');
            // Avoid infinite loop when requesting this page 
            if (strstr($url, 'refresh-cache' || $modx->$resource->get('id') == $pageId)) {
                continue;
            }
    
            $output = '<p>(' . $i . '/' . $count . ") " . $refreshingMsg . "</p><p>" . $pagetitle . '</p>';
            $install->save($output);
            $install->delay($delay);
    
            $i++;
    
            curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
    
            $unused = curl_exec($ch); // get the page - do nothing with it
    
            if (curl_errno($ch)) {
                $output = 'cURL error: ' . curl_errno($ch) . " - " . curl_error($ch);
                $install->save($output);
                $install->delay($delay);
            }
        } // end foreach($resources) loop 
    
    
        curl_close($ch);
    
        unset($output, $unused, $ch, $pagetitle, $url, $pageId);
    
    	if($_POST['refreshParents']=='1'){
    
    	  // PARENTS CACHE BUILD ==============================================================================================
    	  // PARENTS ID retrieve all parents that needs cache rebuild too
    	  $parentsID = array();
    	  foreach ($resources as $resource) {
    		  $parentsID = array_merge($parentsID, $modx->getParentIds($resource->get('id'), 30, array('context' => $_POST['contextKey'])));
    	  }
    	  $parentsID= array_unique($parentsID);
      
      
    	  $query_parents = $modx->newQuery('modResource');
    	  $query_parents->where(array(
    		 'id:IN' => $parentsID
    	  ));
    	  $resources_parents = $modx->getCollection('modResource', $query_parents);
    	  $count_parents = count($resources_parents);  //set number of process steps
      
      
    	  $install->setSteps($count_parents+2);
    	  $install->defineBar();
      
    	  //$install->save(implode(",", $parentsID)." ".$count_parents."_");
      
      
    	  if (empty($resources_parents)) {
    		  $output = $modx->lexicon('rc_no_resources');
    		  $install->save($output);
    		  $install->delay(3);
    	  }
      
    	  $ch = curl_init(); // Initialize Curl
    	  if ($ch === false) {
    		  $output = $modx->lexicon("rc_no_curl");
    		  $install->save($output);
    		  $install->delay(3);
    	  }
      
    	  @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    	  @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	  @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    	  @curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
      
    	  ignore_user_abort(true); // keep on going even if user pulls the plug*
      
    	  $i = 1;
    	  $refreshingMsg = $modx->lexicon('rc_refreshing');
    	  $resourceMsg = $modx->lexicon('resources');
    	  $output = "<p>Parents: </p><p>" . $refreshingMsg . " " . $count . " " . $resourceMsg . "</p><p> </p>";
    	  $install->save($output);
    	  $install->delay(2);
    	  sleep(1);
      
      
      
    	  foreach ($resources_parents as $resource) {
    		  // @var $resource modResource 
    		  $pageId = $resource->get('id');
    		  $pagetitle = $resource->get('pagetitle');
    		  $url = $modx->makeUrl($pageId, '', '', 'full');
    		  // Avoid infinite loop when requesting this page 
    		  if (strstr($url, 'refresh-cache' || $modx->$resource->get('id') == $pageId)) {
    			  continue;
    		  }
      
    		  $output = '<p>(' . $i . '/' . $count_parents . ") " . $refreshingMsg . "</p><p>" . $pagetitle . '</p>';
    		  $install->save($output);
    		  $install->delay($delay);
      
    		  $i++;
      
    		  curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
      
    		  $unused = curl_exec($ch); // get the page - do nothing with it
      
    		  if (curl_errno($ch)) {
    			  $output = 'cURL error: ' . curl_errno($ch) . " - " . curl_error($ch);
    			  $install->save($output);
    			  $install->delay($delay);
    		  }
    	  } // end foreach($resources) loop 
      
      
    	  curl_close($ch);
      
    	  unset($output, $unused, $ch, $pagetitle, $url, $pageId);
    	}
    
    
    
    
    
    
        $mtime = microtime();
        $mtime = explode(" ", $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $tend = $mtime;
        $seconds = ($tend - $tstart);
        $totalTime = sprintf( "%02.2d:%02.2d", floor( $seconds / 60 ), $seconds % 60 );
        /* No lex string here -- JS checks for this string to terminate loop */
        $install->save("<p>FINISHED -- Execution time</p><p>(minutes:seconds): {$totalTime}</p>");
    }
    
    return '';


    The css of refreshcache must be modified too on line 66 in refreshcache.css :

    div#apinstall {
        position: absolute;
    	position: relative;
        /*top: 20%;*/
        left: 50%;
        width: 30em;
        height: 130px;
        margin-top: 20px; /*set to a negative number 1/2 of your height*/
        margin-left: -15em; /*set to a negative number 1/2 of your width*/
        border: 1px solid #ccc;
    
        background-color: white;
    
        padding-top: 35px;
        padding-bottom: 7px;
    
        -moz-border-radius: 15px;
        -webkit-border-radius: 15px;
        border-radius: 15px;
        box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.86);
        -moz-box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.86);
        -webkit-box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.86);
        z-index:999;
    }
    



    TO DO: if editedon field is not set, take into account publishedon. Indeed, when we create a new resources, unless we save it again, editedon field is not set
    Anyone can help me do this?

    TO DO 2 : make a clean progress bar and tidy up the code smiley
    [ed. note: robotpapier last edited this post 12 years, 5 months ago.]
      • 47243
      • 30 Posts
      oh, also wanted to share how I use the cache thing,

      cachemaster is installed and is supposed to uncheck "empty cache" so it won't refresh all cache, but it seems like for a newly created resource , it does'nt work.
      I still installed it because, on save it will mark it as uncached in modx.

      So in order to not empty all cache by accident, I installed cacheguard which unchecks really the empty cache option.

      Also, at first I used uncacher extra but the onsave time is really long, so I ended up using refreshcache.
      Then once I made all my changes I just have to start the refreshcache snippet.
        • 3749
        • 24,544 Posts
        Interesting.

        BTW, I think checking the class_key in the query is unnecessary since the Article class extends modResource. A query on modResource will still find Articles.

        It might make sense, though, to make it modDocument and Article to skip static resources, symlinks, and weblinks.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 47243
          • 30 Posts
          ok, this part of the code was done by the one who wrote refreshcache.
          But I guess you're write haven't tried it without.

          Besides, here is the update of the code I posted.
          I noticed that when I use it to refresh an article that had been uncached by cachemaster on save, it won't uncached its parents that I refresh with refreshcache.
          So I added the code of cachemaster which uncache onmy a specific resource.

          Now if I add, an resource and refresh its cache, it will refresh it and uncache all its parents and then refresh its parents.

          /* setup default properties */
          $isLimit = 1;//!empty($scriptProperties['limit']);
          $start = $modx->getOption('start',$scriptProperties,0);
          $limit = $modx->getOption('limit',$scriptProperties,1);
          $sort = $modx->getOption('sort',$scriptProperties,'editedon');
          $dir = $modx->getOption('dir',$scriptProperties,'DESC');
          $dateFormat = $modx->getOption('dateFormat',$scriptProperties,'%b %d, %Y %I:%M %p');
          
          
          /** @var $modx modX */
          $modx->lexicon->load('refreshcache:default');
          if (! $modx->user->isMember('Administrator') ) {
              $msg = $modx->lexicon('rc_admin_only');
              return $msg;
          }
          
          // $modx->regClientStartupScript("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js");
          
          //include class
          $path = $modx->getOption('refresh_cache_core_path', null, $modx->getOption('core_path') . 'components/refreshcache/') . 'model/refreshcache/';
          require_once($path . 'install.class.php');
          
          //initialize class
          $install = new Installer($modx);
          $buttonMsg = $modx->lexicon('rc_button_message');
          
          
          $contextKeys = array();
          $query = $modx->newQuery('modContext', array('key:NOT IN' => array('mgr')));
          $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
          $query->sortby('rank','ASC');
          if ($query->prepare() && $query->stmt->execute()) {
              $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
          }
          
          
          
          echo "\n" . '
          <!-- Remember to add form id="apiform" and target="progressFrame" to make script work -->
          <center>
          <form id="apiform" target="progressFrame" method="post">
          	<br><br>
          	<label style="font-size:20px; ">Select context</label><br>';
          	echo '<div style="margin:7px 0 20px;">';
          
          	foreach ($contextKeys as $contextKey) {
          		$check = '';
          		if($contextKey == 'web') { $check = 'checked';}
          		echo '<label>  <input type="radio" name="contextKey" value="'.$contextKey.'" '.$check.'> '.$contextKey.'</label>';
          
          	}
          	echo '</div>';
          	echo '
          	<label style="font-size:20px; ">Restrict in parents (type in ids separated by comma)</label><br>
          	<div style="margin:7px 0 20px;">
          		<input type="text" size="25" value="" name="parentsIds">
          	</div>
          	<label style="font-size:20px; ">Number of last resources to refresh</label><br><br>
          	<label>  <input type="radio" name="nbLast" value="0">All</label>
          	<label>  <input type="radio" name="nbLast" value="1" checked> 1</label>
          	<label>  <input type="radio" name="nbLast" value="2"> 2</label>
          	<label>  <input type="radio" name="nbLast" value="3"> 3</label>
          	<label>  <input type="radio" name="nbLast" value="4"> 4</label>
          	<label>  <input type="radio" name="nbLast" value="5"> 5</label>
          	<label>  <input type="radio" name="nbLast" value="10"> 10</label>
          	<label>  <input type="radio" name="nbLast" value="15"> 15</label>
          	<label>  <input type="radio" name="nbLast" value="20"> 20</label>
          	<label>  <input type="radio" name="nbLast" value="30"> 30</label>
          	<label>  <input type="radio" name="nbLast" value="50"> 50</label>
          	<label>  <input type="radio" name="nbLast" value="75"> 75</label>
          	<label>  <input type="radio" name="nbLast" value="100"> 100</label><br>
          	<br>
          	<label style="font-size:20px; ">Refresh resources parents</label><br>
          	<div style="margin:7px 0 20px;">
          		<input type="checkbox" name="refreshParents" value="1" checked> 
          	</div>
          
              <input style="position : inherit;" id="apisubmit" type="submit" name="submit" value="' . $buttonMsg . '">
          </form>
          </center>';
          
          $limit= $_POST['nbLast'];
          $arrayParents = '';
          if($_POST['parentsIds']!='') { $arrayParents = explode(',',$_POST['parentsIds']);};
          
          
          
          
          
          
          
          
          
          //load form, define progress bar colours
          $install->placeholder();
          
          /* Get curl delay from System Setting */
          $delay = $modx->getOption('refreshcache_curl_delay', null, 0);
          
          $mtime = microtime();
          $mtime = explode(" ", $mtime);
          $mtime = $mtime[1] + $mtime[0];
          $tstart = $mtime;
          
          
          
          if (isset($_POST['submit'])) {
          
          
              /* Ignore resources that are uncached, deleted,
                  or unpublished */
              set_time_limit(0);
              $query = $modx->newQuery('modResource');
          
          
          	if(!empty($arrayParents)){
          	  $query->where(array(
          			 array(
          				 'class_key:=' => 'modDocument',
          				 'OR:class_key:=' => 'Article'
          			 ),
          			 array(
          				 'AND:published:=' => '1',
          				 'AND:deleted:=' => '0',
          				 'AND:cacheable:=' => '1',
          				 'AND:parent:IN' => $arrayParents
          			 )
          	  ));
          	} else {
          		$query->where(array(
          			 array(
          				 'class_key:=' => 'modDocument',
          				 'OR:class_key:=' => 'Article'
          			 ),
          			 array(
          				 'AND:published:=' => '1',
          				 'AND:deleted:=' => '0',
          				 'AND:cacheable:=' => '1'
          			 )
          	  ));
          
          	}
          
          	// custom 
          	$query->sortby($sort,$dir);
          	if ($isLimit) $query->limit($limit,$start);
          
          
          	// test of other queries
          	//$query2 = $modx->newQuery('modResource', array('parent' => 204, 'deleted' => false, 'published' => true));
          	//$query2->limit(3,$start);
          
          
          	// getCollection
              $resources = $modx->getCollection('modResource', $query);
              $count = count($resources);  //set number of process steps
          
          
          	$install->setSteps($count+2);
              $install->defineBar();
          
          	//$install->save(implode(",", $parentsID)." ".$count_parents."_");
          
          
          
              if (empty($resources)) {
                  $output = $modx->lexicon('rc_no_resources');
                  $install->save($output);
                  $install->delay(3);
              }
          
              $ch = curl_init(); // Initialize Curl
              if ($ch === false) {
                  $output = $modx->lexicon("rc_no_curl");
                  $install->save($output);
                  $install->delay(3);
              }
          
              @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
              @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
              @curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
          
              ignore_user_abort(true); // keep on going even if user pulls the plug*
          
              $i = 1;
              $refreshingMsg = $modx->lexicon('rc_refreshing');
              $resourceMsg = $modx->lexicon('resources');
              $output = "<p>" . $refreshingMsg . " " . $count . " " . $resourceMsg . "</p><p> </p>";
              $install->save($output.$_POST['contextKey'].' in'.$_POST['parentsIds'].'('.$_POST['nbLast'].' - '.$_POST['refreshParents'].')');
              $install->delay(1);
              sleep(1);
          
          
          
              foreach ($resources as $resource) {
                  // @var $resource modResource 
                  $pageId = $resource->get('id');
                  $pagetitle = $resource->get('pagetitle');
                  $url = $modx->makeUrl($pageId, '', '', 'full');
                  // Avoid infinite loop when requesting this page 
                  if (strstr($url, 'refresh-cache' || $modx->$resource->get('id') == $pageId)) {
                      continue;
                  }
          
                  $output = '<p>(' . $i . '/' . $count . ") " . $refreshingMsg . "</p><p>" . $pagetitle . '</p>';
                  $install->save($output);
                  $install->delay($delay);
          
                  $i++;
          
                  curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
          
                  $unused = curl_exec($ch); // get the page - do nothing with it
          
                  if (curl_errno($ch)) {
                      $output = 'cURL error: ' . curl_errno($ch) . " - " . curl_error($ch);
                      $install->save($output);
                      $install->delay($delay);
                  }
              } // end foreach($resources) loop 
          
          
              curl_close($ch);
          
              unset($output, $unused, $ch, $pagetitle, $url, $pageId);
          
          	if($_POST['refreshParents']=='1'){
          
          	  // PARENTS CACHE BUILD ==============================================================================================
          	  // PARENTS ID retrieve all parents that needs cache rebuild too
          	  $parentsID = array();
          	  foreach ($resources as $resource) {
          		  $parentsID = array_merge($parentsID, $modx->getParentIds($resource->get('id'), 30, array('context' => $_POST['contextKey'])));
          
          	  }
          	  $parentsID= array_unique($parentsID);
            
            
          	  $query_parents = $modx->newQuery('modResource');
          	  $query_parents->where(array(
          		 'id:IN' => $parentsID
          	  ));
          	  $resources_parents = $modx->getCollection('modResource', $query_parents);
          	  $count_parents = count($resources_parents);  //set number of process steps
            
            
          	  $install->setSteps($count_parents+2);
          	  $install->defineBar();
            
          	  //$install->save(implode(",", $parentsID)." ".$count_parents."_");
            
            
          	  if (empty($resources_parents)) {
          		  $output = $modx->lexicon('rc_no_resources');
          		  $install->save($output);
          		  $install->delay(3);
          	  }
            
          	  $ch = curl_init(); // Initialize Curl
          	  if ($ch === false) {
          		  $output = $modx->lexicon("rc_no_curl");
          		  $install->save($output);
          		  $install->delay(3);
          	  }
            
          	  @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
          	  @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          	  @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
          	  @curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
            
          	  ignore_user_abort(true); // keep on going even if user pulls the plug*
            
          	  $i = 1;
          	  $refreshingMsg = $modx->lexicon('rc_refreshing');
          	  $resourceMsg = $modx->lexicon('resources');
          	  $output = "<p>Parents: </p><p>" . $refreshingMsg . " " . $count . " " . $resourceMsg . "</p><p> </p>";
          	  $install->save($output);
          	  $install->delay(2);
          	  sleep(1);
            
            
            
          	  foreach ($resources_parents as $resource) {
          		 // clear also the cache of each parents so it can be regenerated uncached
          
          			// this is taken from https://github.com/modxcms/revolution/pull/428 but it doesn't work
          			//$res = $modx->getObject('modResource', $resource);
          		  	//$res->clearCache();
          
          			//instead I use the code of cachemaster to uncache a specific resource
          		    // get resource context and id 
                      $ctx = $resource->get('context_key');
                      $docId = $resource->get('id');
          
                      // set path to default cache file 
                      $path = MODX_CORE_PATH . 'cache/resource/' . $ctx . '/resources/' . $docId . '.cache.php';
          
                      $ck = $resource->getCacheKey();
                      $mgrCtx = $modx->context->get('key');
                      $cKey = str_replace($mgrCtx, $ctx, $ck);
          
                      $modx->cacheManager->delete($cKey, array(
                          xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                          xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                              $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                          xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                              $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                          )
                      );
          
          
          
          		  // @var $resource modResource 
          		  $pageId = $resource->get('id');
          		  $pagetitle = $resource->get('pagetitle');
          		  $url = $modx->makeUrl($pageId, '', '', 'full');
          		  // Avoid infinite loop when requesting this page 
          		  if (strstr($url, 'refresh-cache' || $modx->$resource->get('id') == $pageId)) {
          			  continue;
          		  }
            
          		  $output = '<p>(' . $i . '/' . $count_parents . ") " . $refreshingMsg . "</p><p>" . $pagetitle . '</p>';
          		  $install->save($output);
          		  $install->delay($delay);
            
          		  $i++;
            
          		  curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
            
          		  $unused = curl_exec($ch); // get the page - do nothing with it
            
          		  if (curl_errno($ch)) {
          			  $output = 'cURL error: ' . curl_errno($ch) . " - " . curl_error($ch);
          			  $install->save($output);
          			  $install->delay($delay);
          		  }
          	  } // end foreach($resources) loop 
            
            
          	  curl_close($ch);
            
          	  unset($output, $unused, $ch, $pagetitle, $url, $pageId);
          	}
          
          
          
          
          
          
              $mtime = microtime();
              $mtime = explode(" ", $mtime);
              $mtime = $mtime[1] + $mtime[0];
              $tend = $mtime;
              $seconds = ($tend - $tstart);
              $totalTime = sprintf( "%02.2d:%02.2d", floor( $seconds / 60 ), $seconds % 60 );
              /* No lex string here -- JS checks for this string to terminate loop */
              $install->save("<p>FINISHED -- Execution time</p><p>(minutes:seconds): {$totalTime}</p>");
          }
          
          return '';
          [ed. note: robotpapier last edited this post 12 years, 5 months ago.]
            • 47243
            • 30 Posts
            yeah and we could even add the uncaching part for the resources that a are not parents.

              • 38593
              • 129 Posts
              As my old post gained new life recently, I thought I would jump back in and update on what I ended up doing.

              I ended up creating a PHP script that runs as a cron in the middle of the night, deleting the cache file and then calling the page to rebuild the cache. So now all pages are served from MODX's cache.

              //set up an paired array (website url, cms cache name)
              $data = array ( 
              'http://www.mydomain.com/category/product_name1.htm' => 'core/cache/resource/web/resources/179.cache.php',
              'http://www.mydomain.com/category/product_name2.htm' => 'core/cache/resource/web/resources/39.cache.php',
              etc => etc
              	);
              	
              //for each pair in array
              foreach ($data as $cms_url => $web_url)
              	{
              	
              	// delete old cached copy
              	unlink($web_url);
              	
              	// get the webpage from CMS
              	file_get_contents($cms_url);
              	
              	}
              


              I also took my experiment a bit further, turning MODX from a content server to a content generator. Moving MODX off the root level, I modified the script to fetch, then minify each page, delete the old stored copy and save the newly built page. This turned everything into a purely static file based website.

              //set up an paired array (modx id, file name)
              $data = array ( 
              'http://www.mydomain.com/modx/?id=179' => 'category/product_name1.htm',
              'http://www.mydomain.com/modx/?id=39' => 'category/product_name2.htm',
              etc + > etc
              	);
              	
              //for each pair in array
              foreach ($data as $cms_url => $web_url) {
              	
              	// get the webpage from CMS
              	$contents = file_get_contents($cms_url);
              	
              	// remove comments
              	$contents = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/Uis', '', $contents);
              	
              	// minify
              	$contents = preg_replace('/^\s+|\n|\r|\s+$/m', '', $contents);
              	
              	// delete old cached copy
              	unlink($web_url);
              	
              	// save new copy
              	file_put_contents($web_url , $contents);	
              	}
              


              The purely static file based test was obviously the fastest response time. The MODX cached version was about double that and the old uncached version was double again (or more depending on the remote database server's response time).

              The later system would be good for small sites, but with the multiplicity of pages due to blogs and product guides, building the array is too complex for my current website. So I am using the first system on only my product pages.

                Pedalers Bicycle Tours my website forged with MODX
                vandergraaf-M static website generator for MODX