• Hi all,
    we added a function to SuperCache, which next to the normal preloading also preloads urls from one or more sitemaps. This came in handy because we run some proprietary software next to our WP installation, and we wanted this to be cached as well. The preloading is spread out over the day. And yes, the logging is only there for development purposes. Just let us know in case you are interested in further details.

    // *** cache pages based on sitemap_files.txt and extra_urls.txt
    function preload_sitemaps() {
    	global $cache_path, $wp_cache_preload_interval;
    	
    	$log_message = date('Y-m-d H:i:s');
    	$urls = array();
    	
    	// add urls from sitemaps in sitemaps_file.txt, if any
    	$filename = $cache_path.'supercache/sitemap_files.txt';
    	try
    	{
    		$fp = @fopen($filename, 'r');
    		if ($fp) {
    			$sitemapFiles = explode(PHP_EOL, fread($fp, filesize($filename)));
    			foreach ($sitemapFiles as $sitemapFile) {
    				if(strlen(trim($sitemapFile)) > 0) {
    					$log_message .= "\r\nsitemap file = ".$sitemapFile;
    					$sitemapHtml = wp_remote_get( $sitemapFile, array('timeout' => 60, 'blocking' => true ) );
    					$sitemapXml = new SimpleXMLElement( wp_remote_retrieve_body($sitemapHtml));
    					foreach ($sitemapXml->url as $url_list) array_push($urls, $url_list->loc);
    				}
    			}
    		}
    		@fclose($fp);
    		
    	} catch ( Exception $e ) {
    		$log_message .= "\r\nException on reading urls from ".$filename."\r\n".$e;
    	}
    	
    	// add urls from extra_urls.txt, if any
    	$filename = $cache_path.'supercache/extra_urls.txt';
    	try
    	{
    		$fp = @fopen($filename, 'r');
    		if ($fp) {
    			$extraUrls = explode(PHP_EOL, fread($fp, filesize($filename)));
    			foreach ($extraUrls as $extraUrl) {
    				if(strlen(trim($extraUrl)) > 0) {
    					$log_message .= "\r\nextra url = ".$extraUrl;
    					array_push($urls, $extraUrl);
    				}
    			}
    		}
    		@fclose($fp);
    
    	} catch ( Exception $e ) {
    		$log_message .= "\r\nException on reading urls from  ".$filename."\r\n".$e;
    	}
    	
    	if( !empty($urls) ) {
    		// preload next chunk of urls	
    		$day = 24*60*60;
    		$interval = 60 * (int) $wp_cache_preload_interval;
    		$chunks_per_day = intdiv($day, $interval);
    		$index_file = $cache_path.'supercache/preload_index.txt';
    		$chunk = 0;
    		if( file_exists( $index_file ) ) {
    			$chunk = (int) file_get_contents( $index_file );
    			if( $chunk > $chunks_per_day ) { $chunk = 0; }
    		} else { $log_message .= "\r\n".date('Y-m-d H:i:s')." ".$index_file." does not exist."; }
    		$begin = intdiv(count($urls) * $chunk * $interval, $day);
    		$end = intdiv(count($urls) * ($chunk+1) * $interval, $day);
    		$urls = array_slice($urls, $begin, ($end-$begin) );
    		foreach ($urls as $url)
    		{	
    			$uri = $cache_path.'supercache/'.strtolower( str_replace("%2A", "*", str_replace("%2F", "/", urlencode( substr($url, 8) ) ) ) );
    			$dir = realpath( $uri );
    			if ($dir !== false && is_dir($dir) && $dh = @opendir( $dir ) ) {
    				while ( ( $file = readdir( $dh ) ) !== false ) {
    					if ( $file != '.' && $file != '..' && $file != '.htaccess' && is_file( $dir.'/'.$file ) ) {
    						@unlink( $dir.'/'.$file );
    					}
    				}
    				closedir( $dh );
    				@rmdir( $dir );
    			} else { $log_message .= "\r\n".date('Y-m-d H:i:s')." ".$uri."[".$dir."] no dir found."; }
    			
    			wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
    		}
    		$log_message .= "\r\npreloaded urls = [".$begin.",".( $begin + count($urls))."], end of chunk = ".$end.".";
    		file_put_contents($cache_path.'supercache/preload'.sprintf("%02s", $chunk).'.log', $log_message );
    
    		$chunk++;
    		file_put_contents( $index_file, $chunk);
    	
    	} else {
    		$log_message .= "\r\nNo urls found in supercache/sitemap_files.txt and supercache/extra_urls.txt.";
    		file_put_contents($cache_path.'supercache/preload.log', $log_message );
    	}
    
    }	

The topic ‘Function to add sitemap to preloading’ is closed to new replies.