<?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 '';
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;
}

/* 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 '';
//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);
}
//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);
}