Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron script to recachify all previously cached pages on HD #146

Open
snrbrnjna opened this issue Oct 18, 2017 · 1 comment
Open

Cron script to recachify all previously cached pages on HD #146

snrbrnjna opened this issue Oct 18, 2017 · 1 comment

Comments

@snrbrnjna
Copy link

snrbrnjna commented Oct 18, 2017

Hi there, I got a script, that regularly recaches every previously cached page via HD cache method with wordpress wp_cron (that rellay better is carried out via system cron):

<?php
namespace My\Namespace;
/*
 * Get recursively Direntries
 */
function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {

            $results[] = $path;
        } else if($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

function getIndexPaths($dirContents) {
    return array_filter($dirContents, function($v) {
        return preg_match('/index\.html$/', $v);
    });
}

function curlUrls($urls) {
  $exitCodes = array();
  foreach ($urls as $key => $url) {
    $cmd = 'curl -L '. $url . ' > /dev/null 2>&1 &';
    exec($cmd, $output, $exit);
    $exitCodes[$url] = $exit;
  }
  return $exitCodes;
}


/**
 * get all cached urls, flush cache and curl every previously cached page again.
 */
function recache() {
  $cacheDir = \CACHIFY_CACHE_DIR;

  if (substr($cacheDir, -1) != '/') { $cacheDir = $cacheDir . '/'; }

  $dirContents = getDirContents($cacheDir);
  $indexFilePaths = getIndexPaths($dirContents);

  $urls = preg_replace(array('/'.preg_replace('/\//', '\/', $cacheDir).'/', '/index\.html$/'), '', $indexFilePaths);
  $urls = preg_replace('/^https-/', '', $urls);

  if (has_action('cachify_flush_cache')) {
    do_action('cachify_flush_cache');
    // DEBUG
    // debug_log('cachify cache flushed');
  }

  $exitCodes = curlUrls($urls);

  // DEBUG
  // debug_log($exitCodes);
}

// Register wp-cron for a daily recache at 2 am in the night.
if (defined('\CACHIFY_CACHE_DIR') && !empty(\CACHIFY_CACHE_DIR)) {
  if (!wp_next_scheduled('recache_cachify')) {
    $start_cron = strtotime('today') + 3600 *2;
    wp_schedule_event(time(), 'daily', 'recache_cachify');
  }
} else {
  if (wp_next_scheduled('recache_cachify')) {
    wp_clear_scheduled_hook('recache_cachify');
  }
}

add_action('recache_cachify', __NAMESPACE__.'\\recache');

just in case, perhaps you want to put a reference to it on your wiki?

@Zodiac1978
Copy link
Member

Related #18

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

No branches or pull requests

4 participants